There are operators we can use in our code, other than the traditional math ones (addition, subtraction, etc). Sometimes we will want to compare the values of variables to each other! Enter the relational operators:
Relational Operators
Operator
Meaning
Usage
<
Less than
a < b
>
Greater than
a > b
⇐
Less or equal to
a ⇐ b
>=
Greater or equal to
a >= b
!=
Not equal to
a != b
==
Equal to
a == b
Remember how we talked about boolean data types, and how they can only be one of two values (which ones?).
Well… when do we actually ever SEE boolean data in our code? What is it used for?
We may want to record the result of a comparison between two different pieces of data (say, two integers, two floats, etc). This is called a condition!
Conditions
(evaluate to a value of True or False, and are stored in bool variables)
The value of the bool variable condition1 is True, because 5 is less than 10.
The value of the bool variable condition2 is False, because 20 is not equal to 30.
The value of the bool variable condition3 is True, because 45.8f is greater than or equal to 45.8f.
WARNING
The ’=’ is the ASSIGNMENT OPERATOR, used to assign a value to a variable.
The ’==’ is the EQUALITY OPERATOR, used to check for equality between two values.
Guess what? These are two COMPLETELY DIFFERENT operations, and you WILL get them mixed up.
Know the difference!
Logical Operators
| Operator | Usage|Description|
|:-:|:-:|:-:|:-:|
|and| a and b|(a and b) is true only if both a and b are true.
| or | a or b| (a or b) is true if either a or b are true.
|not| not a| (not a) is the opposite of a (true is false, false is true)
We can combine conditions using the logical operators! The truth table below can help with understanding how the conditions above work:
The value of the bool variable condition5 is false:
5 is less than 10 → this is true
20 is equal to 30 → this is false
Since one of these is false, the entire thing is false, meaning the value assigned to condition5 is false
The value of the bool variable condition6 is true:
5 is less than 10 → this is true
20 is equal to 30 → this is false
Since one of these is true, the entire thing is true, meaning the value assigned to condition6 is true
The value of the bool variable condition7 is false:
We are looking at the value of condition6, reversing it, and assigning this new value to condition7
If Statement
(for only running code if a condition is true)
if condition: #your code here! #this runs if the condition is True
If Statement, Examples
x = 5if(x < 20): x = 100
Result
Once this code is done, the variable x will have the integer value 100. When the condition is evaluated, x has a value of 5. Because the condition (x < 20) is true, the indented code will be executed, meaning x will be assigned a value of 100.
A note about indentation
Any lines of code that are indented after an if statement will be in that if statement. For example:
if condition: #This runs #Yup, this too #Also this #This is ALSO a part of the if statement#THIS is NOT part of the if statement
Other languages use curly braces to denote which lines of code are part of an if statement. But the creators of python chose to use indentation instead. Thus, when reading code, keep in mind that indentation is the way to tell if a particular line of code is part of an if statement or not.
Else Statement
(for running code when a condition is false, only pairs with an If statement)
if condition: #your code here! #this runs if the condition is Trueelse: #your code here! #this runs if the condition is False
Else Statement, Examples
myCondition = (10 <= 0)if(myCondition): print("IF IF IF IF IF")else: print("ELSE ELSE ELSE ELSE")
Output/Result
ELSE ELSE ELSE ELSE
The value of myCondition is false, because 10 is not less than or equal to 0. The code under the if statement won’t run. Since the else statement is paired with the if statement and myCondition was false, the second set of indented code will run.
anotherCondition = ((0 == 0) and (1000 > -1000))if(anotherCondition): print("IF IF IF IF IF")else: print("ELSE ELSE ELSE ELSE")
Output/Result
IF IF IF IF IF
The value of anotherCondition is true, because 0 is equal to 0 AND 1000 is greater than -1000. The code under the if statement will run. Since the else statement is paired with the if statement and anotherCondition was true, the code under the else statement will not run.
Incorrect ways to use Else
Remember, the code below is NOT correct
We didn’t pair the else statement below with an if statement
#code, not importantx = 5else: #Wrong! We didn't pair this with an If statement
Remember, the code below is NOT correct
Since there is code between the if/else statements, we didn’t pair the else statement below with an if statement
if condition: #codex = 5 #You can't put this here! Now the if/else statements aren't paired!else: #Wrong! We didn't pair this with an If statement
Elif (Else If)
We can actually combine the If statement and the Else statement into a single thing, called an elif statement. This allows us to expand our check beyond just a yes/no answer to the first if statement:
if condition1: #your code here! #this runs if condition1 is trueelif condition2: #your code here #this runs if condition1 is false, and condition2 is trueelse: #your code here! #this runs if all previous conditions (1 and 2) are false
Elif, Examples
if False: print("IF IF IF IF IF")elif(176 == (175+1)): print("MIDDLE!")else: print("ELSE ELSE ELSE ELSE")
Output/Result
MIDDLE
The first condition fails because it is always false.
The second condition will evaluate to true, because 176 is equal to (175+1).
Nested If Statements
You can also put if statements inside each other, like so:
x = 42if (x > 40): print("Whoa, over 40!") if(x > 50): print("Wow, greater than 50!") else: print("Ah, still shy of 50...")
Output/Result
Whoa, over 40!Ah, still shy of 50...
The outer condition is true because x is greater than 40, so the code inside the block will be executed
The first print call will run.
The second condition is false, since x is less than 50.
The else statement paired with the inner if statement will run, executing the second print call.
The language shouldn’t limit the number of if statements that can be nested inside each other. However, nesting too many statements will quickly turn your code into an unreadable mess!
Nested if statements and if/else/elif will be very useful, but it may not be immediately clear WHY they’re useful. As we run into more complicated examples, they’ll be used for more complex combinations of conditions that would be difficult to handle otherwise.