tuples in python class 11 notes

chapter-8
tuples in python notes

 

Python tuples class 11 computer science notes

tuples in python

tuples in python

Introduction:-

the python tuples are sequences that are used to store a tuple of any type. Python tuples are immutable i.e, you cannot change the elements of a tuple in place.
 

Tuples in Python Tutorial 2021

 

Tuple:-

tuples are sequence which are used to store tuple of values of any data type.
tuple are immutable means we cannot change element of tuple in place. A fresh tuple is created when we make change to an element of tuple.
 

Creating and accessing tuples:-

A tuple is a standard data type of python that can store a sequence of values belonging to any type. The tuple are depicted through parentheses i.e., round brackets, e.g., following are some tuples in python:
 
( )                                                # empty tuple
(1,2,3)                                       # tuple of integer
(1,2,3,4.5)                                # tuple of number(integer and floating point)
(‘a’,’b’,’c’)                                 # tuple of character
(‘a’,1,’b’,3.5,’zero’)                 # tuple of mixed value types
(‘one’,’two’)                             # tuples of strings
 

Creating tuples:-

creating tuple is similar to list creation, but here you need to put a number of expressions in parenthesis. that is, use round brackets to indicate the start and end of the tuple, and separate the items by commas. for example:
 
(2,4,6)
(‘abc’,’def’)
(1,2.0,3,4.0)
( )
(1)

the empty tuple:

the empty tuple is ( ).it is the equalent of 0 or “.you can also create empty tuple as:
T=tuple()
it will generate empty tuple and name that tuple as T.
 

(2)Single element tuple:

making a tuple with a single element is tricky because if you just give a single element in round brackets, python considers it a value only, e.g.,
>>>t=(1)
>>>t
1      ————-> (1) was treated as an integer expression.
                                   hence t stores an integer 1.not a tuple
 

To construct a tuple with one element just add a comma after the single element as shown below:

>>>t=3,
>>>t
(3,)
>>>t2=(4,)
>>>t2
(4,)
 

(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:
sqr=(0,1,2,3,4,5,6,3,55,66,65,56,32,122,333,234,236,555,564,775,123,467,244,625)
notice the opening parenthesis and closing parenthesis appear just in the beginning and end of the tuple.
 

(4)Nested tuple:

if a tuple contains an element which is a tuple itself then it is called nested tuple e.g., following is a nested tuple:

 
t1=(1,2,(3,4))
the tuple t1 has three elements in it: 1,2 and (3,4).the third element of tuple t1 is a tuple itself. hence,t1 is a nested tuple.
 

Creating tuples from existing sequences:-

you can also use built-in tuple type object (tuple( ) ) to create tuples from sequences as per the syntax given below:
 
T=tuple(<sequence>)
 
where <sequence> can be any kind of sequence object including string, lists and tuples.
Python creates the individual elements of the tuple from the individual sequence. If you pass in another tuple, the tuple function makes a copy.
consider following examples:
 
>>>t1=tuple(‘hello’)
>>>t1
(‘h’,’e’,’l’,’l’,’o’)
 
t1=tuple(input(‘enter tuple elements:’))
enter tuple elements: 234567
>>>t1
(‘2′,’3′,’4′,’5′,’6′,’7’)
 

Most commonly used method to input tuples is eval(input())

 

Accessing tuples:-

tuples are immutable sequences having a progression of elements. Thus, other than editing items, you can do all that you can do with lists. Thus like lists, you can access the individual elements.
*length: len(T)
*indexing and slicing
*membership operator: ‘in’ and ‘not in’
*concatenation and replication operator + and –
 

Difference from lists:-

although tuples are similar to lists in many ways, yet there is an important difference in mutability of the two. Tuples are immutable, while list are mutable.

Tuple operations:-

(i) joining tuples

>>>t1=(1,3,5)
>>>t2=(6,7,8)
>>>t1+t2
(1,3,5,6,7,8)
only tuple are added to another tuple. you cannot add a number or any other value to tuple.
 

(ii) Slicing the tuples:-

>>>t1=(10,12,14,20,22,24,30,32,34)
>>>seq=t1[3,-3]
>>>seq
(20,22,24)
>>>t1[3,-3]*3
(20,22,24,20,22,24,20,22,24).
 

comparing tuples:-

for comparing two tuples, you can using comparison operators, i.e., <,>,==,!= etc.
>>>a=(2,3)
>>>b=(2,3)
>>>a=b
true
you can try some other example by yourself.
 

Unpacking tuples:-

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 that tuples are immutable, which also means that individual elements of a tuple cannot be deleted, i.e., if you give a code like:
 
>>>t1=(5,7,3,9,12)
>>>t1
(5,7,3,9,12)
>>>del t1
>>>print(t1)
name error : name ‘t1’ is not defined

Tuple function and methods:-

1. The len() method
syntax:
len(<tuple>)
example:
>>>employee=(‘hn’,1000,24,’sales’)
>>>len(employee)
4
 

2. the max() method

syntax:
max(<tuple>)
example:
>>>tp1=(10,12,14,20,22,24,30,32,34)
>>>max(tp1)
34
 
>>>tp2=(“Karan”,”Zubin”,”Zara”,”Ana”)
>>>max(tp2)
‘Zubin’
 

3. the min() method

syntax:
min(<tuple>)
 
example:
>>>tp1=(10,12,14,20,22,24,30,32,34)
>>>min(tp1)
10
 
>>>tp2=(“Karan”,”Zubin”,”Zara”,”Ana”)
>>>min(tp2)
‘Ana’
 

4. the index() method

>>>t1=(3,4,5,6.0)
>>>t1.index(5)
2
 

5. the count() function

syntax:
<sequence name>.count(<object>)
example:
>>>t1=(2,4,2,5,7,4,8,9,9,11,7,2)
>>>t1.count(2)
3
>>>t1.count(7)
2
for an element not in tuple, it return 0(zero)

6. the tuple() method

syntax:
tuple(<sequence>)
example:
creating empty tuple
>>>tuple()
empty
creating tuple from string
>>>t=tuple(“abc”)
>>>t
(‘a’,’b’,’c’)
 
 

Tuples in Python Important Questions

download tuple notes pdf

List manipulation in python notes

string manipulation in python notes