0% found this document useful (0 votes)
5 views

tuple notes

The document provides an overview of tuples in Python, highlighting their immutable nature and how they can store various types of values. It explains tuple creation, accessing elements, and operations such as slicing, concatenation, and replication. Additionally, it covers tuple functions like len(), max(), min(), index(), and the use of the tuple() function for creating tuples from other sequences.

Uploaded by

Shouryar97
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

tuple notes

The document provides an overview of tuples in Python, highlighting their immutable nature and how they can store various types of values. It explains tuple creation, accessing elements, and operations such as slicing, concatenation, and replication. Additionally, it covers tuple functions like len(), max(), min(), index(), and the use of the tuple() function for creating tuples from other sequences.

Uploaded by

Shouryar97
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Tuple Manipulation

Based on CBSE Curriculum


Class -11
Introduction
• In Python, tuple is also a kind of container which can
store list of any kind of values.

• Tuple is an immutable data type which means we


can not
change any value of tuple.

• Tuple is a sequence like string and list but the


difference is that list is mutable whereas string and
tuple are immutable.
Creation of
Tuple
• In Python, “( )” parenthesis are used for tuple
creation. ( ) empty tuple
( 1, 2, 3) integers tuple
( 1, 2.5, 3.7, 7) numbers tuple
(‘a’, ’b’, ’c’ ) characters tuple
( ‘a’, 1, ‘b’, 3.5, ‘zero’) mixed values tuple
(‘one’, ’two’, ’three’, ’four’) string tuple

*Tuple is an immutable sequence whose values can not be changed.


Creation of
Tuple
Ex:
• Empty tuple:

• Single element tuple:

• Long tuple:

• Nested tuple:
Creation of Tuple
tuple() function is used to create a tuple from other
sequences. See examples-
Tuple creation from string Tuple creation from list

Tuple creation from input

All these elements are of


character type. To have
these in different types,
need to write following
statement.-
Tuple=eval(input(“Enter
elements”))
Accessing a
Tuple
• In Python, the process of tuple accessing is
same as with list. Like a list, we can access each
and every element of a tuple.
• Similarity with List- like list, tuple also has index. All
functionality of a list and a tuple is same except
mutability.
Forward index 0 1 2 3 4 5 6 7 8 9 10 11 12 13
Tupl R E S P O N S I B I L I T Y
e Backward -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
index
• len ( ) function is used to get the length of tuple.
Accessing a
• Indexing andTuple
Slicing:
• T[ i ] returns the item present at index i.
• T[ i : j ] returns a new tuple having all the items of
T from index i to j.
• T[i:j:n] returns a new tuple having difference of n
elements of T from index i to j.
index i to j.

• Membership operator:
• Working of membership operator “in” and “not in” is
same as in a list.
• Concatenation and Replication operators:
• + operator adds second tuple at the end of first tuple. *
operator repeats elements of tuple.
Accessing a Tuple
• Accessing Individual elements-

• Traversal of a Tuple –
for <item> in <tuple>:
#to process every element.

OUTPUT
Tuple Operations
• Tuple
joining
• Both the tuples should be there to
add with +.

Some errors in tuple joining-


• In Tuple + number
• In Tuple + complex number
• In Tuple + string
• In Tuple + list
• Tuple + (5) will also generate error because when adding a tuple with a
single value, tuple will also be considered as a value and not a tuple..

• Tuple Replication-
Tuple Slicing

Tuple will show till last element of list irrespective of upper


limit.

Every alternate element will be shown.

Every third element will be shown.


Tuple Comparision Tuple unpacking
Tuple Functions
len( ) Function

max( ) Function

min( ) Function

index( ) Function

tuple( ) Function
Thank you

You might also like