Python Tutorial– PassStatement and Assert Function

Python Exercise Lists

Python – Pass Statement

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.

Python - Assert Function

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)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises