Python Exam
Python Exam
Python Exam
Review Problems for Chapter 1 using Python 3.2+ (Solutions follow the problems.)
1. What is printed by the Python code?
x = 5
y = x + 3
x = x - 1
z = 10
x = x + z
print('x: {}, y: {}, z: {}'.format(x, y, z))
2. What is printed by the Python code?
print(14//4, 14%4, 14.0/4)
3. What is printed by the Python code?
print(2*'No' + 3*'!')
print(2 * ('No' + 3*'!'))
f2()
f1()
f1()
8. What is printed by the Python code?
def func():
print('Yes')
print('No')
func()
9. What is printed by the Python code?
def func(x):
print(2*x)
func(5)
func(4)
#3
#4
#3
#4
#5
#6
3 2 3.5
NoNo!!!
No!!!No!!!
4. how
is it
now
9.
10
8
First the function is remembered. Afterward it is called
with x = 5, returning 10=2*5. Finally it is called with
x=4, returning 8 = 2*4.
10.
18
details:
short version:
long version:
line n x
1
3 2
2
3
5
2
5
3
10
2
8
3
18
2
4
12.
3+2+5+8 = 18
comment
first value in list
5=3+2
second value in list
10=5+5
last value in list
18=10+8
done with list and loop
prints 18
[0, 1, 2]
5.
13.
1
3
6
8
Hello again!
Hello again!
Hello again!
6.
23
14.
7.
Lo
Hi
Hi
No
Yes
First the function is remembered. It is only
called after 'No' is printed.
0
1
2
3
range(4) contains 0, 1, 2, 3
15.
1
4
100
16.
35
details:
line tot n x comment
1-2
definition
3
0
4
1
first value in list
5
evaluate s(1), returns 1 = 1*1
1
so tot = tot+1 = 0+1 = 1
4
3
next value in list
5
evaluate s(3), returns 9 = 3*3
10
so tot = tot+9 = 1+9 = 10
4
5
last value in list
5
evaluate s(5), returns 25 = 5*5
35
so tot = tot+25 = 10+25 = 35
4
done with list and loop
6
prints 35
17.
Substitutions into format string with floating point formats. Both show 3 decimal places because of the 3's in
the floating point formats. Results are also rounded automatically: 2.568, not 2.567.
18.
# or:
))
s+s+s+s+s
23.
24.
def doubleList(numberList):
''' skip repeating docs... '''
for n in numberList:
print(2*n)
25.