You can use if statements to run code if a certain condition holds.
If an expression evaluates to True, some statements are carried out. Otherwise, they aren’t carried out.
An if statement looks like this:
Here is an example if statement:
if 10 > 5:
print(“10 greater than 5”)
The expression determines whether 10 is greater than five. Since it is, the indented statement runs, and “10 greater than 5” is output. Then, the unindented statement, which is not part of the if statement, is run, and “Program ended” is displayed.
10 greater than 5
Notice the colon at the end of the expression in the if statement.
To perform more complex checks, if statements can be nested, one inside the other.
This means that the inner if statement is the statement part of the outer one. This is one way to see whether multiple conditions are satisfied.
num = 12
if num > 5:
print(“Bigger than 5”)
if num <=47:
print(“Between 5 and 47”)
Bigger than 5
Between 5 and 47
CLICK ON THE BUTTON BELOW TO GO TO THE PYTHON MAIN COURSE PAGE.
©️ License: All Rights Reserved
CONTACT HOLLA TECH – LEARN SUPPORT