Function v.s. Method:
Function | Method |
---|---|
Independent of any object | Associated to an object |
All the parameters are passed explicitly | The associated object is implicitly passed on |
May or may not return any data | May or may not return any data |
Example of a function
Example of a method (in the class "MyClass")
Operation | Syntax | Function | Comments |
---|---|---|---|
Addition | a + b | add(a, b) | |
Concatenation | seq1 + seq2 | concat(seq1, seq2) | |
Containment Test | obj in seq | contains(seq, obj) | |
Division | a / b | truediv(a, b) | |
Floor Division | a // b | floordiv(a, b) | 13 // 5 = 2 |
Bitwise And | a & b | and_(a, b) | |
Bitwise Exclusive Or | a ^ b | xor(a, b) | |
Bitwise Inversion | ~ a | invert(a) | |
Bitwise Or | a | b | or_(a, b) |
Exponentiation | a ** b | pow(a, b) | |
Identity | a is b | is_(a, b) | |
Identity | a is not b | is_not(a, b) | |
Indexed Assignment | obj[k] = v | setitem(obj, k, v) | |
Indexed Deletion | del obj[k] | delitem(obj, k) | |
Indexing | obj[k] | getitem(obj, k) | |
Left Shift | @@a << b@@ | lshift(a, b) | |
Modulo | a % b | mod(a, b) | |
Multiplication | a * b | mul(a, b) | |
Negation (Arithmetic) | - a | neg(a) | |
Negation (Logical) | not a | not_(a) | |
Positive | + a | pos(a) | |
Right Shift | @@a >> b@@ | rshift(a, b) | |
Slice Assignment | seq[i:j] = values | setitem(seq, slice(i, j), values) | |
Slice Deletion | del seq[i:j] | delitem(seq, slice(i, j)) | |
Slicing | seq[i:j] | getitem(seq, slice(i, j)) | |
String Formatting | s % obj | mod(s, obj) | |
Subtraction | a - b | sub(a, b) | |
Truth Test | obj | truth(obj) | |
Ordering | a < b | lt(a, b) | |
Ordering | a <= b | le(a, b) | |
Equality | a == b | eq(a, b) | |
Difference | a != b | ne(a, b) | |
Ordering | a >= b | ge(a, b) | |
Ordering | a > b | gt(a, b) |
Ternary condition operator:
The last printed expression is assigned to the variable _
The keys have to be of immutable type (e.g. numbers or strings, but not lists); the values can be any variable.
If a tuple contains only immutable objects, then the tuple can be used as a key.
while
statement
if
statement
for
statement
To modify the list while iterating through its elements, it's highly recommended to first make a copy of the list and then iterate through the copy instead of the origin:
break
, continue
, and else
on LoopsThe else
clause for a loop is executed when the loop terminates through exhaustion of the list (with for
) or when the condition becomes false (with while
), but not when the loop is terminated by a break
statement. For example:
The continue
statement continues with the next iteration of the loop.
pass
StatementThe pass
statement does nothing. It can be used when a statement is required syntactically but the program requires no action.
An iterable objec is an object which returns the successive items of the desired sequence when being iterated over. While it may behave like a list in some ways, it is essentially not a list.
Usage of range()
:
Print the elements in a list along with their indices.
Move to next element
Basic file operations
Get current working directory
Check if the file/directory exists:
List all files/dirs in a directory
Create directory if it does not exist
New in version 3.5.
Reading the arguments
Basic usage
Set the progress based on stdout:
quick-ref