[Learn Python from scratch_1.1] "Strange" code line structure

Official documentation: Lexical analysis https://docs.python.org/zh-cn/3.10/reference/lexical_analysis.html

Glossary:

Python programs can be split into multiple logical lines :

1. Logical line : An executable logical statement, consisting of one or more physical lines according to explicit or implicit line splicing rules .

2. Physical line : A line of valid code characters. The end of input is regarded as the end of the line.

print('hello world')

3.1 Single-line comments : Start with the hash sign "#" and apply to the current physical line.

#单行注释

3.2 Multi-line comments : start with three single quotes (''') or double quotes ("""), end with three single quotes (''') or double quotes ("""), and the The content is comment content. A single quotation mark can only start and end with a single quotation mark. Single and double quotation marks cannot be mixed, and quotation marks cannot be nested.

'''
多行注释
多行注释
'''
"""
多行注释
多行注释
"""

The commented code will not be parsed by the Python program. It is simply used for programmers to write notes. The number of comments written does not affect the running of the code at all.

4. Coding statement : (Tucao-_-|, coding statement written in comments)

Python defaults to UTF-8 encoding format. You can declare the encoding format according to the specified regular expression in the comments on the first or second line of the Python script. There are two ways of writing: "# -*- coding: UTF-8 -*- " or "# vim:fileencoding=UTF-8", note: If the encoding format is declared on the second line, the first line must be a comment.

# 如果在第二行声明编码格式,第一行必须是注释
# -*- coding: utf-8 -*-
# coding = utf-8

5.1 Display splicing lines :

Use the backslash "\" to split a logical line into multiple physical lines.

a = 1
b = 2
c = 3
d = 4
s = a + b + \
    c + d
print(s)

If you do not use backslashes, you can also achieve multi-line effects by using the implicit splicing rules of parentheses in some cases. 

a = 1
b = 2
c = 3
d = 4
s = (a + b +
    c + d)
print(s)

 5.2 Implicit splicing lines:

Expressions in parentheses "()", square brackets "[]", and curly braces "{}" do not need to use backslashes. The newline operation directly divides a logical line into multiple physical lines.

names = ["xiaoming",
         "xiaohong",
         "xiaohui"]

Splicing rows : Split a logical row into multiple physical rows, which is just for viewing and does not affect the running of the code.

6. Multiple statements in one line: (not recommended)

Write multiple statements on the same line, separated by semicolons ";". Similar to writing multiple logical lines in one physical line:

a = 1; b = 2; c = 3; d = 4
s = a + b + c + d

7. Indentation level:

The most unique thing about Python is that it uses space indentation to represent code blocks and does not use curly braces "{}".

The number of spaces before the first non-empty character of a logical line determines the indentation level of the line; statements in the same code block must have the same number of indented spaces. If they are inconsistent, an error will be reported!

a = 10
if a > 0:
    print(1)    #只有if语句为true,才会执行if的子语句print(1)
    print(2)    #同上
print(3)        #执行完if语句后,会执行if的同级语句print(3)

8. Blank lines:

Whether or not there are blank lines or how many blank lines there are will not affect the running of the Python program. Insert blank lines between functions or classes to facilitate code reading.

Guess you like

Origin blog.csdn.net/qq_39108767/article/details/124549125