Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Tuple Indexes

Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Tuple Indexes

Python Tuple Indexing

In Python, every element inside a tuple has its own index, which helps us access specific values quickly. Indexing works the same way as with lists — just remember that tuples can’t be changed.

Accessing Items by Index

Let’s take a look at a sample tuple:

				
					languages = ("Python", "Java", "C++", "JavaScript", "Go")
#               [0]      [1]     [2]       [3]        [4]

				
			
Positive Indexing

You can access elements starting from the beginning using positive numbers.

				
					print(languages[0])
print(languages[2])
print(languages[4])

				
			

Output:

				
					Python
C++
Go

				
			
Negative Indexing

You can also count backward using negative indexes.

				
					print(languages[-1])  # last item
print(languages[-3])  # third last
print(languages[-5])  # first item

				
			

Output:

				
					Go
C++
Python

				
			

Check If Item Exists

Use the in keyword to verify if a value exists in the tuple.

				
					tools = ("Hammer", "Wrench", "Screwdriver", "Drill")

if "Drill" in tools:
    print("Drill is available.")
else:
    print("Drill is not available.")

				
			

Output:

				
					Drill is available.

				
			

Try it with something that’s not there:

				
					if "Saw" in tools:
    print("Saw is available.")
else:
    print("Saw is not available.")

				
			

Output:

				
					Saw is not available.

				
			

Range of Indexes (Slicing)

You can extract a portion of the tuple using slicing.

Syntax:
				
					tuple[start : end : step]

				
			
Examples:
				
					fruits = ("Apple", "Banana", "Cherry", "Dates", "Fig", "Grapes", "Kiwi")

				
			

Items from index 2 to 5 (end is not included):

				
					print(fruits[2:6])

				
			

Output:

				
					('Cherry', 'Dates', 'Fig', 'Grapes')

				
			

All items from index 4 till end:

				
					print(fruits[4:])

				
			

Output:

				
					('Fig', 'Grapes', 'Kiwi')

				
			

From beginning till index 3:

				
					print(fruits[:3])

				
			

Output:

				
					('Apple', 'Banana', 'Cherry', 'Dates')

				
			

 Alternate values:.

				
					print(fruits[::2])

				
			

Output:

				
					('Apple', 'Cherry', 'Fig', 'Kiwi')

				
			

Every 3rd item from index 1 to 7:

				
					print(fruits[1:7:3])

				
			

Output:

				
					('Banana', 'Fig')

				
			

Summary

  • Tuples support both positive and negative indexing.

  • You can use in to check if a value is present.

  • Use slicing to extract ranges or skip items.

Stay consistent in practice — tuple indexing is an essential skill in Python programming

only on Learn With Arshyan!

Scroll to Top