SoFunction
Updated on 2024-11-19

Python pass statement role and Python assert function usage

I. Python pass statement

In the actual development, sometimes we will first build up the overall logical structure of the program, but not to achieve certain details, but in these places to add some comments, in terms of adding code later.

See the example below:

age = int( input("Please enter your age:") )
if age < 12 :
    print("Infant")
elif age >= 12 and age < 18:
    print("Teenagers")
elif age >= 18 and age < 30:
    print("Adults.")
elif age >= 30 and age < 50:
    #TODO: Adults
else:
    print("Older people")

Instead of using a print() statement when the age is greater than or equal to 30 and less than 50, we use a comment, hoping to deal with the adult case later. When Python executes to that elif branch, it skips the comment and executes nothing.

But Python offers a more specialized approach, the null statementpasspass is a keyword in Python used to make the interpreter skip here and do nothing.

As in the above case, sometimes a program needs to take up a position, or put a statement in, but doesn't want that statement to do anything, which can be accomplished with a pass statement. Using thepass statement is more elegant than using comments.

Use the pass statement to change the code above:

age = int( input("Please enter your age:") )
if age < 12 :
    print("Infant")
elif age >= 12 and age < 18:
    print("Teenagers")
elif age >= 18 and age < 30:
    print("Adults.")
elif age >= 30 and age < 50:
    pass
else:
    print("Older people")

Run results:

Please enter your age: 40↙

As you can see from the results, the program does not do anything even though it executes to the 10th line of code.

II. Python assert assertion function

Python assert statement, also known as an assertion statement, can be viewed as a functionally reduced version of the if statement, which 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 theAssertionError Error.

The syntactic structure of the assert statement is:

assert expression

The flow of execution of an assert statement can be represented by an if statement.As shown below:

if Expression==True: If expression==True.
Program execution continues
else:
The program reports an AssertionError error

Some readers may ask, clearlyassert Why use it when it will crash the program? This is because, rather than crashing the program at a later time, it is better to just crash the program when the error condition occurs, which helps us troubleshoot the program and improves its robustness.

As a result, the assert statement is commonly used to check user input for compliance, and is often used as an aid in the initial testing and debugging of a program.

III. Python assert function usage

The following program demonstrates the use of the assert statement:

mathmark = int(input())
# Assert that math test scores are within normal range
assert 0 <= mathmark <= 100
# The program will continue only if mathmark is in the range [0,100].
print("Math test scores are:",mathmark)

Run the program and the test data is as follows:

90
Math test scores are: 90

The program is executed again and the test data is:

159
Traceback (most recent call last):
  File "C:\Users\mengma\Desktop\", line 3, in <module>
    assert 0 <= mathmark <= 100
AssertionError

It can be seen that whenassert If the value of the expression following the statement is true, the program continues to execute; otherwise, the program stops and reports theAssertionError Error.

to this article on the role of the Python pass statement and Python assertion function of the use of the article is introduced to this, more related to the content of the pass statement and assertion function, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!