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

Tuples in Python

The document provides an overview of tuples in Python, highlighting their immutability and similarity to lists. It explains how to define, create, access, and manipulate tuples, including operations like concatenation and slicing. Additionally, it covers tuple functions and tasks for practical application, emphasizing the differences between tuples and lists.

Uploaded by

ritamdawn01
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)
6 views

Tuples in Python

The document provides an overview of tuples in Python, highlighting their immutability and similarity to lists. It explains how to define, create, access, and manipulate tuples, including operations like concatenation and slicing. Additionally, it covers tuple functions and tasks for practical application, emphasizing the differences between tuples and lists.

Uploaded by

ritamdawn01
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/ 11

Computer Science

Tuples in Python
Class - 11
Tuples are a lot like lists, and that's why we can define them in a pretty similar way as we did to define
the lists. Simply put, a tuple is a sequence of data.

What makes them different from lists is that tuples are immutable, i.e., the data inside the tuples can't
be modified, which is opposite in the case of lists. Other than this, tuples are very similar to lists and
that would make it much easier for us to understand as we already know about lists.

Defining a Tuple
A tuple is a standard data type of Python than can store a sequence of values belonging to any type and
the values should be enclosed in parenthesis i.e. round brackets. Tuples are immutable sequences
of python.

To define a tuple, we just have to assign a single variable with multiple values separated by commas,
and that variable will be known as a Tuple.

Example:-

myTuple = (1, 2, 3, 4) # tuple on integers

myTuple = (‘a’,’b’,’c’,’d’) # tuple on characters

myTuple = (1, 2.4, 3.7, 4) # tuple on numbers, integers and


floating point numbers
myTuple = (1, 2.3, ‘x’, 4, ‘ram’) # tuple of mixed values

myTuple = (‘one’,’two’,’three’,’four’) # tuple on strings

Creating an empty tuple:


t = ( ) # tuple with no values……..empty tuple

OR
t = tuple()

Single element tuple:-

t = (‘a’) # tuple contain single element

OR
>>> t = 4,

OR
>>> t = (4,)

if you print the above it will give:

>>> t = ('a')
>>> t
'a'

OR
>>> t = 4,
>>> t
(4,)

OR
>>> t = (4,)
>>> t
(4,)

Nested tuples:-
one tuple contain another tuple within it.

Example:

t = (110, 115, (11, 22, 33), 119, 155)

Creating tuple from an existing sequence:-


syntax:

t = tuple(sequence)

Example:

>>> t = tuple('welcome') # tuple t is created from a string sequence


>>> t
('w', 'e', 'l', 'c', 'o', 'm', 'e')
>>>

Example:

>>> x = ['p', 'y', 't', 'h', 'o', 'n'] # x is a list sequence


>>> t = tuple(x) # tuple t is created from a list sequence
>>> t
('p', 'y', 't', 'h', 'o', 'n')
>>>
Tuple creation from user input:-
You can use following from user given single digits or single
characters input.

Example:

Output:

Example:

Output:-

Note: In the above example, you see using tuple( ) method, even you
not put parenthesis, it will create tuple using individual characters
as elements

But most commonly used method to input tuple is using


eval(), as shown below:

Example:

Output:

Note: on the above case you should enclose the values


within parenthesis at the time of inputting values.
Accessing tuples:
As you know that tuples are immutable (non-editable) sequence, thus other than editing
items, you can do all that you can do with list. You can access individual elements in the
same way as a list.

So, before accessing elements first we understand the indexing of elements which is
similar as lists in python.

Indexing in Tuples
Indexing in tuples is also pretty similar to that in lists, the first element has index zero, and it keeps on
increasing for the next consecutive elements. Also, backward indexing is also valid in tuples, i.e., the
last element can be accessed using the index -1 and the consecutive previous numbers by -2, -3 and so
on. Let's take an example,

Example:

t = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100) # t is a tuple

so the indexing of elements as follows:

Accessing individual elements:-


Membership operator : - in and not in
Both ‘in’ and ‘not in’ operators work on tuples just like they work for other sequences
i.e. ‘in’ tells if an element is present in the tuple or not and ‘not in’ does the opposite.

Example:

Concatenation (+) and Replication operators (*) – works same as for other
sequences.

Example:-

Example:-

Traversing a tuple – accessing all elements in tuple also works same as Lists.
Example:
Output:

OR

Output:

Difference from Lists:-


Although tuples are similar to lists, but the main difference is tuples are immutable i.e.
you cannot change individual elements of a tuple in place, but Lists allow you to do so.

If we try to change individual elements of tuple…..then it produces error….as following:


Tuple Operations:-
The most common operations on tuple is joining tuples and slicing tuples.

Joining tuples:-
the ‘+’ operator is used to join tuples.

Example:

Example:

Example: ‘+’ operator works if both the operands must be of tuple types. But if not then
it generates error….as shown below:

So, the solution of the problem is to make the number as tuple types…..as shown below:

Slicing in Tuples
Slicing in tuples, works exactly the same like in the case of lists. Let's start with an example:
Slicing can be done backwards as well using the negative indexes for traversing the tuple from
backward direction.

Example:

on the above example 0 indicate starting value, 7 indicate ending value, and 2 indicate the step value

Example:-

Comparing Tuples:-
Example:-
Example:-

Packing and Unpacking Tuples:-


Packing – creating a tuple from a set of values.

Unpacking – creating individual values from a tuple’s elements.

Example:-

Output:

Deleting a Tuple:-
In order to delete a tuple, the del keyword is used.

Example:-
Tuple Functions:-
1) len() function:-
As you might have already guessed, this function is used to get the number of elements inside any
tuple.

2) max() function :-
To find the maximum value in a tuple, we can use the max() function.

3) min() function :-
To find the minimum value, min() function can be used.

4) sum() function:-
It returns the sum of all elements of a tuple.

output:
5) sorted() function:-
arrange the elements in ascending order and returns a list of sorted elements.

Output:-

For descending order sort:

Output:-

6) index() function :-
It returns the index of an existing element of a tuple.

7) count() function :-
It returns number of occurrence of a given elements in a tuple.

Tasks:-

1) Write a program that inputs two tuples and creates a third that contains all elements of the
first followed by all elements of the second.

2) Write a program which takes a tuple as input from the user and find the second largest
element in the tuple. you can use any of the standard python tuple’s functions to obtain your
result.

Go through the above discussion carefully. If any


query then message/call me

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