Chapter 4, Lesson 1 Text URL

Chapter Four: Making Decisions

Lesson One: Logical Expressions

 

You are familiar with variables and expressions that hold integers (like 10), floating point numbers (1.732) or strings ("orange"). In this chapter, you are going to learn all about Boolean values and the ways they are used in a computer program. Your code can use Boolean values or Boolean expressions to select blocks of code to run or make other decisions.

Boolean Values and Variables

Integers, floating point numbers and strings can all hold a wide variety of data. However, a Boolean data type can hold only two different values. That's right, an entire data type is defined that can store only two things! A Boolean can hold the value True or the value False, but nothing else.

To declare a Boolean variable, simply use a normal variable name and assignment statement. The value to the right must be either True or False, or some expression that evaluates to True or False.

You need to write True with a capital "T" and False with a capital "F". Do not use any quote marks around True or False. These are keywords in Python that have special meaning.

The example below declares a Boolean variable, sets it to True, and prints out both the variable value and the data type. Try it and see for yourself. Then, change the value to False and run the code again. Did you get the expected results?

Try It Now

You can try changing the assignment statement to use any value other than True or False - like 1, 0, "T", or "false". Notice that the resulting data type of myBoolean is no longer a Boolean, because you have stored non-Boolean data inside the variable.

Logical Expressions

It is possible to ask all sorts of questions that only have two possible answers like True or False. You can think of True as a "Yes" answer and False as the "No" answer. These kinds of questions are called logical expressionswhen written in code. The True or False answer they produce can be stored in a Boolean variable or used in some other statement.

Logical expressions in Python are often formed using a comparison as shown below. The greater than sign (>) will compare the value on the left and the value on the right and return either True (if the left is greater) or False(if the right is greater or equal).

Try It Now

What happens when you change the right number (25) to a value like 35 that is greater than the left number? You should see False produced in that case, because the answer to the question "is 30 greater than 35" is False.

Logical expressions like "30 > 25" are not very interesting, because the answer will never change. However, logical expressions can use any combination of fixed numbers and variables to produce results that change depending on the values in the variables. The example below declares two variables, input1 and input2, and then uses them logical expressions. You can change the value of the variables and the code below will produce different results; try it and see!

Try It Now

What happens when you change the input variables so input2 is greater than or equal to input1? You should see the True and False outputs change.

Notice that the True or False answer produced by a logical expression does not have to be stored in a Boolean variable. You can pass the expression results directly into another function like print(). You can also use these expressions to make decisions, as you'll learn in the next lesson.

Comparison Operators

We have demonstrated how to use the greater than sign (>) to compare two values. All logical expressions that compare two values will use a similar comparison operator. Comparison operators are symbols like > that tell Python to compare the left and right sides and produce True or False, depending on the values.

Equal To (==)

The equal to operator is formed with two equals signs together (==). The result of this operator is True if both sides of a logical expression are the same. Run the code below to see the results.

Try It Now

Not Equal To (!=)

The not equal to operator use an exclamation point and equals sign together (!=). The result of this operator is True if the two sides of the expression are not equal to each other. The result is False if they are equal to each other. See for yourself in the sample code below.

Try It Now

Less Than (<)

The less than operator (<) will return True if the left side of the expression is smaller than (less than) the right side. The expression is False if the left side of the equation is not less than the right side.

Try It Now

Greater Than (>)

The greater than (>) operator will return True if the left side of an expression is larger than the right side. The expression is False if the left side is not greater than the right side.

Try It Now

Less Than or Equal To (<=)

The less than or equal to (<=) operator will produce True if the left side of an expression is smaller than or equal to the right side. The result is False if the left side is not smaller or equal to the right side.

Try It Now

Greater Than or Equal To (>=)

The "greater than or equal to (>=) operator will return True if the left side of an expression is larger than or equal to the right side. The result is False if the left side is not larger than or equal to the right side.

Try It Now

Logical expressions that produce Boolean results are very important in most computer programs. The operator symbols may seem strange at first, but you should quickly become comfortable reading expressions and understanding how the True or False result will be produced.

Math Expressions vs. Logical Expressions

Sometimes, it's easy to confuse mathematical and logical operators. Math expressions and logical expressions may appear similar, but they produce very different results.

math expression will use a mathematical operator such as + (addition)- (subtraction)* (multiplication)or / (division). Math expressions result in a number value. Each of the examples below is a mathematical expression.

1 + 2
age – 3
5 * 2 + playerScore
3.14159 / 32.1

logical expression will use one of the comparison operators like < (less than)== (equal to), and so on. Logical expressions will produce a Boolean True or False result.

playerScore > 10
4 < 3 
5 == age

Mathematical expressions are used when you need to calculate a new numeric result. Logical expressions are used when you are asking a question or testing a condition to produce a True or False result. You'll make good use of logical expressions in the next lesson when your program starts to make decisions by asking logical questions.

 

Work with Me: Comparison Challenge

 

The following code sets up a few input variables and then uses them in a variety of logical expressions. The results of each logical expression (such as gameCost == posterCost) is sent directly to a print() function to display the True or False value on the screen.

Can you correctly predict the results of each expression before you run the code? Study the code carefully and come up with your answers. Then run the program to see the actual results.

Try It Now

Did you correctly predict the results in every case? If not, review the description of the comparison operator until you are confident of the results.

Then, try changing the input variables to see how new values modify the results. What happens if you make gameCost equal to posterCost? What happens if you make your savings greater than or less than the gameCost or posterCost?

End of Lesson


Last modified: Wednesday, 8 May 2019, 8:15 AM