Conditional Statements
This lesson will introduce conditional statements and focus on how to use them.
We'll cover the following...
Conditional statements in Python
A conditional statement controls the flow of execution in a program based on a Boolean expression. If the expression evaluates to True
, then a different piece of code might execute, If it is False
, then some other piece of code will execute. This is also explained in the following illustration.
These statements give the computer the ability to decide the flow of execution of the program based on the information it receives. There are three kinds of conditional statements:
if
if
-else
if
-elif
-else
if
statement
It is the simplest conditional statement. The structure of the statement is:
if (condition):
code
If the condition provided is satisfied, then the code is executed, otherwise, it is skipped. The colon, :
, after the condition is necessary as it ...