Len
The len() function will return the length of a string or the number of items in a list. >>> spam = ['cat', 'dog', 'moose'] >>> len(spam) 3 >>> len('European Swallow') 16
The len() function will return the length of a string or the number of items in a list. >>> spam = ['cat', 'dog', 'moose'] >>> len(spam) 3 >>> len('European Swallow') 16
A list is just like it sounds. A list of things in sequence. A list value is a list. A list can be stored in a variable or passed to a function . Example list: ['cat', 'bat', 'rat', 'elephant'] Values inside the list are called items Items are comma-delimited This is an empty list: []
Just like with strings lists can be concatenated and replicated with + and *. >>> [1, 2, 3] + ['A', 'B', 'C'] [1, 2, 3, 'A', 'B', 'C'] >>> ['X', 'Y', 'Z'] * 3 ['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z'] >>> spam = [1, 2, 3] >>> spam = spam + ['A', 'B', 'C']
Local Scope # Variables that are assigned inside a function have a local scope to that function. These are called local variables. Global Scope # Variables that have a global scope are (typically) defined outside of functions. These are called global variables. Global Statements # Global variables can be modified in a function with a global statement . Why do scopes matter? # Code in global scope cannot use local variables Code in local scope can access global variables Code in a function’s local scope cannot use variables in any other local scope You can use the same name for different variables if they are in different scopes. ...
Converts string to lowercase. >>> spam = 'Hello, world!' >>> spam.lower() 'hello, world!'
chmod 755 scriptfile (Or 700 if only executable by owner) ls -l script_file # to check ./script_file # to execute the script
Math Operators from Highest to Lowest Precedence Operator Operation Example Evaluates to… ** Exponenent 2 ** 3 8 % Modulus/remainder 22 % 8 6 // Integer division/floored quotient 22 // 8 2 / Division 22 / 8 2.75 * Multiplication 3 * 5 15 - Subtraction 5 - 2 3 + Addition 2 + 2 4
A Methods is like functions except that it’s called on a value. Example using index() function. >>> spam = ['hello', 'hi', 'howdy', 'heyas'] >>> spam.index('hello') 0 >>> spam.index('heyas') 3 Note the syntax is value.method(someDataType) Unlike functions methods don’t return a value . Methods belong to a single date type. For example: lists, strings, etc.
A multiline string begins and ends with either 3 single-quotes or double-quotes. print('''Dear Alice, Eve's cat has been arrested for catnapping, cat burglary, and extortion. Sincerely, Bob''') Note, escape characters aren’t necessary in multiline strings. Multiline Comments # A multiline string is often used for multiline comments. """This is a test Python program. Written by Al Sweigart [email protected] this program was designed for Python 3, not Python 2. """ def spam(): """This is a multiline comment to help explain what the spam() function does. ...
You can assign multiple values to list items with the multiple assignment trick also called tuple unpacking. This is a shortcut technique as opposed to assigning each variable individually. >>> cat = ['fat', 'gray', 'loud'] >>> size, color, disposition = cat >>> cat ['fat', 'gray', 'loud'] >>> size 'fat' >>> color 'gray' >>> disposition 'loud' >>>