[go: up one dir, main page]

0% found this document useful (0 votes)
13 views34 pages

11 Tuples

This document provides an overview of Python tuples, highlighting their characteristics such as immutability and how they differ from lists. It covers the creation, accessing, and operations on tuples, including tuple functions and methods, as well as unpacking and nested tuples. Additionally, it discusses the limitations of certain functions when applied to nested tuples.

Uploaded by

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

11 Tuples

This document provides an overview of Python tuples, highlighting their characteristics such as immutability and how they differ from lists. It covers the creation, accessing, and operations on tuples, including tuple functions and methods, as well as unpacking and nested tuples. Additionally, it discusses the limitations of certain functions when applied to nested tuples.

Uploaded by

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

Ch 06 Tuples

Prepared By Chirag Parikh


Introduction

• The Python tuples are sequences that are used to store a collection of
values of any type.
• Python tuples are immutable i.e., we cannot change the elements of tuples
in place; Python will create a fresh tuple when you make changes to an
element of a tuple.
• Tuple is a type of sequence like strings and lists but it differs from them in
the way that lists are mutable but strings and tuples are immutable.

By Chirag Parikh
Creating and Accessing Tuples
• A tuple is a standard data type of Python that can store a sequence of values
belonging to any type.
• Tuples are depicted through parenthesis i.e., round brackets, e.g.,
() # tuple with no member, empty tuple
(1, 2, 3) # tuple of integers
(1, 2.5, 3.7, 9) # tuple of numbers
(‘a’, ‘b’, ‘c’) # tuple of characters
(‘a’, 1, ‘b’, 3.5, ‘zero’) # tuple of mixed value types
(‘one’, ‘two’, ‘three’) # tuple of strings
By Chirag Parikh
Creating Tuples

• To create a tuple, put a number of expressions in parentheses. That is, use


round brackets to indicate the start and end of the tuple, and separate the
items by commas.
• Assign the reference of tuple after creating it, e.g.,
T=()
T = (value, …)
This construct is known as a tuple display construct.

By Chirag Parikh
Cont. Creating Tuples
• The Empty Tuple
• The empty tuple is ( ). It is equivalent of 0 or ‘’. We can also create empty tuple as:
T = tuple( )
• 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.,
• To construct a tuple with one element just add a comma
after the single element as shown below:

By Chirag Parikh
Cont. Creating Tuples
• Long tuples
• If a tuple contains many elements, then to enter such long tuples, we can split it across several
lines, as shown below:

• Nested tuples
• If a tuple contains an element which is a tuple itself then it is called nested tuple, e.g.,

By Chirag Parikh
Creating Tuples from Existing Sequences
• Syntax: T = tuple(<sequence>)
• Python creates the individual elements of the tuple from the individual elements of passed
sequence. If you pass in another tuple, the tuple function makes a copy.

By Chirag Parikh
Creating Tuple From User Input
• With tuple( ) around input( ), even if you not put parenthesis, it will create a tuple using
individual characters as elements.

• With eval( ), make sure to enclose the tuple elements in parenthesis.

By Chirag Parikh
Example: Program

By Chirag Parikh
Accessing Tuples

• Like lists, tuple-elements are also indexed, i.e., forward indexing as 0, 1, 2,


3,… and backward indexing as -1, -2, -3,…
• Thus, we can access the tuple elements just like we access a list’s or a string’s
elements e.g., Tuple[i] will give you element at ith index; Tuple[a:b] will give
us elements between indexes a to b-1 and so on.

By Chirag Parikh
Similarities between tuple and list
• Length. Function len(T) returns the number of items (count) in the tuple T.
• Indexing and Slicing
T[i] returns the item at index I
T[i:j] returns a new tuple, containing the objects between i and j excluding index j
T[I : j : n] returns a new tuple containing every nth item from index i to j, excluding index j.
• Membership operators. Both ‘in’ and ‘not in’ operators work on Tuples just like they work
for other sequences. That is, in tells if an element is present in the tuple or not and not in
does the opposite.
• Concatenation and Replication operators + and *. The + operator adds one tuple to the
end of another. The * operator repeats a tuple.

