Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views6 pages

Tuples

The document provides an overview of Python tuples, including their creation, accessing methods, and operations such as concatenation and slicing. It highlights the immutability of tuples, differences from lists, and various tuple functions and methods like len(), max(), min(), index(), and count(). Additionally, it covers tuple unpacking, nested tuples, and the use of the built-in tuple() method to create tuples from existing sequences.
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)
10 views6 pages

Tuples

The document provides an overview of Python tuples, including their creation, accessing methods, and operations such as concatenation and slicing. It highlights the immutability of tuples, differences from lists, and various tuple functions and methods like len(), max(), min(), index(), and count(). Additionally, it covers tuple unpacking, nested tuples, and the use of the built-in tuple() method to create tuples from existing sequences.
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
You are on page 1/ 6

TUPLES

The python tuples are sequences that are used to store a tuple of value of any type. Tuples are
immutable i.e. you cannot change or modify.

CREATING & ACCESSING TUPLES

() empty tuple
(1,2,3) tuple of integer
(1,2.5,3.7,9) tuple of integer & floating point number
(‘a’,1,’b’,3.5,’zero’) tuple of mixed value type
(‘a’,’b’,’c’) tuple of characters
(‘one’,’two’,’three’) tuple of strings

CREATING TUPLES

1. The EMPTY tuples: The empty tuple is ( ). It is tuple equivalent of 0.


T=tuple()

2. Single Element Tuple: Making a tuple with a single element.


T=(1) >>>1

3.Long Tuples: If a tuple contains many elements, then to enter such long tuples, you can split it
across several lines, as given below:
sqrs=(0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484,529,57
6,625)

4. Nested Tuples: If a tuple contain an element which is a tuple itself then it is called nested
tuple.
T1=(1,2,(3,4))

CREATING TUPLES FROM EXISTING SEQUENCES


We can also use the built-in tuple type object (tuple( )) to create tuples from sequences.
T=tuple(sequences)
Where (sequences) can be any kind of sequence object including strings, list and tuples.

>>> t1=tuple(„hello‟) # String


>>> t1
(„h‟,‟e‟,‟l‟,‟l‟,‟o‟)

>>>L=[„w‟,‟e‟,‟r‟,‟t‟,‟y‟] # List
>>>t2=tuple(L)
>>>t2
(„w‟,‟e‟,‟r‟,‟t‟,‟y‟)

T1=tuple(input(“Enter tuple elements:”))


Enter tuple elements: 234567
>>>T1
(„2‟,‟3‟,‟4‟,‟5‟,‟6‟,‟7‟)
ACCESSING TUPLES (INDEX VALUES)
Tuples are immutable sequences having a progression of elements. Tuples are similar to lists in
following ways:
- Length – Len(T)
- Indexing – T(i:j) excluding j term
- Membership operators
- Concatenation and replication operator + & *

ACCESSING INDIVIDUAL ELEMENTS


The individual elements of a tuple are accessed through their indexes given in square brackets.
>>> vowels=(‘a’,’e’,’i’,’o’,’u’)
>>> vowels[0]
‘a’
>>>vowels[4]
‘u’
>>>vowels[-1]
‘u’

DIFFERENCE FROM LIST


Although tuples are similar to list in many ways, yet there is an important difference in the
mutability of two.
L[i]=element/value # VALID
T[i]=element/value # INVALID

TRAVERSING A TUPLE
Syntax: for <item> in <tuple>:
process each item here

For Example:
T=(‘P’,’Y’,’T’,’H’,’O’,’N’)
for a in T:
print(T[a])
The above loop will produce result as:
P
Y
T
H
O
N

Program to print element of a tuple(‘Hello’,”Isn’t”,’Python’,’fun’,’?’)


T=(‘Hello’,”Isn’t”,’Python’,’fun’,’?’)
Length=len(T)
for a in range(length):
print(‘At indixes’, a, ‘and’, (a-length),’element:’, T[a])
TUPLE OPERATIONS
The most common operations that you perform with tuple include joining tuples and slicing
tuples.

Joining Tuples CONCATENATION


>>>t1=(1,2,3)
>>>t2=(4,5,6)
>>>t1+t2
(1,2,3,4,5,6)

>>>T1=(10,20,30)
>>>T2=(40,80,120)
>>>T3=(50,100,150)
>>>T=T1+T2+T3
>>>T
(10,20,30,40,80,120,50,100,150)

