0% found this document useful (0 votes)
7 views18 pages

Understanding Tuples in Python

The document provides an overview of tuples in Python, describing them as immutable containers that can store heterogeneous data types. It explains how to create tuples, access their elements, and perform operations such as slicing, joining, and replicating. Additionally, it highlights the differences between tuples and lists, emphasizing that tuples are immutable while lists are mutable.

Uploaded by

MonSoon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Understanding Tuples in Python

The document provides an overview of tuples in Python, describing them as immutable containers that can store heterogeneous data types. It explains how to create tuples, access their elements, and perform operations such as slicing, joining, and replicating. Additionally, it highlights the differences between tuples and lists, emphasizing that tuples are immutable while lists are mutable.

Uploaded by

MonSoon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMPUTER SCIENCE

CLASS XI
01 Tuples
Tuples in Python
•A tuple is a container which is used to store a list of values of any
type. It can store heterogeneous type of data.
•It is a immutable data structure. We cannot change the elements
of a tuple in place.
•It stores data in an organized manner.
•Data stored in the tuple are called elements.
•Each element can be accessed with the help of
the index number placed in square brackets.
•The index number starts with 0.
•The list elements are stored within
parenthesis ().
Tuples in Python
Examples
• T = (22, 35, 21, 27, 31)
• T = (“Apple”, “Orange”, “Mango”, “Grapes”)
• T = ( 9.3, 8.35, 9.44, 38.25)

L = (“Apple”, 25.5, “Orange”, 40, “Mango”, 100)


Creating Tuples
•Tuples are formed by placing a comma-separated-list of values in
parenthesis.
T = ()
T = (1, 4, 9, 16, 25)
•An empty Tuple
L = tuple()

•A nested tuple
L = (1, 4, 9, (1, 8), 16)
Creating Tuples from Existing sequences
T = tuple(“hello”)
print (T) (‘h’, ‘e’, ‘l’, ‘l’, ‘o’)
L = [‘c’, ‘b’, ‘s’, ‘e’, ‘2’, ‘0’, ‘2’, ‘0’]
T1 = tuple(L)
print(T1) (‘c’, ‘b’, ‘s’, ‘e’, ‘2’, ‘0’, ‘2’, ‘0’)
T = tuple(input(“Enter tuple elements :”))
Enter tuple elements : 786171
print(T) (‘7’, ‘8’, ‘6’, ‘1’, ‘7’, ‘1’)
L = eval(input(“Enter tuple elements :”))
Enter tuple elements : (7, 8, 6, 1, 7, 1)
print(L) (7, 8, 6, 1, 7, 1)
Similarity with Lists
•Length : Function len() returns the number of items in the tuple.
•Indexing and slicing :
L[i] returns the item at index i and
L[i : j] returns the new tuple, containing the objects at indexes
between i and j (excluding index j)
•Membership operators:
Both ‘in’ and ‘not in’ operators work.
•Concatenation and Replication: The + operator
adds one tuple to the end of another.
The * operator repeats a tuple.
Difference with Lists
•Lists are mutable, but Tuples are immutable.

L = list(“hat”)
L[0]=‘m’
print(L)

T = tuple(“hat”)
T[0]=‘m’
print(T)
Accessing tuples
T = tuple("GOOD MANNERS")

print(T[0]) 0 1 2 3 4 5 6 7 8 9 10 11
‘G’ ‘O’ ‘O’ ‘D’ ‘ ‘ ‘M’ ‘A’ ‘N’ ‘N’ ‘E’ ‘R’ ‘S’
print(T[3])
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
print(T[-5])

print(T[5])

n = len(T)
print(T[-n])

print(T[12])
Traversing a Tuple
T = (‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
for i in T:
print(i)

T = (‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)


for i in range(0, len(T)):
print(T[i])
Operations in Tuples
• Joining Tuples
tuple1 = (1, 3, 5)
tuple2 = (6, 7, 8)
T = tuple1 + tuple2
print(T)

(1, 3, 5, 6, 7, 8)
Operations in Tuples
• Joining Tuples
tuple1 = (1, 3, 5)
tuple2 = (6, 7, 8)
tuple1 += tuple2
print(tuple1)

(1, 3, 5, 6, 7, 8)
Operations in Tuples
• Joining Tuples
tuple1 = (1, 3, 5)
tuple1 += tuple("abc")
print(tuple1)

(1, 3, 5, ‘a’, ‘b’, ‘c’)


Operations in Tuples
• Repeating or Replicating Tuples
tuple1 = (1, 3, 5)
tuple2 = tuple1 * 3
print(tuple2)

(1, 3, 5, 1, 3, 5 , 1, 3, 5)
Operations in Tuples
• Slicing
0 1 2 3 4 5
T = tuple("python“) ‘P’ ‘Y’ ‘T’ ‘H’ ‘O’ ‘N’
-6 -5 -4 -3 -2 -1
print( T[0:6]) (‘p’,’y’,’t’,’h’,’o’,’n’)

print( T[0:3]) (‘p’,’y’,’t’)

print( T[-6:-2]) (‘p’,’y’,’t’,’h’)

print( T[-4:-1]) (’t’,’h’,’o’)


Operations in Tuples
• Slicing
0 1 2 3 4 5
T = tuple("python“) ‘P’ ‘Y’ ‘T’ ‘H’ ‘O’ ‘N’
-6 -5 -4 -3 -2 -1
print( T[:6]) (‘p’,’y’,’t’,’h’,’o’,’n’)

print( T[:4]) (‘p’,’y’,’t’,’h’)

print( T[2:]) (’t’,’h’,’o’,’n’)

print( T[4:]) (’o’,’n’)


Operations in Tuples
• Slicing
0 1 2 3 4 5
T = tuple("python") ‘P’ ‘Y’ ‘T’ ‘H’ ‘O’ ‘N’
-6 -5 -4 -3 -2 -1

print( T[1:6:2]) ('y‘,'h‘,'n')

print( T[-6:-2:3]) (‘p‘,'h’)

print( T[::-2]) (‘n‘,'h‘,‘y')

print( T[::-1]) (‘n‘,‘o‘,‘h‘,’t’,’y’, p’)


Unpacking Tuples
T = (7, 8, 6)

a, b, c = T

Here, the values of the tuples are assigned to the


different variables.
“Tuple unpacking requires that the list of variables
on the left has the same numbr of elements as the
length of the tuple.”

print(a) 7
print(b) 8
print(c) 6

You might also like