Quick Reference Pages
 Operators
 Builtin Functions
 Common Methods
 Builtin Modules
 Turtle graphics
 Music synthesis
 Testing
Operators Reference
Right from the start of the class, we will be dealing with operators in Python, which work somewhat like the same operators you're familiar with from math, although while their use in mathematical formulae indicates some kind of relationship between values, in Python, they correspond to simplification steps for the computer to carry out. Python also deals with more than just numbers, and the same operator can have different behavior depending on the types of its operands.
The table below includes very brief reference explanations for every operator that we'll use in this class, along with tiny examples of how they can be used correctly (feel free to paste those examples into your Python shell to see what the results are and doublecheck your understanding). This is meant as a quick reference guide, not a thorough explanation of all of the details.
Reference
Math Operators  
Operator  Effect  Example(s) 

+

Adds the left and righthand numbers together, resulting in a number, or concatenates the left and righthand strings, resulting in a string. Can also be used to concatenate lists or tuples. If either operand is a floating point number, the result will be a floatingpoint number, but if they're both integers the result will be an integer. 
1 + 2.5 "abc" + "def"



Subtracts the righthand number from the lefthand number, resulting in a number. Like + , the type of the result (int or float ) depends on the types of the operands.  Can also be used as a unary operator to construct a negative value. Note that  is much less flexible than + as it cannot be used with strings or other sequences.

2  1 4.5

*

Multiplies the left and righthand numbers together, or creates a longer string by repeating the lefthand string the righthand integer number of times. When used with a string, the righthand value must be an integer. 
3 * 4 'ha' * 3

/

Divides the lefthand number by the righthand number. The result will always be a floatingpoint number, even if the two numbers divide evenly. Attempting to divide by 0 causes a ZeroDivisionError .

12 / 3 0.5/0.2

//

Divides the lefthand number by the righthand number, and then rounds towards negative infinity to produce an integer result. Attempting to divide by zero is still an error. 
12 // 3 999 // 1000 12 // 5

%

Computes the modulus of the lefthand number by the righthand number. For positive numbers, this is the same as the remainder after division. For negative numbers, it's different. The modulus will always have the same sign as the righthand number, unless the result is 0 (which it is for all multiples of the righthand number). 
5 % 3 4 % 5 4 % 5

**

Raises the lefthand number to the righthand exponent. Result is a floatingpoint number if either operand is one, but is an integer if both operands are integers. 
3 ** 2 3 ** 0.0 5 ** 1

Comparators  
Operator  Effect  Example(s) 
Note 
Two comparators can be chained together and the result will be True only if both comparisons succeed. For example 1 <= x < 5 will be true if the value of the variable x is greater than or equal to 1 and also strictly less than 5.


==

Read as 'equals', this comparator results in True when its left and righthand sides are the same as each other. Note that with floatingpoint numbers, two numbers that should be the same may exhibit different rounding errors.

1 == 1 "abc" == 'abc' 1.0 == 1 (5 * 1.0) / 2.54 == 5 * (1.0 / 2.54)

!=

Read as 'not equals', this comparator results in True when its left and righthand sides are not the same as each other. Any kind of values may be compared.

1 != 3 'abc' != 123 1.4 != 1.4

< / <=

True when the lefthand number is smaller than the righthand number, or when the lefthand sequence (e.g., string or list) would come before the righthand sequence in sorted order (i.e., dictionary order for strings). The <= form is additionally True when == would be True .

0 < 1 'aardvark' < 'aardvarl'

> / >=

The inverses of < and <= these are True when their lefthand value is greater than their righthand value, either numerically or based on ordering. >= is also true when the two sides are the same (based on == ).

15 > 15 [3, 1] > [2, 10]

is / is not

True when the objects on the left and right hand side are the same object, whether or not they have the same structure (which == can check). Use this to distinguish between aliasing and cloning situations. Should usually only be used with None and with mutable types like lists and dictionaries, since results are interesting for immutable types like numbers and strings. The is not form just returns the opposite result.

x is None `

Logical Operators  
Operator  Effect  Example(s) 
Note 
Logical operators treat their operands as boolean values even when they aren't. For numbers, any value other than 0 is treated as True , and exactly 0 is treated as False . For strings and other sequences, any nonempty sequence is treated as True , and any empty sequence is treated as False . We call these other values "truthy" or "falsey" respectively, although it's generally better to use a comparison operator or predicate to create a real boolean value yourself. The bool function can be used to explicitly convert a value to a boolean, e.g., bool(4) .


and

True if both the left and righthand values are truthy. and happens **after** or , just like + happens after * . When used with nonboolean values, the result is actually the righthand value if both are truthy, or the first value that isn't truthy if one of them isn't, but relying on this behavior is bad style.

True and True x > 3 and x % 2 == 0

or

True if either the left or righthand values are truthy. or happens **before** and , just like * happens before + . When used with nonboolean values, the result is actually the lefthand value if it is truthy, or the righthand value if the lefthand value isn't truthy (regardless of whether or not the righthand value is truthy), but relying on this behavior is bad style.

False or True x < 'd' or x > 's' w and (x or y) and z

not

Reverses a boolean value, turning True into False and vice versa. For nonboolean values, the value is first converted to a boolean and the reversed, so the result is always a boolean. Applies **before** both and and or , just like a  sign for a negative number applies before + and * .

not (1 <= x <= 3) not False or True not (False or True)

Special Operators  
Operator  Effect  Example(s) 
in / not in

Checks whether the lefthand operator is part of the righthand sequence. If they are both strings, this will be True if the lefthand string can be found as a contiguous substring of the righthand string. If the righthandside is a list, it will be True if the lefthandside value is == to one of the elements of that list. If the righthand side is a dictionary, it will be True if the lefthand is == to one of the keys of the dictionary (values are ignored). The not in variant just returns the opposite result.

'ab' in 'abc' 'ab' in 'acb' 'ab' in ['a', 'b', 'c'] [1, 2] in [[1, 2], [3, 4]]

.

This is not really an operator, but it's used followed by a name to retrieve an associated or bound value, often a function. For example, after importing a module, use . to extract and use functions or values from that module. Or use it to refer to buildin methods of common types like strings.

'abc'.split('') ''.join(['a', 'b', 'c'])

[]

Not really an operator, but used to access an individual item from a collection (this is called "subscripting"). For lists, tuples, and strings, the value inside the brackets must be an integer, and it will be used as an index to pull out a single item from the sequence. For dictionaries, the value inside the brackets must be a key, and the result will be the associated value. In both cases, an invalid index (e.g., past the end of the sequence) or key (e.g., a key that's not in the dictionary) will result in an error. For sequences, zero is the first element, and negative indices count backwards from the end. 
'abc'[1] 'last'[1]

[:]

When square brackets have a colon inside, the result is a slice instead of a subscript. Slices are used to pull out multiple items from a sequence, so the result will be a sequence of the same type as the sequence that was sliced. There can be one or two colons, and thus two or three values specified, but missing values will be filled with defaults. The first value specifies the start of the slice, the second specifies the end, and the third (if present) specifies the step value. The resulting sequence will start within the base sequence at the given start index (default is the beginning of the sequence), include all items whose indices are that index plus multiples of the step value (default is 1 meaning every item), and it will stop before the given end index (default is the end of the sequence). With a negative step value, the order will be reversed, and the start index must be after the end index (in this case, the default start and end are swapped). Slicing only works with ordered sequences (e.g., strings, lists, and tuples), not with dictionaries or sets. 
'abcd'[1:3] 'abcd'[::2] 'abcd'[::1]