By Chirag Parikh
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 not mutable, while lists are.
• You cannot change individual elements of a tuple in place but lists allow you
to do so.
• E.g., L[i] = element is VALID for Lists. BUT
T[i] = element is INVALID for Tuples.

By Chirag Parikh
Immutable Types with Mutable Elements
• If a tuple contains mutable elements such as lists or dictionaries, then tuple’s elements
once assigned will not change, but its individual mutable elements can change their own
elements’ value.

By Chirag Parikh
Traversing a Tuple

By Chirag Parikh
Tuple Operations
• Joining Tuples

• The + operator when used with tuples requires that both the operands must be of tuple
types.

By Chirag Parikh
Cont. Tuple Operations
• Repeating or Replicating Tuples
• Like strings and lists, you can use * operator to replicate a tuple specified number of
times, e.g.

• * operator when used with tuples, requires a tuple and an integer as operands.

By Chirag Parikh
Cont. Tuple Operations
• Slicing the Tuples

By Chirag Parikh
Cont. Tuple Operations
• We can use the + and * operators with tuple slice too. E.g.,

By Chirag Parikh
Comparing Tuples

By Chirag Parikh
Unpacking Tuples
• Creating a tuple from a set of values is called packing and its reverse, i.e., creating
individual values from a tuple’s elements is called unpacking.
• Syntax for unpacking is as follows:
<variable1>, <variable2>, <variable3>, … = t
here the number of variables in the left side of assignment must match the number of elements in
the tuple.

By Chirag Parikh
Deleting Tuples
• The del statement of Python is used to delete elements and objects, but as you know that
the tuples are immutable, which also means that individual elements of a tuple cannot be
deleted. E.g.,

• But you can delete a complete tuple with del statement. E.g.,

By Chirag Parikh
Tuple Functions And Methods
• len( ) returns length of the tuple, i.e., the count of elements in the tuple.
Syntax: len(<tuple>)

• max( ) returns the element from the tuple having the maximum value.
Syntax: max(<tuple>)

By Chirag Parikh
Cont. Tuple Functions And Methods
• max( ) works ONLY IF the sequence contains values of the same type.

By Chirag Parikh
Cont. Tuple Functions And Methods
• min( ) returns the element from the tuple having the minimum value.
Syntax: min(<tuple>)

• sum( ) returns the sum of all elements in a given tuple.


Syntax: sum(<tuple>)

By Chirag Parikh
Cont. Tuple Functions And Methods
• Index( ) returns the index of the existing element of a tuple.
Syntax: <tuplename> . index(<item>)

But if the given item does not exist in tuple, it raises ValueError exception.
• count( ) returns the count of a member element/object in a given sequence.
Syntax: <sequence name> . count(<object>)

By Chirag Parikh
Cont. Tuple Functions And Methods
• sorted( ): returns a new sorted list with sorted elements in it.
Syntax: sorted(<iterable_sequence>, [reverse = True])

By Chirag Parikh
Cont. Tuple Functions And Methods
• tuple( ): This method is actually the constructor method that can be used to create
tuples from different types of values.
Syntax: tuple(<sequence>)

By Chirag Parikh
Program to input a tuple t1 and sort its elements.

By Chirag Parikh
Indirectly Modifying Tuples
1. Using Tuple Unpacking
Tuples are immutable. To change a tuple we would need to first unpack it, change the
values and then again repack it.

By Chirag Parikh
Cont. Indirectly Modifying Tuples
2. Using the constructor functions of lists and tuples.

By Chirag Parikh
Nested Tuples
• Like lists, you can have nested tuples too. A tuple containing another tuple in it as a member
is called a nested tuple, e.g.,

By Chirag Parikh
Functions for Nested Tuples
• Functions that work for all nested tuples: len( ), count( ), index( )

By Chirag Parikh
Cont. Functions for Nested Tuples
• Functions that work for nested tuples having only tuples as elements: max( ) and min( )

By Chirag Parikh
Cont. Functions for Nested Tuples
• Functions that do not work for nested tuples: sum( )
Function sum( ) cannot work with nested tuples as they have at least one tuple as
element and tuples cannot be added with other element types. Thus, using sum( ) with a
nested tuple will raise an error, e.g.,

By Chirag Parikh

You might also like