Python Conditions and If statements

JK1708 
Created at Jun 16, 2023 09:57:37
Updated at May 09, 2024 16:05:54 
  6,635   0   0  

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in "if statements" and loops.

An "if statement" is written by using the if keyword.

a = 33
b = 200
if b > a:
  print("b is greater than a")

In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that "b is greater than a".


Elif

The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".

a = 33
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")

In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal".


Else

The else keyword catches anything which isn't caught by the preceding conditions.

a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b".

You can also have an else without the elif:

a = 200
b = 33
if b > a:
  print("b is greater than a")
else:
  print("b is not greater than a")

Short Hand If

If you have only one statement to execute, you can put it on the same line as the if statement.

if a > b: print("a is greater than b")

Short Hand If ... Else

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:

a = 2
b = 330
print("A") if a > b else print("B")

You can also have multiple else statements on the same line:

a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")

And

The and keyword is a logical operator, and is used to combine conditional statements:

a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True")

Or

The or keyword is a logical operator, and is used to combine conditional statements:

a = 200
b = 33
c = 500
if a > b or a > c:
  print("At least one of the conditions is True")

Not

The not keyword is a logical operator, and is used to reverse the result of the conditional statement:

a = 33
b = 200
if not a > b:
  print("a is NOT greater than b")

Nested If

You can have if statements inside if statements, this is called nested if statements.

x = 41

if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")

The pass Statement

if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.

a = 33
b = 200

if b > a:
  pass

 

Below YouTube content will be helpful for better understanding:

 



Tags: Python Python If Python Nested If Python Short Hand If Share on Facebook Share on X

◀ PREVIOUS
Python Dictionaries

▶ NEXT
Python While Loops/For Loops

  Comments 0
SIMILAR POSTS

Python While Loops/For Loops

(updated at May 09, 2024)

Python Functions

(updated at May 09, 2024)

Python Dictionaries

(updated at May 09, 2024)

Python Sets

(updated at May 09, 2024)

Python Tuples

(updated at May 09, 2024)

Python Lambda

(updated at May 09, 2024)

Python Arrays

(updated at May 09, 2024)

Python Classes/Objects

(updated at May 09, 2024)

Python Comparison Operators

(updated at May 10, 2024)

Python Arithmetic Operators

(created at Jun 14, 2023)

Printing string n times

(created at Jun 14, 2023)

String concatenation by join()

(created at Jun 14, 2023)

Python Lists

(updated at May 10, 2024)

Python String Operations

(updated at May 10, 2024)

Python Data Types

(updated at May 10, 2024)

Python Inheritance

(updated at May 09, 2024)

Python Iterators

(updated at May 09, 2024)

Python Variables

(updated at May 10, 2024)

Python Comments

(updated at May 10, 2024)

Python Syntax

(updated at May 10, 2024)

Python Polymorphism

(updated at May 09, 2024)

Python Scope

(updated at May 09, 2024)

Python Modules

(updated at May 09, 2024)

Python Getting Started

(updated at May 10, 2024)

Python Introduction

(created at Jun 12, 2023)

What is Python?

(updated at Jan 04, 2024)

Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering

(updated at Sep 23, 2024)

Machine Learning Types and Programming Languages

(updated at Nov 29, 2023)

Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship

(updated at May 10, 2024)

Mastering Excel Data Manipulation with Python

(updated at Apr 26, 2024)

OTHER POSTS IN THE SAME CATEGORY

Try...Catch Helps Ignoring Data Type Miss-Match Error in Python

(updated at Mar 26, 2024)

RegExp example in Python to exclude javascript from HTML code

(created at Mar 22, 2024)

Python code to convert from Lunar to Solar

(created at Mar 22, 2024)

Python example to download webpage

(updated at May 15, 2024)

Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship

(updated at May 10, 2024)

Python Modules

(updated at May 09, 2024)

Python Scope

(updated at May 09, 2024)

Python Polymorphism

(updated at May 09, 2024)

Python Iterators

(updated at May 09, 2024)

Python Inheritance

(updated at May 09, 2024)

Python Classes/Objects

(updated at May 09, 2024)

Python Arrays

(updated at May 09, 2024)

Python Lambda

(updated at May 09, 2024)

Python Functions

(updated at May 09, 2024)

Python While Loops/For Loops

(updated at May 09, 2024)

Python Dictionaries

(updated at May 09, 2024)

Python Sets

(updated at May 09, 2024)

Python Tuples

(updated at May 09, 2024)

Python Comparison Operators

(updated at May 10, 2024)

Python Arithmetic Operators

(created at Jun 14, 2023)

Printing string n times

(created at Jun 14, 2023)

String concatenation by join()

(created at Jun 14, 2023)

Python Lists

(updated at May 10, 2024)

Python String Operations

(updated at May 10, 2024)

Python Data Types

(updated at May 10, 2024)

Python Variables

(updated at May 10, 2024)

Python Comments

(updated at May 10, 2024)

Python Syntax

(updated at May 10, 2024)

Python Getting Started

(updated at May 10, 2024)

Python Introduction

(created at Jun 12, 2023)

UPDATES

Where to Eat with Your i-Card at UIUC and How to Track Your Dining Dollars

(updated at Aug 24, 2025)

Why Every Freshman Needs the Illinois App at UIUC

(updated at Aug 24, 2025)

