Holla Tech - Learn

if Statements
 

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: 

if expression:
   statements 

 

NOTE!
Greater than and smaller than operators can also be used to compare strings lexicographically (the alphabetical order of words is based on the alphabetical order of their component letters).

if Statements
 

Here is an example if statement:

if 10 > 5:
   print(“10 greater than 5”)

print(“Program ended”

 

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.

Result:

>>
10 greater than 5
Program ended
>>> 

 

Notice the colon at the end of the expression in the if statement.

NOTE!
As the program contains multiple lines of code, you should create it as a separate file and run it.

if Statements
 

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.

For example:

num = 12
if num > 5:
   print(“Bigger than 5”)
   if num <=47:
      print(“Between 5 and 47”

 

Result: 

>>
Bigger than 5
Between 5 and 47
>>> 

 


BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE PYTHON MAIN COURSE PAGE. 

PYTHON MAIN COURSE PAGE

 


©️ License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT