🎓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:
○ Data Types in Python - • DATA TYPES in Python (Numbers, String...
○ Operators in Python - • OPERATORS in Python (Arithmetic, Assi...
○ IF Statements in Python - • CONDITIONAL Statements in Python (IF,...
○ FOR Loops in Python - • FOR Loop in Python (Syntax, Break, Co...
📖 Blog: http://digital.academy.free.fr/blog
📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners: • 🐍 Python 101: Learn Python Basics for...
📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...
🧑🎓 [COURSE] http://digital.academy.free.fr/courses
📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV
🛒 Shopping and Discounts: http://digital.academy.free.fr/store
💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...
#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/
✧ / digitalacademyy
✧ / digitalacademyfr
✧ / digital_academy_fr
✧ / digitalacademyonline
♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧ / digital_academy
✧ https://www.buymeacoffee.com/digital_...
***
In questa pagina del sito puoi guardare il video online Tuples in Python: Practical Exercises (+ Solution) - Python Tutorial for Beginners della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Digital Academy 06 aprile 2021, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 678 volte e gli è piaciuto 14 spettatori. Buona visione!