Tuples in Python: Practical Exercises (+ Solution) - Python Tutorial for Beginners

preview_player
Показать описание
🎓Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Tuples in Python: Practical Exercises (+ Solution)

Awesome, It is your time to practice now. ALL of these instructions are in the description bellow and will combine all the methods you have discovered during this Python Tutorial.

In this exercise, you will have to declare a tuple with the values 'red', 'green', 'blue', 'yellow' and 'black':

my_tuple = ('red', 'green', 'blue', 'yellow', 'black')

1. Unpack my_tuple into multiple variables, within their respective names
2. If the tuple has a value 'green' and that it corresponds to the 2nd item, update variable yellow with the value 'orange', then change the order of the items by swaping the values of the variables red and black together
3. Pack all these variables into a new tuple
4. Iterate through the tuple items until you meet the value 'orange'
5. Once you have encountered this item: convert this tuple into a list, use its index to update the value 'orange' back to its original value 'yellow', and convert it back to your tuple
6. Break out of the loop once it's done
7. Use the slicing operator so that you do not keep the last value
8. Print out the resulting tuple

☞ Please, do tell me in the comment: What did you get?

---

○ Solution for Tuple Exercise in Python

my_tuple = ('red', 'green', 'blue', 'yellow', 'black')

red, green, blue, yellow, black = my_tuple # Unpack values

if 'green' in my_tuple and my_tuple[1] == 'green':
yellow = 'orange' # yellow = 'orange'
red, black = black, red # red='black', black='red'

my_tuple = (red, green, blue, yellow, black) # ('black', 'green', 'blue', 'orange', 'red')

for index, colour in enumerate(my_tuple): # Iterate through items (index, value)

if colour == 'orange':
my_list = list(my_tuple) # Convert into list (mutable)
my_list[index] = 'yellow' # Update value located at index
my_tuple = tuple(my_list) # Convert back inot tuple (immutable)
break # Break out of the Loop

# Do not keep the last item
my_tuple = my_tuple[:-1] # my_tuple = ('black', 'green', 'blue', 'yellow')

Congrats! You just wrote your first complex program using tuple in Python, and some of the Operations you can perform on it. Please, tell me in the comments if you found the answer, or have any question? And check out our Blog, if you want more examples.

Let's play this video, stick around and watch until the end of this video! 👍🏻

- Digital Academy™ 🎓

***

☞ WATCH NEXT:

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡

♡ SUPPORT US ♡

***
Рекомендации по теме
Комментарии
Автор

Let’s practice and use various methods in Python. What result did you get?👇🏻

DigitalAcademyOnline
Автор

Thanks so much for sharing this video and this exercise with us 🙏🏻👌🏻

xJrmy
join shbcf.ru