Loops, functions, and methods have allowed us to deal with a wide range of problems. Usually, these problems have a simple logic flow, which usually involves taking data, performing some action, then outputting the data in some manner (print, write, etc.). Although these skills are important, there are still many problems we are unable to solve.
Digital logic provides a way of thinking that allows us to break complex (often non-numerical) problems into smaller problems, all which evaluate to one of two possible values. The two values are True, and False. These are represented in Boolean data types.
A Boolean is a type in Python, just like a Float or a String is a type. “Bools” or Boolean data are named after George Boole, the logician who prototyped boolean logic.
A Boolean is a type that only has two possible states. The states are True and False. These can also be represented by the digits 1 (True) and 0 (False). For the purposes of this course, we will refer to them as True and False whenever possible.
Let’s define a boolean variable:
myBool = True
yourBool = False
Remember that the True and False operators in Python must start with a capital letter!
- Any Number except zero evaluates to “True”
- Any String except the empty string evaluates to “True”
- The NULL operator (in Python: “None”) evaluates to False
A boolean expression is a Python expression that evaluates to either True or False. Much like Python’s mathematical operations, which evaluate to mathematical data types such as Int or Float, the Python interpreter calculates them inline.
x = 8*6**(9/12.0) # This is a mathematical expression. x is type Float.
y = "This is" + " a string." # This is a string expression. y is a String.
z = 1 > 0 #This is a boolean expression. z is a Bool.
# Is "z" True or False?
Boolean statements use operators such as “<“, “>”, “and”, “or”, and “==”.
w = 1 > 0
x = 1 < 0
y = (1 > 0) and (1 < 0)
z = (1 > 0) or (1 < 0)
a = 0 == 1
b = 1 == 1
Try these yourself in the Shell. Do you understand why the values of w, x, y, and z are True or False? When you’re done, take a look at the output:
If it is unclear why these values are returned, let’s break the problem down:
Breaking it Down
*Note: Keep in mind that I will use “AND” and “OR” to make my references to these operators clear. However, in Python, the correct syntax is without capital letters (“and”, “or”).
We know it is fact that 1 > 0. Therefore, we expect this value to evaluate to True.
It is true that 1 is greater than 0.
We also know from the same logic that 1 < 0 should be False.
It is false that 1 is less than 0.
That explains the first two examples, w and x. Let’s look further into our values for y and z.
To understand all the operators, let’s use a a few examples.
The “!=”, “==”, “>”, “<” Operators
These operators are used to evaluate the difference between two variables. They are used just like you would think. The only confusing operator is “==” which compares the equivalence of two values, and is often confused with the “=” operator, which sets a variable equal to some value. Another confusing operator is “!=”, which is True if both elements are not equal.
The AND Operator
x and y # True if both x and y are True
Let’s say your 1P03 group has three members. Member1, Member2, and Member3. You are being graded by a computer that checks if each member is in attendance by using the function memberAttended(member) where member is a group member. This function returns True if the member has attended, and False otherwise.
If all group members must attend a tutorial, you can check if you have succeeded the requirement by checking:
memberAttended(Member1) and memberAttended(Member2) and memberAttended(Member3)
If this evaluates to True (True and True and True), then you pass. Otherwise, it evaluates to false (ex. False and True and True) because Member1 did not attend, you will fail!
The OR Operator
x or y # True if either x or y are True
If only one of your group members must be present to hand in an assignment, you can check if you have succeeded the requirement by checking:
memberAttended(Member1) or memberAttended(Member2) or memberAttended(Member3)
AND and OR
Even further, you can check if the assignment was handed in by the function assignmentSubmitted(), and can rearrange the previous example:
(memberAttended(Member1) or memberAttended(Member2) or memberAttended(Member3)) and assignmentSubmitted()
|-------------------------Only one needs to be true---------------------------| |-Needs to be true--|
The brackets are evaluated just like they are in arithmetic.
The IF statement
The IF statement is the basic framework for computation based on boolean logic. It executes code based on a boolean statement.
The basic structure for an if statement is as follows:
# Do something
This runs the code in the “Do something” section IF AND ONLY IF the term statement returns “True”. Let’s look at a basic example:
x = 0
if (x == 0):
if (x == 1):
What will print? Try running this in IDLE.
ANY Boolean statement can be used in the IF statement. That means as long as (statement) evaluates to True, it will run. Otherwise, it will not. We can use the function bool() to check the Boolean value of something.
The elif statement is a little different. It can be read as “else, if”. For example:
# Do something
# Do something else
This can be read as “If some statement is true, do something. Else, if some other statement is true, do something else.”
The else statement is just like elif, but accounts for ALL other cases. It can be read as “else” or “otherwise”. For example:
# Do something
# Do something else
This can be read as “If some statement is true, do something. Otherwise, do something else.”
This is a great way to “catch” certain interesting cases that variables may have. (Maybe catching a variable to check if it is set to zero before dividing)
I will update this section more. IF statements should be fairly easy, and your textbook has a great section on them. Attempt the practice problems below and let me know if I need to elaborate the
What is the result of the following statement? (Do not use the Shell)
True and True and (False or False or True)
What will print when running the following code?
x = 5
y = 100
statement = (x>y) or (y==0)
First, the program checks if the statement is true. We can see that it is not true, since x is not greater than y, and y is not equal to zero. Therefore, the first IF statement evaluates to False and does not run. The second statement is elif, which only occurs if the IF fails. Since it did, the elif statement will check its statement. The “!” operator inverts the result. Therefore, the statement is now True, and it prints. Since this case is not accounted for, else will never run. We then enter a whole new IF statement, which will evaluate to True, and print “four”.