Did you get Selective Service System(SSS) Form 3C?

(updated at Aug 17, 2025)

How to Receive Mail and Packages in University Housing at UIUC

(updated at Aug 11, 2025)

BlackPink's refreshing song - Jump

(updated at Aug 08, 2025)

Poisonous Mushrooms sprouted along the roadside after Typhoon

(updated at Aug 06, 2025)

Annual Weather Forecasting in Illinois based on Month

(updated at Aug 06, 2025)

My name has a typo in MyIllini - Need a Biographical change form

(updated at Jul 31, 2025)

Free Transportation Systems for UIUC students, faculty, and staff with I-Card

(updated at Jul 31, 2025)

What you can do with I-Card at UIUC

(updated at Jul 31, 2025)

Public Transportation between Chicago O'Hare International Airport and UIUC (University of Illinois at Urbana-Champaign)

(updated at Jul 31, 2025)

Selecting a Bed Configuration before you move-in at UIUC Dormitory

(updated at Jul 30, 2025)

Student Health Insurance Waiver: Major Deadlines You Can’t Miss

(updated at Jul 22, 2025)

Types of Memory and Storage

(updated at Jul 22, 2025)

Sample Curriculum Comparison CS versus CS+GGIS at UIUC

(updated at May 31, 2025)

UIUC 2025-2026 Academic Calendar

(updated at May 26, 2025)

IU (아이유) appeared at Mask Singer with Violet Fragrance (보라빛 향기)

(updated at Apr 17, 2025)

What is Model Context Protocol (MCP)? How to build AI Agents?

(updated at Apr 17, 2025)

송소희(Song Sohee) - Not a Dream

(updated at Apr 08, 2025)

DOH KYUNG SOO & LEE SUHYUN - Rewrite The Stars cover

(created at Apr 08, 2025)

😲😭 디오(D.O.) - 후라이의 꿈 + Rewrite the stars(with. 수현)

(created at Apr 08, 2025)

D.O. (도경수) & IU (아이유) - Love Wins All | IU’s Palette (아이유의 팔레트)

(updated at Apr 08, 2025)

Lie - Legend song by BIGBANG

(updated at Feb 26, 2025)

Happy New Year Message with Mathematical Equations

(updated at Jan 02, 2025)

Life Quotes from Google CTO Will Grannis emphasizes the importance of data and the problem definition

(updated at Dec 17, 2024)

Life Quotes from Netflix CTO Elizabeth Stone in 2023

(updated at Dec 17, 2024)

Exploring UC Irvine (aka UCI) - School and its Majors

(updated at Dec 13, 2024)

Understanding Rose-Hulman Institute of Technology

(updated at Dec 13, 2024)

Chilling Acrobatic Taekwondo! The Birth of a Poomsae Prodigy - Byeon Jae-yeong Wins 1st Place at the Hong Kong World Poomsae Championships

(created at Dec 12, 2024)

IU's breathtakingly beautiful "eight" live performance, captivating the hearts of the audience with her dazzling vocals

(created at Dec 10, 2024)

Navigation for UMass Amherst (aka University of Massachusetts Amherst) - Campus Life and Underground Majors

(updated at Dec 10, 2024)

Exploring UC San Diego (aka UCSD) - School and its Majors

(updated at Dec 10, 2024)

How to access websites blocked by ESNI and ECH settings with Firefox!

(updated at Nov 29, 2024)

[#2024MAMA] G-DRAGON - HOME SWEET HOME (feat. Taeyang, Daesung) | Mnet 241123

(updated at Nov 27, 2024)

Eveything you tell HR is confidential

(updated at Nov 27, 2024)

The hippie perm of NewJeans' Danielle 

(updated at Nov 23, 2024)

LoL Worldcup - Worlds 2024 Finals Opening Ceremony Presented by Mastercard ft. Linkin Park, Ashnikko and More!

(created at Nov 18, 2024)

Danielle was featured on the UK Fashion Pop Magazine cover

(updated at Nov 15, 2024)

IU Photos from her family trip

(updated at Nov 09, 2024)

Men vs. Women Taekwondo Sparring - Beautiful Taekwondo Star Tammy's Dazzling Roundhouse Kicks

(updated at Nov 09, 2024)

Legendary Taekwondo Match of the Korean National Sports Festival in High School Division

(updated at Nov 09, 2024)

Legendary Taekwondo 540 degree Kick - Champion Hyun-goo Noh

(created at Nov 09, 2024)

The difference between Equation and Formula

(created at Nov 08, 2024)

Lengendary Turkish Taekwondo player Tazegul at 2015 WTF World Taekwondo Championships

(updated at Nov 08, 2024)

World Rank #2 - Turkey TKD Legend Servet Tazegül

(created at Nov 07, 2024)

Irvine Restaurant American-Style Vietnamese Food Brodard (ft. Ultimate Spring Roll)

(updated at Nov 03, 2024)

Block unwanted URLs for comfortable web browsing with Chrome Addon - URL Blocker

(updated at Nov 01, 2024)

The Gigant Cowboys of Virginia City, Nevada 1889 - AI Generated Photos

(updated at Oct 28, 2024)

Sushi Koto: The "Ohtani" Sushi Spot in Irvine

(created at Oct 26, 2024)

Modern Web Indexing Technology - IndexNow

(updated at Oct 24, 2024)