0% found this document useful (0 votes)
8 views12 pages

Understanding Python Tuples Basics

Uploaded by

navyasri04goud
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)
8 views12 pages

Understanding Python Tuples Basics

Uploaded by

navyasri04goud
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

Tuple

A tuple is a standard data type that can store sequence of


values belonging to any type.
Tuples are immutable. So you cannot change any elements of
tuple.(same as string).
Creating an empty tuple:
>>> t=()
>>> print(t)
No output
>>> t=tuple()
>>> print()
No output
>>> x=[4,5,6,7]
>>> t=tuple(x)
>>> print(t)
(4, 5, 6, 7)
>>> print(x)
[4, 5, 6, 7]
Creating a tuple using strings:
>>> s="India"
>>> t=tuple(s)
>>> print(s)
India
>>> print(t)
('I', 'n', 'd', 'i', 'a')
Creating a tuple using input() function:
>>> t=tuple(input("enter values:"))
enter values: hello
>>> print(t)
('h', 'e', 'l', 'l', 'o')
Accessing tuple elements

A tuple consists of any collection of items which are stored according to its index.
Like strings any item in a tuple can be accessed by using its index value.
tuple indices start with zero (0). The tuple index can be positive or negative
integer value. Hence indexing in tuples can be positive or negative, i.e., we can
access the elements from a tuple either moving from left to right (positive index)
or from right to left (negative index).
Forward indexing
0 1 2 3 4 5
1 2 3 55 66 32
-6 -5 -4 -3 -2 -1
Backward indexing

>>> x=(1,2,3,55,66,32)
>>> x[0]
1
>>> x[3]
55
>>> x[5]
32
>>> x[-1]
32
>>> x[-4]
3
>>> x[-5]
2
Tuples are immutable:
The contents tuple cannot be changed after it has been created. Using the index
number.
>>> x=(1,2,3,4)
>>> x[0]=21
Error

Tuple operators
1. Concatenating tuples
Concatenation refers to creating a new tuple by adding two tuples. The
+ Operator joins or concatenate tuples written on both sides of the
Operator and creates a new tuples.
>>> x=(12,34,56,67)
>>> y=(44,22,55,63)
>>> z=x+y
>>> print(z)
(12, 34, 56, 67, 44, 22, 55, 63)

2..Repetition / Replication operator

Multiply (*asterisk) operator replicates the tuple for specified number


of times.

>>> t=(10,20,30)

>>> t*2

(10, 20, 30, 10, 20, 30)

>>> x=('python')*3

>>> print(x)

pythonpythonpython

Membership operators:

Membership testing is an operation carried out to check or test


whether a particular element /item is a member of that sequence or
not.

>>> x=("python")

>>> print('o' in x)
True

>>> t=(10,20,30,50)

>>> print(40 in list1)

False

>>>>>> print(50 not in list1)

False

Comparison of tuple

Python allows you to compare two tuples. Each element is individually


compared in lexicographical order (alphabetical order or dictionary)
order. We use relational operators to compare two tuples.

>>> (1,2,3,4)<(4,5,6)

True

>>> (1,2,3,4)<(1,2,5,3)

True

>>>
(1,2,3,4)<(1,2,3,2)

False

>>> (1,2,3,4)<(1,2,3,6)

True

>>> (1,2,3,4)>(0,3,4,5)

True
Indexing

Indexing is nothing but there is an index value for each item present in
the sequence. in python positive indexing starts from 0 like other
programming languages.

Similarly negative indexing starts from -1 like other programming


languages.

>>> x=('red','green','blue')

>>> y=(10,20,30,40,50)

>>> print(x[3])

IndexError: tuple index out of range

>>> print(x[2])

blue

>>> print(x[-1])

blue

>>> print(y[-3])

30

Slicing
Slicing and indexing are two inter related operations in tuples. Slicing is
done through indexing. Slicing is an operation in which you can slice a
particular range from that sequence. A slice of tuple is basically its sub
tuple.
When we enter a range of values to extract it is called as slicing.

Ex:

tuple[start:stop:step]

>>> x=(100,3.2,"alphores","computerscience",754,456.3)

>>> print(x[2:5])

('alphores', 'computerscience', 754)

>>> print(x[:5])

(100, 3.2, 'alphores', 'computerscience', 754)

>>> print(x[3:])

('computerscience', 754, 456.3)

>>> print(x[::2])

(100, 'alphores', 754)

>>> print(x[::-2])

(456.3, 'computerscience', 3.2)

>>> print(x[-5:-2])

(3.2, 'alphores', 'computerscience')

>>> print(x[5:2:-1])

(456.3, 754, 'computerscience')

>>> print(x[-2:-5:-1])

(754, 'computerscience', 'alphores')


>>> print(x[-2:-5:-2])

(754, 'alphores')

Unpacking Tuples or Tuple assignment


Creating a tuple from a set of values is called packing.

Ex.

>>> t=(8,9,7)

Creating individual values from a tuple element is called unpacking. It


allows tuple of variables on the left side of assignment operator to be
assigned respective values from a tuple on the right side.

The number of variables on the left side should be same as number of


elements in the tuple.

Ex.

>>> p,q,r=t

Indirectly modifying the tuples:


There are two ways

1) using tuple unpacking

>>> t=(8,9,7)

>>> p,q,r=t
>>> p

>>> q

>>> r

7
>>> p=6

>>> t=p,q,r

>>> t

(6, 9, 7)

2) Using list and tuple functions


(i)convert the tuple to a list using list() function

T=(10,20,40)

L=list(T)

(ii) Make changes in desired element in the list

L[2]=30

Now create a tuple from the modified list

T=tuple(L)
>>> T

(10, 20, 30)

Tuple functions

[Link](): returns the length of a tuple


t=(5,7,8)
x=len(t)
print(x)
3

[Link]() : returns minimum value from tuple. Error will displayed


if string value is passed.

t=10,20,30

>>> min(t)

10

>>> t="a","b","c"

>>> min(t)

'a'
>>> t="a",1,"c"

>>> min(t)

TypeError: '<' not supported between instances of 'int' and


'str'

[Link]() : returns maximum value from tuple. Error will


displayed if string value is passed.

t=10,20,30

>>> max(t)

30

>>> t="a","b","c"

>>> max(t)

'c'

>>> t="a",1,"c"

>>> max(t)

TypeError: '>' not supported between instances of 'int' and


'str'
[Link]() : returns index of first occurrence value

>>> t=(4,5,6,7,5)

>>> m=[Link](4)

>>> print(m)

>>> m=[Link](5,1,4)

>>> m

>>> m=[Link](5,2,5)

>>> m

[Link](): returns count value of a particular item.

t=(3,4,5,4)

m=[Link](4)

print(m)

You might also like