Python Conditional Statements and Expressions Exercises with Solutions, Exams of Computer Networks

This collection of Python exercises focuses on conditional statements, expressions, and basic input/output. Problems involve boolean expressions, if/else statements, and calculations based on user input, covering topics like comparing numbers, strings, and boolean values. Scenarios include overtime pay, stock transactions, and color mixing. Designed to enhance problem-solving and coding skills, it's a practical guide for learning Python syntax and logic, ideal for beginners and intermediate learners. Exercises are presented clearly with sample solutions for immediate feedback.

Typology: Exams

2024/2025

Available from 07/30/2025

TheHub
TheHub 🇺🇸

3.9

(35)

11K documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python Chapter 3 EXAMS WITH 100%
VERIFIED SOLUTIONS 2025/2026 STUDY
GUIDE
Write an expression that evaluates to True if and only if the value of x is equal to zero.
x == 0
Write an expression that evaluates to True if and only if the variables profits and losses are
exactly equal.
profits==losses
Given the variable c, whose associated value is a str, write an expression that is True if and only if
c is not equal to a string consisting of a single blank.
c != str(" ")
Working overtime is defined as having worked more than 40 hours during the week. Given the
variable hours_worked, write an expression that evaluates to True if the employee worked
overtime.
hours_worked > 40
Write an expression that evaluates to True if x is greater than or equal to y.
x >= y
Given the variables number_of_men and number_of_women, write an expression that evaluates to
True if the number of men is greater than or equal to the number of women.
number_of_men >= number_of_women
Given an int variable gross_pay, write an expression that evaluates to True if and only if
gross_pay is less than 10,000.
gross_pay < 10000
Write a conditional that assigns True to fever if temperature is greater than 98.6.
if temperature > 98.6: fever = True
Write a conditional that decreases the value associated with shelf_life by 4 if the value associated
with outside_temperature is greater than 90.
if outside_temperature > 90: shelf_life -= 4
Write a conditional that multiplies the value associated with pay by one-and-a-half if
worked_overtime is associated with True.
if worked_overtime== True: pay *= 1.5
pf3
pf4
pf5

Partial preview of the text

Download Python Conditional Statements and Expressions Exercises with Solutions and more Exams Computer Networks in PDF only on Docsity!

Python Chapter 3 EXAMS WITH 100%

VERIFIED SOLUTIONS 2025/2026 STUDY

GUIDE

Write an expression that evaluates to True if and only if the value of x is equal to zero. x == 0 Write an expression that evaluates to True if and only if the variables profits and losses are exactly equal. profits==losses Given the variable c, whose associated value is a str, write an expression that is True if and only if c is not equal to a string consisting of a single blank. c != str(" ") Working overtime is defined as having worked more than 40 hours during the week. Given the variable hours_worked, write an expression that evaluates to True if the employee worked overtime. hours_worked > 40 Write an expression that evaluates to True if x is greater than or equal to y. x >= y Given the variables number_of_men and number_of_women, write an expression that evaluates to True if the number of men is greater than or equal to the number of women. number_of_men >= number_of_women Given an int variable gross_pay, write an expression that evaluates to True if and only if gross_pay is less than 10,000. gross_pay < 10000 Write a conditional that assigns True to fever if temperature is greater than 98.6. if temperature > 98.6: fever = True Write a conditional that decreases the value associated with shelf_life by 4 if the value associated with outside_temperature is greater than 90. if outside_temperature > 90: shelf_life - = 4 Write a conditional that multiplies the value associated with pay by one-and-a-half if worked_overtime is associated with True. if worked_overtime== True: pay *= 1.

Given x and y, each associated with an int, write a fragment of code that associates the larger of these with another variable named max. if x > y:max=x else: max=y Given the variables x, y, and z, each associated with an int, write a fragment of code that assigns the smallest of these to min. if x<=y and x<=z: min=x if y<=x and y<=z: min=y if z<=x and z<=y: min=z Write an if/else statement that compares age with 65, adds 1 to senior_citizens if age is greater than or equal to 65, and adds 1 to non_seniors otherwise if age >= 65: senior_citizens += 1 else: non_seniors += 1 Write an if/else statement that compares sold_yesterday and sold_today, and based upon that comparison assigns sales_trend the value - 1 (the case where sold_yesterday is greater than sold_today) or 1. if sold_yesterday > sold_today: sales_trend = - 1 else: sales_trend = 1 Write a program that assigns two integer values from standard input to the variables int1 and int2, then prints "True" if they are equal, and "False" if they are not. int1=int(input()) int2=int(input()) if int1==int2: print("True") else: print("False") Write an expression that evaluates to True if and only if s refers to the str "end". s=="end"

elif ph==7: neutral= base= acid= Write an expression that evaluates to True if and only if the bool associated with worked_overtime is True. worked_overtime==True Given the already defined variables is_full_time_student and age, write an expression that evaluates to True if age is less than 19 or is_full_time_student is True. age<19 or is_full_time_student==True Write an expression that evaluates to True if and only if is_a_member is False. is_a_member==False Given that the variables x and y have been defined, write an expression that evaluates to True if x is non-negative and y is negative. x>=0 and y< Given two variables , is_empty which is associated with a bool indicating whether a class roster is empty or not, and number_of_credits which is associated with an int (the number of credits for a class), write an expression that evaluates to True if the class roster is empty or the class is exactly three credits. is_empty==True or number_of_credits== Given the already defined variables temperature and humidity, write an expression that evaluates to True if and only if temperature is greater than 90 and humidity is less than 10. temperature>90 and humidity< Given the already defined variables years_with_company and department, write an expression that evaluates to True if years_with_company is less than 5 and department is not equal to 99. years_with_company<5 and department!= Given two variables , is_empty of type bool, indicating whether a class roster is empty or not, and number_of_credits of type integer, containing the number of credits for a class, write an expression that evaluates to True if the class roster is not empty and the class is one or three credits. not is_empty and (number_of_credits==1 or number_of_credits==3) Initialize the variable oneSpace, to a string consisting of a single space. oneSpace=" " Assign true to the variable hasPassedTest. hasPassedTest=True

Assume that a variable variable , numberOfSides has been initialized. Write a statement that assigns the value True to the variable isQuadrilateral if numberOfSides is exactly 4 and False otherwise isQuadrilateral=numberOfSides== 4 Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. Given a variable modelYear write a statement that assigns true to norecall if the value of modelYear does NOT fall within the two recall ranges and assigns false otherwise. Do not use an if statement in this exercise! norecall = not((modelYear >= 1995 and modelYear<=1998) or (modelYear >=2004 and modelYear<=2006)) The colors red, blue, and yellow are known as the primary colors because they cannot be made by mixing other colors. When you mix two primary colors, you get a secondary color: When you mix red and blue, you get purple. When you mix red and yellow, you get orange. When you mix blue and yellow, you get green. Design a program that prompts the user to enter the names of two primary colors, one at a time. If the user enters anything other than "red," "blue," or "yellow," the program should print "You didn't input two primary colors." Otherwise, it should print something in the format: "When you mix red and blue, you get purple." (Assuming the user entered "red" and "blue".) primary_color1 = input("Enter primary color:") primary_color2 = input("Enter primary color:") if (primary_color1 == "red" and primary_color2 == "blue") or (primary_color1 == "blue" and primary_color == "red"): print("When you mix red and blue, you get purple.") elif (primary_color1 == "blue" and primary_color2 == "yellow") or (primary_color1 == "yellow" and primary_color2 == "blue"): print("When you mix blue and yellow, you get green.") elif (primary_color1 == "yellow" and primary_color2 == "red") or (primary_color1 == "red" and primary_color2 == "yellow"): print("When you mix yellow and red, you get orange.") else: print("You didn't input two primary colors.")

print("After the transaction, you lost " +str(profit)+ " dollars.") elif(profit<0): print("After the transaction, you gained " +str(loss)+ " dollars.") Write a program that asks the user to enter a number of seconds and then prints the same amount of time in days, hours, minutes, and seconds. For example, 3667 seconds is equivalent to 0 days, 1 hour, 1 minute, and 7 seconds. Print out the result in the format: "0 day(s), 1 hour(s), 1 minute(s), and 7 second(s)." time=input("Enter number of seconds:") t=int(time) d=int(t//86400) z=t/86400-d p=z* h=int(p//3600) y=p/3600-h j=y* m=int(j//60) b=j/60-m r=int((b+.0000001)*60) s=int(r//1) print(d,"day(s),",h,"hour(s),",m,"minute(s), and",s, "second(s).")