MPL Ch. 3
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 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 that has assigned a string value. Write an expression whose value is true if and only if c is a tab character.
c == '\t'
Write a sequence of statements that finds the first comma in the string associated with the variable line, and associates the variable clause the portion of line up to, but not including the comma.
clause = line.split(",")[0]
Given the variables name1 and name2 , write a fragment of code that assigns the larger of their associated values to first .
first = max(name1,name2)
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
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
You have to do your chemistry homework, but you hate looking up elements on the periodic table! Write a program that takes the name of an element (as a string, independent of case) from standard input and prints a double representing its atomic weight to standard output. Only implement the program for the first three elements, hydrogren, helium, and lithium, which have the respective atomic weights 1.008, 4.0026, and 6.94. If anything else is given as input, print the statement "Sorry, I don't recognize that element!"
hy = 1.008 he = 4.0026 li = 6.94 element_name = input("").lower() if element_name == "hydrogen": print(hy) elif element_name == "helium": print(he) elif element_name == "lithium": print(li) else: print("Sorry, I don't recognize that element!")
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 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 assigns 10,000 to bonus if the goods_sold is greater than 500,000
if goods_sold > 500000 bonus = 10000
Assume that a variable hoursWorked has been initialized. Write a statement that assigns the value True to the variable workedOvertime if hoursWorked is greater than 40 and False otherwise.
if hoursWorked > 40: workedOvertime = True else: workedOvertime = False
Assume that isIsosceles is a boolean variable, and that the variables isoCount,triangleCount, and polygonCount have all been initialized. Write a statement that adds 1 to each of these count variables (isoCount,triangleCount, and polygonCount)if isIsosceles is true.
if isIsosceles == True: isoCount += 1 triangleCount += 1 polygonCount += 1
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.
if numberOfSides == 4: isQuadrilateral = True else: isQuadrilateral = False
Write a statement that toggles on_off_switch. That is, if on_off_switch is False, it is made equal to True; if on_off_switch is True, it is made equal to False.
if on_off_switch == False: on_off_switch = True else: on_off_switch = False
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 else: 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
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 elif y <= x and y <= z: min = y else: min = z
Write an expression that evaluates to True if the value of index is greater than the value of last_index.
index > last_index
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")
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.
is_empty == False and (number_of_credits == 1 or number_of_credits == 3)
Write an expression that evaluates to True if the variable last_name refers to a str that is greater than the str "Dexter".
last_day > "Dexter"
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 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.
number_of_credits == 3 or is_empty == True
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
Write an expression that evaluates to True if the int associated with number_of_prizes is divisible (with no remainder) by the int associated with number_of_participants. Assume that the latter is not zero.
number_of_prizes % number_of_participants == 0
Write an expression that evaluates to True if and only if the variables profits and losses are exactly equal.
profits == losses
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 recalled if the value of modelYear falls within the two recall ranges and assigns False otherwise. Do not use an if statement in this exercise!
recalled = (modelYear >= 1995 and modelYear <= 1998) or (modelYear >= 2004 and modelYear <= 2006)
Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002. Given variables modelYear and modelName write a statement that assigns True to recalled if the values of modelYear and modelName match the recall details and assigns false otherwise.
recalled = (modelYear >= 1999 and modelYear <= 2002) and modelName == "Extravagant"
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 the str associated with s1 is greater than the str associated with s2.
s1 > s2
Write an expression that evaluates to True if the int associated with width_of_box is not divisible (with no remainder) by the int associated with width_of_book. Assume that the latter is not zero.
width_of_box % width_of_book != 0
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 the value associated with x is even, but False if the value is odd.
x % 2 == 0
Write an expression that evaluates to True if and only if the value of x is equal to zero.
x == 0
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
Write an expression that evaluates to True if x is greater than or equal to y.
x >= y
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