In actual development, sometimes we first build the overall logical structure of the program, but for the time being we will not implement some details, but add some comments in these places, and then add code later, please see the following example:
age = int (input ("Please enter your age:")) if age <12: print ("Infant") elif age >= 12 and age <18: print ("teenage") elif age >= 18 and age <50: print ("adult") else: print ("elderly")
The output is:
Please enter your age:43
adult
When the age is greater than or equal to 30 and less than 50, we do not use the print statement, but use a comment, hoping to deal with the adult situation later. When Python reaches the elif branch, the comment is skipped and nothing is executed.
But Python provides a more professional approach, which is the empty statement pass. pass is a keyword in Python that lets the interpreter skip here and do nothing.
Just like the situation above, sometimes the program needs to occupy a place, or put a statement, but you don't want this statement to do anything. At this time, you can achieve it with a pass statement. Using pass statements is more elegant than using comments.
Change the above code with a pass statement:
age = int (input ("Please enter your age:")) if age <12: print ("Infant") elif age >= 12 and age <18: print ("teenage") elif age >= 18 and age <30: print ("adult") elif age >= 30 and age <50: pass else: print ("elderly")
The output is:
Please enter your age:33
It can be seen from the running results that although the program reaches the 10th line of code, it does nothing.
The Python assert statement, also known as an assertion statement, can be regarded as a reduced function if statement. It is used to determine the value of an expression. If the value is true, the program can continue to execute; otherwise, the Python interpreter will report AssertionError.
The syntax of the assert statement is:
assert expression
The execution flow of an assert statement can be represented by an if statement, as follows:
if expression == True:
Program continues execution
else:
Program reports AssertionError
Some readers may ask, why would you use it if you obviously assert it? This is because instead of crashing the program at a later time, it is better to crash the program directly when an error condition occurs, which helps us debug the program and improve the robustness of the program.
Therefore, the assert statement is usually used to check whether the user's input meets the requirements and is often used as an auxiliary tool in the initial testing and debugging of the program.
The following program demonstrates the use of the assert statement:
age = int (input ()) #Assert whether the age is within the older range assert 65 <= age <= 100 #The program will continue to execute only if age is in the range [65,100] print ("You age is:", age)
The output is:
70
You age is: 70
age = int (input ()) #Assert whether the age is within the older range assert 65 <= age <= 100 #The program will continue to execute only if age is in the range [65,100] print ("You age is:", age)
The output is:
30
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
[ipython-input-27-d78defe45664] in [module]
1 age = int (input ())
2 #Assert whether the age is within the older range
----> 3 assert 65 <= age <= 100
4 #The program will continue to execute only if age is in the range [65,100]
5 print ("You age is:", age)
AssertionError:
You can see that when the expression after the assert statement evaluates to true, the program continues to execute; otherwise, the program stops executing and reports an AssertionError.
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises