Python chapter 3
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
Assign true to the variable hasPassedTest.
hasPassedTest=True
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 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
Given that ph refers to a float, write a statement that compares ph to 7.0 and makes the following assignments (RESPECTIVELY!) to the variables neutral, base, and acid: 0,0,1 if ph is less than 7 0,1,0 if ph is greater than 7 1,0,0 if ph is equal to 7
if ph<7: neutral=0 base=0 acid=1 elif ph>7: neutral=0 base=1 acid=0 elif ph==7: neutral=1 base=0 acid=0
Write a conditional that assigns True to fever if temperature is greater than 98.6.
if temperature > 98.6: fever = True
Write an expression that evaluates to True if and only if is_a_member is False.
is_a_member==False
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))
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 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
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(" ")
Assume that c is a variable that has been assigned a string value. Write an expression whose value is true if and only if c is a space character.
c == " "
Assume that c is a variable that has assigned a string value. Write an expression whose value is true if and only if c is a newline character.
c == '\n'
Assume that c is a variable has been assigned a string value. Write an expression whose value is true if and only if c is a tab character.
c=="\t"
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_color2 == "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.")
Write an expression that evaluates to True if and only if the variables profits and losses are exactly equal.
profits==losses
Write an expression that evaluates to True if and only if the bool associated with worked_overtime is True.
worked_overtime==True
Write an expression that evaluates to True if and only if the value of x is equal to zero.
x == 0
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=" "
Write an expression that evaluates to True if and only if the variable s does not refer to the str "end".
s!="end"
Write an expression that evaluates to True if and only if s refers to the str "end".
s=="end"
Write an expression that evaluates to True if x is greater than or equal to y.
x >= y
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<0
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!=99
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 if/else statement that adds 1 to minors if age is less than 18, adds 1 to adults if age is 18 through 64 and adds 1 to seniors if age is 65 or older.
if age<18: minors+=1 elif age>=65: seniors+=1 else: adults+=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 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
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 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")
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
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==3
You decide to buy some stocks for a certain price and then sell them at another price. Write a program that determines whether or not the transaction was profitable. Here are the details: • Take three separate inputs: the number of shares, the purchase price of the stocks, and the sale price, in that order. • You purchase the number of stocks determined by the input. • When you purchase the stocks, you pay the price determined by the input. • You pay your stockbroker a commission of 3 percent on the amount paid for the stocks. • Later, you sell the all of the stocks for the price determined by the input. • You pay your stockbroker another commission of 3 percent on the amount you received for the stock. Your program should calculate your net gain or loss during this transaction and print it in the following format: If your transaction was profitable (or if there was a net gain/loss of 0) print: "After the transaction, you made 300 dollars." (If, for example, you gained 300 dollars during the transaction.) If your transaction was not profitable, print: "After the transaction, you lost 300 dollars." (If, for example, you lost 300 dollars during the transaction.) Use string formatting.
shares=int(input("Enter number of shares:")) price1=float(input("Enter purchase price:")) price2=float(input("Enter sale price:")) profit=0-(price2*.97*shares)+(price1*1.03*shares) loss=0-profit if(profit>0): print("After the transaction, you lost " +str(profit)+ " dollars.") elif(profit<0): print("After the transaction, you gained " +str(loss)+ " dollars.")
Write an expression that evaluates to True if the str associated with s1 is greater than the str associated with s2.
str(s1)>str(s2)
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<10
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*86400 h=int(p//3600) y=p/3600-h j=y*3600 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).")