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
[a, b] = [1, 2]
print('a=', a)Answer
a= 1
Question 2
[a, b, c] = [1, 2]
print('a=', a)Answer
ValueError
Question 3
[a, b] = 12
print('a=', a)Answer
TypeError
Question 4
[a, b] = '12'
print('a=', a)Answer
a= 1
Question 5
a, b = [1, 2]
print('a=', a)Answer
a= 1
Question 6
a = 10
b = 5
a, b = b, a
print('a=', a)Answer
a= 5
Question 7
a = 5
b = 10
for i in range(2, 5):
a, b = b, a
print('a=', a)Answer
a= 10
Question 8
total = 0
for a, b in [(1, 2), (3, 4)]:
total += a
print('total=', total)Answer
total= 4
Question 9
total = 0
for a, b in [(1, 2), (3, 4), 5]:
total += a
print('total=', total)Answer
TypeError
Question 10
total = 0
for a, b in [(1, 2), (3, 4, 5)]:
total += a
print('total=', total)Answer
ValueError
Question 11
result = ''
for a, b in zip('hello', 'world'):
result += a
result += b
print('result=', result)Answer
result= hweolrllod
Question 12
names = ['alice', 'bob', 'charlie', 'dave', 'eve']
accumulator = []
for i, name in enumerate(names):
text = name + ' is number ' + str(i)
accumulator.append(text)
print('accumulator[3]=', accumulator[3])Answer
accumulator[3]= dave is number 3
Question 13
names = ['alice', 'bob', 'charlie', 'dave', 'eve']
accumulator = []
for i, name in enumerate(names):
text = 'number ' + str(i) + ' is ' + name
accumulator.append(text)
print('accumulator[-1]=', accumulator[-1])Answer
accumulator[-1]= number 4 is eve
Question 14
sentence = 'python is *weird*'
accumulator = ''
for i, char in enumerate(sentence):
if char == '*' and sentence[i - 1] == ' ':
accumulator += '_'
elif char == '*':
accumulator += '_!'
else:
accumulator += char
print('accumulator=', accumulator)Answer
accumulator= python is _weird_!
Question 15
names = ['alice', 'bob', 'charlie', 'dave', 'eve']
greetings = ['hello ' + name for name in names]
greeting = greetings[2]
print('greeting=', greeting)Answer
greeting= hello charlie
Question 16
xs = [x * x for x in range(10)]
num = xs[5]
print('num=', num)Answer
num= 25
Question 17
xs = [x * x for x in range(10) if x % 2]
num = xs[3]
print('num=', num)Answer
num= 49
Question 18
xs = [x**x for x in range(-3, 3)]
num = xs[5]
print('num=', num)Answer
num= 4
Question 19
xs = [x for x in range(-3, 3) if x]
num = xs[3]
print('num=', num)Answer
num= 1
Question 20
sentence = 'This is an example sentence with a few words in it.'
small_words = [word.lower() for word in sentence.split() if len(word) <= 2]
print('len(small_words)=', len(small_words))Answer
len(small_words)= 4
Question 21
sentence = 'This is an example sentence with a few words in it.'
small_words = [word.lower() for word in sentence.split() if word <= 2]
print('len(small_words)=', len(small_words))Answer
TypeError
Question 22
sentence = 'This is an example sentence with a few words in it.'
small_words = [word.lower() for word in sentence if len(word) <= 2]
print('len(small_words)=', len(small_words))Answer
len(small_words)= 51
Question 23
names = ['alice', 'bob', 'charlie', 'dave', 'eve']
greetings = [name[0].upper() + name[1:].lower() for name in names]
greeting = greetings[1]
print('greeting=', greeting)Answer
greeting= Bob
Question 24
names = ['alice', 'bob', 'charlie', 'dave', 'eve']
greetings = [names[0].upper() + names[1:].lower() for name in names]
greeting = greetings[1]
print('greeting=', greeting)Answer
AttributeError
Question 25
sentence = 'This is an example sentence with a few words in it.'
words = [word.lower() for word in sentence.split() if 't' in word]
print('len(words)=', len(words))Answer
len(words)= 3
Question 26
sentence = 'This is an example sentence with a few words in it.'
xs = [x.upper() for x in sentence if 't' in x]
print('xs[1]=', xs[1])Answer
xs[1]= T
Question 27
responses = [('home', 200), ('archive', 404), ('search', 200)]
status_by_page = {page: status for page, status in responses}
print(status_by_page['archive'])
print('search' in status_by_page)Answer
404
True
Question 28
xss = [[i for i in range(x)] for x in [2, 3, 4]]
x = xss[-1][-2]
print('x=', x)Answer
x= 2
Question 29
xss = [[i for i in range(x)] for x in [2, 3, 4] if x % 2 == 0]
x = xss[-1][-2]
print('x=', x)Answer
x= 2
Question 30
xss = [[i for i in range(x)] for x in [2, 3, 4] if x % 2 == 0]
x = xss[-1:][-2][1]
print('x=', x)Answer
IndexError
Question 31
xss = [[i for i in range(x)] for x in [2, 3, 4] if x % 2 == 0]
x = xss[-1][-2:][1]
print('x=', x)Answer
x= 3
Question 32
xss = [[i for i in range(x) if i % 3 == 1] for x in [4, 5, 6] if x % 2 == 1]
x = xss[-1][-1]
print('x=', x)Answer
x= 4
Question 33
xss = [[i for i in range(x) if x % 3 == 1] for x in [4, 5, 6] if x % 2 == 1]
print('xss=', xss)Answer
xss= [[]]
Question 34
xss = [i for x in [2, 3, 4] if x % 2 == 0 for i in range(x)]
x = xss[-1][-2]
print('x=', x)Answer
TypeError
Question 35
xss = [i for x in [4, 5, 6] if x % 2 == 1 for i in range(x) if i % 3 == 1]
x = xss[-1]
print('x=', x)Answer
x= 4
Question 36
xss = [i for x in [4, 5, 6] if x % 2 == 1 for i in range(x) if x % 3 == 1]
print('xss=', xss)Answer
xss= []
Question 37
tweets = [
{
'source': 'Twitter Web Client',
'text': (
'From Donald Trump: Wishing everyone a wonderful holiday & '
'a happy, healthy, prosperous New Year. Let\u2019s think like '
'champions in 2010!'
),
'retweet_count': 28,
},
{
'source': 'Twitter Web Client',
'text': (
'Trump International Tower in Chicago ranked 6th tallest '
'building in world by Council on Tall Buildings & Urban Habitat '
'http://bit.ly/sqvQq'
),
'retweet_count': 33,
},
{
'source': 'Twitter Web Client',
'text': 'Wishing you and yours a very Happy and Bountiful Thanksgiving!',
'retweet_count': 13,
},
{
'source': 'Twitter for iPhone',
'text': (
'RT @realDonaldTrump: Happy Birthday @DonaldJTrumpJr!\n'
'https://t.co/uRxyCD3hBz'
),
'retweet_count': 9529,
},
{
'source': 'Twitter for iPhone',
'text': ('Happy Birthday @DonaldJTrumpJr!\nhttps://t.co/uRxyCD3hBz'),
'retweet_count': 9529,
},
{
'source': 'Twitter for Android',
'text': (
'Happy New Year to all, including to my many enemies and '
"those who have fought me and lost so badly they just don't "
'know what to do. Love!'
),
'retweet_count': 141853,
},
{
'source': 'Twitter for Android',
'text': (
'Russians are playing @CNN and @NBCNews for such fools - '
"funny to watch, they don't have a clue! @FoxNews totally "
'gets it!'
),
'retweet_count': 23213,
},
{
'source': 'Twitter for iPhone',
'text': (
'Join @AmerIcan32, founded by Hall of Fame legend '
'@JimBrownNFL32 on 1/19/2017 in Washington, D.C.\u2026 '
'https://t.co/9WJZ8iTCQV'
),
'retweet_count': 7366,
},
]
trump_tweets = [tweet for tweet in tweets if 'trump' in tweet['text'].lower()]
print('len(trump_tweets)=', len(trump_tweets))Answer
len(trump_tweets)= 4
Question 38
tweets = [
{
'source': 'Twitter Web Client',
'text': (
'From Donald Trump: Wishing everyone a wonderful holiday & '
'a happy, healthy, prosperous New Year. Let\u2019s think like '
'champions in 2010!'
),
'retweet_count': 28,
},
{
'source': 'Twitter Web Client',
'text': (
'Trump International Tower in Chicago ranked 6th tallest '
'building in world by Council on Tall Buildings & Urban Habitat '
'http://bit.ly/sqvQq'
),
'retweet_count': 33,
},
{
'source': 'Twitter Web Client',
'text': 'Wishing you and yours a very Happy and Bountiful Thanksgiving!',
'retweet_count': 13,
},
{
'source': 'Twitter for iPhone',
'text': (
'RT @realDonaldTrump: Happy Birthday @DonaldJTrumpJr!\n'
'https://t.co/uRxyCD3hBz'
),
'retweet_count': 9529,
},
{
'source': 'Twitter for iPhone',
'text': ('Happy Birthday @DonaldJTrumpJr!\nhttps://t.co/uRxyCD3hBz'),
'retweet_count': 9529,
},
{
'source': 'Twitter for Android',
'text': (
'Happy New Year to all, including to my many enemies and '
"those who have fought me and lost so badly they just don't "
'know what to do. Love!'
),
'retweet_count': 141853,
},
{
'source': 'Twitter for Android',
'text': (
'Russians are playing @CNN and @NBCNews for such fools - '
"funny to watch, they don't have a clue! @FoxNews totally "
'gets it!'
),
'retweet_count': 23213,
},
{
'source': 'Twitter for iPhone',
'text': (
'Join @AmerIcan32, founded by Hall of Fame legend '
'@JimBrownNFL32 on 1/19/2017 in Washington, D.C.\u2026 '
'https://t.co/9WJZ8iTCQV'
),
'retweet_count': 7366,
},
]
popular_tweets = [tweet for tweet in tweets if tweet['retweet_count'] > 100]
print('len(popular_tweets)=', len(popular_tweets))Answer
len(popular_tweets)= 5