0% found this document useful (0 votes)
2 views22 pages

Programming ZyBooks2

The document discusses the use of if and if-else statements in programming, providing examples related to hotel discounts and car insurance pricing based on user age. It explains how these statements execute different code blocks depending on whether certain conditions are true or false. Additionally, it covers multi-branch if-else statements and includes participation activities for practice.

Uploaded by

cadabradanny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views22 pages

Programming ZyBooks2

The document discusses the use of if and if-else statements in programming, providing examples related to hotel discounts and car insurance pricing based on user age. It explains how these statements execute different code blocks depending on whether certain conditions are true or false. Additionally, it covers multi-branch if-else statements and includes participation activities for practice.

Uploaded by

cadabradanny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

PARTICIPATION
ACTIVITY 2.1.2: If statement.
2.1 If-else statement
What is the final value of num_items?

1) bonus_val = 19
If statements num_items = 1

if bonus_val > 10:


©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
An if statement executes a group of statements if an expression is true. The statements
Daniel Reyes
in a branch num_items = num_items + 3
Daniel Reyes
must be indented some number of spaces, typically four spaces. CS119-2402A-06-2402A CS119-2402A-06-2402A

PARTICIPATION
ACTIVITY
2.1.1: If statement: Hotel discount. Check Show answer

2) bonus_val = 0
num_items = 1
hotel_rate = 155
if bonus_val > 10:
num_items = num_items + 3
hotel_rate = 155 user_age = int(input('Enter age: '))

user_age = int(input('Enter age: ')) if user_age > 65:


if user_age > 65: hotel_rate = hotel_rate - 20
hotel_rate = hotel_rate - 20 Check Show answer
print('Your hotel rate:', hotel_rate)
print('Your hotel rate:', hotel_rate)

If-else statement

An if-else statement executes one group of statements when an expression is true and another group
Python interpreter
Enter age: 68 of statements when the expression is false.
Your hotel rate: 135 135 hotel_rate
68 user_age Construct 2.1.1: If-else statement.

# Statements that execute before the branches

if expression:
# Statements that execute when expression is true (first
Animation captions: branch)
else:
# Statements that execute when expression is false (second
1. An if statement executes a group of statements if an expression is true. The program assigns branch)
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
hotel_rate with 155 and then gets the user's age from input. Daniel Reyes Daniel Reyes
# Statements that execute after the branches
2. user_age is 68, so the expression 68 > 65 is true, and the if's statement will execute. Thus, the
CS119-2402A-06-2402A CS119-2402A-06-2402A
statement following the colon : will execute next.
3. hotel_rate is decreased by 20, which is the discount for guests older than 65.
4. The program completes by printing the hotel rate.
In the example below, if a user inputs an age less than 25, the statement
insurance_price = 4800 executes. Otherwise, insurance_price = 2200 executes.

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 1/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 2/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks
PARTICIPATION 1) What is the final value of
ACTIVITY 2.1.3: If-else statement: Car insurance.
num_items?
bonus_val = 5

if bonus_val < 12:


num_items = 100
user_age = int(input('Enter age: ')) else:
Python interpreter
num_items = 200
if user_age < 25:
insurance_price = 4800 ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
else: 2200 insurance_price
Daniel Reyes Daniel Reyes
insurance_price = 2200 40 CS119-2402A-06-2402A
user_age CS119-2402A-06-2402A
print('Annual price: ${}'.format(insurance_price)) Check Show answer

Enter age: 22 2) What is the final value of


Annual price: $ 4800 num_items?
bonus_val = 12
Enter age: 40 if bonus_val < 12:
Annual price: $ 2200 num_items = 100
else:
num_items = 200

Check Show answer

Animation captions:
3) What is the final value of
1. An if-else statement executes a group of statements if an expression is true, and executes num_items?
another group of statements otherwise. bonus_val = 15
2. user_age is 22, so the expression 22 < 25 is true, and the if's statements will execute. num_items = 44

3. user's age is 40, so the expression 40 < 25 is false, and the else's statements will execute. if bonus_val < 12:
num_items = num_items + 3
else:
num_items = num_items + 6

num_items = num_items + 1
aside-elaboration

(Car insurance prices for drivers under 25 are higher because 1 in 6 such drivers are Check Show answer
involved in an accident each year, vs. 1 in 15 for older drivers. Source: www.census.gov,
2009)
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A

PARTICIPATION
ACTIVITY
2.1.4: If-else statements.

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 3/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 4/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

4) What is the final value of 2) If num_people is greater than 10,


bonus_val? execute group_size = 2 *
bonus_val = 11 group_size. Otherwise, execute
if bonus_val < 12: group_size = 3 * group_size and
bonus_val = bonus_val + 2 num_people = num_people - 1.
else:
bonus_val = bonus_val + 10

©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475


Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
Check Show answer

5) What is the final value of


bonus_val? Check Show answer
bonus_val = 11

if bonus_val < 12: 3) If num_players is greater than 11,


