How to use if in Python
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.
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.
If and else with 2 conditions
It is totally feasible to add multiple conditional statements within the same if clause.
Here is an example.
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")
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.
Related Articles
Continue your learning journey with these related topics
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. 📚