❇️ Python Tech Trivia ❇️ Chapter 6

❇️ Python Tech Trivia ❇️ Chapter 6

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

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 == 'black' or color == 'yellow':
        print(color)
##############################
##### LIST COMPREHENSION #####
##############################

my_color = [color for color in colors if color == 'black' or color == 'yellow']
print(my_color[0])

#A) black

#B) yellow

Exercise 2

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

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

for color in colors:
    if color == 'red':
        print(color.title())

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

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

#A) RED

#B) Red

Exercise 3

def yes():
    return True

if yes():
    print("Let's do this")
else:
    print("My answer is yes!")

#A)Let's do this
#B)My answer is yes!

Exercise 4


def who_likes(color):
    if color =='red':
        print("Pratham loves red!")
    elif color == 'blue':
        print("Insha loves blue!")
    else:
        print(f'no one likes {color}')

who_likes('blue')

#A)Pratham loves red!
#B)Insha love blue!

Exercise 5

def who_likes(color):
    if color =='red':
        print("Pratham loves red!")
    elif color == 'blue':
        print("Insha loves blue!")
    else:
        print(f'no one likes {color}')

who_likes('Red')

#A)Pratham loves red!
#B)no one likes 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 == 'black' or color == 'yellow':
        print(color)
##############################
##### LIST COMPREHENSION #####
##############################

my_color = [color for color in colors if color == 'black' or color == 'yellow']
print(my_color[0])



#B) yellow

The correct answer is B!

Exercise 2 - Answer

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

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

for color in colors:
    if color == 'red':
        print(color.title())

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

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


#B) Red

The correct answer is B!

Exercise 3 - Answer

def yes():
    return True

if yes():
    print("Let's do this")
else:
    print("My answer is yes!")

#A)Let's do this

The correct answer is A!

Exercise 4 - Answer


def who_likes(color):
    if color =='red':
        print("Pratham loves red!")
    elif color == 'blue':
        print("Insha loves blue!")
    else:
        print(f'no one likes {color}')

who_likes('blue')


#B)Insha love blue!

The correct answer is B!

Exercise 5 - Answer

def who_likes(color):
    if color =='red':
        print("Pratham loves red!")
    elif color == 'blue':
        print("Insha loves blue!")
    else:
        print(f'no one likes {color}')

who_likes('Red')


#B)no one likes Red

The correct answer is B!