Learn Python data science, play games, learn Japanese, and engage in programming all in one package.
The data applied in the whole set of self-study courses are the contents of the games of "Three Kingdoms" and "Dynasty Three Kingdoms Warriors" series.
The Python grammar defines all the rulesets used to create sentences in Python programming.
If you want to study Python lexical structure in more depth, you need to understand the syntactic elements that make up a statement, the basic unit that makes up a Python program, covering the control structures that guide the construction of program flow between different code groups.
Article directory
Python statement
A statement is the basic unit of instruction that is parsed and processed by the Python interpreter. Usually the interpreter executes the statements one after the other in order.
In a REPL session, statements are executed as they are entered until the interpreter terminates. When executing a script file, the interpreter reads statements from the file and executes them until end-of-file is encountered.
Insert picture description here
Usually each statement occupies one line, and the end of the statement is separated by a newline character that marks the end of the line.
print('真・三國無双')
真・三國無双
x = ['劉備', '関羽','張飛']
print(x[1:2])
['関羽']
Continuation
Suppose a single statement in the Python code is particularly long (contains multiple assignment statements).
person1_age = 42
person2_age = 16
person3_age = 71
someone_is_of_working_age = (person1_age >= 18 and person1_age <= 65) or (person2_age >= 18 and person2_age <= 65) or (person3_age >= 18 and person3_age <= 65)
someone_is_of_working_age
True
A lengthy nested list is best with its own formatting convention.
list_ = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]
list_
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]
The official Python how-to guide defines a maximum line length of 79 characters.
way of implicit continuation
list_ = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]
]
list_
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]
PEP8 explicitly advocates using parentheses when appropriate to optimize code structure.
someone_is_of_working_age = (
(person1_age >= 18 and person1_age <= 65)
or (person2_age >= 18 and person2_age <= 65)
or (person3_age >= 18 and person3_age <= 65)
)
someone_is_of_working_age
True
Explicit way of continuation
Use the backslash ( \ ) character as the last character on the line.
str_ = \
'真・三國無双'
str_
'真・三國無双'
str_ = '劉備'+'関羽'\
+ '張飛'
str_
'劉備関羽張飛'
Multiple statements per line
If multiple statements are separated by a semicolon ( ; ) character.
x = '劉備'; y = '関羽'; z = '張飛'
print(x); print(y); print(z)
劉備
関羽
張飛
x, y, z = '劉備','関羽','張飛'
print(x, y, z, sep='\n')
劉備
関羽
張飛
Notes
A hash character ( # ) indicates a comment, and the interpreter ignores everything from the hash character to the end of the line.
list_ = ['劉備', '関羽', '張飛'] # 这是个注释
list_
['劉備', '関羽', '張飛']
If the first non-whitespace character of the line is ( # ), the entire line will be ignored.
# 这是个注释
# 这是个注释
Hash characters in string literals are protected and do not represent comments.
str_ = '劉備 関羽 # 張飛'
str_
'劉備 関羽 # 張飛'
Some other annotation methods
# 隐式续行
list_ = [
'劉備', '関羽', # 注释
'張飛', # 注释
]
list_
['劉備', '関羽', '張飛']
# 这里是个注释1
#
# 这里是个注释2
# 这里是个注释3
"""这里是个注释1
这里是个注释2
这里是个注释3
"""
whitespace
Usually what separates tokens from each other is whitespace, a whitespace character that provides whitespace to improve readability.
character | ASCII code | Written Expression |
---|---|---|
space | 32( 0x20) | ’ ’ |
tab | 9( 0x9) | ‘\t’ |
newline | 10 (0xa) | ‘\n’ |
Whitespace is normally ignored by the Python interpreter.
x=1;y=2
x+y
3
(x==3)and(x<y)
True
list_=['劉備','関羽','張飛']
list_
['劉備', '関羽', '張飛']
list_={
'劉備':1,'関羽':2}
list_
{
'劉備': 1, '関羽': 2}
x,y,z='劉備',14,21.1
(x,y,z)
('劉備', 14, 21.1)
str_='劉備'"関羽"'張飛'
str_
'劉備関羽張飛'
str_ = '関羽'
str_ in ['劉備', '関羽', '張飛']
True
whitespace as indentation
Indentation, the space to the left of the first token on a line - has a very special meaning. In most interpreted languages, leading whitespace before a statement is ignored.
For example windows environment.
C:\Users\Lenovo>hello
hello
C:\Users\Lenovo> hello
hello
But it's not feasible in python environment.
print('真・三國無双')
真・三國無双
print('真・三國無双')
File "<stdin>", line 1
print('真・三國無双')
^
IndentationError: unexpected indent