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
>>> bool(100) True >>> bool(0) False >>>
- Any String except the empty string evaluates to
>>> bool("Hello") True >>> bool("") False >>>
- The NULL operator (in Python:
None) evaluates to
>>> bool(None) 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.
# This is a mathematical expression. x is type Float. x = 8*6**(9/12.0) # This is a string expression. y is a String. y = "This is" + " a string." #This is a boolean expression. z is a Bool. # Is "z" True or False? z = 1 > 0
Boolean statements use operators such as
a = 0 == 1 b = 1 == 1 w = 1 > 0 x = 1 < 0 y = (1 > 0) and (1 < 0) z = (1 > 0) or (1 < 0)
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:
>>> a False >>> b True >>> w True >>> x False >>> y False >>> z True
If it is unclear why these values are returned, let’s break the problem down.
Breaking it Down
We know it is fact that
1 > 0. Therefore, we expect this value to evaluate to
It is true that 1 is greater than 0.
We also know from the same logic that:
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.
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.
x and y # True if both x and y are True
Let’s say your 1P03 group has three members.
Member3. You are being graded by a computer that checks if each member is in attendance by using the function
member is a group member. This function returns
True if the member has attended, and
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 and True and True), then you pass. Otherwise, it evaluates to
False and True and True) because
Member1 did not attend, you will fail!
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)
if statement is the basic framework for computation based on boolean logic. It executes code based on a boolean expression.
The basic structure for an if statement is as follows:
if (expression): # Do something
This runs the code in the “Do something” section IF AND ONLY IF the term
True. Let’s look at a basic example:
x = 0 if (x == 0): print "zero" if (x == 1): print "one"
What will print? Try running this in IDLE.
ANY Boolean statement can be used in the
if statement. That means as long as (expression) evaluates to
True, it will run. Otherwise, it will not. We can use the function
bool() to check the Boolean value of something.
elif statement is a little different. It can be read as else, if. For example:
if (expression1): # Do something elif (expression2): # 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.”
else statement is just like
elif, but accounts for ALL other cases (it takes no expression and assumes
True). It can be read as “else” or “otherwise”. For example:
if (expression): # Do something else: # Do something else
This can be read as “If some statement is true, do something. Otherwise, in any other case, 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)
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