Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Tuple in Python

Tuple notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Tuple in Python

Tuple notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Tuples in Python

Points to Learn:
●Introduction of tuple
●Creation of tuple and Nested tuple
●Accessing and Traversing a tuple
●Common tuple Operation
●Functions of tuple
●Comparing tuples
●Deleting a tuple
What is Tuples:
Tuple is a sequence of immutable Python object. Tuples are sequences, just like lists. The only difference
is that tuples are immutable , tuples use parentheses, and lists use square brackets.
Tuples are immutable it means we cannot perform insert, update and delete operation on them.
Example: fruits= (‘mango’,’apple’,’grapes’)
Tuple creation:
Empty tuple: t=()
Tuple with one element: t=(10,)
Tuple with multiple value: t(10, “Hello”,20,”Kanpur”)
Creating tuple using function tuple()
Nested Tuple:
Example: fruits= (10 , 20 , (‘Ram’ , ’Mohan’) , 40 , 50 , [‘Red’ , ’blue’] , 60)
Accessing a Tuple and Nested Tuple:

Traversing a Tuple:
Traversing a tuple means accessing each element of a tuple. This can be done by using either
for or while looping statement.
Common Tuple operations:
●Tuple Slicing: Slicing is used to retrieve a subset of values.
Syntax: tuple_name[start:stop:step]
●Tuple Addition/Concatenation/joining:
●Tuple Multiplication/Repetition
●Membership Operator like ‘in’ and ‘not in’
●Comparing Tuples

Tuple Functions:
●len(): This function return the length of tuple.
Syntax: len(tuple_name)
●count(): This function is used to count the occurrence of an item in the tuple.
Syntax: tup.count(n)
●any(): This function returns True if a tuple is having at least one item. If tuple is empty, it
will return Fasle.
Syntax: any(tuple_name)
●min(), max(), sum(): These functions return minimum, maximum, and sum of all value in
tuple.
Syntax: max(tuple_name), min(tuple_name), sum(tuple_name)
●sorted(): This function is used to sort the element of a tuple. It return a list after sorting.
Syntax: sorted(tuple_name)
●index(): This function find the first index of a given item and returns the index.
Syntax: tuple.index(value, start, end)
●del statement: del statement is used to delete a tuple.
Syntax: del tuple_name

You might also like