bonus_val = bonus_val + 2
bonus_val = 3 * bonus_val execute team_size = 11. Otherwise,
execute team_size = num_players.
else:
bonus_val = bonus_val + 10 Then, no matter the value of
num_players, execute team_size =
2 * team_size.

Check Show answer

PARTICIPATION
ACTIVITY
2.1.5: Writing an if-else statement.

Translate each description to an if-else statement as directly as possible. (Not checked, but
Check Show answer
please indent a branch's statements some consistent number of spaces, such as 3 spaces.)

1) If user_age is greater than 62,


assign item_discount with 15. Else, CHALLENGE
2.1.1: Enter the output for the if-else branches.
ACTIVITY
assign item_discount with 0.

550678.3854950.qx3zqy7

Start
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A Type the program'sCS119-2402A-06-2402A
output

Check Show answer

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 5/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 6/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

num_apples = 5 CHALLENGE
2.1.3: Basic if-else.
if num_apples < 3: f ACTIVITY

print('a')
else:
k
print('f') Write an if-else statement for the following:
If user_tickets is less than 5, assign num_tickets with 1. Else, assign num_tickets with
print('k')
user_tickets.
1 2©zyBooks
02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Sample output with input: 3 Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
Check Next Value of num_tickets: 1

CHALLENGE
ACTIVITY 2.1.2: Basic if-else expression.
Learn how our autograder works

550678.3854950.qx3zqy7
Write an expression that will cause the following code to print "18 or less" if the value of
user_age is 18 or less. Write only the expression. 1 user_tickets = int(input())
2
3 ''' Your solution goes here '''
Sample output with input: 17 4
5 print('Value of num_tickets:', num_tickets)
18 or less

Learn how our autograder works

550678.3854950.qx3zqy7

1 user_age = int(input())
2 if ''' Your solution goes here ''':
3 print('18 or less')
4 else:
5 print('Over 18')
Run

View your last submission keyboard_arrow_down

©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475


Daniel Reyes
CS119-2402A-06-2402A
2.2 Multi-branch if-else statements
Daniel Reyes
CS119-2402A-06-2402A

Run Multi-branch if-else statements

An if-else statement can be extended to have three (or more) branches. Each branch's expression is
View your last submission keyboard_arrow_down checked in sequence. As soon as one branch's expression is found to be true, that branch's statement

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 7/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 8/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

executes (and no subsequent branch is considered). If no expression is true, the else branch executes. PARTICIPATION
ACTIVITY 2.2.1: Multi-branch if-else statements.

Construct 2.2.1: Multi-branch if-else statement. Only 1 branch will execute. What is the final value of employee_bonus for each given value of num_sales?

if num_sales == 0:
employee_bonus = 0
if expression1: elif num_sales == 1:
# Statements that execute when expression1 is true employee_bonus = 2
# (first branch) ©zyBooks 02/27/24 20:06 1927475 elif num_sales == 2: ©zyBooks 02/27/24 20:06 1927475
elif expression2: Daniel Reyes employee_bonus = 5 Daniel Reyes
# Statements that execute when expression1 is false and expression2 is
CS119-2402A-06-2402A else: CS119-2402A-06-2402A
true employee_bonus = 10
# (second branch)
else:
# Statements that execute when expression1 is false and expression2 is 1) num_sales is 2
false
# (third branch)

Check Show answer

The equality operator == evaluates to true if the left side and right side are equal. Ex: If num_years 2) num_sales is 0
holds the value 10, then the expression num_years == 10 evaluates to true.

Note that the equality operator is ==, not =.


Note: Good Practice is to be as explicit as possible and reduce the chances of logical errors by using the Check Show answer

less-than-or-equal-to operator (<=) rather than just the less-than operator. Thus, the constant is the
number of relevance, e.g., less-than-or-equal-to 39 rather than less-than 40, because 40 is not the 3) num_sales is 7
relevant number.

Figure 2.2.1: Multi-branch if-else example: Anniversaries. Check Show answer

Enter number years married: 10


