Welcome to ❇️ Python Tech Trivia ❇️ Chapter 4!
These are some exercises I post on Twitter every day.
These exercises are meant for beginners to practice what you've learned.
After all, practice makes perfect!
Content
Exercises
Answers
Let's start :)
Exercise 1
colors = [
{'name':'raza', 'color':'black'},
{'name':'pratham','color':'white'},
{'name':'insha','color':'blue'}
]
print(f'''{colors[2]['name'].title()}'s favourite color is {colors[2]['color']}.''')
#A) Insha's favourite color is blue.
#
#B) Pratham's favourite color is white.
Exercise 2
colors = [
{'name':'raza', 'color':'black'},
{'name':'pratham','color':'white'},
{'name':'insha','color':'blue'}
]
for color in colors:
if color['name'] == 'raza':
continue
elif color['name'] == 'pratham':
print(f'{color["name"].title()} likes the color {color["color"]}.')
#A) Nothing
#
#B) Pratham likes the color white.
Exercise 3
colors = [
{'name':'raza', 'color':'black'},
{'name':'pratham','color':'white'},
{'name':'insha','color':'blue'}
]
if colors[0]['name'] == 'Raza':
print(colors[0]['name'].title())
#A) Raza
#
#B) Nothing
Exercise 4
colors = [
{'name':'raza', 'color':'black'},
{'name':'pratham','color':'white'},
{'name':'insha','color':'blue'}
]
name = colors[2]['name'].title()
if name == 'Insha':
print(name)
#A) Pratham
#
#B) Insha
Exercise 5
colors = [
{'name':'raza', 'color':'black'},
{'name':'pratham','color':'white'},
{'name':'insha','color':'blue'}
]
if 'raza' in colors[0].values():
print(colors[0]['color'])
#A) nothing
#
#B) black
Answers
Did you do all the exercises first? You didn't cheat did you!?
Alright, go ahead. Here are the answers.
Exercise 1
colors = [
{'name':'raza', 'color':'black'},
{'name':'pratham','color':'white'},
{'name':'insha','color':'blue'}
]
print(f'''{colors[2]['name'].title()}'s favourite color is {colors[2]['color']}.''')
#The correct answer is A!
#A) Insha's favourite color is blue.
Exercise 2
colors = [
{'name':'raza', 'color':'black'},
{'name':'pratham','color':'white'},
{'name':'insha','color':'blue'}
]
for color in colors:
if color['name'] == 'raza':
continue
elif color['name'] == 'pratham':
print(f'{color["name"].title()} likes the color {color["color"]}.')
#The correct answer is B!
#B) Pratham likes the color white.
Exercise 3
colors = [
{'name':'raza', 'color':'black'},
{'name':'pratham','color':'white'},
{'name':'insha','color':'blue'}
]
if colors[0]['name'] == 'Raza':
print(colors[0]['name'].title())
#The correct answer is B!
#B) Nothing
Exercise 4
colors = [
{'name':'raza', 'color':'black'},
{'name':'pratham','color':'white'},
{'name':'insha','color':'blue'}
]
name = colors[2]['name'].title()
if name == 'Insha':
print(name)
#The correct answer is B!
#B) Insha
Exercise 5
colors = [
{'name':'raza', 'color':'black'},
{'name':'pratham','color':'white'},
{'name':'insha','color':'blue'}
]
if 'raza' in colors[0].values():
print(colors[0]['color'])
#The correct answer is B!
#B) black