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“.
myTuple = (1, 2, 3, 4, "Hello", "World") # defining a list print(myTuple) # printing a list print(myTuple) # printing first element print(myTuple) # printing 2nd element print(myTuple) # printing last element print(myTuple[-1]) # printing last element print(myTuple[-2]) # printing last 2nd element
(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.
myTuple = (1) myTuple1 = ("1") myTuple2 = (1,) print(type(myTuple)) print(type(myTuple1)) print(type(myTuple2))
<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.
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])
(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.
myTuple = (1, 2, 3, 4, "Hello", "World") myTuple = 2 print(myTuple)
Traceback (most recent call last): File "/tmp/sessions/95fd31eb9f7a5397/main.py", line 2, in <module> myTuple = 2 TypeError: 'tuple' object does not support item assignment
Deleting a Tuple
We can delete an entire Tuple using the
myTuple = (1, 2, 3, 4, "Hello", "World") del myTuple print(myTuple)
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.
myTuple = (1, 2, 3, 4, "Hello", "World") del myTuple
Traceback (most recent call last): File "/tmp/sessions/5fa1dff2b174e015/main.py", line 2, in <module> del myTuple TypeError: 'tuple' object doesn't support item deletion
"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" .
myString = "I am a String" myList = ["I", "am", "a", "List"] myTuple = ("I", "am", "a", "Tuple") print(len(myString)) print(len(myList)) print(len(myTuple))
13 4 4
Joining Two Tuples
We can add or join two or more tuples using
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)
(1, 2, 3, 4, 5, 6, 7, 8) (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
We can use
"tuple()" constructor to create a Tuple.
myStr = "1234" myList = [1, 2, 3, 4] myTuple1 = tuple((myStr)) myTuple2 = tuple((myList)) emptyTuple = tuple() print(emptyTuple) print(myTuple1) print(myTuple2)
() ('1', '2', '3', '4') (1, 2, 3, 4)
There are two built-in methods in Python that we can use on Tuples.
It returns the number of times a specific value is found inside a Tuple.
myTuple = (1, 2, 3, 4, 1, 2, 1) print(myTuple.count(1))
Searches for the specific value inside a Tuple and returns its index.
myTuple = (1, 2, 3, 4) print(myTuple.index(2))
- Oracle Hiring for Python internship 2023: Apply NowJob Description Oracle is hiring for a Python development internship in 2023. Oracle is looking for top-notch interns who are passionate about solving complex problems. You will work with others to design and construct cutting-edge new systems from the ground up using your knowledge and creativity. You will be writing code, completing programming projects, and…
- IIT Kanpur’s Python Learning Program: Apply NowIIT Kanpur has recently released a notification for its Summer Training program on Python programming language. This is organized by IIT Kanpur Intensive Training School (ITS). In this Summer Python program, you will be learning and working on Machine Learning, Data Science, and Deep Learning. You will also be learning various Python libraries like NUMPY,…
- Microsoft Giving Free Machine Learning Course: Enroll NowMicrosoft offers many courses for free on its website. There are more than 4000 courses and all are available free of cost. Today, I will show you a Free Machine learning cum data science course where you will learn Machine learning with data science from scratch. Let’s start. Microsoft Free Machine Learning course overview Name…
- Accenture Giving Free Developer Certificate in 2023Accenture recently announced Accenture Virtual Program 2023 for freshers of the Computer science stream. There are 2 categories in the Virtual Program The Consultant Program and The Developer Program. In The Consultant Program, you will learn how to handle clients and in The Developer Program you will be learning and working on the following technologies:…