A whole decade -- impressive. CHALLENGE
num_years = int(input('Enter number years married: ACTIVITY 2.2.1: Multi-branch if-else statements: Print century.
')) ....

if num_years == 1: Enter number years married: 25 Write an if-else statement with multiple branches.
print('Your first year -- great!') Your silver anniversary --
elif num_years == 10: enjoy. If year is 2101 or later, print "Distant future" (without quotes). Otherwise, if year is 2001 or
print('A whole decade -- impressive.')
....
greater, print "21st century". Otherwise, if year is 1901 or greater, print "20th century". Else
elif num_years == 25:
print('Your silver anniversary -- enjoy.') (1900 or earlier), print "Long ago".
elif num_years == 50: ©zyBooks
Enter number 02/27/24
years 20:06
married: 30 1927475 ©zyBooks 02/27/24 20:06 1927475
print('Your golden anniversary -- amazing.') Nothing special. Daniel Reyes Daniel Reyes
else: CS119-2402A-06-2402A Sample output with input: 1776 CS119-2402A-06-2402A
print('Nothing special.') ....

Enter number years married: 1 Long ago


Your first year -- great!

Learn how our autograder works

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 9/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 10/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks
550678.3854950.qx3zqy7

1 year = int(input()) 1 user_choice = 2 # Hardcoded values for this tool. Could be in


2
2 num_items = 5
3 ''' Your solution goes here '''
4 3
4 if user_choice == 1:
5 print('user_choice is 1')

©zyBooks 02/27/24 20:06 1927475 6 elif user_choice == 2: ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes 7 if num_items < 0: Daniel Reyes
CS119-2402A-06-2402A 8 CS119-2402A-06-2402A
print('user_choice is 2 and num_items < 0')
9 else:
10 print('user_choice is 2 and num_items >= 0')
11 else:
12 print('user_choice is neither 1 or 2')
line that just executed
next line to execute
Run

<< First < Prev Next > Last >>


View your last submission keyboard_arrow_down Step 1 of 6
Program output

2.3 More if-else

Nested if-else statements PARTICIPATION


ACTIVITY
2.3.2: Nested if-else statements.

A branch's statements can include any valid statements, including another if-else statement, which are
Determine the final value of sales_bonus given the initial values specified below.
known as nested if-else statements.
if sales_type == 2:
The below Python Tutor tool traces a Python program's execution. The Python Tutor tool is available at if sales_bonus < 5:
www.pythontutor.com. sales_bonus = 10
else:
sales_bonus = sales_bonus + 2
PARTICIPATION
ACTIVITY
2.3.1: Nested if-else else:
sales_bonus = sales_bonus + 1

©zyBooks 02/27/24 20:06 1927475 1) sales_type = 1; sales_bonus = 0; ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A 0 CS119-2402A-06-2402A

1
10

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 11/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 12/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

2) sales_type = 2; sales_bonus = 4;
5 1 user_age = 26 # Hardcoded for this tool. Could replace with "int(input
2
6 3 # Note that more than one "if" statement can execute
10 4 if user_age < 16:
5 print('Enjoy your early years.')
3) sales_type = 2; sales_bonus = 7;
6
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
8 Daniel Reyes 7 if user_age > 15: Daniel Reyes
CS119-2402A-06-2402A 8 print('You are old enough to drive.') CS119-2402A-06-2402A
9
9
10 10 if user_age > 17:
11 print('You are old enough to vote.')
Multiple distinct if statements 12
13 if user_age > 24:
Sometimes the programmer has multiple if statements in sequence, which looks similar to a multi- 14 print('Most car rental companies will rent to you.')
branch if-else statement but has a very different meaning. Each if statement is independent, and thus 15
more than one branch can execute, in contrast to the multi-branch if-else arrangement. 16 if user_age > 34:
17 print('You can run for president.')
PARTICIPATION line that just executed
ACTIVITY
2.3.3: Multiple distinct if statements.
next line to execute

<< First < Prev Next > Last >>


Step 1 of 9
Program output

PARTICIPATION
ACTIVITY
2.3.4: If statements.

©zyBooks 02/27/24 20:06 1927475 Determine the final value of num_boxes. ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 13/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 14/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

1) num_boxes = 0
Check Next
num_apples = 9

if num_apples < 20:


num_boxes = 3
CHALLENGE
if num_apples < 10:
ACTIVITY 2.3.2: Multiple if statements: Printing car features.
num_boxes = num_boxes -
1
©zyBooks 02/27/24 20:06 1927475 Write multiple if statements. If car_year is 1969 or earlier, print "Few safety features."
©zyBooks If 1970
02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
or later, print "Probably has seat belts." If 1990 or later, print "Probably has antilock brakes." If
CS119-2402A-06-2402A CS119-2402A-06-2402A
2000 or later, print "Probably has airbags." End each phrase with a period and a newline.
Check Show answer

Sample output for input: 1995


2) num_boxes = 0
num_apples = 9 Probably has seat belts.
Probably has antilock brakes.
if num_apples < 10:
if num_apples < 5:
num_boxes = 1
else:
num_boxes = 2 Learn how our autograder works
elif num_apples < 20:
num_boxes = num_boxes + 550678.3854950.qx3zqy7
1
1 car_year = int(input())
2
3 ''' Your solution goes here '''
4
Check Show answer

CHALLENGE
ACTIVITY
2.3.1: Enter the output for the multiple if-else branches.

550678.3854950.qx3zqy7

Start

Type the program's output

num_items = 6 Run
if num_items > 2:
b ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
print('b')
Daniel Reyes
View your last submission keyboard_arrow_down Daniel Reyes
elif num_items < 9:
print('e')
r CS119-2402A-06-2402A CS119-2402A-06-2402A
else:
print('g')

print('r')

1 2
2.4 Equality and relational operators

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 15/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 16/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

Equality operators 4) y != 99
True
An equality operator checks whether two operands' values are the same (==) or different (!=). Note
that equality is ==, not just =. False

