Class XI : Tuples

What is tuple?

In Python programming, a tuple is similar to a list. The difference between the two is that we cannot change the elements of a tuple once it is assigned whereas in a list, elements can be changed.
A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.

Advantages of Tuple over List

Since, tuples are quite similar to lists, both of them are used in similar situations as well.
However, there are certain advantages of implementing a tuple over a list.

Below listed are some of the main advantages:

• We generally use tuple for heterogeneous (different) data types and list for homogeneous (similar) datatypes.
• Since tuple are immutable, iterating through tuple is faster than with list. So there is a slight performance boost.
• Tuples that contain immutable elements can be used as key for a dictionary. With list, this is not possible.
• If you have data that doesn’t change, implementing it as tuple will guarantee that it remains write-protected.

Creating a Tuple

A tuple is created by placing all the items (elements) inside a parentheses (), separated by comma. The parentheses are optional but is a good practice to write it.
A tuple can have any number of items and they may be of different types (integer, float, list, string etc.).

# empty tuple
n = ()

# tuple having integers
n = (1, 2, 3)

# tuple with mixed datatypes
n = (1, “Hello”, 3.4)

# nested tuple
n = (“mouse”, [8, 4, 6], (1, 2, 3))

Creating a tuple with one element is a bit tricky.

# only parentheses is not enough

n = (“hello”)
print(type(n))
# Output: <class ‘str’>

# need a comma at the end

n = (“hello”,)
print(type(n))
# Output: <class ‘tuple’>

# parentheses is optional

n = “hello”,
print(type(n))
# Output: <class ‘tuple’>

Creating tuples from existing sequences

We can use the built-in type object (tuple()) to create tuple from sequences using the syntax given below.

t=tuple(<sequence>)
where <sequence> can be any kind of sequence object including string , list and tuples.

Python creates the individual elements of the tuple from the individual elements of passed sequence. If we pass in another tuple , the tuple function make a copy.

Example:
t1=tuple(‘computer’)
print(t)

Output:
(‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’)
>>>

Example:
t2=tuple(‘hello’)
print(t2)
Output:
(‘h’, ‘e’, ‘l’, ‘l’, ‘o’)
>>>

We can also create a tuple by taking input from the user.
Example:
n=input(“Enter tuple elements “)
t1=tuple(n)
print(t1)
Output:

Enter tuple elements Computer
(‘C’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’)
>>>

The most commonly use method to input a tuple is eval(input()) as given below:
Example:
n=eval(input(“Enter tuple elements “))
print(n)
t1=tuple(n)
print(t1)

Output:
Enter tuple elements (1,2,3,’hello’,10)
(1, 2, 3, ‘hello’, 10)
(1, 2, 3, ‘hello’, 10)
>>>

Accessing Elements in a Tuple

There are various ways in which we can access the elements of a tuple.

Indexing

  • We can use the index operator [] to access an item in a tuple where the index starts from 0.
  • So, a tuple having 6 elements will have index from 0 to 5. Trying to access an element other that (6, 7,…) will raise an IndexError.
  • The index must be an integer, so we cannot use float or other types. This will result into TypeError.
  • Likewise, nested tuple are accessed using nested indexing, as shown in the example below.

c=(‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’)
Positive index

0

1

2

3

4

5

6

7

c

o

m

p

u

t

e

r

Example:

n=('c','o','m','p','u','t','e','r')
 print(n)
 print(n[0])
 print(n[3])
 print(n[6])
 print(n[2])
 print(n[1.5]) 

Output:

(‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’)
c
p
e
m
Error

Displaying the elements of tuple through loop

n=(1,2,3,4,5,6,7,8,9,10)
 print(n)
 for i in n:
     print(n[i-1])

Output:

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

Negative Indexing

Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and so on.
n=(‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’)

Positive index

0

1

2

3

4

5

6

7

c

o

m

p

u

t

e

r

-8

-7

-6

-5

-4

-3

-2

-1

Negative index

n=('c','o','m','p','u','t','e','r')
 print(n)
 print(n[-0])
 print(n[-3])
 print(n[-6])
 print(n[-2])
 print(n[1.5]) #error

Output:

(‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’)
c
t
m
e
>>>

Slicing

We can access a range of items in a tuple by using the slicing operator – colon “:”.
n=(‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’)

Positive index

0

1

2

3

4

5

6

7

c

o

m

p

u

t

e

r

-8

-7

-6

-5

-4

-3

-2

-1

Negative index

n=('c','o','m','p','u','t','e','r')
 print(n)
 print(n[0:2])
 print(n[3:6])
 print(n[6:7])
 print(n[2:4])
 print(n[:4])
 print(n[4:])

Output:

(‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’)
(‘c’, ‘o’)
(‘p’, ‘u’, ‘t’)
(‘e’,)
Note: if there is only one element in the output then we will have “,” end the end of tuple output.
(‘m’, ‘p’)
(‘c’, ‘o’, ‘m’, ‘p’)
(‘u’, ‘t’, ‘e’, ‘r’)
>>>

Changing a Tuple

Unlike lists, tuples are immutable.
This means that elements of a tuple cannot be changed once it has been assigned. But, if the element is itself a mutable data type like list, its nested items can be changed.
We can also assign a tuple to different values (reassignment).
n=(0,1,2,3,4,5,6,7,8,9,10)
print(n)
output:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

n[1]=10
#error will get generated as tuple elements cannot be changed
print(n)

#but list within a tuple can be changed
n = (4, 2, 3, [6, 5])
print(n)
output:
(4, 2, 3, [6, 5])

n[3][0]=10
print(n)
Output:
(4, 2, 3, [10, 5])
n[3][1]=100
print(n)
Output:
(4, 2, 3, [10, 100])

Comparing Tuples

We can compare two tuples using the comparison operators i.e. <, >, <=, >=, == and !=. Python internally compares individual elements of two tuples.

Example:
a=(1,2)
b=(1,2)
print(a==b)
print(a>b)
print(a<b)
output:
True
False
False

Example:
a=(1,2,3)
b=(1,2)
print(a==b)
print(a<b)
print(a>b)
Output:
False
False
True
Example:
a=(1,2)
b=(1.4,3.4)
print(a==b)
print(a>b)
print(a<b)
Output:
False
False
True

Deleting a Tuple

As discussed above, we cannot change the elements in a tuple. That also means we cannot delete or remove items from a tuple.
But deleting a tuple entirely is possible using the keyword del.

n=(1,2,3,4,5)
print(n)
del n[1]
output:
error : TypeError: ‘tuple’ object doesn’t support item deletion


#But we can delete entire tuple
n=(1,2,3,4,5)
print(n)
del n
print(n)
output:
(1, 2, 3, 4, 5)
# NameError: name ‘n’ is not defined

Number Of Visitors

0125398