Python Tuples

Python Tuples

Tuples are sequential data types in Python.

A Tuple is an immutable data type in Python i.e. once defined it can not be changed.

Use round brackets"()" to define a Tuple in Python and comma(,) to separate elements.

We can access Tuple elements using the index value of the element.

Like lists, there is both side indexing in Tuples in Python i.e. from start indexing starts with “0” and from the end, indexing starts with “-1“.


Example


myTuple = (1, 2, 3, 4, "Hello", "World")  # defining a list
print(myTuple)  # printing a list
print(myTuple[0])  # printing first element
print(myTuple[1])  # printing 2nd element
print(myTuple[5])  # printing last element
print(myTuple[-1])  # printing last element
print(myTuple[-2])  # printing last 2nd element

Output


(1, 2, 3, 4, 'Hello', 'World')
1
2
World
World
Hello

One Element Tuple


If we want to declare a one element Tuple then we need to put a comma(,) after the element.


Example

myTuple = (1)
myTuple1 = ("1")
myTuple2 = (1,)
print(type(myTuple))
print(type(myTuple1))
print(type(myTuple2))

Output

<class 'int'>
<class 'str'>
<class 'tuple'>


Range of Indexes


Like in Lists, we can specify the range of indexes to the tuples to return a part from the tuple.

When specifying a range, the return value will be a new tuple with the specified items excluding the last value.


Example

myTuple = (1, 2, 3, 4, "Hello", "World")  # index starts with 0
print(myTuple[0:3])  # 0 to 3-1=2(excluding last value)
print(myTuple[4:5])
print(myTuple[-6:-1])  # negative indexing similiar to list
print(myTuple[-3:-2])

Output

(1, 2, 3)
('Hello',)
(1, 2, 3, 4, 'Hello')
(4,)

Changing Element Values


It is not possible to change Tuple values after a Tuple is created.

Python will show you an error if we try do change the values.


Example

myTuple = (1, 2, 3, 4, "Hello", "World")
myTuple[0] = 2
print(myTuple)

Output

Traceback (most recent call last):
  File "/tmp/sessions/95fd31eb9f7a5397/main.py", line 2, in <module>
    myTuple[0] = 2
TypeError: 'tuple' object does not support item assignment

Deleting a Tuple


We can delete an entire Tuple using the "del" keyword.


Example

myTuple = (1, 2, 3, 4, "Hello", "World")
del myTuple
print(myTuple)

Output

Traceback (most recent call last):
  File "/tmp/sessions/1dd8fce2f6c4006a/main.py", line 3, in <module>
    print(myTuple)
NameError: name 'myTuple' is not defined

We can delete an entire Tuple but it is not possible to delete one or more elements of a Tuple. Because it will be considered as a change in Tuple which is not possible as Tuples are immutable i.e. can not be changed or modified after their creation.


Example

myTuple = (1, 2, 3, 4, "Hello", "World")
del myTuple[0]

Output

Traceback (most recent call last):
  File "/tmp/sessions/5fa1dff2b174e015/main.py", line 2, in <module>
    del myTuple[0]
TypeError: 'tuple' object doesn't support item deletion


Tuple Length


We can "len()" to know the length of a Tuple.

"len()" function is used to calculate the length of other Python objects as well like "Strings and Lists" .


Example

myString = "I am a String"
myList = ["I", "am", "a", "List"]
myTuple = ("I", "am", "a", "Tuple")
print(len(myString))
print(len(myList))
print(len(myTuple))

Output

13
4
4

Joining Two Tuples


We can add or join two or more tuples using "+" operator.


Example

myTuple1 = (1, 2, 3, 4)
myTuple2 = (5, 6, 7, 8)
myTuple3 = (9, 10, 11, 12)
add1 = myTuple1 + myTuple2
add2 = myTuple1 + myTuple2 + myTuple3
print(add1)
print(add2)

Output

(1, 2, 3, 4, 5, 6, 7, 8)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

Tuple Constructor


We can use "tuple()" constructor to create a Tuple.


Example

myStr = "1234"
myList = [1, 2, 3, 4]
myTuple1 = tuple((myStr))
myTuple2 = tuple((myList))
emptyTuple = tuple()
print(emptyTuple)
print(myTuple1)
print(myTuple2)

Output

()
('1', '2', '3', '4')
(1, 2, 3, 4)

Tuple Methods


There are two built-in methods in Python that we can use on Tuples.


1. count()


It returns the number of times a specific value is found inside a Tuple.


Example

myTuple = (1, 2, 3, 4, 1, 2, 1)
print(myTuple.count(1))

Output

3

2. index()


Searches for the specific value inside a Tuple and returns its index.


Example

myTuple = (1, 2, 3, 4)
print(myTuple.index(2))

Output

1



Also Read:

  • Viral Moment: China’s AgiBot X2 Makes History With World’s First Webster Backflip
    The news AgiBot’s humanoid X2 just stuck what the company calls the world’s first seamless Webster backflip by a robot, a tightly executed acrobatic front-flip variation more common in parkour than in labs, according to posts circulating on X and robotics channels this week. The clip shows the X2 gathering momentum, swinging through, and cleanly…
  • Terminator Rising: Albania Hands Power to AI, Echoing a Nightmare of Human Extinction
    Imagine a world where your smart home assistant doesn’t just turn on the lights—it decides you’re a threat and locks you in. That’s the chilling reality Albania just cracked open by appointing Diella, an AI “minister,” to control government decisions. This isn’t some distant sci-fi plot; it’s happening now, in 2025, and it echoes the…
  • What Is Albania’s World-First AI-Generated Minister and How Does It Work?
    Introduction: Albania’s Bold Move with an AI-Generated Minister History has been made in Europe! Albania has just sworn in the world’s first AI-generated minister — and the internet can’t stop talking about it. Meet Diella, a digital “politician” designed by Prime Minister Edi Rama to fight corruption and manage public tenders with zero human bias….
  • Does ChatGPT believe in God? ChatGPT’s Personal Opinion
    Introduction Since the launch of ChatGPT, users have asked it all kinds of interesting, funny, and even thought-provoking questions. Recently, one user asked ChatGPT to share its personal opinion on the existence of God — without relying on references from religious texts like the Gita, Quran, or Bible. Instead, the request was for a purely…
  • ChatGPT vs Human: The Breath-Holding Chat That Ends in “System Failure”
    People experiment with ChatGPT in countless ways every day. While many rely on ChatGPT and other AI tools for productivity and work, others turn to it purely for fun. Almost daily, new entertaining and funny conversations with ChatGPT surface online, and one such chat is currently going viral on social media. This particular exchange is…


Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you

Related Articles