3. Han with the old school of Python Python code writing style

1, the habit as early as possible is recommended for beginners Python indentation rules
for code indentation is Python in terms of syntax, Python is not like other languages using {} or begin ... end separate blocks of code, instead of using the code indentation and colon to distinguish between code level.
The number of spaces to indent is variable, but all of the code block statement must contain the same number of spaces to indent, this must be strictly enforced.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
^————————^———————————^
ProjectName:python-2019
Author: 韩艳威
Description:
CreateTime:in  2019-10-25 10:40
Modified By:
FileName: idea_if
Description:
Question:
Version:
^————————^———————————^
'''
name = 'laohan'
if name == 'laohan':
    print('真的是老韩')
else:
    print('哦,不是老韩')

2, pep8 Compliant four spaces for the statement block indentation.
3, is generally used in an assignment (a blank space equal to plus or minus), function parameters (after the comma, a blank space).
There are other, refer to the specification pep8 PEP 0008 - Style Guide for Python Code beginners (4 spaces to indent a block of statements) often make the mistake of tab key and the space key mix, resulting in inconsistent indentation. Those who see the error message: IndentationError: unexpected indent, is to represent the indentation inconsistent.

name = 'laohan'
if name == 'laohan':
    print('真的是老韩')
 else:
    print('哦,不是老韩')

The output is shown below:

/usr/local/bin/python3.7 /Users/hanyanwei/python-2019/blog/idea_if.py
  File "/Users/hanyanwei/python-2019/blog/idea_if.py", line 19
    else:
        ^
IndentationError: unindent does not match any outer indentation level

Process finished with exit code 1

How to break? Unification with the shortcut editor ide or indented or left.

4, summarized
this compound statements like if, while, def and class, the first line to start with keywords, a colon (:) end, following the line of code constitutes one or more lines of code groups.
We will be the first line and the following code group called a clause (clause)

num = 0
while num <= 10:
    print(num)
    num += num + 1

Guess you like

Origin blog.51cto.com/hanyanwei/2445390