4.14. Group Work  Conditionals and Logic¶
It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home.
Note
If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
Content Learning Objectives
After completing this activity, students should be able to:
Evaluate boolean expressions with comparison operators (<, >, <=, >=, ==, !=).
Explain the syntax and meaning of if/else statements and indented blocks.
Evaluate boolean expressions that involve comparisons with and, or, and not.
Process Skill Goals:
During the activity, students should make progress toward:
Evaluating complex logic expressions based on operator precedence (Critical Thinking).
4.14.1. Comparison Operators¶
In Python, a comparison (e.g., 100 < 200) will yield a Boolean value of either True
or False
.
Most data types (including int
, float
, str
, list
, and tuple
) can be compared
using the following operators:
Less than (<), less than or equal (<=), greater than (>), greater than or equal (>=), equal (==), and not equal (!=).
Run this code to see what it prints. Each line of code may or may not print something.
 True
 Incorrect! Although the variable "three" does equal "four", the == operator has a return value, while the = operator does not. Try again.
 False
 Incorrect! "three" has been set equal to "four", but this is irrelevant because the = operator does not have a return value. Try again.
 class 'bool'
 Incorrect! "print(type())" prints output that looks like that, not just "print()". Try again.
 TypeError
 Correct! "three = four" (with a single equals sign) doesn't return anything, so it cannot be printed and a TypeError occurs.
Q2: What would be printed if the commented line above was uncommented?

Q4: Match examples from the previous code block to their appropriate term.
Incorrect  keep trying!
 check
 Boolean variables
 <, ==, >
 Boolean operators
 3 < 4, three == four, three > four
 Boolean expressions
 5 != 6
 Incorrect! 5 does not equal 6, which makes this expression True. Try again.
 2 + 2 != 5
 Incorrect! 4 does not equal 5, so this expression is True. Try again.
 4 + 6 != 11  1
 Correct! 10 equals 10, so the statement "10 != 10" is False.
Q5: Which of these Boolean expressions evaluates to False
?
4.14.2. if/else Statements¶
An if
statement makes it possible to control what code will be executed in a
program, based on a condition. For example:
Run this code to see what it prints.
Python uses indentation to define the structure of programs. The line indented
under the if
statement is executed only when number < 0
is True
.
Likewise, the line indented under the else
statement is executed only when
number < 0
is False
.
Statements that are indented under an if/else statement are executed based on the
status of the if’s condition. Statements indented at the same level as the if/else
statement later in the program are always executed. If you indent code incorrectly
or inconsistently, a SyntaxError: unexpected indent
may be in your future.
Modify this code to print (number) is 10
if number
equals 10, and (number) is not 10
otherwise.
 True
 Incorrect! An if statement does not necessarily need to be followed by an else statement. Try again.
 False
 Correct! An else statement must follow an if statement, however.
Q10: True or False: An if
statement must always be followed by an else
statement.
4.14.3. Boolean Operations¶
Expressions may include Boolean operators to implement basic logic. If all three
operators appear in the same expression, Python will evaluate not
first, then
and
, and finally or
. If there are multiple of the same operator, they are
evaluated from left to right.
Run this code to see what it prints.
 bool, bool
 Correct! The type of each is bool; both are Boolean expressions.
 True, bool
 Incorrect! True is not a data type. Try again.
 True, True
 Incorrect! True is not a data type. Try again.
 bool, True
 Incorrect! True is not a data type. Try again.
Q12: What data type would be the result of a < b
? What about the result of a < b and b < c
? Use the values of a
, b
, and c
from the code block above.
 True, True
 Correct! The value of each statement is True.
 True, False
 Incorrect! 4 is less than 5, so "b < c" is True. Try again.
 False, False
 Incorrect! 3 is less than 4 and 4 is less than 5. Try again.
 False, True
 Incorrect! 3 is less than 4, so "a < b" is True. Try again.
Q13: What would be the value of a < b
? What about the value of a < b and b < c
? Use the values of a
, b
, and c
from the code block above.
 True, True
 Incorrect! "and" only returns True if the expressions on both sides are True. Try again.
 True, False
 Correct! "and" only returns True if the expressions on both sides are True and returns False in any other situation.
 False, False
 Incorrect! "and" only returns True if the expressions on both sides are True. Try again.
 False, True
 Incorrect! "and" only returns True if the expressions on both sides are True. Try again.
Q14: If two True
Boolean expressions are compared using the and
operator, what is the resulting Boolean value? What if you compare two False
expressions instead?
 True, True
 Incorrect! "or" only returns True if the expressions on one or both sides are True. Try again.
 True, False
 Correct! "or" only returns True if the expressions on one or both sides are True and returns False if both sides are false.
 False, False
 Incorrect! "or" only returns True if the expressions on one or both sides are True. Try again.
 False, True
 Incorrect! "or" only returns True if the expressions on one or both sides are True. Try again.
Q15: If two True
Boolean expressions are compared using the or
operator, what is the resulting Boolean value? What if you compare two False
expressions instead?
 True, True
 Incorrect! "and" only returns True if the expressions on both sides are True. Try again.
 True, False
 Incorrect! "or" returns True if the expressions on one or both sides are True. Try again.
 False, False
 Incorrect! "or" returns True if the expressions on one or both sides are True. Try again.
 False, True
 Correct! "and" needs both sides to be True, while "or" only needs of of them.
Q16: If a True
and a False
Boolean expression are compared using the and
operator, what is the resulting Boolean value? What if you use the or
operator instead?
Suppose you wanted to print the sum of x
and y
only when both x
and y
are positive. Write a block of code to achieve this that uses only one if
statement.
Rewrite your code from the previous code block using the not
operator. Your answer should yield the same result as before, not the opposite, and still only use one if
statement. Hint: you’ll need to change the > signs!
Suppose that you instead wanted to print the sum of x
and y
except when both x
and y
are positive. Write a block of code to achieve this that only uses one if
statement.
If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.
The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.