= [100, 144, 169, 1000, 8] list_variable
Write a Python code that uses print()
and
max()
functions to print out the largest value in the list,
list_variable
, as follows:
# Answer
= [100, 144, 169, 1000, 8]
list_variable
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 .
= "$10.00"
fare = "2.00$"
tip = "$ 0.80" tax
Write a Python code that uses slicing and the
print()
function to print out the following message:
# Answer
= "$10.00"
fare = "2.00$"
tip = "$ 0.80"
tax
= fare[1:]
fare = tip[0:3]
tip = tax[2:]
tax
# add up float values and assign to trip_total variable
= float(fare) + float(tip) + float(tax)
trip_total print ("The total trip cost is:", "$" + str(trip_total))
## The total trip cost is: $12.8
= 4.0
x = .5 y
For each expression below, what is the value of the expression? Explain thoroughly.
20 == '20'
bool(0) != bool('')
< y or 3*y < x
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:
= (65, 29, 25, 35, 58, 23, 19) respondent_ages
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
= (65, 29, 25, 35, 58, 23, 19)
respondent_ages
= []
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]
= (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple_var = 0 total
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
= (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple_var = 0
total for value in tuple_var: # tuple is iterable.
+= value
total
print("The total of the values in the tuple is: ", total)
## The total of the values in the tuple is: 55
for
loop.
= (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tuple_var = 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]
total
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
= 0
i while i < 5:
print("Programming for Data Analytics is so fun!")
+= 1 i
## 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.