Python | if statement
If a particular condition evaluates to true, Python will perform a particular action:
if condition_is_true:perform particular action
To check if a person is old enough to buy alcohol and tell them if they are:
age = 21if age >= 20:print("We can confirm you are old enough to buy alcohol.")We can confirm you are old enough to buy alcohol.
If they are not old enough nothing will be printed as the
if statement evaluates to false:
All indented lines after an
if statement will be executed if the condition evaluates to true.
Multiple If Statements
You can use multiple
if statements to check multiple conditions:
Adding bread to your sandwichAdding tomato to your sandwichFinished making your sandwich