0% found this document useful (0 votes)
2 views71 pages

Python Unit 3 - Part I

The document outlines the key concepts of Python programming, focusing on data structures such as lists, tuples, and dictionaries. It covers operations, methods, and advanced processing techniques like list comprehension, along with illustrative programming examples. Learning outcomes include understanding Python syntax, program flow control, and the ability to create programs using various data structures.

Uploaded by

sharada k
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)
2 views71 pages

Python Unit 3 - Part I

The document outlines the key concepts of Python programming, focusing on data structures such as lists, tuples, and dictionaries. It covers operations, methods, and advanced processing techniques like list comprehension, along with illustrative programming examples. Learning outcomes include understanding Python syntax, program flow control, and the ability to create programs using various data structures.

Uploaded by

sharada k
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/ 71

Vidya Vikas Mandal’s

SHREE DAMODAR COLLEGE OF COMMERCE & ECONOMICS


Affiliated to Goa University
Accredited by NAAC with Grade ‘A’

Python Programming
Unit III
Programming

LIST, TUPLE AND DICTIONARY


Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Outline
• Lists: list operations, list slices, list methods, list loop, mutability,
aliasing, cloning lists, list parameters, Lists as arrays
• Tuples: tuple assignment, tuple as return value
Programming

• Dictionaries: operations and methods


• advanced list processing - list comprehension
Python

• Illustrative programs: selection sort, insertion sort, merge sort,


histogram

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Learning Outcomes

CO1. Explain fundamental principles, syntax and semantics of Python


Programming

programming.
CO2. Demonstrate the understanding of program flow control and handling of strings,
Python

functions, files & exception handling.


CO3. Determine the methods to create and develop Python programs by utilizing the
data structures like lists, dictionaries, tuples and sets.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Vidya Vikas Mandal’s
SHREE DAMODAR COLLEGE OF COMMERCE & ECONOMICS
Affiliated to Goa University
Accredited by NAAC with Grade ‘A’

Lists
operations, slices, methods, loop, mutability,
aliasing, cloning lists, list parameters,
Lists as arrays

29-03-2022 5
Programming
Python

08/07/2025 VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Lists
• A list is an ordered sequence of items/ values.
– In a string, the values are characters;
– In a list, they can be any type.
• The values in a list are called elements or sometimes items.
Programming

• There are several ways to create a new list


• Simplest is to enclose the elements in square brackets ([ and ]):
Python

– [10, 20, 30, 40] list


of four integers
– ['crunchy frog', 'ram bladder', 'lark vomit'] list of three strings

• Elements of a list can be of different data type.


– ['spam', 2.0, 5, [10, 20]] list contains string, float, integer, &
another list
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Lists
• A list within another list is nested.
• A list that contains no elements is called an empty list;
• A list can be created with empty brackets, [ ].
• list values can be assigned to variables
Programming

cheeses = ['Cheddar', 'Edam', 'Gouda']


numbers = [17, 123]
Python

empty = []
print cheeses, numbers, empty

Output: ['Cheddar', 'Edam', 'Gouda'] [17, 123] []

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Lists are mutable
• The syntax for accessing the elements of a list is the same as for
accessing the characters of a string - the bracket operator.
– The expression inside the brackets specifies the index.
– Remember that the indices start at 0:
Programming

print cheeses[0]
output : Cheddar

• Unlike strings, lists are mutable.


Python

• When the bracket operator appears on the left side of an assignment, it


identifies the element of the list that will be assigned.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Lists are mutable
numbers = [17, 123]
numbers[1] = 5
print numbers
Output: [17, 5]
Programming

• The element at index 1 numbers, which used to be 123, is now 5.


Python

• A list has a relationship between indices and elements.


– This relationship is called a mapping;
– Each index “maps to” one of the elements.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Lists are mutable
• The in operator also works on lists.
cheeses = ['Cheddar', 'Edam', 'Gouda']
'Edam' in cheeses
Output: True
Programming

'Brie' in cheeses
Python

Output: False

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Traversing a list
• Most common way to traverse a list is using for loop
• Syntax is the same as for strings:
for cheese in cheeses:
print cheese
Programming

• For loop works well if only need to read list elements.


• To write / update the elements, you need the indices, usually done using
Python

range and len functions


for i in range(len(numbers)): • traverses the list and updates each element.
numbers[i] = numbers[i] * 2 • len returns the number of elements in the list.
• range returns a list of indices from 0 to n-1,
where n is the length of the list.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Traversing a list
• A for loop over an empty list never executes the body:
for x in []:
print 'This never happens.'
• Although a list can contain another list, the nested list still counts as a
Programming

