🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ Iterate through Items of a Tuple in Python (FOR Loops + Enumerate)
Tuple is a very useful and widely used data structure. As a Python developer, you will often be in situations where you will need to iterate through the item of a tuple while you perform some actions. Fortunately, the most common way to iterate through the items of a tuple in Python, is with a for loop.
my_tuple = ('red', 'green', 'blue')
for item in my_tuple:
print(item)
# OUTPUT:
# red
# green
# blue
○ Enumerate Function in Python
This works well if you only need to read the items of a tuple. But, if you want to update them, you need the indexes. A common way to do that, is to combine the range() and len() functions altogether – or use the enumerate() function.
For instance, this loop iterates through the tuple and print out each item. If you do not know How to use the range() function nor how the enumerate() function works, please check out the suggested video about: FOR Loops in Python.
my_tuple = ('red', 'green', 'blue')
for index, item in enumerate(my_tuple):
print(index, item)
# OUTPUT:
# 1, red
# 2, green
# 3, blue
○ Tuple Length in Python
Eventually, you may want to find how many items a tuple has. In that case, you do not have to iterate through the entire tuple and count each item, but simply use the function len() – which returns the number of items inside your tuple.
my_tuple = ('red', 'green', 'blue')
print(len(my_tuple)) # OUTPUT: 3
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
***