Ken Wade
@kenjyco (github)
kwade@optionsaway.com
Everything in Python is an object and every object has a type.
A variable is a name you specify in your code that maps to a particular object, object instance, or value.
There are different types of operators (special symbols) that operate on different values
When multiple operators are used in a single expression, operator precedence determines which parts of the expression are evaluated in which order (like PEMDAS in math).
Containers are objects that can be used to group other objects together.
Strings, lists, and tuples are all sequence types that can use the +, *, +=, and *= operators
For strings, lists, tuples, and dicts, we can use subscript notation (square brackets) to access data at an index.
A function is an object you can "call" to perform an action, by placing parentheses to the right of the function name. Some functions allow you to pass in arguments.
Different types of objects have different attributes that can be accessed via dot notation (i.e. obj.attribute).
The built-in dir() function can be used to return a list of an object's attributes.
Different types of objects have different attributes that can be accessed via dot notation (i.e. obj.attribute).
When using positional args, you must provide them in the order the function defined them.
string_1 = 'My name is {name} and I like {thing}'
string_2 = 'Three things are {}, {}, and {}'
print(string_1.format(name='Mary', thing='to dance'))
print(string_1.format(name='Todd', thing='dogs'))
print(string_2.format('apples', 'oranges', 'grapes')
You can use named or unnamed place-holders in strings you define. When you call the "format" method on a string, you can pass keyword arguments or positional arguments.
It is easy to iterate over a collection of items using a for loop. The strings, lists, tuples, sets, and dictionaries are all iterable containers.
The for loop will go through the specified container, one item at a time, and provide a temporary variable for the current item.
for item in [-2, 5, 7, -11, 4]:
print(abs(item))
for key, value in some_dict.items():
print('{} -> {}'.format(key, value))
The if statement allows you to test a condition and perform some actions if the condition evaluates to True. You can also provide elif and/or else clauses to an if statement to take alternative actions.
The while loop will keep looping until its conditional expression evaluates to False (possible to loop "forever").
Note: Since the for loop will iterate over a container of items until there are no more, there is no need to specify a "stop looping" condition.