single element.
• The length of this list is four:
Python

['spam', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Operations on list
1. Indexing
2. Slicing
3. Concatenation
4. Repetitions
Programming

5. Updating
6. Membership
Python

7. Comparison

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Programming
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Programming
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List operations
• The + operator concatenates lists:
a = [1, 2, 3]
b = [4, 5, 6]
c=a+b
Programming

print c
Output: [1, 2, 3, 4, 5, 6]
Python

• Similarly, the * operator repeats a list a given number of times:


[0] * 4 Output: [0, 0, 0, 0]
[0] repeated 4 times
[1, 2, 3] * 3 Output: [1, 2, 3, 1, 2, 3, 1, 2, 3] [1,2,3] repeated 3
times

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List slices
• List slicing is an operation that extracts a subset of elements from an list
and packages them as another list.
• Syntax:
Listname[start:stop]
Programming

Listname[start:stop:steps]
• default start value is 0
Python

• default stop value is n-1


• [:] this will print the entire list
• [2:2] this will create a empty slice

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List slices
• The slice operator also works on lists:
t = ['a', 'b', 'c', 'd', 'e', 'f']
t[1:3] Output : ['b', 'c']
t[:4] Output : ['a', 'b', 'c', 'd']
Programming

t[3:] Output : ['d', 'e', 'f']


t[:] Output : ['a', 'b', 'c', 'd', 'e', 'f']
Python

• If you omit the first index, the slice starts at the beginning.
• If you omit the second, the slice goes to the end.
• If you omit both, the slice is a copy of the whole list.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List slices
• Note: Since lists are mutable
– It is often useful to make a copy before performing operations that fold,
spindle or mutilate lists.
• A slice operator on the left side of an assignment can update multiple
Programming

elements:
t = ['a', 'b', 'c', 'd', 'e', 'f']
Python

t[1:3] = ['x', 'y']


print t Output: ['a', 'x',
'y', 'd', 'e', 'f']

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List methods
• Methods used in lists are used to manipulate the data quickly.
• These methods work only on lists.
• They do not work on the other sequence types that are not mutable, that is,
the values they contain cannot be changed, added, or deleted.
Programming

• syntax: list name.method name( element/index/list)


• Python provides methods that operate on lists. For example, append adds
Python

a new element to the end of a list:


t = ['a', 'b', 'c']
t.append('d')
print t Output: ['a', 'b', 'c', 'd']

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List methods
• extend takes a list as an argument and appends all of the elements:
t1 = ['a', 'b', 'c']
t2 = ['d', 'e']
t1.extend(t2)
Programming

print t1 Output: ['a', 'b', 'c', 'd', 'e'] This example


leaves t2 unmodified.

• Sort arranges the elements of the list from low to high:


Python

t = ['d', 'c', 'e', 'b', 'a']


t.sort()
print t Output: ['a', 'b', 'c', 'd', 'e']
• List methods are all void; they modify the list and return None.
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Programming
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Programming
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List Loop
• lists are considered a type of iterable
• List Loops
• For loop
• While loop
Programming

• Infinite loop
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List Loop
• Iterating over a sequence is called traversal.
• An iterable is a data type that can return its elements separately
i.e., one at a time.
• The for loop in Python is used to iterate over a sequence (list, tuple, string)
Programming

or other iterable objects.


for <item> in <iterable>:
Python

<body>
Eg:
names = ["Uma","Utta","Ursula","Eunice","Unix"]
for name in names:
print("Hi "+ name +"!")

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Programming List Loop
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List Loop - while
• The while loop in Python is used to iterate over a block of code as long as
the test expression (condition) is true.
• When the condition is tested and the result is false, the loop body will be
skipped and the first statement after the while loop will be executed.
• Syntax:
Programming

while (condition):
Python

body of while

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List Loop - while
• Sum of elements in list
a=[1,2,3,4,5]
i=0
sum=0
Programming

while i<len(a):
sum=sum+a[i]
Python

i=i+1
print(sum)
• Output: 15

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List Loop - infinite
• A loop becomes infinite loop if the condition given never becomes false. It
keeps on running. Such loops are called infinite loop.
• Example
a=1
Programming

while (a==1):
n=int(input("enter the number"))
print("you entered:" , n
Python

• Output:
Enter the number 10
you entered:10
Enter the number 12
you entered:12
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Map, filter and reduce
• To add up all the numbers in a list, you can use a loop like this:
def add_all(t):
total = 0
for x in t:
Programming

total += x
return total
Python

Here, total is initialized to 0. Each time through the loop, x gets one element from the list.
The += operator provides a short way to update a variable.

• In above example, loop executes, total accumulates the sum of the


elements; a variable used this way is sometimes called an accumulator.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Map, filter and reduce
• Adding up the elements of a list is such a common operation that Python
provides it as a built-in function, sum:
t = [1, 2, 3]
sum(t) 6
Programming

• An operation like this that combines a sequence of elements into a single


value is sometimes called reduce.
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Deleting elements
• There are several ways to delete elements from a list.
– If element index is known, pop can be used.
– pop modifies the list and returns the element that was removed.
– If index is not provided, it deletes and returns the last element.
Programming

t = ['a', 'b', 'c']


x = t.pop(1)
Python

print t Output: ['a', 'c']


print x Output: b

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Deleting elements
• If don’t need the removed value, can use the del operator:
t = ['a', 'b', 'c']
del t[1]
print t Output: ['a', 'c']
Programming

• If know the element to be removed (but not the index), can use remove:
t = ['a', 'b', 'c']
Python

t.remove('b')
print t Output: ['a', 'c']
– The return value from remove is None.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Deleting elements
• To remove more than one element, can use del with a slice index:
t = ['a', 'b', 'c', 'd', 'e', 'f']
del t[1:5]
print t Output: ['a', 'f']
Programming

• As usual, the slice selects all the elements up to, but not including, the
second index.
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Lists and strings
• A string is a sequence of characters and a list is a sequence of values
• A list of characters is not the same as a string.
• To convert from a string to a list of characters, can use list
s = 'spam'
Programming

t = list(s)
print t Output: ['s', 'p', 'a', 'm']
Python

• The list function breaks a string into individual letters.


• To break a string into words, use the split method
s = 'pining for the fjords'
t = s.split()
print t Output: ['pining', 'for', 'the', 'fjords']

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Lists and strings

• An optional argument called a delimiter specifies which characters to use


as word boundaries.
• The following example uses a hyphen as a delimiter:
Programming

s = 'spam-spam-spam'
delimiter = '-'
s.split(delimiter) ['spam', 'spam', 'spam']
Python

• join is the inverse of split.


• It takes a list of strings and concatenates the elements.
• join is a string method, so you have to invoke it on the delimiter and pass
the list as a parameter.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Lists and strings
t = ['pining', 'for', 'the', 'fjords']
delimiter = ' '
delimiter.join(t) 'pining for the fjords'
Programming

• In above case the delimiter is a space character, so join puts a space


between words.
Python

• To concatenate strings without spaces, use empty string, '', as a delimiter.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Objects and values
• If we execute these assignment statements:
a = 'banana'
b = 'banana'

• Here, a and b both refer to a string, Whether both refer to the same string?
Programming

• Let’s use is operator to check


a = 'banana'
Python

b = 'banana'
a is b Output: True

• Conclusion: Python only created one string object, both a & b refer to it.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Objects and values

• But when two lists are created, you get two objects:
a = [1, 2, 3]
b = [1, 2, 3]
Programming

a is b Output: False

• Here, we would say that both lists are


– Are equivalent, because they have the same elements
Python

– Not identical, because they are not the same object


• If two objects are identical, they are also equivalent, but if they are
equivalent, they are not necessarily identical.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Objects and values
• Until now, we have been using “object” and “value” interchangeably, but it
is more precise to say that an object has a value.
– If [1,2,3] is executed, will get a list object whose value is a sequence of integers.
– If another list has the same elements, then it has the same value, but it’s not the
Programming

same object.
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Mutability
• Lists are mutable. (can be changed)
• Mutability is the ability for certain types of data to be changed without
entirely recreating it.
• An item can be changed in a list by accessing it directly as part of the
Programming

assignment statement.
• Using the indexing operator (square brackets[ ]) on the left side of an
assignment, one of the list items can be updated.
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Programming Mutability
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Aliasing / Copying
• Creating a copy of a list is called aliasing.
• Both refers to same memory location. changes in one will affect another
list.
• If a refers to an object and b = a is assigned, then both variables refer to
Programming

the same object:


a = [1, 2, 3]
Python

b=a
b is a True

• The association of a variable with an object is called a reference.


• In above example, there are two references to the same object.
• An object with more than one reference has more than one name, so we
say that the object is aliased.
VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Aliasing
• If the aliased object is mutable, changes made with one alias affects other:
b[0] = 17
print a Output: [17, 2, 3]

• This behavior can be useful, it is error-prone.


Programming

• It is safer to avoid aliasing when working with mutable objects.


• For immutable objects like strings, aliasing is not as much of a problem as it
Python

won’t makes a difference whether a and b refer to the same string or not.
a = 'banana'
b = 'banana'

• To prevent aliasing, a new object can be created and the contents of the
original can be copied which is called cloning

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Cloning Lists
• To avoid the disadvantages of copying we are using cloning.
• Creating a copy of a same list of elements with two different memory
locations is called cloning.
• Changes in one list will not affect locations of aother list.
Programming

• Cloning is a process of making a copy of the list without modifying the


original list.
Python

• Cloning Methods original_list = [10, 22, 44, 23, 4]


new_list = list(original_list)
1. Slicing print(original_list)
Output: [10, 22, 44, 23, 4]
2. list()method print(new_list)
3. copy() method Output: [10, 22, 44, 23, 4]

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Cloning Lists
• Using Slicing • Using copy() method
a=[1,2,3,4,5] a=[1,2,3,4,5]
b=a[:] b=a.copy()
print(b) print(b)
Programming

• Output : [1,2,3,4,5] Output: [1, 2, 3, 4, 5]

• Using List( ) method


Python

a=[1,2,3,4,5]
• a is b
b=list • False
print(b)
Output : [1,2,3,4,5]

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List as parameters
• In python, arguments are passed by reference.
• If any changes are done in the parameter which refers within the function,
then the changes also reflects back in the calling function.
• When a list to a function is passed, the function gets a reference to the list.
Programming

• Passing a list as an argument actually passes a reference to the list, not a


copy of the list.
Python

• Since lists are mutable, changes made to the elements referenced by the
parameter change the same list that the argument is referencing.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List as parameters
• If the function modifies a list parameter, the caller sees the change.
For example, delete_head removes the first element from a list:
def delete_head(t):
del t[0]
Programming

Here’s how it is used:


letters = ['a', 'b', 'c']
Python

delete_head(letters)
print letters ['b', 'c']

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List as parameters
• The parameter t and the variable letters are aliases for the same object.
• It is important to distinguish between operations that modify lists and
operations that create new lists. For example, the append method modifies
a list, but the + operator creates a new list:
Programming

t1 = [1, 2]
t2 = t1.append(3)
Python

print t1 [1, 2, 3]
print t2 None
t3 = t1 + [4]
print t3 [1, 2, 3, 4]

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List as parameters
• This difference is important when you write functions that are supposed to
modify lists.
• For example, this function does not delete the head of a list:
def bad_delete_head(t):
Programming

t = t[1:] # WRONG!

• The slice operator creates a new list and the assignment makes t refer to
Python

it, but none of that has any effect on the list that was passed as an
argument.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List as parameters
• An alternative is to write a function that creates and returns a new list. For
example, tail returns all but the first element of a list:
def tail(t):
Programming

return t[1:]
• This function leaves the original list unmodified. Here’s how it is used:
Python

letters = ['a', 'b', 'c']


rest = tail(letters)
print rest ['b', 'c']

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
List as parameters : More Examples
Example 1: Example 2: Example 3
def remove(a) def inside(a): def insert(a):
for i in range(0,len(a),1): a.insert(0,30)
a.remove(1) a[i]=a[i]
Programming

+10
a=[1,2,3,4,5]
a=[1,2,3,4,5] print("inside",a)
insert(a)
remove(a)
Python

a=[1,2,3,4,5] print(a)
print(a)
inside(a) • Output: [30, 1, 2, 3,
print("outside",a) 4, 5]
Output:
[2,3,4,5] • Output:
inside [11, 12, 13,
14, 15]
08/07/2025 VVM’s Shree Damodar College of Commerce
outside [11, 12,and
13,Economics, Margao – Goa
14,www.damodarcollege.edu.in
15]
Vidya Vikas Mandal’s
SHREE DAMODAR COLLEGE OF COMMERCE & ECONOMICS
Affiliated to Goa University
Accredited by NAAC with Grade ‘A’

Lists as arrays

08/07/2025 54
Array
• Array is a collection of similar elements. Elements in the array can be
accessed by index. Index starts with 0. Array can be handled in python by
module named array.
• To create array have to import array module in the program.
Programming

• Syntax : import array


• Syntax to create array:
Python

Array_name = module_name.function_name(‘datatype’,[elements])

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Array
• example: Program to find sum of array elements
a=array.array(‘i’,[1,2,3,4]) import array
# a- array name sum=0
Programming

# array- module name a=array.array('i',[1,2,3,4])


# i- integer datatype for i in a:
Python

sum=sum+i
print(sum)

Output 10

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Convert list into array
• fromlist() function is used to program to convert list into array
append list to array. Here the import array
list is act like a array.
sum=0
Programming

i=[6,7,8,9,5]
• Syntax: a=array.array('i',[])
arrayname.fromlist(list_name)
Python

a.fromlist(l)
for i in a:
sum=sum+i
print(sum)

08/07/2025 VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Methods in array
• a=[2,3,4,5]
Programming
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Vidya Vikas Mandal’s
SHREE DAMODAR COLLEGE OF COMMERCE & ECONOMICS
Affiliated to Goa University
Accredited by NAAC with Grade ‘A’

Tuples
tuple assignment, tuple as return value

29-03-2022 59
Tuples
• A tuple is same as list, except that the set of elements is enclosed in
parentheses instead of square brackets.
• A tuple is an immutable list.
• Once a tuple is created, can't add / remove elements to / from a tuple
Programming

• But tuple can be converted into list and list can be converted in to tuple.
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Tuples
• Benefits of Tuple
– Tuples are faster than lists.
– If the user wants to protect the data from accidental changes, tuple can be used.
– Tuples can be used as keys in dictionaries, while lists can't.
Programming

• Operations on Tuples
1. Indexing
Python

2. Slicing
3. Concatenation
4. Repetitions
5. Membership
6. Comparison

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Programming
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Tuples
• Tuple methods
– Tuple is immutable so changes cannot be done on the elements of a tuple once it is
assigned.
Programming
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Tuples
• Tuple Assignment
– Tuple assignment allows, variables on the left of an assignment operator and
values of tuple on the right of the assignment operator.
– Multiple assignment works by creating a tuple of expressions from the right
Programming

hand side, and a tuple of targets from the left, and then matching each
expression to a target.
– Because multiple assignments use tuples to work, it is often termed tuple
Python

assignment.
• Uses of Tuple assignment
– It is often useful to swap the values of two variables.

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Tuples
• Example: Swapping using temporary variable:
a=20
b=50
temp = a
a=b
Programming

b = temp
print("value after swapping is",a,b)
Python

• Swapping using tuple assignment:


a=20
b=50
(a,b)=(b,a)
print("value after swapping is",a,b)

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Tuples
• Multiple assignments Multiple values can be assigned to multiple variables
using tuple assignment.
(a,b,c)=(1,2,3)
print(a) 1
Programming

print(b) 2
print(c) 3
Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Tuples
• Tuple as return value
– A Tuple is a comma separated sequence of items.
– It is created with or without ( ).
– A function can return one value. if you want to return more than one value from
Programming

a function. we can use tuple as return value.


Python

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Tuples
Example1:
def div(a,b): Example2:
r=a%b def min_max(a):
q=a//b small=min(a)
return(r,q) big=max(a)
return(small,big)
Programming

a=eval(input("enter a
value:")) a=[1,2,3,4,6]
b=eval(input("enter b small,big=min_max(a)
value:"))
Python

r,q=div(a,b) print("smallest:",small)
print("reminder:",r) print("biggest:",big)
print("quotient:",q)
Output:
Output: smallest: 1
enter a value:4 biggest: 6
enter b value:3
reminder: 1
VVM’s
quotient: 1 Shree Damodar College of Commerce and Economics, Margao – Goa
www.damodarcollege.edu.in
Tuples
• Tuple as argument
– The parameter name that begins with * gathers argument into a tuple.
Example:
def printall(*args):
Programming

print(args)
printall(2,3,'a')
Python

Output: (2, 3, 'a')

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
For remaining topics listed below, Refer to Unit 3 Part- II

• Dictionaries: operations and methods


Programming

• Advanced list processing - list comprehension


• Illustrative programs: selection sort, insertion sort, merge sort,
Python

histogram

VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa


www.damodarcollege.edu.in
Programming

End of Unit III - Part I


Thank You
Python

08/07/2025 VVM’s Shree Damodar College of Commerce and Economics, Margao – Goa 71

www.damodarcollege.edu.in

You might also like