tuples in python class 11 notes

tuples in python notes


Python tuples class 11 computer science notes

tuples in python

tuples in python


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



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:
( )

the empty tuple:

the empty tuple is ( ).it is the equalent of 0 or “.you can also create empty tuple as:
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.,
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:


(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:
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:

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:
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(input(‘enter tuple elements:’))
enter tuple elements: 234567

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

only tuple are added to another tuple. you cannot add a number or any other value to tuple.

(ii) Slicing the tuples:-


comparing tuples:-

for comparing two tuples, you can using comparison operators, i.e., <,>,==,!= etc.
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:
>>>del t1
name error : name ‘t1’ is not defined

Tuple function and methods:-

1. The len() method

2. the max() method


3. the min() method


4. the index() method


5. the count() function

<sequence name>.count(<object>)
for an element not in tuple, it return 0(zero)

6. the tuple() method

creating empty tuple
creating tuple from string

Tuples in Python Important Questions

download tuple notes pdf

List manipulation in python notes

string manipulation in python notes