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

Unit 3 Tuples

Uploaded by

ck2842816
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)
9 views

Unit 3 Tuples

Uploaded by

ck2842816
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/ 19

Unit-3

Chapter 2: Tuples
Tuples
• Tuples are a sequence of values separated by commas.
• The values stored in the tuple can be of any type.
• They can be indexed by integers just like list.
• Tuples are immutable unlike lists.
• Defining tuples:
t=‘a’,’b’,’c’
or
t=(‘a’,’b’,’c’)
To create a tuple with a single
element, you have to include the final comma:

Without the comma Python treats (’a’) as an expression


with a string in parentheses that evaluates to a string:
Another way to construct a tuple is the built-in
function tuple.
● If the argument is a sequence (string,
● With no argument,it creates list, or tuple), the result of the call to
an empty tuple: tuple is a tuple with the elements of
the sequence:
Tuple
• A tuple is accessed in the same way as a list.
• But if we try to modify one of the elements of the tuple, we get an
error:

• We can’t modify the elements of a tuple, but we can replace one


tuple with another:
Tuple Assignment
• Python allows tuple to be written on the left side of the
assignment operator.

- The assignment takes place


element by element from right
hand side to left hand side.
- Given an e-mail id, find the username and the domain name.
Dictionaries and Tuples
• We can create a list of key-value pairs, using the items()
function.
• In this list, each element is a tuple.
Multiple Assignment with Dictionaries

For each iteration through the loop, both key and value are
advanced to the next key-value pair in the dictionary.
Multiple Assignment with Dictionaries - Sorting the Dictionary by values

Output:
Convert tuple into a dictionary

Output:

• zip() function is used to combine two or more lists (or any other iterables) into
a single iterable, where elements from corresponding positions are paired
together.
• The resulting iterable contains tuples, where the first element from each list is
paired together, the second element from each list is paired together, and so
on.
• If the passed iterables have different lengths, the iterable with the least items
decides the length of the new iterator.
Remove empty tuple from a list of tuples

Output:
List Tuple Dictionaries
A list is a non-homogeneous A Tuple is also a non- A dictionary is also a non-
data structure that stores the homogeneous data structure that homogeneous data structure that
elements in sequence. stores elements in sequence. stores key-value pairs.

The list can be represented by [ Tuple can be represented by ( ) The dictionary can be represented
] by { }

The list allows duplicate Tuple allows duplicate elements The dictionary doesn’t allow
elements duplicate keys.

Example: [1, 2, 3, 4, 5] Example: (1, 2, 3, 4, 5) Example: {1: “a”, 2: “b”, 3: “c”, 4:


“d”}

A list can be created using the Tuple can be created using the A dictionary can be created using
list() function tuple() function. the dict() function.

A list is mutable i.e we can A tuple is immutable i.e we can A dictionary is mutable, ut Keys are
make any changes in the list. not make any changes in the not duplicated.
tuple.

List is ordered Tuple is ordered Dictionary is ordered (Python 3.7


and above)
Comparing Tuples
● In Python, tuples are compared lexicographically, meaning they are compared
element by element, starting from the first one. When comparing two tuples:
1. Python first compares the first elements of each tuple.
2. If the first elements are equal, it proceeds to compare the second elements.
3. This process continues until a pair of elements is found that is not equal, or one
of the tuples ends.

● For example:
Here, the first elements are both 0. Since they are equal, Python
moves to compare the second elements. The second element of
the first tuple, 3, is less than the second element of the second
tuple, 4. Therefore, (0, 3, 4) is considered smaller than (0, 4, 5).
Comparing Tuples
● Similarly, Here, the first elements are both 0, and the second
elements are 3 and 4, respectively. Since 3 is less
than 4, the first tuple is considered smaller.

In this case, both tuples start with 0, 1, and 2, and


the first tuple has an additional element, 3. Since the
second tuple ends earlier, it is considered smaller.

Both tuples start with 1, 2, and 3. The second tuple


has an additional element, 4. Since it extends
further, the first tuple is considered smaller.
Comparing Tuples
Programs
• Write a program to count the occurrence of each word in the given text file. And sort
the result based on the word length.
• Write a program to sort the list of words from longest to shortest.
• Write a Python program to add an item in a tuple.
• Write a Python program to convert a tuple to a string.
• Write a Python program to get the 4th element and 4th element from last of a tuple.
• Write a Python program to find the repeated items of a tuple.
• Write a Python program to check whether an element exists within a tuple.
• Write a Python program to convert a list to a tuple.
• Write a Python program to remove an item from a tuple.
• Write a Python program to find the index of an item of a tuple.
• Write a Python program to find the length of a tuple.

You might also like