Fall 2026
  • Discord
  • Gradescope
  • Syllabus

On this page

  • Basic Math
  • Question 1
  • Question 2
  • Question 3
  • Question 4
  • Question 5
  • if statements
  • Question 6
  • Question 7
  • Question 8
  • Question 9
  • Loops
  • Question 10
  • Question 11
  • Question 12
  • Question 13
  • Question 14
  • Question 15
  • Question 16
  • Question 17
  • Question 18
  • Question 19
  • Question 20
  • Question 21
  • Question 22
  • Question 23
  • Lists
  • Question 24
  • Question 25
  • Question 26
  • Question 27
  • Question 28
  • Question 29
  • Question 30
  • Question 31
  • Question 32
  • Question 33
  • Question 34
  • Question 35
  • Question 36
  • Question 37
  • Question 38
  • Question 39
  • Question 40
  • Question 41
  • Question 42
  • Question 43
  • Question 44
  • Question 45
  • Question 46
  • Question 47
  • Question 48
  • Functions
  • Question 49
  • Question 50
  • Question 51
  • Question 52
  • Question 53
  • Question 54
  • Question 55
  • Question 56
  • Question 57

Basic Python: Practice Questions

For each snippet, write the output. If it prints nothing, write “No Output”.

Basic Math

You are responsible for all of the basic math operators and understanding how order of operations works. The quizzes will only test integer math that you should be able to complete easily with paper and pencil.

Question 1

x = 2 // 3
y = x * 4 
z = y + 5 
print("z=", z)
Answer
z= 5

Question 2

x = 3 // 2
y = x % 4
z = y - 5
print("z=", z)
Answer
z= -4

Question 3

x = 3 // 2 # 4
y = x % 4
z = y - 5 # 2 - 1
print("z=", z)
Answer
z= -4

Question 4

print(2 * 2 ** 2 + 3 * 4 % 2)
Answer
8

Question 5

print(2 ** 2 * 2 // 3 - 4 + 2)
Answer
0

if statements

You are responsible for understanding basic boolean operations (and, or, not) and comparison operators (>=, >, <, <=, ==).

Question 6

x = 4%5
y = 4//5
if x == 1 or y == 1:
    result = 0
else:
    result = 1
print('result=', result)
Answer
result= 1

Question 7

x = 4-5
y = 5-4
if x == 1 and y == 1:
    result = 0
else:
    result = 1
print('result=', result)
Answer
result= 1

In python, it is common to rely on “truthy” and “falsey” values rather than only the boolean values True and False.

Question 8

if '':
    result = 0
else:
    result = 1
print('result=', result)
Answer
result= 1

Question 9

if 1.0:
    result = 0
else:
    result = 1
print('result=', result)
Answer
result= 0

Loops

You are responsible for understanding how while loops behave.

Question 10

i = 0
total = 0
while i < 5:
    total = total + i
    i += 1
print('total=', total)
Answer
total= 10

Question 11

i = 3
total = 0
while i < 8:
    total = total * i
    i += 1
print('total=', total)
Answer
total= 0

Question 12

i = 34567
total = 0
while i > 0:
    total += 1
    i //= 10
print('total=', total)
Answer
total= 5

Truthy/falsey values commonly are used within the condition of while loops in python, not just for if statements.

Question 13

i = 123
total = 0
while i:
    total += 1
    i //= 10
print('total=', total)
Answer
total= 3

For loops are much more common in python (and most other programming languages) because they are easier to reason about. Everything that a for loop can do, a while loop can also do. But a while loop can do things that for loops can’t (like loop forever). It is considered good style to use for loops whenever possible. The hardest part about for loops is understanding the range function, which is commonly used with for loops.

Question 14

total = 0
for i in range(5):
    total = total - 1
print('total=', total)
Answer
total= -5

Question 15

total = 0
for i in range(3, 5):
    total = total + 1
print('total=', total)
Answer
total= 2

Question 16

total = 42
for i in range(10, 15, 1):
    total %= i
print("total=", total)
Answer
total= 2

Question 17

total = 0
for i in range(10, 0, -2):
    total -= i
print("total=", total)
Answer
total= -30

Nested for loops are very common with lists. You will need to implement many nested loops in this class, and you can expect a problem with nested loops on almost every quiz for the remainder of the course.

Question 18

result = 1
for i in range(5):
    if i<3:
        result *= 1
    else:
        result *= (-1)
print('result=', result)
Answer
result= 1

Question 19

result = 1
for i in range(3, 5):
    if i<3:
        result += 1
    else:
        result -= 1
print('result=', result)
Answer
result= -1

Question 20

result = 1
for i in range(5):
    if i <= 3:
        result += i
    else:
        result += 1
print('result=', result)
Answer
result= 8

Question 21

result = 1
for i in range(5):
    if i > 3:
        result += i
    else:
        result += 1
print('result=', result)
Answer
result= 9

Question 22

total = 0
for i in range(10, 20, 5):
    if i%2 == 1 or i<15:
        total += i
print("total=", total)
Answer
total= 25

Question 23

total = 0
for i in range(0, 10, 3):
    if i%2 == 0 or i<5:
        total += i
print("total=", total)
Answer
total= 9

Lists

A list is the most basic container type in python. For this quiz, you are responsible for how to read from a list, but not how to modify a list. You are responsible for the functions sum, min, and max; you are also responsible for understanding list slices and list indexing.

Question 24

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] 
total = 0 
total += xs[0]
total += xs[1]
total += xs[-3]
print('total=', total)
Answer
total= 21

