0% found this document useful (0 votes)
16 views17 pages

Tuples 2

Uploaded by

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

Tuples 2

Uploaded by

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

TUPLES

NESTING OF TUPLE
 Tuples can be placed inside other tuples.
 When you add one or more tuples inside another tuple, the items in the
nested tuples are combined together to form a new tuple.

METHOD 1 METHOD 2
ACCESSING AND TRAVERSING A TUPLE
 A tuple is a sequence of values which can be of any type and they
are indexed by integer.
 Like lists, there could be positive indexing and negative indexing.

ACCESSING ELEMENTS
TRAVERSING A TUPLE

METHOD 1

METHOD 2
TUPLE OPERATIONS
SLICING
0 1 2 3 4 5 6 7 8

-9 -8 -7 -6 -5 -4 -3 -2 -1
TUPLE ADDITION / CONCATENATION
 Python allow to join/concatenate two tuples.
 New elements can be added to a tuple using +
operator.
TUPLE MULTIPLICATION / REPLICATION
 The multiplication operator (*) is used for repetition of the tuple
 ‘in’ and ‘not in’ membership operators
TUPLE FUNCTIONS
 len() – this function returns the length of the tuple. i.e
count the total number of elements of the tuple.

 count() - this function is used to count the occurrences of


an item in the tuple. Returns 0 if element not found
 any() – this function returns True if a tuple is having atleast
one item.
If the tuple is empty, it will return False.
 min() and max() – max() function returns maximum
value from the tuple. min() function returns minimum
value from the tuple.
 sorted() – This function is used to sort the elements
of a tuple.
It returns a list after sorting.

To convert list to tuple:


 index() – This function finds the first index of a given
item and returns the index. tuple . index (value,
start, end)
COMPARING TUPLES
 You can compare two tuples by using comparison
operators, <, >, ==, !=, <=, >=
DELETING A TUPLE
 del statement is used to delete a tuple
 ADVANTAGES OF USING TUPLE COMPARING TO LIST.

 Tuples use less memory whereas lists use more


memory.

 We can use tuples in a dictionary as a key but it’s not

possible with lists.

 We can access element with an index in both tuples and

lists.
 DISADVANTAGES OF USING TUPLE COMPARING TO LIST.

 We cannot add an element to a tuple but we can add an


element to a list.

 we can’t sort a tuple but in a list we can sort.

 we can’t remove an element in a tuple but in list we can.

 we can’t replace an element in a tuple but n list we can

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy