Python if statements, if else statements and conditions tutorial

By Admin

Python if statements, if else statements and conditions tutorial

March 29 10:01PM • 5 min read

In this Article You will:

If statements are nessecary in python if you want a block of code to run if a certain condition is fulfilled. This can be useful when you want your code to run at a certain moment. For example, the following code will only print "So many Apples!" if the value of the "apple" variable is greater than 2:


if apple >= 2:
    print("So many Apples!")
            

If statement syntax

In python, the If statement follows a syntax of:


if condition:
    action 
    
Where an action will be performed if a condition is true. There are several mathematical operators in python that will compare different variables/numbers such as:
  • a >= b
  • a < b
  • a == b
These will compare the value of two things and return either true or false. For example, with "a >= b" the >= operator returns true only if a is greater than or equal to b, the <= operator in "a < b" returns true only if a is less than b, and with "a == b" the == operator returns true if a is equal to b.

Going back to the if statement syntax, our condition will return a boolean value, either true or false. If our condition returns true, then the code, or action, part of our if statement will execute.

Conditions demonstration


#create a variable called number of apples and set it to 7
number_of_apples = 7
            
To start, we create a variable called number_of_apples and set its value to be equal to the number 7.

number_of_apples > 7
            
Run this code. You should get this output:

False
            
As stated above, the > operator compares the value of the variable "number_of_apples" and 7, and returns True if the value of the variable "number_of_apples" is greater than 7. Because the value of the "number_of_apples" variable is not greater than 7, it returns False.

Now run this code:

number_of_apples >= 7
            
You should get the output:

True
            
The >= operator returns True because the value of the variable "number_of_apples" is greater than or equal to 7. In this case, as the value of the variable "number_of_apples" is equal to 7, it returns True. Lets now write a complete if statement:

if number_of_apples >= 7:
    print("Yay, so many apples!")
            
Here, we have our condition (number_of_apples >= 7). This condition returns True, so the action, or code inside the if statement, will execute, printing:

Yay, so many apples!
            

Else Statements

Else statements are used in conjunction with if statements. We can combine them to check for multiple conditions. The syntax is as follows:


if condition:
    action1
else:
    action2

Here, the second action, or code inside the else statement (denoted as action2) will be executed if the condition returns False.

For example, if we wanted to create a code to print "Not enough apples!" if we had less than 7 apples but also be able to print "Too many apples!" if we had more than 7 apples", we would need to use an else statement:

if number_of_apples < 7:
    print("Not enough apples!")
else:
    print("Too many apples!")
            
As the value of the variable "number_of_apples" is set to 7, the condition "number_of_apples < 7" will return False as we have more than 7 apples. This means that the action, or code in the if statement will not execute.

Here, the code inside the else statement is triggered and executes because the condition of the if statement returned False. This means that our code will print:

Too many apples!
                        

To recap, if statements check if a condition is True and execute an action (run a block of code). We can use an else statement to execute another action (run another block of code) if the condition is False. Congratulations for reaching the end of this article! you should now have a basic understanding of if and else statements.