= 4
semesters if semesters == 4:
print("Data Science Master's program is 4 semesters.") # Data Science Master's program is 4 semesters.
Data Science Master's program is 4 semesters.
✅ Conditionals let your code make decisions. With if
, elif
, and else
, you can control which blocks of code run based on whether a condition is True
or False
.
if
StatementRuns a block of code only if the condition is True
.
else
StatementUse else
to run code when the if
condition is False
.
elif
StatementUse elif
(short for “else if”) to check additional conditions.
You can combine conditions using and
, or
, and not
.
gpa = 3.7
has_internship = True
has_research_experience = False
number_of_projects = 6
if gpa > 3.0 and has_internship:
print("You have good chances of finding a full-time job!") # You have good chances of finding a full-time job!
if gpa > 3.5 or has_research_experience:
print("You have good chances of finding a research opportunity!") # You have good chances of finding a research opportunity!
if not number_of_projects < 5:
print("You have completed a solid number of projects!") # You have completed a solid number of projects!
You have good chances of finding a full-time job!
You have good chances of finding a research opportunity!
You have completed a solid number of projects!
You can nest conditionals inside each other.
In conditionals, Python treats certain values as False
:
False
, None
, 0
, ""
, []
, {}
, set()
All other values are truthy.
Some frequent errors encountered when dealing with Conditional Statements in Python.
Cell In[8], line 2 if semesters == 4 ^ SyntaxError: expected ':'