Raza Zaidi
razacodes

razacodes

❇️ Python Tech Trivia ❇️ Chapter 3

❇️ Python Tech Trivia ❇️ Chapter 3

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

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Welcome to ❇️ Python Tech Trivia ❇️

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 = ['red','black','yellow']

colors_2 = ['black','blue','green']

for color in colors:
    if color in colors_2:
        del colors[colors.index(color)]

print(colors)

Exercise 2

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

for color in colors[:2]:
    print(color)

Exercise 3

languages = ['python','javascript']

for language in languages:
    if language != 'python':
        print(f'Are you sure you want to program in {language.title()}?')
    else:
        print(f'{language.upper()} is a great choice!')

Exercise 4

languages = ['java','c','c++','python','javascript']

for language in languages[2:4]:
    if language != 'python':
        print(f'Are you sure you want to program in {language.title()}?')
    else:
        print(f'Choose {language.upper()} instead!')

Exercise 5

colors = {
    'raza':'black',
    'pratham':'white',
    'insha':'blue'
}

for name,color in colors.items():
    if name == 'pratham':
        print(f'''{name.title()}'s favourite color is {color}.''')
    else:
        break

Answers

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

Alright, go ahead. Here are the answers.

Exercise 1

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

colors_2 = ['black','blue','green']

for color in colors:
    if color in colors_2:
        del colors[colors.index(color)]

print(colors)
>>>['red', 'yellow']

Exercise 2

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

for color in colors[:2]:
    print(color)
>>> red
        black

Exercise 3

languages = ['python','javascript']

for language in languages:
    if language != 'python':
        print(f'Are you sure you want to program in {language.title()}?')
    else:
        print(f'{language.upper()} is a great choice!')
>>>PYTHON is a great choice!
       Are you sure you want to program in Javascript?

Exercise 4

languages = ['java','c','c++','python','javascript']

for language in languages[2:4]:
    if language != 'python':
        print(f'Are you sure you want to program in {language.title()}?')
    else:
        print(f'Choose {language.upper()} instead!')
>>>Are you sure you want to program in C++?
       Choose PYTHON instead!

Exercise 5

colors = {
    'raza':'black',
    'pratham':'white',
    'insha':'blue'
}

for name,color in colors.items():
    if name == 'pratham':
        print(f'''{name.title()}'s favourite color is {color}.''')
    else:
        break

The output in the terminal is nothing, because of the break statement!

 
Share this