An expression involving an equality operator evaluates to a Boolean value. A Boolean is a type that has 5) x != y
just two values: True or False.
True
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes False Daniel Reyes
Table 2.4.1: Equality operators. CS119-2402A-06-2402A CS119-2402A-06-2402A

PARTICIPATION
Equality Example (assume x is ACTIVITY
2.4.2: Creating expressions with equality operators.
Description
operators 3)
Type the operator to complete the desired expression.
x == 3 is True
== a == b means a is equal to b
x == 4 is False 1) num_dogs is 0

a != b means a is not equal x != 3 is False num_dogs 0


!=
to b x != 4 is True
Check Show answer

2) num_dogs and num_cats are the


PARTICIPATION
same
ACTIVITY
2.4.1: Evaluating expressions that have equality operators.
num_dogs num_cats

Indicate whether the expression evaluates to True or False. Check Show answer
x is 5, y is 7.

1) x == 5 3) num_dogs and num_cats differ


True num_dogs num_cats

False
Check Show answer
2) x == y
True 4) num_dogs is either less than or
greater than num_cats
False ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes num_dogs num_cats Daniel Reyes
3) y != 7 CS119-2402A-06-2402A CS119-2402A-06-2402A

True Check Show answer

False

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 17/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 18/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

5) user_char is the character 'x'. 1) x <= 7


user_char 'x' True
False
Check Show answer
2) y >= 7
True
Relational operators ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes False Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
A relational operator checks how one operand's value relates to another, like being greater than.
3) Is x <> y a valid expression?
Some operators, like >=, involve two characters. A programmer cannot arbitrarily combine the >, =, and
< symbols; only the shown two-character sequences represent valid operators. Yes
No

Table 2.4.2: Relational operators. 4) Is x =< y a valid expression?


Yes
Relational Example (assume x is No
Description
operators 3)

x < 4 is True
< a < b means a is less than b PARTICIPATION
x < 3 is False ACTIVITY
2.4.4: Creating expressions with relational operators.

x > 2 is True
> a > b means a is greater than b Type the operator to complete the desired expression.
x > 3 is False
1) num_dogs is greater than 10
x <= 4 is True
<= a <= b means a is less than or equal to b x <= 3 is True num_dogs 10
x <= 2 is False
Check Show answer
x >= 2 is True
a >= b means a is greater than or equal
>= x >= 3 is True
to b 2) num_cars is greater than or equal
x >= 4 is False
to 5
num_cars 5

Check Show answer


PARTICIPATION ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
ACTIVITY
2.4.3: Evaluating equations having relational operators. Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A 3) num_cars is 5 or greater CS119-2402A-06-2402A

Indicate whether the expression evaluates to True or False. num_cars 5


x is 5, y is 7.
Check Show answer

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 19/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 20/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

4) cents_lost is a negative number num_eggs = 5


if num_eggs <= 6: c
cents_lost 0 print('c')
else: k
print('f')
Check Show answer
print('k')

1 2
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
zyDE 2.4.1: If-else with expression: Non-negative. Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A Check Next CS119-2402A-06-2402A

The program prints "Zero" if the user enters 0, else prints "Non-zero".
Modify the program to print "Non-negative" if the user enters 0 or
greater, else print "Negative". Comparing characters, strings, and floating-point types

99 The relational and equality operators work for integer, character, and floating-point built-in types.
Load default template...
Floating-point types should not be compared using the equality operators, due to the imprecise
1 user_num = int(input('Enter a representation of floating-point numbers, as discussed in a later section.
2 Run
3 if user_num == 0: The operators can also be used for the string type. Strings are equal if they have the same number of
4 print('Zero') characters and corresponding characters are identical. If string my_str = 'Tuesday', then (my_str ==
5 else:
6 print('Non-zero') 'Tuesday') is True, while (my_str == 'tuesday') is False because T differs from t.
7
PARTICIPATION
ACTIVITY 2.4.5: Comparing various types.

Which comparisons will not result in a syntax error AND consistently yield expected results?
Variables have types denoted by their names.

1) my_int == 42
OK
Not OK

2) my_float == 3.14
CHALLENGE
ACTIVITY 2.4.1: Enter the output for the branches with relational and equality operators. OK
Not OK
550678.3854950.qx3zqy7

©zyBooks 02/27/24 20:06 1927475 3) my_string == 'Hello' ©zyBooks 02/27/24 20:06 1927475
Start
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A OK CS119-2402A-06-2402A
Type the program's output
Not OK

The types of the values being compared determines the meaning of a comparison. If both values are
numbers, then the numbers are compared arithmetically (5 < 2 is False). Comparisons that make no
sense, such as 1 < 'abc' result in a TypeError.
https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 21/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 22/43
2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

Comparison of values with the same type, like 5 < 2, or 'abc' >= 'ABCDEF', depend on the types PARTICIPATION
ACTIVITY 2.4.7: Watch out for assignment in an if-else expression.
being compared.

