One of the biggest advantages of Python's language is its simplicity of grammar, which is as clean, neat, and straightforward as a pseudo code. But sometimes we write code, especially Python beginners, often in accordance with the thinking habits of other languages to write, so that the writing not only slow running, code read up too much, give a feeling of a muddy, after a period of time even they can not read.
Hall Abelson, author of the construction and interpretation of computer programs, said: "Programs must is written for people to read, and just incidentally for machines to execute."
To write Pythonic (elegant, authentic, neat) code, but also at ordinary times to observe those Daniel Code, Github on a lot of very good source code is worth reading, such as: Requests, flask, Tornado, the author enumerates some common pythonic writing, Hope to bring you a little enlightenment. 1. Variable Exchange
In most programming languages, when you exchange values for two variables, you have to introduce a temporary variable: Python
| 1 2 3 4 5 |
>>> A = 1 >>> b = 2 >>> tmp = a >>> a = b >>> b = tmp |
Pythonic Python
2, loop traversal interval elements
Python
| 1 2 3 4 5 |
For i in [0, 1, 2, 3, 4, 5]: (Print i) # or for I in range (6): (Print i) |
Pythonic Python