Question 25

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
total = 0
total += xs[3]
total += xs[-1]
total += xs[0]
print('total=', total)
Answer
total= 29

Question 26

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
total = 0
total += xs[5]
total += xs[-5]
total += xs[-4]
print('total=', total)
Answer
total= 39

Question 27

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[3:5]
total = 0
total += ys[0]
total += ys[1]
total += ys[-1]
print('total=', total)
Answer
total= 25

Question 28

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[-5:-3]
total = 0
total += ys[0]
total += ys[1]
total += ys[-1]
print('total=', total)
Answer
total= 43

Question 29

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[-5:-3]
total = len(ys)
print('total=', total)
Answer
total= 2

Question 30

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[-5:-3]
total = sum(ys)
print('total=', total)
Answer
total= 28

Question 31

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[-3:-5]
total = sum(ys)
print('total=', total)
Answer
total= 0

Question 32

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[-3:-5:-1]
total = sum(ys)
print('total=', total)
Answer
total= 32

Question 33

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[:3]
total = sum(ys)
print('total=', total)
Answer
total= 9

Question 34

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[:3]
total = min(ys)
print('total=', total)
Answer
total= 1

Question 35

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[:3]
total = max(ys)
print('total=', total)
Answer
total= 5

Question 36

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[7:]
total = sum(ys)
print('total=', total)
Answer
total= 72

Question 37

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
ys = xs[-3:]
total = sum(ys)
print('total=', total)
Answer
total= 57

In practice, lists are almost always combined with for loops.

Question 38

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
total = 0
for i in range(3):
    total += xs[i]
print('total=', total)
Answer
total= 9

Question 39

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
total = 0
for i in range(3, 8, 2):
    total += xs[i]
print('total=', total)
Answer
total= 33

Question 40

xs = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
total = 0
for i in range(-3, -6, -1):
    total += xs[i]
print('total=', total)
Answer
total= 45

Question 41

xs = [1, 3, 5]
total = 0
for x in xs:
    total += x
print('total=', total)
Answer
total= 9

Question 42

xs = [1, 3, 5]
total = 0
for x in xs:
    for i in range(3):
        total += x*i
print('total=', total)
Answer
total= 27

Question 43

xs = [1, 3, 5]
ys = [2, 4, 6]
total = 0
for x in xs:
    total += x
    for y in ys:
        total -= x*y
print('total=', total)
Answer
total= -99

Question 44

xss = [[1, 3, 5], [2, 4], [0, 1, 2, 3, 4, 5]]
total = 0
total += xss[0][0]
total += xss[1][1]
total += xss[2][2]
print('total=', total)
Answer
total= 7

Question 45

xss = [[1, 3, 5], [2, 4], [0, 1, 2, 3, 4, 5]]
total = 0
total += xss[1][0]
total += xss[0][1]
total += xss[0][2]
print('total=', total)
Answer
total= 10

Question 46

xss = [[1, 3, 5], [2, 4], [0, 1, 2, 3, 4, 5]]
total = 0
for xs in xss:
    total += xs[0]
    for x in xs:
        total += x
print('total=', total)
Answer
total= 33

Question 47

xss = [[1, 3, 5], [2, 4], [0, 1, 2, 3, 4, 5]]
total = 0
for i in range(2):
    for j in range(len(xss[i])):
        total += xss[i][-j]
print('total=', total)
Answer
total= 15

Question 48

xss = [[1, 3, 5], [2, 4], [0, 1, 2, 3, 4, 5]]
total = 0
for i in range(len(xss)):
    for j in range(i + 1):
        total += xss[i][j]
print('total=', total)
Answer
total= 10

Functions

Functions allow you to reuse the same piece of code many times. A common acronym for good programming is DRY (don’t repeat yourself), and so functions are used all the time. Like nested for loops, expect many problems in future quizzes to involve functions.

Question 49

x = 10
def foo(x):
    if x - 5:
        return 1
    else:
        x += 1
    return x
x += foo(4)
x += foo(5)
x += foo(6)
print("x=", x)
Answer
x= 18

Question 50

x = 10
def foo(x):
    x += 2
    return x
x += foo(9 + 39 // 10) * 3
x += foo(9 + 19 // 10) * 2
print("x=", x)
Answer
x= 76

Question 51

x = 10
def foo(x):
    if x % 2:
        return 1
    x -= 1
    return x
x += foo(4)
x += foo(5)
x += foo(6)
print("x=", x)
Answer
x= 19

Question 52

x = 10
def foo(x):
    x += 1
    return x
x += foo(9 + 39 // 10) * 2
print("x=", x)
Answer
x= 36

Question 53

x = 10
def foo(x):
    return x * 2
for i in range(3):
    x += foo(i)
print("x=", x)
Answer
x= 16

Question 54

x = 10
def foo(x):
    if x - 5:
        return 1
    else:
        x -= 1
    return x
x += foo(4)
x += foo(5)
x += foo(6)
print("x=", x)
Answer
x= 16

Question 55

x = 10
def foo(x):
    x += 2
    return x
x += foo(9 + 39 // 10) * 3
x += foo(9 + 39 // 10) * 2
print("x=", x)
Answer
x= 80

Question 56

def foo(x):
    total = 0
    while x > 0:
        total += 1
        x //= 10
    return total
x = foo(100)
x += foo(1234567)
x += foo(3)
print("x=", x)
Answer
x= 11

Question 57

def foo(x):
    total = 0
    while x > 0:
        total = total + x % 10
        x //= 10
    return total
x = foo(100)
x += foo(1234567)
x += foo(3)
print("x=", x)
Answer
x= 32