Lesson 2:
Conditional Statements &
Boolean Logic
In the programs we have seen till now, there has always been a series of statements faithfully executed by Python in exact top-down order. What if you wanted to change the flow of how it works? For example, you want the program to take some decisions and do different things depending on different situations, such as printing 'Good Morning' or 'Good Evening' depending on the time of the day?
In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Conditional statements give us this ability.
In order to check a condition, we need to be able to compare different values and determine whether the condition is true or false. To do these actions, we first need to know about Boolean expressions, comparison operators and logical operators.
Boolean Expressions
A Boolean expression is an expression that is either true or false. For example:
5 equals 5
This statement would evaluate as True. The number five does equal the number five. How about this one:
5 equals 6
This statement would evaluate as False. The number 5 is not the same value as the number 6.
When writing in code, Python uses the comparison operator == to
represent the word "equals." Our two examples above may be re-written in Python
as:
| Code | Evaluates As |
|---|---|
5 == 5 |
True |
5 == 6 |
False |
Note the capitalization of the names. True and
False are special values that belong to the type
bool; they are not strings:
>>> type(True) <class 'bool'> >>> type(False) <class 'bool'>
Boolean Data Types
You can also assign a variable directly to the data type of
bool:
| Code | Evaluates As |
|---|---|
is_enabled = True |
True |
my_variable = False |
False |
Comparison Operators
The equals operator == is one of several comparison
operators. We use comparison operators to compare different objects –
be it strings, numbers, integers, variables, or other data types. Here are the
most common:
== (equal to)
This compares whether the objects are equal. x == y means that
x is equal to y. When you use x == y, you
are asking the question "Is x equal to y?".
A common error is to use a single equal sign (=) instead of a double equal
sign (==). Remember that = is an assignment operator and
== is a comparison operator.
| Code | Output | Notes |
|---|---|---|
x = 2 |
True |
Both |
x = 'str' |
False |
Remember that strings are case sensitive. |
x = 'str' |
True |
Both values are the same, so |
!= (not equal to)
This compares whether the objects are not equal. x != y means
that x is not equal to y. When you use x !=
y, you are asking the question "Is x not equal to y?".
| Code | Output | Notes |
|---|---|---|
x = 2 |
True |
The numbers 2 and 3 are different. So the return value of |
x = 'str' |
True |
Remember that strings are case sensitive. |
x = 'str' |
False |
Both values are the same, so |
< (less than)
This compares whether the first object is less than the second object. x <
y means that x is less than y. When you use x < y, you are
asking the question "Is the value of x less than the value of y?".
Comparisons can be chained arbitrarily: 3 < 5 < 7 gives
True because 3 is less than 5 which is also less than 7.
You can also compare strings using less than, but it is complicated. The string characters are converted into ASCII decimal values and those numbers are compared. In most cases, you will not want to use less than to compare strings.
| Code | Output | Notes |
|---|---|---|
5 < 3 |
False |
5 is not less than 3, so this evaluates to false. |
3 < 5 |
True |
3 is less than 5, so this evaluates to true. |
3 < 3 |
False |
3 is not less than itself, so this returns as false. |
'a' < 'b' |
True |
In this case, |
> (greater than)
This compares if the first object is greater or larger than the second
object. x > y means that x is greater than
y. When you use x > y, you are asking the question
"Is the value of x more than the value of y?".
| Code | Output | Notes |
|---|---|---|
5 > 3 |
True |
5 is greater than 3, so this evaluates to true. |
3 > 5 |
False |
3 is not greater than 5, so this evaluates to false. |
3 > 3 |
False |
3 is not greater than itself, so this returns as false. |
'a' > 'b' |
False |
In this case, |
<= (less than or equal to)
This compares whether the first object is less than or equal to the second
object. x <= y means that x is less than or equal
to y. The "or equal to" part is important because as we saw
before, 3 < 3 evaluates as false, but 3 <= 3
would evaluate as true because or the "or equal to" part. When you use x
<= y, you are asking the question "Is the value of x less than or
the same as the value of y?"
Remember that the equals sign comes last. There is no such thing as
=<.
| Code | Output | Notes |
|---|---|---|
5 <= 3 |
False |
5 is not less than 3 and it is not equal to 3, so this evaluates to false. |
3 <= 5 |
True |
3 is less than 5, so this evaluates to true. |
3 <= 3 |
True |
3 is not less than itself, but it is equal to itself so this returns as true. |
'a' <= 'b' |
True |
In this case, |
x = 3 |
True |
3 is less than 6, so this evaluates as true. |
>= (greater than or equal to)
This compares whether the first object is greater than or equal to the second
object. x >= y means that x is greater than or
equal to y. The "or equal to" part is important because as we saw
before, 3 > 3 evaluates as false, but 3 >= 3
would evaluate as true because or the "or equal to" part. When you use x
<= y, you are asking the question "Is the value of x more than or
the same as the value of y?"
Remember that the equals sign comes last. There is no such thing as
=>.
| Code | Output | Notes |
|---|---|---|
5 >= 3 |
True |
5 is greater than 3, so this evaluates to true. |
3 >= 5 |
False |
3 is not greater than 5, so this evaluates to false. |
3 >= 3 |
True |
3 is not greater than itself, but it is equal to itself so this returns as false. |
'a' >= 'b' |
False |
In this case, |
x = 4 |
True |
4 is greater than 3, so it evaluates as true. |
