If Statements

Written by Anish Ahuja and Harnoor Chima (8/2/2020)

Introduction

Comparison operators are used, as is evident in the name, to compare 2 values in condition statements. There are multiple types of comparison operators. When a comparison operator is performed, the computer checks to see if the comparison is True or False. If it is True, the following operation is performed. If False, the operation is skipped. This is a Boolean check being performed here.

For this example, assume a = 5 and b = 10

Comparison Operator

Meaning

Example

==

Checks to see if 2 values are equal

a==b is False

!=

Checks if 2 values are not equal to each other

a!=b is True

>

Checks if 1 value is greater than the other

a>b is False

<

Checks if 1 value is less than the other

a<b is True

>=

Checks if 1 value is greater than or equal to the other

a>=b is False

<=

Checks if 1 value is less than or equal to the other

a<=b is True

If Statements

These comparison operators are commonly used in if statements so you can determine if a certain set of conditions is true, if they are true you can initiate an appropriate response.

x = 9

if x == 9:
    print("x is equal to 9")

Let's break down how to write an if statement, following if you write the condition you are checking for in this case it would be x is equal to 9. Then we put a colon and move to the next line, this is where indentation comes in, press tab to indent four spaces and write the response if the condition is met. Indentation is very important in python because it tells the interpreter what our code is being applied to. In this case xis equal to 9 so the statement "x is equal to 9" is printed.

Else If

Else if is used when you want to check for more than one condition in an if statement.

y = 5

if y < 2:
    print("y is less than 2")
elif y > 4:
    print("y is greater than 4") 

In this example, y is equal to 5 and our first test sees if y is less than 2 which is not true in this case so it moves on to the next condition. This condition checks if y is greater than 4 which is true so it prints the statement, "y is greater than 4."

Else

However, what if y is not less than 2 or greater than 4? This is a situation in which else would be used. You can use else to make sure that your program gives a response if the given conditions are not actually met.

z = 9

if z < 7:
    print("z is less than 7")
elif z > 13:
    print("z is greater than 13")
else:
    print("z is in between 7 and 13")

In this situation neither of the two conditions are met so the program automatically moves on to the else response and prints the statement, "z is in between 7 and 13."

Here's an example of these operators in action:

a = 5
if a == 5:
    print("a is equal to 5")

b = 18.5
if apple >= 18:
    print ("b is more than 18")

x = 38.9090
y = 0
if x != y:
    print ("x is not y")

If using a Boolean, it is unnecessary to write Boolean == True in the condition. Instead, simply writing the variable name means True, and entering not before the name means False

guess = True
powerState = False

if (guess):
    print ("The guess is correct")
if (not powerState):
    print ("The power is off :(")

Conclusion

Comparison Operators are important as they allow you to write if statements, which are frequently used in programming.

Last updated