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:

Process Skill Goals:

During the activity, students should make progress toward:

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.

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.

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.

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.

You have attempted of activities on this page