list_variable = [100, 144, 169, 1000, 8]Write a Python code that uses print() and
max() functions to print out the largest value in the list,
list_variable, as follows:
# Answer
list_variable = [100, 144, 169, 1000, 8]
print('The largest value in the list is:', max(list_variable),',\n'
'The smallest value in the list is:', min(list_variable), '.')## The largest value in the list is: 1000 ,
## The smallest value in the list is: 8 .
fare = "$10.00"
tip = "2.00$"
tax = "$ 0.80"Write a Python code that uses slicing and the
print() function to print out the following message:
# Answer
fare = "$10.00"
tip = "2.00$"
tax = "$ 0.80"
fare = fare[1:]
tip = tip[0:3]
tax = tax[2:]
# add up float values and assign to trip_total variable
trip_total = float(fare) + float(tip) + float(tax)
print ("The total trip cost is:", "$" + str(trip_total))## The total trip cost is: $12.8
x = 4.0
y = .5For each expression below, what is the value of the expression? Explain thoroughly.
20 == '20'
bool(0) != bool('')
x < y or 3*y < x
not (100 == '100' and 25 < 36)20 == '20' is False.20 is integer and the right-hand
side '20' is string.bool(0) != bool('') is
False.bool(0) is False.bool('') is
False.False != False is
False.x < y or 3*y < x is
True.x < y be the statement A. The statement A is
False.3*y < x be the statement B. The statement B is
True.A or B is True, because
either A or B is True.not (100 == '100' and 25 < 36) is
True.100 == '100' be the statement C.False, because the left-hand side
100 is integer and the right-hand side ‘100’ is string.25 < 36 be the statement D. The statement D is
True.C and D is False, because
either C or D is False.not (C and D) is True,
because not (False) is not False, which is
True.This Python code creates a tuple with seven different ages:
respondent_ages = (65, 29, 25, 35, 58, 23, 19)Write a Python code that uses a for-loop statement and a
if-else statement to assign the list,
under40_list, to [29, 25, 35, 23, 19] and the
list, over40_list, to [65, 58].
# Answer
respondent_ages = (65, 29, 25, 35, 58, 23, 19)
under40_list = []
over40_list = []
for age in respondent_ages:
if age < 40:
under40_list.append(age)
else:
over40_list.append(age)
print(under40_list)## [29, 25, 35, 23, 19]
print(over40_list)## [65, 58]
tuple_var = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
total = 0Write a Python code that uses the following two lines (1) a
for-loop statement to process each element of
tuple_var and (2) the following line.
print("The total of the values in the tuple is: ", total)# Answer
tuple_var = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
total = 0
for value in tuple_var: # tuple is iterable.
total += value
print("The total of the values in the tuple is: ", total)## The total of the values in the tuple is: 55
for loop.
tuple_var = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
total = 0
total = total + tuple_var[0]
total = total + tuple_var[1]
total = total + tuple_var[2]
total = total + tuple_var[3]
total = total + tuple_var[4]
total = total + tuple_var[5]
total = total + tuple_var[6]
total = total + tuple_var[7]
total = total + tuple_var[8]
total = total + tuple_var[9]
print("The total of the values in the tuple is: ", total)## The total of the values in the tuple is: 55
Write a Python code that uses a while-loop and the
print() function to print out the following message 5
times:
# Answer
i = 0
while i < 5:
print("Programming for Data Analytics is so fun!")
i += 1## Programming for Data Analytics is so fun!
## Programming for Data Analytics is so fun!
## Programming for Data Analytics is so fun!
## Programming for Data Analytics is so fun!
## Programming for Data Analytics is so fun!
Step 1. We first assigned the value 0 to i.
Step 2. The while loop compared the value of
i to 5 and continued if i was less than
5.
Step 3. Inside the while loop, we printed
Programming for Data Analytics is so fun! and then
incremented i by 1 with the statement
i += 1.
Step 4. Python goes back to the top of the loop, and again
compares i with 5.
Step 5. The value of i is now 1, so the contents of
the while loop are again executed, and i is
incremented to 2.
Step 6. This continues until i is incremented from 4
to 5 at the bottom of the loop.
Step 7. On the next trip to the top, i < 5 is now
False, and the while loop ends.