Open In App

Check multiple conditions in if statement – Python

Last Updated : 26 Mar, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true.

Syntax:

if (condition):
    code1
else:
    code2
[on_true] if [expression] else [on_false]

Note: For more information, refer to Decision Making in Python (if , if..else, Nested if, if-elif)

Multiple conditions in if statement

Here we’ll study how can we check multiple conditions in a single if statement. This can be done by using ‘and’ or ‘or’ or BOTH in a single statement.

Syntax:

if (cond1 AND/OR COND2) AND/OR (cond3 AND/OR cond4):
    code1
else:
    code2
  • and comparison = for this to work normally both conditions provided with should be true. If the first condition falls false, the compiler doesn’t check the second one. If the first condition is true and the compiler moves to the second and if the second comes out to be false, false is returned to the if statement.
  • or Comparison = for this to work normally either condition needs to be true. The compiler checks the first condition first and if that turns out to be true, the compiler runs the assigned code and the second condition is not evaluated. If the first condition turns out to be false, the compiler checks the second, if that is true the assigned code runs but if that fails too, false is returned to the if statement.

The following examples will help understand this better:
PROGRAM 1: program that grants access only to kids aged between 8-12




age = 18
  
if ((age>= 8) and (age<= 12)):
    print("YOU ARE ALLOWED. WELCOME !")
else:
    print("SORRY ! YOU ARE NOT ALLOWED. BYE !")


Output:

SORRY ! YOU ARE NOT ALLOWED. BYE !
PROGRAM 2:

program that checks the agreement of the user to the terms




var = 'N'
  
if (var =='Y' or var =='y'):
    print("YOU SAID YES")
elif(var =='N' or var =='n'):
    print("YOU SAID NO")
else:
    print("INVALID INPUT")


Output:

YOU SAID NO

PROGRAM 3: program to compare the entered three numbers




a = 7
b = 9
c = 3
  
  
if((a>b and a>c) and (a != b and a != c)):
    print(a, " is the largest")
elif((b>a and b>c) and (b != a and b != c)):
    print(b, " is the largest")
elif((c>a and c>b) and (c != a and c != b)):
    print(c, " is the largest")
else:
    print("entered numbers are equal")


Output:

9  is the largest

Not just two conditions we can check more than that by using ‘and’ and ‘or’.
PROGRAM 4:




a = 1
b = 1
c = 1
if(a == 1 and b == 1 and c == 1):
    print("working")
else:
    print("stopped")


Output:

working


Similar Reads

How to use NumPy where() with multiple conditions in Python ?
In Python, NumPy has a number of library functions to create the array and where is one of them to create an array from the satisfied conditions of another array. The numpy.where() function returns the indices of elements in an input array where the given condition is satisfied. Syntax: numpy.where(condition[, x, y]) Parameters: condition : When Tr
3 min read
Subset or Filter data with multiple conditions in PySpark
Sometimes while dealing with a big dataframe that consists of multiple rows and columns we have to filter the dataframe, or we want the subset of the dataframe for applying operation according to our need. For getting subset or filter the data sometimes it is not sufficient with only a single condition many times we have to pass the multiple condit
3 min read
NumPy - Filtering rows by multiple conditions
In this article, we will discuss how to filter rows of NumPy array by multiple conditions. Before jumping into filtering rows by multiple conditions, let us first see how can we apply filter based on one condition. There are basically two approaches to do so: Method 1: Using mask array The mask function filters out the numbers from array arr which
4 min read
How to remove rows from a Numpy array based on multiple conditions ?
In this article, we will learn how to remove rows from a NumPy array based on multiple conditions. For doing our task, we will need some inbuilt methods provided by the NumPy module which are as follows: np.delete(ndarray, index, axis): Delete items of rows or columns from the NumPy array based on given index conditions and axis specified, the para
3 min read
Delete rows in PySpark dataframe based on multiple conditions
In this article, we are going to see how to delete rows in PySpark dataframe based on multiple conditions. Method 1: Using Logical expression Here we are going to use the logical expression to filter the row. Filter() function is used to filter the rows from RDD/DataFrame based on the given condition or SQL expression. Syntax: filter( condition) Pa
3 min read
Pyspark - Filter dataframe based on multiple conditions
In this article, we are going to see how to Filter dataframe based on multiple conditions. Let's Create a Dataframe for demonstration: C/C++ Code # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and giving an app name spark = SparkSession.builder.appName(
3 min read
Filter Pandas Dataframe with multiple conditions
In this article, let's discuss how to filter pandas dataframe with multiple conditions. There are possibilities of filtering data from Pandas dataframe with multiple conditions during the entire software development. Filter Pandas Dataframe with multiple conditionsThe reason is dataframe may be having multiple columns and multiple rows. Selective d
5 min read
Python | Set 2 (Variables, Expressions, Conditions and Functions)
Introduction to Python has been dealt with in this article. Now, let us begin with learning python. Running your First Code in Python Python programs are not compiled, rather they are interpreted. Now, let us move to writing python code and running it. Please make sure that python is installed on the system you are working on. If it is not installe
3 min read
Python | Exceptional Conditions Testing in Unit Tests
This article aims to write a unit test that cleanly tests if an exception is raised. To test for exceptions, the assertRaises() method is used. Code #1 : Testing that a function raised a ValueError exception import unittest # A simple function to illustrate def parse_int(s): return int(s) class TestConversion(unittest.TestCase): def test_bad_int(se
2 min read
How to write an empty function in Python - pass statement?
In C/C++ and Java, we can write empty function as following // An empty function in C/C++/Java void fun() { } In Python, if we write something like following in Python, it would produce compiler error. # Incorrect empty function in Python def fun(): Output : IndentationError: expected an indented block In Python, to write empty functions, we use pa
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg