0% found this document useful (0 votes)
0 views9 pages

Conditional Statements in Python

Uploaded by

Sachin Samriddh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views9 pages

Conditional Statements in Python

Uploaded by

Sachin Samriddh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

17/09/2025, 16:08 Conditional Statements in Python - GeeksforGeeks

Search... Sign In

Conditional Statements in Python


Last Updated : 29 Aug, 2025

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.

Let's explore different types of conditional statements in python.

If Conditional Statement
If statement is the simplest form of a conditional statement. It executes a
block of code if the given condition is true.

If Statement

Example:

age = 20
if age >= 18:
print("Eligible to vote.")

[Link] 1/9
17/09/2025, 16:08 Conditional Statements in Python - GeeksforGeeks

Output

Eligible to vote.

Short Hand if

Short-hand if statement allows us to write a single-line if statement.

Example:

age = 19
if age > 18: print("Eligible to Vote.")

Output

Eligible to Vote.

This is a compact way to write an if statement. It executes print statement


if the condition is true.

If else Conditional Statement


If Else allows us to specify a block of code that will execute if the
condition(s) associated with an if or elif statement evaluates to False. Else
block provides a way to handle all other cases that don't meet the
specified conditions.

[Link] 2/9
17/09/2025, 16:08 Conditional Statements in Python - GeeksforGeeks

If Else Statement

Example:

age = 10
if age <= 12:
print("Travel for free.")
else:
print("Pay for ticket.")

Output

Travel for free.

Short Hand if-else

The short-hand if-else statement allows us to write a single-line if-else


statement.

Example:

marks = 45
res = "Pass" if marks >= 40 else "Fail"
print(f"Result: {res}")

[Link] 3/9
17/09/2025, 16:08 Conditional Statements in Python - GeeksforGeeks

Output

Result: Pass

Note: This method is also known as ternary operator. Ternary Operator


essentially a shorthand for the if-else statement that allows us to write
more compact and readable code, especially for simple conditions.

elif Statement
elif statement in Python stands for "else if." It allows us to check multiple
conditions, providing a way to execute different blocks of code based on
which condition is true. Using elif statements makes our code more
readable and efficient by eliminating the need for multiple nested if
statements.

1/3

Example:

age = 25

if age <= 12:


print("Child.")
elif age <= 19:
print("Teenager.")
elif age <= 35:
print("Young adult.")
[Link] 4/9
17/09/2025, 16:08 Conditional Statements in Python - GeeksforGeeks

else:
print("Adult.")

Output

Young adult.

The code checks the value of age using if-elif-else. Since age is 25, it skips
the first two conditions (age <= 12 and age <= 19), and the third condition
(age <= 35) is True, so it prints "Young adult.".

Nested if..else Conditional Statement


Nested if..else means an if-else statement inside another if statement. We
can use nested if statements to check conditions within conditions.

Nested If Else

age = 70
is_member = True

if age >= 60:


if is_member:

[Link] 5/9
17/09/2025, 16:08 Conditional Statements in Python - GeeksforGeeks

print("30% senior discount!")


else:
print("20% senior discount.")
else:
print("Not eligible for a senior discount.")

Output

30% senior discount!

Free Python 3 Tutorial Data Types Control Flow Functions List String Set Sign In
Ternary Conditional Statement
A ternary conditional statement is a compact way to write an if-else
condition in a single line. It’s sometimes called a "conditional expression."

Example:

# Assign a value based on a condition


age = 20
s = "Adult" if age >= 18 else "Minor"
print(s)

Output

Adult

Here:

If age >= 18 is True, status is assigned "Adult".


Otherwise, status is assigned "Minor".

Match-Case Statement
match-case statement is Python's version of a switch-case found in other
languages. It allows us to match a variable's value against a set of
patterns.

Example:

number = 2

[Link] 6/9
17/09/2025, 16:08 Conditional Statements in Python - GeeksforGeeks

match number:
case 1:
print("One")
case 2 | 3:
print("Two or Three")
case _:
print("Other number")

Output:

Two or Three

Related Links:

Python Control Flow Quiz


Check multiple conditions in if statement
Python if AND

Recommended Problems:

If conditional statement
Mark Even and Odd
The Else Statement
Odd or Even
Greatest of Three
Calculator
Closest Number
Factorial
Check Prime
Fibonacci Number
Check Strong Number

[Link] 7/9
17/09/2025, 16:08 Conditional Statements in Python - GeeksforGeeks

if, else and elif in Python Visit Course

Comment More info

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

Company Explore
About Us POTD
Legal Job-A-Thon
Privacy Policy Connect

[Link] 8/9
17/09/2025, 16:08 Conditional Statements in Python - GeeksforGeeks

Careers Community
Contact Us Blogs
Corporate Solution Nation Skill Up
Campus Training Program

Tutorials Courses
Programming Languages IBM Certification
DSA DSA and Placements
Web Technology Web Development
AI, ML & Data Science Data Science
DevOps Programming Languages
CS Core Subjects DevOps & Cloud
Interview Preparation GATE
GATE Trending Technologies
School Subjects
Software and Tools

Offline Centers Preparation Corner


Noida Aptitude
Bengaluru Puzzles
Pune GfG 160
Hyderabad DSA 360
Patna System Design

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

[Link] 9/9

You might also like