All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Assert Multiple Conditions

Last Updated : Mar 11, 2024

Python Assert Multiple Conditions

In this article we will show you the solution of python assert multiple conditions, in python, assert statement can check multiple conditions by using and,or,and not logical operator. Assert statement is used for testing process.

If the condition is false then it give Assertion Error. In and operator, if both the conditions is true then it will true and if any of the one condition is false then it will false.

In or condition, if both conditions is false then it will false otherwise it will true. In not operator, if the condition is true it will give the result false

Step By Step Guide On Python Assert Multiple Conditions :-

Method 1 - Using and operator to check multiple conditions in python

a=6
b=7
c=8
assert a>4 and b<9 and c!=10, “condition is not met”
  1. Firstly, we can assign a variable “a” and the value of “a” is 6.
  2. Secondly, we can assign a variable b and the value of b is 7.
  3. Next, we can can assign a variable c and the value of c is 8.
  4. In assert statement, we can check conditions of a,b and c. If all the conditions of a,b and c is True then it will give true otherwise it will give “condition is not met”

Method 2 - Using or operator to check multiple conditions in python

a=6
b=7
c=8
assert a>4 or b<6 or c==9, “condition is not met”
  1. Firstly, we can assign a variable “a” and the value of “a” is 6.
  2. Secondly, we can assign a variable b and the value of b is 7.
  3. Next, we can assign a variable c and the value of c is 8.
  4. Then, we can check the conditions using assert statement.
  5. In assert statement, we can check the conditions of a,b and c using or operator. If any one of the condition of a,b and c is true then it will give assert true otherwise it will give “condition is not met”

Method 3 - Using not operator to assert multiple conditions in python

a = 4
assert not( a > 6 and a>9), "condition is not met"
  1. We can assign a variable “a” and the value of “a” is 4
  2. Then we can check the conditions using assert statement.
  3. In assert statement, we can check the conditions.
  4. If all the conditions of “a” in code is true then it will give false due to reason of “not” operator hence, the result will be “condition is not met”

Conclusion :-

In conclusion, to check multiple condition using assert statement is an easy task.

We can use logical operators to check multiple conditions using assert statement such as and,or and not operator.

Using and operator, when both the condition is true it will true otherwise false, using or operator if any one of the condition is true it will be true otherwise false, using not operator, if the condition is true it will be false.

I hope this article on python assert multiple conditions helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