Hashnode
Welcome to ❇️ Python Tech Trivia ❇️ Chapter 7!
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
def who_likes(color):
if color is None:
print("This is not a color...")
else:
print(f"It's {color}! ")
who_likes(None)
#A)This is not a color...
#B)It's None!
Exercise 2
def who_are_you(name, age, gender):
return{'name':name, 'age':age,'gender':gender}
print(who_are_you('Prashant', 44,'male'))
#A)Prashant, 44, male
#B){'name': 'Prashant', 'age': 44, 'gender': 'male'}
Exercise 3
def who_are_you(name, age, gender):
return{'name':name, 'age':age,'gender':gender}
Prashant = who_are_you('Prashant', 44,'male')
print(f"Prashant doesn't look like {Prashant['age']}!?")
#A)Prashant doesn't look 44!?
#B)Prashant doesn't look {'age':44}!?
Exercise 4
def my_colors(color, colors=[]):
colors.append(color)
print(color)
my_colors('red')
my_colors('blue')
#A)['red','blue']
#B) red
# blue
Exercise 5
def rython(sentence, start=0, end=2):
for letter in (sentence[start:end]):
print(letter)
my_sentence = "Now go and code some Python!"
rython(my_sentence)
#A) N
# o
#B) N
# o
# w
Answers
Did you do all the exercises first? You didn't cheat did you!?
Alright, go ahead. Here are the answers.
Exercise 1 - Answer
def who_likes(color):
if color is None:
print("This is not a color...")
else:
print(f"It's {color}! ")
who_likes(None)
#A)This is not a color...
The correct answer is A!
Exercise 2 - Answer
def who_are_you(name, age, gender):
return{'name':name, 'age':age,'gender':gender}
print(who_are_you('Prashant', 44,'male'))
#B){'name': 'Prashant', 'age': 44, 'gender': 'male'}
The correct answer is B!
Exercise 3 - Answer
def who_are_you(name, age, gender):
return{'name':name, 'age':age,'gender':gender}
Prashant = who_are_you('Prashant', 44,'male')
print(f"Prashant doesn't look like {Prashant['age']}!?")
#A)Prashant doesn't look 44!?
The correct answer is A!
Exercise 4 - Answer
def my_colors(color, colors=[]):
colors.append(color)
print(color)
my_colors('red')
my_colors('blue')
#B) red
# blue
The correct answer is B!
The function doesn’t print the list called ‘colors’ but the variable ‘color’! So the output is ‘red’ and then ‘blue’, instead of a list containing both colors :)
Exercise 5 - Answer
def rython(sentence, start=0, end=2):
for letter in (sentence[start:end]):
print(letter)
my_sentence = "Now go and code some Python!"
rython(my_sentence)
#A) N
# o
The correct answer is A!
The code prints does not include the 3d character. If we wanted to include the 3d character we needed to store the value 3 instead of 2 in the argument ‘end’