NOTE: The operator when used with tuples requires that both the operands must be of
tuple types.

>>> T4=(10,12,14)
>>>T4+2 >>>ERROR

IMPORTANT
Sometimes you need to concatenate a tuple with another tuple containing only one element.

>>>T=T1+T2+T3
>>>T+(3)
>>> ERROR

NOTE: A single value in () is treated as single value not as a tuple. So avoid error just add
a comma after that single element.

REPLICATION

Like strings and list you can use * operator to replicate a tuple specified number of times. Eg. If
T=(1,2,3) .
>>> T* 3
>>>(1,2,3,1,2,3,1,2,3)

SLICING THE TUPLES


Tuple slices, like list slices or string slices are the sub parts of the tuple extracted out.
seq=T[start:stop]

Eg: T=[10,12,14,20,22,24,30,32,34]
>>>seq=T[3 : -3]
>>> seq >>>(20,22,24)
>>>T2=(10,12,14,20,22,24,30,32,34)
>>>T2[3:30]
(20,22,24,30,32,34)
>>>T2[-15:7]
(10,12,14,20,22,24,30)

STEP VALUE
T=[10,12,14,20,22,24,30,32,34]
>>>T [ 0 : 10 : 2]
( 10,14,22,30,34)

You can use the + or * operators with tuple slices too.

For Eg: if a tuple with Tp has values as (2,4,5,7,8,9,11,12,34)


Tp[2:5]*3
(5,7,8,5,7,8,5,7,8)

Tp [2:5] + (3,4)
(5,7,8,3,4)

COMPARING TUPLES
We can compare our tuples without having to write code with loops with it. For comparing two
tuples, you can using comparision operators, i.e. <,>,==,!=

>>>a=(2,3)
>>>b=(2,3)
>>>a==b
TRUE

>>>c=(‘2’,’3’)
>>>a==c
FALSE

>>>d=(2.0,3.0)
>>>d>a
FALSE

>>>e=(2,3,4)
>>>a<e
TRUE

UNPACKING TUPLES
Creating a tuple from a set of value is called packing and its reverse, i.e. creating individual
values from a tuple’s element is called unpacking.

For Eg: t=( 1 , 2 ,’A’ , ’B’)


The length of the above tuple t is 4 as there are four element in it. Now to unpack it, we can
write. w,x,y,z=t
Python will now assign each of the elements to tuple t to the variables on the left side of
assignment operator.
print(w)
print(x)
print(y)
print(z)

Forming a tuple from individual values is called packing and creating individual values from a
tuple’s element is called unpacking.

DELETING TUPLES

The del statement of python is used to delete elements and objects but as you know the tuples are
immutable, which also means that individual elements of a tuple cannot be deleted.

T1=(1,2,3,4)
>>> del t1[2]
>>>ERROR

But you can delete a complete tuple with del as:


Syntax: del <tuple name>

T1=(1,2,3,4)
del T1

TUPLE FUNCTIONS AND METHODS

1. The len() method: This method returns the length of the tuple, i.e. count the elements in the
tuple.
Syntax: len(tuple)
Eg: employee=( ‘prav’, 20000 , 24, ’IT’ )
>>>len(employee)

2. The max() method: This method returns the element from the tuple having maximum value.
Syntax: max(tuple)
Eg: T=( 10 , 12 , 14 , 20 , 22 , 24 , 30 , 32 , 34 )
>>>max(T)
34

NOTE: The max() applied on sequences like tuple/list etc. will return a maximum value ONLY
IF the sequence contains value of same type. If you tuple or list consist of different data type
elements it gives you an error

3. The min() method: This method returns the element from the tuple having minimum value.
Syntax: max(tuple)
Eg: T=( 10 , 12 , 14 , 20 , 22 , 24 , 30 , 32 , 34 )
>>>min(T)
10
4. The index() method: The index() works with tuples in the same way it works with lists. That
is, it returns the index of an existing element of a tuple.
Syntax: tuplename.index (item/element)
Eg: T=( 3 , 4 , 5 , 6.0 )
>>>T.index(5)
2

5. The count() method: The count() method returns the count of member element/ object in a
given sequence (list/tuple).
Syntax: T.count(item)
T=( 2,4,2,5,7,4,8,9,9,11,7,2)
>>>T.count(2)
3

6. The tuple() method: This method is a constructor method that can be used to create tuples
from different types of values.

You might also like