Code writes more gracefully (Python version) __python

Source: Internet
Author: User

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

1 >>> A, B = B, a

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

1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.