Home >Backend Development >Python Tutorial >What are the basic statements of python language?
python statements and syntax
1. Basic introduction to simple python statements
>>> while True: #简单的while循环 ... reply = input('Enter text:') #调用了Input,将输入传参给reply ... if reply == 'stop': break #如果输入的是stop就退出循环 ... print(reply.upper()) #如果输入的不是stop就一直将输入的转换为大写字母 ... Enter text:abc #这是第一个输入abc,看到下面转换成大写的ABC了 ABC Enter text:nihao123da NIHAO123DA Enter text:stop #这里输入了一个stop,然后循环就退出了 >>>
The above code uses Python while loop, it is the most common loop statement in Python. To put it simply, its composition is: the word while, followed by an expression whose result is true or false, and then continued when the top test is true (True at this time is regarded as always true) Iterate through nested blocks of code.
This Input built-in function is used here for output through the console. It prints optional parameter strings as prompts and returns the reply string entered by the user.
Single-line if statements that take advantage of the special rules for nested code blocks also appear here: the body of the if statement appears on the first line after the colon, rather than being indented on the next line after the first line.
Finally, Python’s break statement is used to exit the loop immediately. That is, the loop statement is completely jumped out and the program will continue with the part after the loop. Without this exit statement, the while loop would loop forever because the test would always be true.
>>> while True: ... reply = input('Enter text:') ... if reply == 'stop': #如果是stop就退出 ... break ... elif not reply.isdigit(): #如果输入的不是数字类型就打印Bad1 8次 ... print('Bad!' * 8) ... else: #否则就打印输入数字的2次方 ... print(int(reply) ** 2) ... #按回车下面是测试结果 Enter text:abc Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad! Enter text:a Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad! Enter text:2 4 Enter text:stop >>>
Python will execute the code block for which the first test is true, in order from top to bottom. If all tests are false, the else part will be executed.
2. Assignment, Expression and Printing
Assignment statements have some special features that need to be remembered, as shown below
Establishment of assignment statements Object reference value, Python assignment statement will store the object reference value in the variable name or element of the data structure. Assignment statements always create a reference to an object, not the assigned object. Therefore, Python variables are more like pointers than data storage areas.
The variable name will be created when it is assigned a value for the first time. Python creates variable names the first time an object reference value is assigned to a variable. Some (not all) data structure elements are also created on assignment (e.g. elements in dictionaries, some object properties). Once assigned, the variable name is replaced by the value it refers to whenever it appears in an expression.
Variable names must be assigned a value before being referenced. It is an error to use a variable name that has not yet been assigned a value. If you try to do this, Python will raise an exception instead of returning some vague default value; if the default value is returned, it will be difficult to find the input error in the program. .
Perform some operations of implicit assignment. In Python, assignment statements are used in many situations. For example, module imports, function and class definitions, for loop variables, and function parameters are all implicit assignment operations.
>>> seq = [1,2,3,4] >>> a,b,c,*d = seq >>> print(a,b,c,d) 1 2 3 [4] >>> L = [1,2,3,4] >>> while L: ... front, *L = L ... print(front,L) ... 1 [2, 3, 4] 2 [3, 4] 3 [4] 4 []
When using an asterisked name, the number of items in the left target does not need to match the length of the subject sequence. In fact, the asterisked name can appear anywhere in the target
PRINT OPERATION
In python, the print statement can achieve printing--just for the program It is just an interface to the standard output stream that is user-friendly. Technically speaking, this is converting one or more objects into their textual representation and then sending it to standard output or another file-like stream.
File object methods: For example, file.write(str). The printing operation is similar, but more focused-the file writing method writes the string to any file, and print prints the object by default. To the stdout stream, some automatic formatting is added. Unlike the file method, there is no need to convert the object to a string when using the print operation.
Standard Output Stream: The standard output stream (often called stdout) is just the default place for sending a program's text output. Together with the standard input stream and the error stream, it is just one of the three data connections created when the script starts. Standard output is usually mapped to the window in which the Python program was launched, unless it has been redirected to a file or pipe in the operating system's shell.
if testing and grammar rules
Python grammar rules
There are some features of Python grammar that we need to know: Statements are run one by one: python general The statements in the nested blocks in the file will be executed in order from beginning to end, but statements like if (and loops) will cause the interpreter to jump within the program. Because the path Python takes through a program is called control flow, statements like if that affect it are usually called control flow statements. Block and statement boundaries are automatically detected. There are no delimiting characters such as braces or "begin/end" in Python's program blocks; instead, Python uses statement indentation under the first line to group statements within nested blocks. Similarly, Python statements are generally not terminated with a semicolon, and the end of a line is usually the end of the statement written on that line.
复合语句=首行+“: ” + 缩进语句。Python中所有复合语句都遵循相同格式:首行会以冒号终止,再接一个或多个嵌套语句,而且通常都是在首行下缩进的。缩进语句叫做块(有时叫做组)。在If语句中,elif和else分句是if的一部分,也是其本身嵌套块的首行。 空白行、空格以及注释通常都会忽略。文件中空白行将忽略(但在交互模式提示符下不会)。语句和表达式中的空格几乎都忽略(除了在字符串常量内,以及用在缩进时)。注释总是忽略:它们以#字符开头(不是在字符串常量内),而且延伸至该行的末尾。 文档字符串(docstring)会忽略,但会保存并由工具显示。Python支持的另一种注释,叫做文档字符串(简称docsting)。和#注释不同的是,文档字符串会在运行时保留下来以便查看。文档字符串只是出现在程序文件和一些语句顶端的字符串中。Python会忽略这些内容,但是,在运行时会自动将其附加在对象上,而且能由 文档工具显示。
while和for循环
while语句是Python语言中最通用的迭代结构。
>>> x = 'spam' >>> while x: ... print(x,end='') ... x = x[1:] ... spampamamm>>>
注意,这里使用end= ‘’关键字参数,使所有输出都出现在同一行,之间用空格隔开;
在python中:
break : 跳出最近所在的循环(跳过整个循环语句)
continue : 跳到最近所在循环的开头处(来到循环的首行)
pass:什么事也不做,只是空占位语句
循环else块:只有当循环正常离开时才会执行(也就是没有碰到break语句)
The above is the detailed content of What are the basic statements of python language?. For more information, please follow other related articles on the PHP Chinese website!