Tuples in Python: Practical Exercises (+ Solution)

https://www.youtube.com/watch?v=ihE5i3iVa00

🎓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 into 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:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

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

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap