Python programmers prefer to be explicit, to choose simple over complex, and to maximize readability.
The Best Way to do the most common things in python
It's strongly recommended to use Python 3 in their next projects
The style guide for how to format Python code
PEP 8 has a wealth of details about how to write clear python code.
Here are few rules you should be sure to follow:
Whitespace:
Naming:
Expressions and Statements:
Things to Remember:
In Python 3, there are two types that represent sequences of characters:
bytes = 8-bit values
str = Unicode characters
In Python 2, there are two types that represent sequences of characters:
str = 8-bit values
unicode = Unicode characters
There are may ways to represent Unicode characters as binary data (raw 8-bit values). The most common encoding is UTF-8.
Importantly, str instances in Python 3 and unicode instances in Python 2 do not have an associated binary encoding.
When you're writing Python programs, it's important to do encoding and decoding of Unicode at the furthest boundary of your interfaces.
The core of your program should use Unicode character types (str in Python 3, unicode in Python 2)
The slip between character types leads to two common situations in Python code:
You'll often need two helper functions to convert between these two cases and to ensure that the type of input values matches your code's expectations.
Python 3:
Python 2:
Things to Remember:
Python includes syntax for slicing sequences into pieces. Slicing lets you access a subset of a sequence's items with minimal effort.
The basic form of the slicing syntax is somelist[start:end]. Where start is inclusive and end is exclusive.
Using negative numbers for slicing is helpful for doing offsets relative to the end of a list.
The result of slicing a list is a whole new list. References to the objects from the original list are maintained. Modifying the result of slicing won't affect the original list.
When used in assignments, slices will replace the specified range in the original list.
The list will grow or shrink to accommodate the new values
If you leave out both the start and the end indexes when slicing, you'll end up with a copy of the original list.
If you assign a slice with no start or end indexes, you'll replace its entire contents with a copy of what's referenced (instead of allocating a new list)
Things to Remember:
Python provides compact syntax for deriving one list from another.
These expressions are called list comprehensions.
Is visually noisy
List comprehensions let you easily filter items from the input list, removing corresponding outputs from the result.
It is much harder to read
Things to Remember:
When you have a data structure to iterate over, like a list of strings, you can loop directly over the sequence.
You'll want to iterate over a list and also know the index of the current item in the list.
Python provides the enumerate built-in function for addressing this situation. enumerate wraps any iterator with a lazy generator. This generator yields pairs of the loop index and the next value from the iterator.
Things to Remember:
There are four distinct times that you may want to take action during exception handling in Python. These are captured in the functionality of try, except, else and finally blocks.
Finally Blocks
Use try/finally when you want exceptions to propagate up, but you also want to run cleanup code even when exceptions occur.
Else Blocks
Use try/except/else to make it clear which exceptions will be handled by your code and which exceptions will propagate up. When the try block doesn't raise an exception, the else block will run.
Everything Together
Things to Remember