Allowed: String/List Note Sheet & One Side of One 8.5 by 11 Inch Paper With Hand Written Notes
Allowed: String/List Note Sheet & One Side of One 8.5 by 11 Inch Paper With Hand Written Notes
________ a. If data = ['Today', 'is', 'Wednesday'], then len(data) is 16, the number of characters in the list.
________ d. If average = 315.87342, then the statement print(f'Average: {average:.5f}') will output
Average: 315.87
________ e. To generate a random integer from 1 to 6 inclusive use the expression random.randrange(1, 7).
2. (1 pt) If you want to use the sqrt function from the math module in your Python code, what statement do you need to
include first?
________________________________
3. (1 pt each) Decide whether each is a valid or invalid identifier for a Python variable. Circle valid or invalid.
4. (2 pts each) Evaluate each Python expression and give its resulting value. Make sure to give a value of the appropriate
type (such as including a .0 at the end of a float).
a. 20 – 15 % 2 * 4 b. 2 * 3 ** 2 + 5 c. 7 / 2 + 7 // 2
5. (4 pts) What is the difference between what is stored in the variable data when the two code snippets execute?
Describe what is stored including its type (int, float, str, or list).
value type
7. (4 pts each) Write what will be printed to the console when each of the following snippets of code is executed. Be
very clear with spacing, line breaks, etc. Write your final answer clearly in each box.
a. a = 12
b = 7
c = a + b
b = b + 2
a = a - b
c = c + a
print(a)
print(b)
print(c)
b. value = 10
for num in range(1, 12, 3):
print(num)
value = value + num
print(value)
c. animal = 'antelope'
stop = len(animal) - 2
for i in range(1, stop):
part = animal[:i]
print(part)
print(animal)
d. x = 12
y = 16
if x > y:
print('A')
if x % 2 == 0:
print('B')
elif y % 2 == 0:
print('C')
else:
print('D')
print('E')
e. x = 4
y = 0
if x > 0:
print('A')
if y < 0:
print('B')
elif y > 0:
print('C')
print('D')
else:
print('E')
if y < 0:
print('F')
elif y > 0:
print('G')
else:
print('H')
print('I')
print('J')
On the next two problems you will be writing code. Indention within your code must be clear.
8. (6 pts) Write code using a loop to generate 100 random numbers each random number from 10 to 50 inclusive. Output
each random number generated and the maximum of those numbers. See sample output.
Sample Output
21
32
14
41
.
. continue to output
. the numbers
.
.
Maximum: 47
9. (6 pts) Each line of a file "data.txt" contains a one-word name followed by two integer test scores separated by spaces
(see sample below for sample lines in the file). Write code to read through the file and print each name followed by
the average of its two test scores in the console window (see sample output).
data.txt output