Numbers are arithmetically compared. What is the final value of num_items? Write "Error" if the code results in an error.
Strings are compared by converting each character to a number value (ASCII or Unicode), and
1) num_items = 3
then comparing each character in order. Most string comparisons use equality operators "==" or if num_items == 3:
num_items = num_items + 1
"!=", as in today == 'Friday'.
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Lists and tuples are compared via an ordered comparison of every element in the sequence.
Daniel Reyes Daniel Reyes
Every element between the sequences must compare as equal for CS119-2402A-06-2402A
an equality operator to CS119-2402A-06-2402A
evaluate to True. Relational operators like < or > can also be used: The result is determined by Check Show answer
the first mismatching elements in the sequences. For example, if x = [1, 5, 2] and y = [1, 4, 3], then
evaluating x < y first evaluates 1 < 1, then 5 < 4, which produces a value of False.
2) num_items = 3
Dictionaries are compared only with == and !=. To be equal, two dictionaries must have the same if num_items = 10:
set of keys and the same corresponding value for each key. num_items = num_items + 1

PARTICIPATION
ACTIVITY
2.4.6: Comparing various types.
Check Show answer

1) Click the expression that is False.


3) num_items = 3
5 <= 5.0 if num_items > 10:
num_items = num_items + 1
10 != 9.999999
(4 + 1) != 5.0

2) Click the expression that is False. Check Show answer


'FRIDAY' == 'friday'
'1' < '2'
'a' != 'b' < 'c'

3) Click the expression that is True. 2.5 Boolean operators and expressions
{'Henrik': '$25'} == {'Daniel': '$25'}
(1,2,3) > (0,2,3)
Booleans and Boolean operators
[1, 2, 3] >= ['1', '2', '3']
A Boolean refers to a value that is either True or False. Note that True and False are keywords in
©zyBooks 02/27/24 20:06 1927475 Python and must be capitalized. A programmer can assign a Boolean value by
©zyBooks specifying
02/27/24 True
20:06 or
1927475
Common errors Daniel Reyes Daniel Reyes
False, or by evaluating an expression that yields a Boolean.
CS119-2402A-06-2402A CS119-2402A-06-2402A
A common error is to use = rather than == in an if-else expression, as in: if numDogs = 9:. In such
cases, the interpreter should generate a syntax error. Figure 2.5.1: Creating a Boolean.
Another common error is to use invalid character sequences like =>, !<, or <>, which are not valid
operators.

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 23/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 24/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

my_bool = True # Assigns my_bool with the boolean value True a and b Boolean AND: True when both operands are True.

is_small = my_val < 3 # Assigns is_small with the result of the expression a or b Boolean OR: True when at least one operand is True.
(False)

Boolean NOT (opposite): True when the single operand is False (and False
not a
when operand is True).

©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475


A Boolean operator treats operands as True or False and evaluates to a value Daniel
of True or False.
Reyes Daniel Reyes
Boolean operators include and, or, and not. A Boolean expression is anCS119-2402A-06-2402A
expression using Boolean CS119-2402A-06-2402A
operators.
Table 2.5.2: Boolean operators examples.
PARTICIPATION
ACTIVITY
2.5.1: Boolean operators: and, or, and not.
Given age = 19, days = 7, user_char = 'q'

(age > 16) and (age < 25) True, because both operands are True.

a b a and b a b a or b a not a False, because both operands are not True (days >
(age > 16) and (days > 10)
False False False False False False False True 10 is False).
False True False False True True True False
True False False True False True True, because at least one operand is True (age >
True True True True True True (age > 16) or (days > 10)
16 is True).
Let x = 7, y = 9
not (days > 10) True, because operand is False.
(x > 0) and (y < 10) True (x < 0) or (y > 10) False not (x < 0) True
True True False False False not (age > 16) False, because operand is True.
(x > 0) and (y < 5) False (x < 0) or (y > 5) True not (x > 0) False
True False False True True not (user_char == 'q') False, because operand is True.

Animation captions:
PARTICIPATION
1. "and" evaluates to True only if BOTH operands are true. ACTIVITY 2.5.2: Evaluating expressions with Boolean operators.
2. "or" evaluates to True if ANY operand is True (one, the other, or both).
3. "not" evaluates to the opposite of the operand. Given num_people = 10, num_cars = 2, user_key = 'q'.
4. Each operand is commonly an expression itself. If x = 7, y = 9, then (x > 0) and (y < 10) is True
and True, so evaluates to true (both operands are True). ©zyBooks 02/27/24 20:06 1927475 1) num_people >= 10 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A True CS119-2402A-06-2402A
False
Table 2.5.1: Boolean operators.

Boolean
Description
operator

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 25/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 26/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

2) (num_people >= 10) and 1) days is greater than 30 and less


(num_cars > 2) than 90

True if (days > 30) (days <


90):
False ....
3) (num_people >= 20) or
(num_cars > 1) ©zyBooks 02/27/24 20:06 1927475 Check Show answer ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
True CS119-2402A-06-2402A CS119-2402A-06-2402A

False 2) max_cars is between 0 and 100.


