Fall 2026
  • Discord
  • Gradescope
  • Syllabus

On this page

  • Question 1
  • Question 2
  • Question 3
  • Question 4
  • Question 5
  • Question 6
  • Question 7
  • Question 8
  • Question 9
  • 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
  • Question 24
  • Question 25
  • Question 26
  • Question 27
  • Question 28
  • Question 29
  • Question 30
  • Question 31
  • Question 32

Python Syntactic Sugar: Practice Questions

For each snippet, write the output, the exception type, or “Runs Forever.” If it prints nothing, write “No Output”.

Question 1

city, temperature = ('Oslo', 12)
print(city)
print(temperature + 3)
Answer
Oslo
15

Question 2

first, second = 'hi'
print(second + first)
Answer
ih

Question 3

name, score = ('Ada', 8, 10)
print(score)
Answer
ValueError

Question 4

left, right = 'red', 'blue'
left, right = right, left
print(left, right)
Answer
blue red

Question 5

orders = [('tea', 2), ('bread', 3)]
total = 0
for item, quantity in orders:
    total += quantity
print(total)
Answer
5

Question 6

names = ['Ada', 'Bo', 'Cy']
scores = [4, 7]
for name, score in zip(names, scores):
    print(name, score)
Answer
Ada 4
Bo 7

Question 7

for position, color in enumerate(['red', 'blue']):
    print(position, color)
Answer
0 red
1 blue

Question 8

pages = ['Intro', 'Loops', 'Files']
labels = []
for number, title in enumerate(pages, start=5):
    labels.append((number, title))
print(labels)
Answer
[(5, 'Intro'), (6, 'Loops'), (7, 'Files')]

Question 9

prices = [2, 5, 3]
loop_result = []
for price in prices:
    loop_result.append(price + 1)
short_result = [price + 1 for price in prices]
print(loop_result)
print(loop_result == short_result)
Answer
[3, 6, 4]
True

Question 10

minutes = [2, 4, 1]
print([value * 60 for value in minutes])
Answer
[120, 240, 60]

Question 11

print([len(word) for word in ['red', 'blue']])
print([len(letter) for letter in 'red'])
Answer
[3, 4]
[1, 1, 1]

Question 12

values = [2, 4]
doubled = [value * 2 for value in values]
print(values)
print(doubled)
Answer
[2, 4]
[4, 8]

Question 13

rectangles = [(2, 3), (4, 2)]
print([(width, width * height) for width, height in rectangles])
Answer
[(2, 6), (4, 8)]

Question 14

items = ['tea', 'bread', 'rice']
prices = [3, 5]
print([(item.upper(), price + 1) for item, price in zip(items, prices)])
Answer
[('TEA', 4), ('BREAD', 6)]

Question 15

values = []
print([10 / value for value in values])
Answer
[]

Question 16

words = ['red', 'blue', 'gold']
print([(i, word[i]) for i, word in enumerate(words)])
Answer
[(0, 'r'), (1, 'l'), (2, 'l')]

Question 17

scores = [7, 3, 8, 5]
print([score for score in scores if score >= 5])
Answer
[7, 8, 5]

Question 18

names = ['Ada', 'ALAN', 'Bo']
print([name for name in names if 'a' in name.lower()])
Answer
['Ada', 'ALAN']

Question 19

values = [2, 0, 4]
print([12 // value for value in values if value != 0])
Answer
[6, 3]

Question 20

prices = [('tea', 3), ('bread', 5)]
print({item: price * 2 for item, price in prices})
Answer
{'tea': 6, 'bread': 10}

Question 21

visits = [('Ada', 1), ('Bo', 2), ('Ada', 4)]
print({name: count for name, count in visits})
Answer
{'Ada': 4, 'Bo': 2}

Question 22

rows = [[1, 2], [3], []]
print([len(row) for row in rows])
Answer
[2, 1, 0]

Question 23

rows = [[1, 2], [3], []]
print([[value * 10 for value in row] for row in rows])
Answer
[[10, 20], [30], []]

Question 24

rows = [[1, 2], [3], []]
grouped = [[value for value in row] for row in rows]
flat = [value for row in rows for value in row]
print(grouped)
print(flat)
Answer
[[1, 2], [3], []]
[1, 2, 3]

Question 25

rows = [[1, 4], [3], []]
print([[value for value in row if value % 2 == 0] for row in rows])
Answer
[[4], [], []]

Question 26

rows = [[1, 4], [3], []]
even_rows = [[value for value in row if value % 2 == 0] for row in rows]
print([row for row in even_rows if row])
Answer
[[4]]

Question 27

rows = [[4, 1, 4], [], [2, 5]]
print([value for row in rows for value in row if value >= 4])
Answer
[4, 4, 5]

Question 28

colors = ['red', 'blue']
sizes = ['S', 'M']
print([(color, size) for color in colors for size in sizes])
print(list(zip(colors, sizes)))
Answer
[('red', 'S'), ('red', 'M'), ('blue', 'S'), ('blue', 'M')]
[('red', 'S'), ('blue', 'M')]

Question 29

names = ['A', 'B']
rows = [[2, 3], [4]]
print([(name, value * 2)
       for name, row in zip(names, rows)
       for value in row])
Answer
[('A', 4), ('A', 6), ('B', 8)]

Question 30

clubs = ['art', 'chess', 'hiking']
members = [['Ada', 'Bo'], [], ['Cy']]
print({club: len(group) for club, group in zip(clubs, members)})
Answer
{'art': 2, 'chess': 0, 'hiking': 1}

Question 31

rows = [[2, 5], [1, 4]]
loop_result = []
for row in rows:
    for value in row:
        if value > 2:
            loop_result.append(value + 1)
short_result = [value + 1 for row in rows for value in row if value > 2]
print(loop_result)
print(loop_result == short_result)
Answer
[6, 5]
True

Question 32

rows = [[0, 3], [], [4], [2, 5]]
print([value for row in rows if len(row) == 2
       for value in row if value > 2])
Answer
[3, 5]