Raza Zaidi
razacodes

razacodes

❇️ Python Tech Trivia ❇️ Chapter 5

❇️ Python Tech Trivia ❇️ Chapter 5

Raza Zaidi's photo
Raza Zaidi
·Oct 16, 2021·

3 min read

Welcome to ❇️ Python Tech Trivia ❇️ Chapter 5!

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

##############################
##### NORMAL VERSION #########
##############################

colors = ['blue','red','yellow']

for color in colors:
    if color == 'blue':
        print(color)

##############################
##### LIST COMPREHENSION #####
##############################

my_color = [color for color in colors if color == 'blue']
print(my_color[0])

#A) nothing

#B) blue

Exercise 2

##############################
##### NORMAL VERSION #########
##############################

colors = ['blue','red','yellow']

for color in colors:
    if color != 'blue' and color != 'yellow':
        print(color)

##############################
##### LIST COMPREHENSION #####
##############################

my_color = [color for color in colors if color != 'blue' and color != 'yellow']
print(my_color[0])

#A) red

#B) yellow

Exercise 3

##############################
##### NORMAL VERSION #########
##############################

colors = ['blue','red','yellow']

for color in colors:
    if color != 'blue' and color != 'red':
        print(color.upper())

##############################
##### LIST COMPREHENSION #####
##############################

my_color = [color.upper() for color in colors if color != 'blue' and color != 'red']
print(my_color[0])

#A) Yellow

#B) YELLOW

Exercise 4

##############################
##### NORMAL VERSION #########
##############################

colors = ['blue','red','yellow']

for color in colors:
    if color == 'red':
        color = 'gold'
        print(color)
##############################
##### LIST COMPREHENSION #####
##############################

my_color = ['gold' for color in colors if color == 'red']
print(my_color[0])

#A) red

#B) gold

Exercise 5

##############################
##### NORMAL VERSION #########
##############################

colors = ['blue','red','yellow']

for color in colors:
    if color == 'red':
        color = 'silver'
        print(color)
##############################
##### LIST COMPREHENSION #####
##############################

my_color = ['silver' for color in colors if color == 'red']
print(my_color[0])

#A) silver

#B) red

Answers

Did you do all the exercises first? You didn't cheat did you!?

Alright, go ahead. Here are the answers.

Exercise 1 - Answer

##############################
##### NORMAL VERSION #########
##############################

colors = ['blue','red','yellow']

for color in colors:
    if color == 'blue':
        print(color)

##############################
##### LIST COMPREHENSION #####
##############################

my_color = [color for color in colors if color == 'blue']
print(my_color[0])

#A) nothing

#B) blue

The correct answer is B!

Exercise 2

##############################
##### NORMAL VERSION #########
##############################

colors = ['blue','red','yellow']

for color in colors:
    if color != 'blue' and color != 'yellow':
        print(color)

##############################
##### LIST COMPREHENSION #####
##############################

my_color = [color for color in colors if color != 'blue' and color != 'yellow']
print(my_color[0])

#A) red

#B) yellow

The correct answer is A!

Exercise 3

##############################
##### NORMAL VERSION #########
##############################

colors = ['blue','red','yellow']

for color in colors:
    if color != 'blue' and color != 'red':
        print(color.upper())

##############################
##### LIST COMPREHENSION #####
##############################

my_color = [color.upper() for color in colors if color != 'blue' and color != 'red']
print(my_color[0])

#A) Yellow

#B) YELLOW

The correct answer is B!

Exercise 4

##############################
##### NORMAL VERSION #########
##############################

colors = ['blue','red','yellow']

for color in colors:
    if color == 'red':
        color = 'gold'
        print(color)
##############################
##### LIST COMPREHENSION #####
##############################

my_color = ['gold' for color in colors if color == 'red']
print(my_color[0])

#A) red

#B) gold

The correct answer is B!

Exercise 5

##############################
##### NORMAL VERSION #########
##############################

colors = ['blue','red','yellow']

for color in colors:
    if color == 'red':
        color = 'silver'
        print(color)
##############################
##### LIST COMPREHENSION #####
##############################

my_color = ['silver' for color in colors if color == 'red']
print(my_color[0])

#A) silver

#B) red

The correct answer is A!

 
Share this