if (max_cars > 0)
4) not (num_cars < 5)
(max_cars < 100):
True ....

False
Check Show answer
5) not (user_key == 'a')
True 3) num_stores is between 10 and 20,
False inclusive.
if (num_stores >= 10) and (
6) user_key != 'a' ):

....
True
False
Check Show answer
7) not ((num_people >= 10) and
(num_cars > 2)) 4) num_dogs is 3 or more and
True num_cats is 3 or more.
if (num_dogs >= 3)
False :
8) (user_key == 'x') or ....
((num_people > 5) and
(num_cars > 1))
Check Show answer
True
False ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
Boolean operators are commonly used in expressions found in if-else statements.

PARTICIPATION
ACTIVITY
2.5.3: Boolean operators: Complete the expressions for the given condition.

Fill in the missing Boolean operators. Assume all variables are integers.

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 27/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 28/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

5) Either wage is greater than 10 or


age is less than 18. Use "or". Use > CHALLENGE
ACTIVITY
2.5.2: Boolean operators: Detect specific values.
and < (not >= and <=). Use
parentheses around sub-
expressions. Write an expression using Boolean operators that prints "Special number" if special_num is
-99, 0, or 44.
if :
.... ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes
Sample output with input: 17 Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
Not special number
Check Show answer

6) num is a 3-digit positive integer,


such as 100, 989, or 523, but not Learn how our autograder works
55, 1000, or -4.
550678.3854950.qx3zqy7

1 special_num = int(input())
For most direct readability, your
2
expression should compare 3 if ''' Your solution goes here ''':
directly with the smallest and 4 print('Special number')
5 else:
largest 3-digit number.
6 print('Not special number')
if (num >= 100)
:

....

Check Show answer

CHALLENGE
ACTIVITY 2.5.1: Enter the output of the Boolean expressions.
Run
550678.3854950.qx3zqy7

Start View your last submission keyboard_arrow_down


Type the program's output
CHALLENGE
ACTIVITY
2.5.3: Boolean operators: Combining test conditions.
x = 3 ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
if (x < 8) and (x > 3): b Daniel Reyes Daniel Reyes
print('a') CS119-2402A-06-2402A Write an expression using Boolean operators that prints "Eligible" if user_age is greater than
CS119-2402A-06-2402A
else:
print('b') 17 and not equal to 25.

1 2 Sample output with input: 17

Check Next
Ineligible

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 29/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 30/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

Learn how our autograder works


550678.3854950.qx3zqy7

1 user_age = int(input())
2
3 if ''' Your solution goes here ''': ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
4 print('Eligible') Daniel Reyes Daniel Reyes
5 else: Run
CS119-2402A-06-2402A CS119-2402A-06-2402A
6 print('Ineligible')
View your last submission keyboard_arrow_down

2.6 Code blocks and indentation

Code blocks
Run
A code block is a series of statements that are grouped together. A code block in Python is defined by
its indentation level, i.e., the number of blank columns from the left edge. The initial code block is not
View your last submission keyboard_arrow_down indented. A new code block can follow a statement that ends with a colon, such as an "if" or "else". In
addition, a new code block must be more indented than the previous code block. The program below
CHALLENGE
2.5.4: Boolean operators: Branching using Boolean variables. includes comments indicating where each new code block begins.
ACTIVITY
The amount of indentation used to indicate a new code block can be arbitrary, as long as the
Write an expression that prints 'You must be rich!' if the variables young and famous are both programmer uses the same indentation consistently for each line in the block. Good practice is to use
True. the standard recommended 4 columns per indentation level.

A common error for new Python programmers is the mixing of tabs and spaces. Never mix tabs and
Sample output with inputs: 'True' 'True' spaces for indentation in the same program. Many editors consider a tab to be equivalent to either 3
or 4 spaces, while in Python a tab is equivalent only to another tab. A program that mixes tabs and
You must be rich!
space to indent code blocks will automatically generate an IndentationError from the interpreter in
Python 3. A good practice is to use spaces only when indenting code, and to set text editor options to
automatically use spaces when possible.
Learn how our autograder works
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
550678.3854950.qx3zqy7
CS119-2402A-06-2402A
Figure 2.6.1: Code blocks are indicated with indentation.CS119-2402A-06-2402A
1 young = (input() == 'True')
2 famous = (input() == 'True')
3
4 if ''' Your solution goes here ''':
5 print('You must be rich!')
6 else:
7 print('There is always the lottery....')

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 31/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 32/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

160000
Load default template...
# First code block has no indentation

model = input('Enter car model: ') 1 # Calculate the velocity r


