Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Check if given multiple keys exist in a dictionary
Next article icon

Check multiple conditions in if statement – Python

Last Updated : 02 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
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

Python3 1==
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

Python3 1==
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

Python3 1==
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:

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

Output:

working


Next Article
Python | Check if given multiple keys exist in a dictionary

V

vanshikagoyal43
Improve
Article Tags :
  • Python
  • python-basics
Practice Tags :
  • python

Similar Reads

  • Conditional Statements in Python
    Conditional statements in Python are used to execute certain blocks of code based on specific conditions. These statements help control the flow of a program, making it behave differently in different situations. If Conditional Statement in PythonIf statement is the simplest form of a conditional st
    6 min read
  • Using Else Conditional Statement With For loop in Python
    Using else conditional statement with for loop in python In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops. The else block just after for/while
    2 min read
  • Check if String Contains Substring in Python
    This article will cover how to check if a Python string contains another string or a substring in Python. Given two strings, check whether a substring is in the given string. Input: Substring = "geeks" String="geeks for geeks"Output: yesInput: Substring = "geek" String="geeks for geeks"Output: yesEx
    8 min read
  • Python | Check if given multiple keys exist in a dictionary
    A dictionary in Python consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value. Input : dict[] = {"geeksforgeeks" : 1, "practice" : 2, "contribute" :3} keys[] = {"geeksforgeeks", "practice"} Output : Yes Input : dict[] = {"geeksforgeeks" : 1, "practice"
    3 min read
  • Nested-if statement in Python
    For more complex decision trees, Python allows for nested if statements where one if statement is placed inside another. This article will explore the concept of nested if statements in Python, providing clarity on how to use them effectively. Python Nested if StatementA nested if statement in Pytho
    2 min read
  • Python If Else Statements - Conditional Statements
    In Python, If-Else is a fundamental conditional statement used for decision-making in programming. If...Else statement allows to execution of specific blocks of code depending on the condition is True or False. if Statementif statement is the most simple decision-making statement. If the condition e
    4 min read
  • Python - if , if..else, Nested if, if-elif statements
    There are situations in real life when we need to do some specific task and based on some specific conditions, we decide what we should do next. Similarly, there comes a situation in programming where a specific task is to be performed if a specific condition is True. In such cases, conditional stat
    7 min read
  • Check if directory contains files using python
    Finding if a directory is empty or not in Python can be achieved using the listdir() method of the os library. OS module in Python provides functions for interacting with the operating system. This module provides a portable way of using operating system dependent functionality. Syntax: os.listdir(
    3 min read
  • Check If Value Is Int or Float in Python
    In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to check if a value is an integer
    4 min read
  • Using Apply in Pandas Lambda functions with multiple if statements
    In this article, we are going to see how to apply multiple if statements with lambda function in a pandas dataframe. Sometimes in the real world, we will need to apply more than one conditional statement to a dataframe to prepare the data for better analysis.  We normally use lambda functions to app
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences