Home > Article > Backend Development > Does python use indentation to reflect the logical relationship between codes?
Yes, Python uses indentation to reflect the logical relationship between codes, and the requirements for indentation are very strict. The Python language organizes blocks of code through indentation, which is a mandatory requirement for Python. You can create a statement block by placing a space before the code to indent the statement. Each line in the statement block must be indented by the same amount.
Unlike other programming languages (such as Java and C language) that use braces "{}" to separate code blocks, Python uses code indentation and colons ( : ) to differentiate between levels of code blocks.
In Python, for class definitions, function definitions, flow control statements, exception handling statements, etc., the colon at the end of the line and the indentation of the next line indicate the beginning of the next code block, and the end of the indentation It indicates the end of this code block.
Note that to indent code in Python, you can use spaces or the Tab key. But whether you type a space manually or use the Tab key, a length of 4 spaces is usually used as an indentation amount (by default, a Tab key represents 4 spaces).
For example, in the following Python code (it involves knowledge that has not been learned yet, beginners do not need to understand the meaning of the code, they only need to understand the indentation rules of the code block):
height=float(input("输入身高:")) #输入身高 weight=float(input("输入体重:")) #输入体重 bmi=weight/(height*height) #计算BMI指数 #判断身材是否合理 if bmi<18.5: #下面 2 行同属于 if 分支语句中包含的代码,因此属于同一作用域 print("BMI指数为:"+str(bmi)) #输出BMI指数 print("体重过轻") if bmi>=18.5 and bmi<24.9: print("BMI指数为:"+str(bmi)) #输出BMI指数 print("正常范围,注意保持") if bmi>=24.9 and bmi<29.9: print("BMI指数为:"+str(bmi)) #输出BMI指数 print("体重过重") if bmi>=29.9: print(BMI指数为:"+str(bmi)) #输出BMI指数 print("肥胖")
Python has very strict requirements for code indentation. The indentation amount of code blocks at the same level must be the same, otherwise the interpreter will report a SyntaxError exception. For example, make wrong changes to the above code and set the indentation of 2 lines of code in the same scope to 4 spaces and 3 spaces respectively, as shown below:
if bmi<18.5: print("BMI指数为:"+str(bmi)) #输出BMI指数 print("体重过轻")
You can see , the second line of code and the third line of code originally belong to the same scope, but we manually modified their respective indentations, which will cause a SyntaxError exception error, as shown in Figure 1.
Figure 1 Indentation does not comply with the specification causing an exception
For Python indentation rules, beginners can understand it this way. Python requires that each line of code belongs to the same scope. Their indentation must be consistent, but there is no hard and fast rule about the specific indentation.
IDLE development environment settings for indentation
In the IDLE development environment, the default is to use 4 spaces as the basic indentation unit of the code. However, this value can be changed manually. Select Options -> Configure in the menu bar, and the following dialog box will pop up:
As shown in the figure, by dragging Use the slider to change the default code indentation amount. For example, drag it to 2. When you use the Tab key to set the code indentation amount, you will find that pressing the Tab key once will indent the code by 2 spaces.
Not only that, when using the IDLE development environment to write Python code, if you want to set the indentation of multiple lines of code, you can use the Ctrl ] and Ctrl [ shortcut keys. This shortcut key can make the selected code faster Indent (or de-indent).
Recommended learning: Python video tutorial
The above is the detailed content of Does python use indentation to reflect the logical relationship between codes?. For more information, please follow other related articles on the PHP Chinese website!