- Module/package names: short, all lowercase, underscores only if needed e.g. imp, sys
- Function names: all lowercase, underscores_for_readability, e.g. foo(), my_func()
- Variable names: all lowercase, underscores_for_readability, e.g. my_var
- Class names: CapitablizeEachWord, e.g. MyClass
- Constant names: ALL_CAPS_WITH_UNDERSCORES, e.g. PI, TAX_RATE
- Indentation: 4 spaces per level
- Comparisons: Don’t compare explicityly to True of False, e.g. if my_var: if not my_var:
Python has a number of build-in data types. Varialbes don’t have to be declared and can have any built-in data type.
(// division with trunction)
built-in function round
library module math.ceil
True and False, when used with numbers, True is 1 and False is 0
A list can contain a mixture of other types.
A list can be indexed from its front or back. You can also refer to a subsegment, or slice, of a list using slice notation
built-in functions: len, max, min
operators : in + * (+ and * create a new list, leaving the original unchanged)
statement : del
list methods : append, count, extend, index, insert, pop, remove, reverse, sort
Tuples are similar to lists but are immutable.
Operators: in, +, *
Built-in functions: len, max, min, count, index
Index and slice notition also work the same way as they do on lists.
x = (1, 2, 3, 4)
Strings are also immutable
in + * len max min operate on strings as they do on lists and tuples
re library module
Dictionary data type provides associative array functionality implemented using hash tables.
built-in functions: len, del, clear, copy, get, has_key, items, keys, update and values
Membership and uniqueness are teh main things you need to know.
create a set by using set on a sequence, like a list.
in is used to check for membership on an object in a set.
- Boolean constant False, 0
- Python nil value None
- empty values, like empty list [ ] or empty string “” are False
- Boolean constant True and everything else are considered True
u, v, x, y = 0, 0, 100, 30
item_list = [3, "string1", 23, 14.0, "string2", 49, 64, 70]
If no returen statement is encountered, Python’s None value is returned
def funct1(x, y, z):
try-except-finally-else compound statement
for file in filename:
# File wo.py
# File sh.py