Variables and Arguments Passing
When assigning a variable (or name) in Python, you are creating a reference to the object shown on the righthand side of the equals sign. Consider a list of integers.
In some languages, the assignment of b will cause the data [1, 2, 3] to be copied. In Python, a and b actually now refer to the same object, the original list [1, 2, 3]
Conditionals
Python has several built-in keywords for conditional logic, loops, and other standard control flow concepts found in other programming languages.
if, elif, else
The if statements is one of the most well-known control flow statement types. It checks a condition that, if True, evaluates the code in the block that follows.
and if the statement can be optionally followed by one or more elif blocks and a catchall else block if all of the conditions are False
For Loops
For loops are for iterating over a collection (like a list or tuple) or an iterater. The standard syntax for a loop is:
for value in collection:
do something with value
You can advance a for loop to the next iteration, skipping the remainder of the block, using the continue keyword. Consider this code, which sums up integers in a list and skips None values
A for loop can be exited altogether with the break keyword. This code sums elements of the list until 5 is reached.
The break keyword only terminates the innermost for loop; any outer for loops will continue to run
While Loop
A while loop specifies a condition and a block of code that is to be executed until the condition evaluates to False or the loop is explicitly ended with break
pass is the “no-op” (or "do nothing") statement in Python. It can be used in blocks where no action is to be taken (or as a placeholder for code not yet implemented); it is required only because Python uses whitespace to delimit blocks
Range
%formatting %d - integer %g - scientific %f - floating number %0.2f - 2 decimal places %s - string
Built in Sequences
Python has a handful of useful sequence functions that you should familiarize yourself with and use at any opportunity.
enumerate
It’s common when iterating over a sequence to want to keep track of the index of the current item.
sorted
The sorted function returns a new sorted list from the elements of any sequence.
zip
zip “pairs” up the elements of a number of lists, tuples, or other sequences to create a list of tuples.
zip can take an arbitrary number of sequences, and the number of elements it produces is determined by the shortest sequence.
A common use of zip is simultaneously iterating over multiple sequences, possibly also combined with enumerate.
reversed
reversed iterates over the elements of a sequence in reverse order
Lambda Functon
Python has support for so-called anonymous or lambda functions, which are a way of writing functions consisting of a single statement, the result of which is the return value. They are defined with the lambda keyword, which has no meaning other than “we are declaring an anonymous function”
def short_function(x): return x * 2
can be written as
lambda x: x *2
Files and OS
To open a file for reading or writing, use the built-in open function with either a relative or absolute file path and an optional file encoding.