Module 3: Use Boolean Logic
https://learn.microsoft.com/en-us/training/modules/python-boolean-types/
Introduction
Booleans are a common type in Python showLineNumbers. Their value can only ever be one of two things: true or false. Understanding how to use Boolean values is critical, because you need them to write conditional logic.
You will learn if, else, elif
commands and branching
You will also learn and
and or
If Statements
When you write an if statement, you're typically going to do a compare statement. Those compare statements will look like one of these:
- Equals:
a == b
- Not Equals:
a != b
- Less than:
a < b
- Less than or equal to:
a <= b
- Greater than:
a > b
- Greater than or equal to:
a >= b
Test expressions
You want to write an expression that checks the condition and then runs a chunk of code only when that condition is met.
a = 102
b = 13
# test expression
if a < b:
# statement to be run
print(b)
# output nothing
The above program will not print anything because the if statement always returns false. The following will return true and will print the results of the if statement:
a = 102
b = 27
if a >= b:
print(a)
# output is 102
Note: Watch your indentation. The if statement here is false, so it doesn't print(a), but print(b) isn't part of the if statement, so it runs.
a = 24
b = 89
if a <= 0:
print(a)
print(b)
# output 89
Else and elif statements
These are used when your expression is false, so you want to not run the inside of the if, but something else. And you want that something else to only run if the if condition is false.
a = 27
b = 93
if a >= b:
print(a)
else:
print(b)
# output 93
The syntax is always this:
if test_expression:
# statement(s) to be run
else:
# statement(s) to be run
Elif
Elif is a way to chain a bunch of if statements together.
a = 27
b = 93
if a <= b:
print("a is less than or equal to b")
elif a == b:
print("a is equal to b")
THe syntax for this is as follows:
if test_expression:
# statement(s) to be run
elif test_expression:
# statement(s) to be run
Combining if, elif, and else statements
You can combine all 3 for some pretty powerful code. At least before you learn what a case or switch statement is.
a = 27
b = 93
if a < b:
print("a is less than b")
elif a > b:
print("a is greater than b")
else:
print ("a is equal to b")
This uses the following syntax:
if test_expression:
# statement(s) to be run
elif test_expression:
# statement(s) to be run
elif test_expression:
# statement(s) to be run
else:
# statement(s) to be run
more nested conditional logic
If you want to dive a bit deeper and get more complex, you can also nest these statements
a = 16
b = 25
c = 27
if a > b:
if b > c:
print ("a is greater than b and b is greater than c")
else:
print ("a is greater than b and less than c")
elif a == b:
print ("a is equal to b")
else:
print ("a is less than b")
Here's a small sample of that type of syntax:
if test_expression:
# statement(s) to be run
if test_expression:
# statement(s) to be run
else:
# statement(s) to be run
elif test_expression:
# statement(s) to be run
if test_expression:
# statement(s) to be run
else:
# statement(s) to be run
else:
# statement(s) to be run
Exercise:
You can compare an object's size
object_size = 10
if object_size > 5:
print('We need to keep an eye on this object')
else:
print('Object poses no threat.')
# Output: We need to keep an eye on this object
And and Or operators
You can check multiple expressions for a True or False result.
a = 23
b = 34
if a == 34 or b == 34:
print(a + b)
And operator
a = 23
b = 34
if a == 34 and b == 34:
print (a + b)
# output: none
# sub-expression1 and sub-expression2
Or operator
a = 23
b = 34
if a == 34 or b == 34:
print (a + b)
# output: 78
Differences between And and Or
And and OR have different truth tables. And means that all need to be true in order for the expression to return true Or means that one needs to return true in order for the expression to return true.
Exercise
Work through the logic of the following application:
object_size = 10
proximity = 9000
if object_size > 5 and proximity < 1000:
print('Evasive maneuvers required')
else:
print('Object poses no threat')
Summary
Boolean expressions and statements are a key part of creating complex Python showLineNumbers programs, where you want your code to run only under certain conditions.
Here are some of the essentials you now know about conditional logic:
- Booleans can have one of two values, True or False.
- if, else, and elif statements can be used to run code under specific conditions.
- and and or operators can be used to combine test expressions and create more complex conditions.