How to use if in Python

1 min readGetting StartedFundamentals
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

If clauses are one of the things you will use the most in Python.

There are few variations that you can use.

If and else

The simplest one, contains one condition and if the condition is not met the execution falls to the else statement.

Here is an example.

age = 25

if age >= 18:
	print("Adult")
else:
	print("Not an adult")
Simple if clause

If and else with 2 conditions

It is totally feasible to add multiple conditional statements within the same if clause.

Here is an example.

has_motorbike = True
has_car = True

# If she has either the motorbike permit or car permit she can drive
if has_motorbike or has_car:
	print("Can drive")
else:
	print("Cannot drive")
If clause with two conditions

If, elif and else

Sometimes one conditional statement might not be enough.

To solve that, you can use the if/elif/else.

Where elif is the in between statement between your if and else.

You can use the elif  as much as you want to add other in-between conditions.

age = 25

if age >= 65:
	print("Retired")
elif age >= 18:
	print("Adult")
else:
	print("Not an adult")
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.