In this tutorial, we will discuss assertion in Python. Also, we will explain how you can use the assert keyword to perform assertion.
What is Assertion in Python?
The assertion is a special statement that we use in Python, and it is similar to the if statement. Like if statement, assertion statement works with boolean data type and output either True or False. We do not often use assertion statements in Python programs, it is only used when we are completely sure about the boolean statement. For example, consider that we create a program that requires a user to enter only 1. The program will execute only if the user enters 1, else the program will crash and throw an error. For such a program, we can use the assertion statement. Like the conditional statements, the assertion statement works on boolean values (True and False), and the execution of the next lines of code depends upon the True boolean value. The only difference between the conditional statement and the assertion statement is that the assertion statement throws an error if the boolean value is False . We typically use the assertion statement as a debugging tool to check the False values. An assertion statement stops the execution of the program where it finds the False value.
In Python, we have a keyword called assert that we can use to create the assertion statement. The execution of the code written after the assertion statement depends on the boolean value output by the assertion statement. If the value is True, the next line of code executes, else it throws an error. assert syntax:
assert <condition> , <optional error message>
The syntax to write an assert statement is very easy. The only thing we require is the assert keyword and a condition which returns a boolean value. The <optional error message> is completely optional, and it is used to provide some additional information about the error. Example: Here in this example, we will write a program that asks a user to enter their age for registration, and we only register those users having ages between 18 and 30 (both values included in the range). If the user age is not between 18 to 30, the program will deny the user registration and throw an Assertion error. Python Program
name = input("Enter your name: ") age = int(input("Enter your age: ")) assert (age >= 18) and (age <= 30) , "Your age is not between 18 to 30" print("You are registered")
Output (without Error)
Enter your name: Sam 25 Enter your age: 25 You are registered
Output (with Error)
Enter your name: Juli Enter your age: 34 assert (age >= 18) and (age <= 30) , "Your age is not between 18 to 30" AssertionError: Your age is not between 18 to 30
Behind the Code: In the above Python program, we have asked the user to enter their name and age. The assert statement within the code checks whether the input age enters satisfy a specific condition or not, i.e. the age of the user is between 18 and 30. If the age entered by the user lies between 18 to 30, the print("You are registered") statement gets executed, else the Assertion statement throws the AssertionError.
Following are the key points to remember about Python assert statement:
- The assertion statement is similar to the conditional statement.
- To create an assertion statement, we use the assert keyword
- If the condition is False, the assertion statement throws the AssertionError
- It is typically used for debugging .