Lesson 2 ❮ Lesson List ❮ Top Page
❯ 2.4 if Statement
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳ Video 6m 59s
☷ Interactive readings 5m
An if statement is written by using the if keyword. Just like for, indentation and colon (:) is important here.
Sometimes we want to declare an action when the condition holds, and something else when it does not hold. You can use if-else syntax in this case.
Many real-world situations have more than two possible conditions. In this case, this if-elif-else chain can be used.
In a long if chains, it is useful to put the conditions into variables.
The main focus in if statement is the condition.
Don't compare boolean values to True or False using ==.
Split long conditions into few lines.
else does not need a condition. If you want to use a second condition, use elif instead. Replace else with elif to fix this error.