Access Items from a Tuple in Python (Indexing, Slicing)

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Access Items from a Tuple in Python (Indexing, Slicing)

You can access individual items in a tuple using an index in square brackets, or using the unpacking method. Note that Tuple indexing in Python starts from 0. The indices for the elements in a tuple are illustrated as below:

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

print(my_tuple[0])
# Prints red

print(my_tuple[2])
# Prints blue



○ Negative Indexing in Python

You can access a tuple by negative indexing as well. Negative indexing counts backward, from the end of the tuple. So, my_tuple[-1] refers to the last item, my_tuple[-2] is the second-last item, and so on. But, be careful not to access an element outside of your tuple, or you would get an “IndexError: tuple index out of range”.

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

print(my_tuple[-1])
# Prints black

print(my_tuple[-2])
# Prints yellow



○ Tuple Slicing in Python

Okay, But How you would access a range of items, from your tuple? You need to slice a tuple using the slicing operator. Tuple slicing is similar to List slicing, in which you will specify the start and end indexes with an optional step size. Each parameter can be positive or negative indexing values. Do you want to discover more about Python slicing Operators? Please, Check out our Blog, and the video about Lists in Python.

my_tuple = ('a', 'b', 'c', 'd', 'e', 'f')

print(my_tuple[2:5]) # ('c', 'd', 'e')
print(my_tuple[0:2]) # ('a', 'b')
print(my_tuple[3:-1]) # ('d', 'e')



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