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 1 .
Note4.14.1.
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:
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).
Subsection4.14.1Comparison 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 (!=).
Checkpoint4.14.2.
Run this code to see what it prints. Each line of code may or may not print something.
Checkpoint4.14.3.
Q-2: What would be printed if the commented line above was uncommented?
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.
Checkpoint4.14.4.
Q-3: What is the name of the data type that represents Boolean values?
Checkpoint4.14.5.
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
Checkpoint4.14.6.
Q-5: Which of these Boolean expressions evaluates to False?
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.
Subsection4.14.2if/else Statements
An if statement makes it possible to control what code will be executed in a program, based on a condition. For example:
Checkpoint4.14.7.
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.
Checkpoint4.14.8.
Q-7: Which line of the previous code block contains a Boolean expression?
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.
Checkpoint4.14.9.
Q-8: What must each line preceding an indented block of code end with?
Checkpoint4.14.10.
Modify this code to print (number) is 10 if number equals 10, and (number) is not 10 otherwise.
Checkpoint4.14.11.
Q-10: True or False: An if statement must always be followed by an else statement.
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.
Subsection4.14.3Boolean 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.
Checkpoint4.14.12.
Run this code to see what it prints.
Checkpoint4.14.13.
Q-12: 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.
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.
Checkpoint4.14.14.
Q-13: 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
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.
Checkpoint4.14.15.
Q-14: 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! "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.
Checkpoint4.14.16.
Q-15: 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! "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.
Checkpoint4.14.17.
Q-16: 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?
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.
Checkpoint4.14.18.
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.
Checkpoint4.14.19.
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!
Checkpoint4.14.20.
Suppose that you instead wanted to print the sum of x and yexcept 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.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=cond_logic_groupsub data-size_limit=3> <div class="col-sm-6"> <select id="assignment_group" multiple class="assignment_partner_select" style="width: 100%"> </select> </div> <div id="groupsub_button" class="col-sm-6"> </div> <p>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.</p> </div> </div>