year = int(input('Enter year of car 2 # depending on a user-ente
manufacture: ')) Run
3
antique = False 4 import math
domestic = False 5
©zyBooks 02/27/24 20:06 1927475 6 escape_velocity_meters_per ©zyBooks 02/27/24 20:06 1927475
if year < 1970: 7 grav_constant = 6.67384e-1
Daniel Reyes Daniel Reyes
# New code block has indentation of 4
columns CS119-2402A-06-2402A 8 earth_mass_kilograms = 5.9 CS119-2402A-06-2402A
antique = True 9
Enter car model: Ford 10 radius_meters = float
# Back to code block 0 Enter year of car manufacture: 11 print()
1918 12
if model in ['Ford', 'Chevrolet', 'Dodge']: My own model-T still runs like a 13 if radius_meters < 6317000
# New code block has indentation of 2 columns charm...
# Any amount of indentation > 0 is OK. 14 escape_velocity_meters
domestic = True 15 print('Houston, we
16
# Back to code block 0 17
if antique:
# New code block has indentation of 4
columns
if domestic:
# New block has 4 additional columns (8
total) Special cases
print('My own model-T still runs like a
charm...')
The number of columns of text considered to be acceptable varies from 80 to 120. Good practice is to
use the widely accepted standard of 80 columns. A few exceptions to the rules of indentation deal with
very long statements that require more than one line and wrap to the next line. Such special situations
do not indicate new code blocks.

zyDE 2.6.1: Code blocks and indentation. Figure 2.6.2: Some indentations are continuations of the previous line.
Fix the errors in the code below so that you can run the program.
Multiple lines enclosed within parentheses are implicitly joined into a single string (without
newlines between each line); use implicit line joining for very long strings or functions with
numerous arguments. Ex: All extra lines are indented to the same column as the opening
quotation mark on the first line.

When declaring list or dict literals, entries can be placed on separate lines for clarity.
declaration = ("When in the Course of human events, it becomes necessary for "
©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
"one people to dissolve the political bands which have connected "
Daniel Reyes "them with another, and to assume among the powers of theDaniel Reyes
earth...")
CS119-2402A-06-2402A CS119-2402A-06-2402A
result_of_power = math.pow(long_variable_name_left_operand,
long_variable_name_right_operand)

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 33/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 34/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

Figure 2.6.3: List, dict multi-line constructs. print('The temperature in New York is',
temperatures['New York'])
else:
Containers like lists and dicts can be broken into multiple lines, with the elements on print('The temperature in New York is unknown.')
separate, indented lines.
Sample output with input: 105
my_list = [ my_dict = {
1, 2, 3, 'entryA': 1, ©zyBooks 02/27/24 20:06 1927475 The city is melting! ©zyBooks 02/27/24 20:06 1927475
4, 5, 6 'entryB': 2
] }
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A

Learn how our autograder works

550678.3854950.qx3zqy7

1 temperatures = {
PARTICIPATION
ACTIVITY 2.6.1: Indentation. 2 'Seattle': 56.5,
3 'New York': float(input()),
4 'Kansas City': 81.9,
1) The standard number of spaces to use 5 'Los Angeles': 76.5
6 }
for indentation is 4. 7
True 8 ''' Your solution goes here '''
9
False

2) Mixing spaces and tabs when


indenting is considered an acceptable
programming style.
True
False
Run
3) A programmer can start new code
blocks at any point in the code, as long
as the indentation for each line in the
View your last submission keyboard_arrow_down
block is consistent.
True
False
2.7 LAB: Smallest of two numbers
CHALLENGE ©zyBooks 02/27/24 20:06 1927475 ©zyBooks
Write a program whose inputs are two integers, and whose output is 02/27/24
the smallest 20:06
of the two1927475
values.
ACTIVITY 2.6.1: Indentation: Fix the program. Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
Ex: If the input is:
Retype the below code. Fix the indentation as necessary to make the program work.
7
if 'New York' in temperatures: 15
if temperatures['New York'] > 90:
print('The city is melting!') the output is:
else:
https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 35/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 36/43
2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

7 Coding trail of your work What is this?

2/15 R-----10 min:7


Note: Please make sure you complete this lab successfully before you process with the next Lab
assignment
550678.3854950.qx3zqy7

2.8 LAB: Smallest of three ©zyBooks


numbers
LAB
ACTIVITY
2.7.1: LAB: Smallest of two numbers 10 1927475
©zyBooks 02/27/24 20:06 / 10 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
main.py NOTE1: Please make sure you complete previous Lab successfully before you proceed with this Lab
Load default template...
assignment

1 # Get input from the user NOTE2: Make sure to copy and past your code from previous into here and update it to complete this
2 num1 = int(input()) lab
3 num2 = int(input())
4 Write a program whose inputs are three integers, and whose output is the smallest of the three values.
5 #Determine the smallest number
6 if num1 < num2: Ex: If the input is:
7 smallest = num1
8
9 else: 7
10 smallest = num2 15
11 3
12 #Print the smallest number
13 print(smallest)
the output is:

550678.3854950.qx3zqy7
Run your program as often as you'd like, before submitting
Develop mode Submit mode
for grading. Below, type any needed input values in the first LAB
ACTIVITY
2.8.1: LAB: Smallest of three numbers 10 / 10
box, then click Run program and observe the program's
output in the second box.
Enter program input (optional) main.py Load default template...

If your code requires input values, provide them here.


1 num1 = int(input())
2 num2 = int(input())
3 num3 = int(input())
4
5 #Determine the smallest number

trending_flat trending_flat
©zyBooks 02/27/24 20:06 1927475
main.py ©zyBooks 02/27/24 20:06 1927475
Run program Input (from above) Daniel Reyes Outp 6 if (num1 < num2 and num1 < num3): Daniel Reyes
(Your program)
CS119-2402A-06-2402A 7 smallest = num1 CS119-2402A-06-2402A
8
Program output displayed here 9 elif (num2 < num1 and num2 < num3):
10 smallest = num2
11
12 else:
13 smallest = num3
14
15 #Print the smallest number
16 print(smallest)
https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 37/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 38/43
2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks
p ( )

Run your program as often as you'd like, before submitting


Develop mode Submit mode
for grading. Below, type any needed input values in the first
Figure 2.9.1: Stop Sign
box, then click Run program and observe the program's
output in the second box.
Enter program input (optional) ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
Daniel Reyes Daniel Reyes
If your code requires input values, provide them here. CS119-2402A-06-2402A CS119-2402A-06-2402A

trending_flat trending_flat
main.py
Run program Input (from above)
(Your program)
Outp

Program output displayed here

Coding trail of your work What is this?

2/15 R---10 min:8

2.9 End of Unit 2 Assignments


If you have gotten here, you have gone through all of the sections within the two zyBook assignments.
Check your grades for (1) Interactive Assignment and (2) Lab Assignment in the VC gradebook for
accuracy.

Remember, you can always continue practicing to improve your score on the assignments.

If you scroll up, you can look for incomplete activities. A check mark will appear to the right of
each activity that has been completed. ©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475
View your 'My Activity' tab to see the assignment completion from a birds Daniel
eye Reyes
view. (Get there by Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
clicking on the course name at the top of this page --> My Activity on the bottom right)

To ensure your grades and scores sync with the VC gradebook, close out of this zyBook and click
on the next assignment link in Virtual Campus rather than clicking the next section in the zyBook
now.
2.10 LAB: Seasons

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 39/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 40/43


2/27/24, 8:06 PM zyBooks 2/27/24, 8:06 PM zyBooks

info This section has been set as optional by your instructor.

Write a program that takes a date as input and outputs the date's season. The input is a string to
represent the month and an int to represent the day.

Ex: If the input is:

©zyBooks 02/27/24 20:06 1927475 Run your program as often ©zyBooks 02/27/24
as you'd 20:06submitting
like, before 1927475
April Daniel Reyes Develop mode Submit mode Daniel Reyes
11 CS119-2402A-06-2402A for grading. Below, type any needed
CS119-2402A-06-2402Athe first
input values in
box, then click Run program and observe the program's
the output is: output in the second box.
Enter program input (optional)
Spring
If your code requires input values, provide them here.
In addition, check if the string and int are valid (an actual month and day).

Ex: If the input is:

trending_flat trending_flat
main.py
Blue Run program Input (from above)
(Your program)
Outp

65
Program output displayed here
the output is:

Invalid

The dates for each season are:


Spring: March 20 - June 20 Coding trail of your work What is this?
Summer: June 21 - September 21 History of your effort will appear here once you begin working
Autumn: September 22 - December 20 on this zyLab.
Winter: December 21 - March 19
550678.3854950.qx3zqy7

LAB
2.10.1: LAB: Seasons 0 / 10
2.11 LAB: Leap year
ACTIVITY

main.py Load default template...


©zyBooks 02/27/24 20:06 1927475 ©zyBooks 02/27/24 20:06 1927475

1 input_month = input()
Daniel Reyes info This section has been set as optional by your instructor. Daniel Reyes
CS119-2402A-06-2402A CS119-2402A-06-2402A
2 input_day = int(input())
3
A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to
4 ''' Type your code here. '''
5 rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A
leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year
to be a leap year are:

1) The year must be divisible by 4


https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 41/43 https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 42/43
2/27/24, 8:06 PM zyBooks

2) If the year is a century year (1700, 1800, etc.), the year must be evenly divisible by 400
Some example leap years are 1600, 1712, and 2016.

Write a program that takes in a year and determines whether that year is a leap year.

Ex: If the input is:

1712 ©zyBooks 02/27/24 20:06 1927475


Daniel Reyes
CS119-2402A-06-2402A
the output is:

1712 - leap year

Ex: If the input is:

1913

the output is:

1913 - not a leap year


550678.3854950.qx3zqy7

LAB
ACTIVITY 2.11.1: LAB: Leap year 0 / 10

main.py Load default template...

1 is_leap_year = False
2
3 input_year = int(input())
4
5 ''' Type your code here. '''

©zyBooks 02/27/24 20:06 1927475


Daniel Reyes
CS119-2402A-06-2402A

Run your program as often as you'd like, before submitting


Develop mode Submit mode
for grading. Below, type any needed input values in the first

https://learn.zybooks.com/zybook/CS119-2402A-06-2402A/chapter/2/print 43/43

You might also like