• Question 1
  • Question 2
  • Question 3
  • Question 4
  • Question 5
  • Question 6

Question 1

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:

The largest value in the list is: 1000
The smallest value in the list is: 8
# 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 .




Question 2

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:

The total trip cost is: $12.80
# 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




Question 3

x = 4.0
y = .5

For 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)
  1. The statement 20 == '20' is False.
  • The left-hand side 20 is integer and the right-hand side '20' is string.


  1. The statement bool(0) != bool('') is False.
  • The left-hand side bool(0) is False.
  • The right-hand side bool('') is False.
  • The statement False != False is False.


  1. The statement x < y or 3*y < x is True.
  • Let x < y be the statement A. The statement A is False.
  • Let 3*y < x be the statement B. The statement B is True.
  • The statement A or B is True, because either A or B is True.


  1. The statement not (100 == '100' and 25 < 36) is True.
  • Let 100 == '100' be the statement C.
  • The statement C is False, because the left-hand side 100 is integer and the right-hand side ‘100’ is string.
  • Let 25 < 36 be the statement D. The statement D is True.
  • The statement C and D is False, because either C or D is False.
  • The statement not (C and D) is True, because not (False) is not False, which is True.




Question 4

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]




Question 5

tuple_var = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
total = 0

Write 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
  • When we have a list of things to loop through, we can construct a for loop.
    • The above for-loop is equivalent to the following Python code without 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




Question 6

Write a Python code that uses a while-loop and the print() function to print out the following message 5 times:

Programming for Data Analytics is so fun!
# 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.