SlideShare a Scribd company logo
Python Programming – Unit 3
Part – 2
Datastructures
1
https://www.slideshare.net/slideshow/python_funct
ions_modules_-user-define-functions/275958501
Dr.VIDHYA B
ASSISTANT PROFESSOR & HEAD
Department of Computer Technology
Sri Ramakrishna College of Arts and Science
Coimbatore - 641 006
Tamil Nadu, India
Python Data Structures
Python Data Structures
- Is a group of data elements that are put together
under one name
- Defines a particular way of storing and organizing
data in a computer so that it can be used efficiently
1. LIST
- It is a sequence in which elements are written as a
list of comma separated values.
- Elements can be of different data types
- It takes the form
list_variable = [val1, val2, val3,…,valn]
List (Examples)
list1=[1,2,3,4,5] Output:
print(list1)  [1,2,3,4,5]
list2 =[‘A’, ‘B’, ‘C’, ‘d’, ‘e’] Output:
print(list2)  [‘A’, ‘B’, ‘C’, ‘d’,
‘e’]
list3=[“Apple”, “Banana”] Output:
print(list3)  [‘Apple’, ‘Banana’]
list4 = [1, ‘a’, “Dog”] Output:
print(list4)  [1, ‘a’, ‘Dog’]
Accessing Values in List
- Similar to strings, lists can also be sliced and
concatenated
- square brackets are used to slice along with the
index/indices to get values stored at that index.
-For example:
seq = list[start:stop:step]
seq = list[::2] #get every other element, starting with index 0
seq = list[1::2] #get every other element, starting with index 1
Accessing Values in List (Example)
l1 = [1,2,3,4,5,6,7,8,9,10]
print(“List is:”, l1)
print(“First element is:”, l1[0])
print(“Index 2 – 5th
element:”, l1[2:5])
print(“From 0th
index, skip one element:”, l1[ : : 2])
print(“From 1st
index, skip two elements:”, l1[1::3])
Output:
List is: [1,2,3,4,5,6,7,8,9,10]
First element is: 1
Index 2 – 5th
element: [3,4,5]
From 0th
index, skip one element: [1,3,5,7,9]
From 1st
index, skip two elements: [2,5,8]
Updating Values in List
- A list can be updated by giving the slice on the left-hand
side of the assignment operator
- New values can be appended in the list with the method
append( ). The input to this method will be appended at
the end of the list
- Existing values can be removed by using the del statement.
Value at the specified index will be deleted.
Updating Values in List (Example)
l1 = [1,2,3,4,5,6,7,8,9,10]
print(“List is:”, l1)
l1[5] = 100
print(“After update:”, l1)
l1.append(200)
print(“After append”, l1)
del l1[3]
print(“After delete:”, l1)
del l1[2:4]
print(“After delete:”, l1)
del l1[:]
print(“After delete:”, l1)
Output:
List is: [1,2,3,4,5,6,7,8,9,10]
After update: [1,2,3,4,5,100,7,8,9,10]
After append: [1,2,3,4,5,100,7,8,9,10, 200]
After delete: [1,2,3,5,100,7,8,9,10, 200]
After delete: [1,2,100,7,8,9,10, 200]
After delete: [ ]
Slice Operation on List
#insert a list in another list using slice operation
li=[1,9,11,13,15]
print(“Original List:”, li)
li[2]=[3,5,7]
print(“After inserting another list, the updated list is:”, li)
Output:
Original List: [1,9,11,13,15]
After inserting another list, the updated list is: [1,9,[3,5,7],13,15]
Nested List
-List within another list
Example:
l1 = [1, ‘a’, “abc”, [2,3,4,5], 8.9]
i = 0
while i<(len[l1]):
print(“l1[“ , i , “] =“, l[i])
i+ = 1
Output:
l1[0] = 1
l1[1] = a
l1[2] = abc
l1[3] = [2,3,4,5]
l1[4] = 8.9
Cloning List
Example:
def Cloning(li1):
li_copy = li1[:]
return li_copy
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
Output:
Original List: [4,8,2,10,15,18]
After Cloning : [4,8,2,10,15,18]
Cloning
If there is a need
to modify a list
and also to keep a
copy of the
original list, then
a separate copy
of the list must be
created
Basic List Operations
1 2
Operation len Concatenation
Description Returns length of the list Joins two list
Example len([1,2,3,4,5,6,7,8,9,10]) [1,2,3,4,5] + [6,7,8,9,10]
Output 10 [1,2,3,4,5,6,7,8,9,10]
3 4
Operation Repetition in
Description Repeats elements in the list Checks if the values is present
in the list
Example “Hello”, “World” *2 ‘a’ in [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
Output [‘Hello’, ‘World’, ‘Hello’,
‘World’]
True
Basic List Operations (Continued…)
5 6
Operation not in max
Description Checks if the value is not available
in the list
Returns maximum value in the
list
Example 3 not in [0,2,4,6,8] n=[2,3,4]
print(max(n))
Output True 4
7 8
Operation min sum
Description Returns minimum value in the list Adds the values in the list that
has number
Example n=[2,3,4]
print(min(n))
n=[2,3,4]
print(“Sum:”, sum(n))
Output 2 9
Basic List Operations (Continued…)
9 10
Operation all any
Description Returns true if all elements of the
list are true (or if the list is
empty)
Returns true if any elements of
the list are true . If empty False
Example n=[0,1,2,3]
print(all(n))
n=[0,1,2,3]
print(any(n))
Output False True
11 12
Operation list sorted
Description Converts an iterable (tuple,
string, set, dictionary) to a list
Returns a new sorted list. The
original list is not sorted
Example list1=list(“HELLO”)
print(list1)
list1=[1,5,3,2]
list2 = sorted(list1)
print(list2)
Output [‘H’, ‘E’, ‘L’, ‘L’, ‘O’] [1,2,3,5]
Basic List Operations (Continued…)
Few more examples on Indexing, Slicing and other
operations
list_a =[“Hello”, “World”, “Good”, “Morning”]
print(list_a[2]) #index starts at 0
print(list_a[-3]) #3rd
element from the end
print(list_a[1:]) # Prints all elements starting from index 1
Output:
Good
World
[ “World”, “Good”, “Morning”]
List Methods
1 2
Operation append( ) count()
Description Appends an element to the list Counts the number of times an
element appears in the list
Example n=[6,3,7,0,1,2,4,9]
append(10)
print(n)
n=[6,3,7,0,1,2,4,9]
print(n.count(4))
Output [6,3,7,0,1,2,4,9,10] 1
3 4
Operation index() insert( )
Description Returns the lowest index of obj in
the list
Inserts obj at the specified index
in the list
Example n = [6,3,7,0,1,2,4,9]
print(n.index(7))
n = [6,3,7,0,1,2,4,9]
n.insert(3,100)
print(n)
Output 2 n = [6,3,7,100,1,2,4,9]
List Methods (Continued…)
5 6
Operation pop( ) remove( )
Description Removes the element in the specified
index. If no index is specified, last
element will be removed
Removes the specified element
from the list.
Example n = [6,3,7,0,1,2,4,9]
print(n.pop( ))
print(n)
n = [6,3,7,0,1,2,4,9]
print(n.remove(0))
print(n)
Output 9 [6,3,7,0,1,2,4] [6,3,7,1,2,4,9]
7 8
Operation reverse( ) sort( )
Description Reverses the elements in the list Sorts the elements in the list
Example n = [6,3,7,0,1,2,4,9]
n.reverse( )
print(n)
n = [6,3,7,0,1,2,4,9]
n.sort( )
print(n)
Output [9,4,2,1,0,7,3,6,] [0, 1, 2, 3, 4, 6, 7, 9]
List Methods (Continued…)
9 Insert()
Remove()
Sort()
The above methods only
modify the list and do not
return any value
Operation extend
Description Adds the element in a list to the end
of the another list
Example n1 = [1,2,3,4,5]
n2 = [6,7,8,9,10]
n1.extend(n2)
print(n1)
Output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
February 23, 2025 20
Using Lists as Stacks
- Stack is a DS which stores
its elements in an ordered
manner.
- Eg: Pile of Plates
- Stack is a linear DS uses the
same principle (ie) elements
are added and removed only
from one end.
- Hence Stack follows LIFO
(Last in First Out)
February 23, 2025 21
Using Lists as Stacks
- Stacks in computer science is used in function
calls.
February 23, 2025 22
Using Lists as Stacks
February 23, 2025 23
Using Lists as Stacks
- Stack supports 3 operations:
PUSH: Adds an element at the end of the stack.
POP: Removes the element from the stack.
PEEP: Returns the value of the last element from the
stack(without deleting it).
In python list methods are used to perform the above
operation
- PUSH- append () method
- POP- pop() method
- PEEP- slicing operation is used
February 23, 2025 24
Using Lists as Stacks
February 23, 2025 25
Using Lists as Queues
- Queue is a DS which stores its elements in an ordered manner.
- Eg:
*People moving in escalator
*People waiting for bus
*Luggage kept at conveyor belt.
*Cars lined at toll bridge.
Stack is a linear DS uses the same principle (ie) elements are
added at one end and removed from other end.
- Hence Queue follows FIFO (First in First Out)
February 23, 2025 26
Using Lists as Queues
February 23, 2025 27
List Comprehensions
Python supports computed lists called List Comprehensions
Syntax:
List= [expression for variable in sequence]
- Beneficial to make new list where each element is
obtained by applying some operations to each
member of another sequence or iterable.
- Also used to create a subsequence of those
elements that satisfy certain conditions.
An iterable is an object that can be used repeatedly
in a subsequent loop statements. Eg: For loop
February 23, 2025 28
List Comprehensions
February 23, 2025 29
Looping in Lists
Python’s for and
in construct useful
when working
with lists, easy to
access each
element in a list.
February 23, 2025 30
Looping in Lists
Multiple ways to
access a List:
Iterator Function:
Loop over the
elements when
used with next()
method.
Uses built-in
iter()function
February 23, 2025 31
February 23, 2025 32
What is Functional Programming ?
Functional Programming
decomposes a problem into set of
functions
Map( ), filter( ), reduce( )
February 23, 2025 33
Filter Function
- Filter function constructs a list from those
elements of the list for which a function returns
True.
- filter(function, sequence)
- If sequence is a string, Unicode or a tuple, then the
result will be of same type otherwise it is always a
list.
February 23, 2025 34
Filter Function (Example)
def check(x):
if (x % 2 == 0 or x % 4 = =0):
return 1
events = list(filter(check, range(2, 22)))
print(events)
Output:
[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Explanation:
The filter function returns True or False. Functions that returns a
Boolean value called as Predicates. Only those values divisible
by 2 or 4 is included in newly created list.
February 23, 2025 35
Map Function
- Map() function applies a particular function to
every element of a list. Its syntax is same as the
filter function.
- map(function, sequence)
- After applying the specified function on the
sequence the map() function returns the modified
list.
- It calls function(item)for each item in a sequence
a returns a list of return values.
February 23, 2025 36
Map Function (Example)
Explanation: Map() calls add_2() which adds 2 to every
element in the list
February 23, 2025 37
Map Function (Example)
def add(x,y):
return x+y
List1 = [1,2,3,4,5]
List2 = [6,7,8,9,10]
List3 = list(map(add, list1, list2))
Print(“sum of” , list1, “ and”, list2, “ =“, list3)
Output:
sum of [1,2,3,4,5] and [6,7,8,9,10] = [7,9,11,13,15]
Explanation:
Here more than one sequence is passed in map().
- Function must have as many as arguments as there are sequences.
- Each argument is called with corresponding item from each
sequence
February 23, 2025 38
Reduce Function
- Reduce() function returns a single value
generated by calling the function.
- reduce(function, sequence)
February 23, 2025 39
Reduce Function (Example)

More Related Content

Similar to Python _dataStructures_ List, Tuples, its functions (20)

The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184
Mahmoud Samir Fayed
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
Megha V
 
Python list tuple dictionary .pptx
Python list tuple dictionary       .pptxPython list tuple dictionary       .pptx
Python list tuple dictionary .pptx
miteshchaudhari4466
 
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189
Mahmoud Samir Fayed
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
AshaWankar1
 
Python list
Python listPython list
Python list
Prof. Dr. K. Adisesha
 
Unit - 4.ppt
Unit - 4.pptUnit - 4.ppt
Unit - 4.ppt
Kongunadu College of Engineering and Technology
 
Chapter 3-Data structure in python programming.pptx
Chapter 3-Data structure in python programming.pptxChapter 3-Data structure in python programming.pptx
Chapter 3-Data structure in python programming.pptx
atharvdeshpande20
 
lists8.pptx of chapter 12 IP Basic Python
lists8.pptx of chapter 12 IP Basic Pythonlists8.pptx of chapter 12 IP Basic Python
lists8.pptx of chapter 12 IP Basic Python
kvpv2023
 
Python lists &amp; sets
Python lists &amp; setsPython lists &amp; sets
Python lists &amp; sets
Aswini Dharmaraj
 
Module-2.pptx
Module-2.pptxModule-2.pptx
Module-2.pptx
GaganRaj28
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 
UNIT 4 PY.ppt - DATA STRUCTURE IN PYTHON
UNIT 4 PY.ppt - DATA STRUCTURE IN PYTHONUNIT 4 PY.ppt - DATA STRUCTURE IN PYTHON
UNIT 4 PY.ppt - DATA STRUCTURE IN PYTHON
drkangurajuphd
 
Python data handling notes
Python data handling notesPython data handling notes
Python data handling notes
Prof. Dr. K. Adisesha
 
Module III.pdf
Module III.pdfModule III.pdf
Module III.pdf
R.K.College of engg & Tech
 
Python lists
Python listsPython lists
Python lists
nuripatidar
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear lists
ToniyaP1
 
Python Data Types.pdf
Python Data Types.pdfPython Data Types.pdf
Python Data Types.pdf
NehaSpillai1
 
Python Data Types (1).pdf
Python Data Types (1).pdfPython Data Types (1).pdf
Python Data Types (1).pdf
NehaSpillai1
 
The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184The Ring programming language version 1.5.3 book - Part 22 of 184
The Ring programming language version 1.5.3 book - Part 22 of 184
Mahmoud Samir Fayed
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
Megha V
 
Python list tuple dictionary .pptx
Python list tuple dictionary       .pptxPython list tuple dictionary       .pptx
Python list tuple dictionary .pptx
miteshchaudhari4466
 
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189The Ring programming language version 1.6 book - Part 24 of 189
The Ring programming language version 1.6 book - Part 24 of 189
Mahmoud Samir Fayed
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
AshaWankar1
 
Chapter 3-Data structure in python programming.pptx
Chapter 3-Data structure in python programming.pptxChapter 3-Data structure in python programming.pptx
Chapter 3-Data structure in python programming.pptx
atharvdeshpande20
 
lists8.pptx of chapter 12 IP Basic Python
lists8.pptx of chapter 12 IP Basic Pythonlists8.pptx of chapter 12 IP Basic Python
lists8.pptx of chapter 12 IP Basic Python
kvpv2023
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
MCCMOTOR
 
UNIT 4 PY.ppt - DATA STRUCTURE IN PYTHON
UNIT 4 PY.ppt - DATA STRUCTURE IN PYTHONUNIT 4 PY.ppt - DATA STRUCTURE IN PYTHON
UNIT 4 PY.ppt - DATA STRUCTURE IN PYTHON
drkangurajuphd
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear lists
ToniyaP1
 
Python Data Types.pdf
Python Data Types.pdfPython Data Types.pdf
Python Data Types.pdf
NehaSpillai1
 
Python Data Types (1).pdf
Python Data Types (1).pdfPython Data Types (1).pdf
Python Data Types (1).pdf
NehaSpillai1
 

More from VidhyaB10 (12)

Preprocessing - Data Integration Tuple Duplication
Preprocessing - Data Integration Tuple DuplicationPreprocessing - Data Integration Tuple Duplication
Preprocessing - Data Integration Tuple Duplication
VidhyaB10
 
Major Tasks in Data Preprocessing - Data cleaning
Major Tasks in Data Preprocessing - Data cleaningMajor Tasks in Data Preprocessing - Data cleaning
Major Tasks in Data Preprocessing - Data cleaning
VidhyaB10
 
Applications ,Issues & Technology in Data mining -
Applications ,Issues & Technology in Data mining -Applications ,Issues & Technology in Data mining -
Applications ,Issues & Technology in Data mining -
VidhyaB10
 
Python Visualization API Primersubplots
Python Visualization  API PrimersubplotsPython Visualization  API Primersubplots
Python Visualization API Primersubplots
VidhyaB10
 
Python_Functions_Modules_ User define Functions-
Python_Functions_Modules_ User define Functions-Python_Functions_Modules_ User define Functions-
Python_Functions_Modules_ User define Functions-
VidhyaB10
 
Datamining - Introduction - Knowledge Discovery in Databases
Datamining  - Introduction - Knowledge Discovery in DatabasesDatamining  - Introduction - Knowledge Discovery in Databases
Datamining - Introduction - Knowledge Discovery in Databases
VidhyaB10
 
INSTRUCTION PROCESSOR DESIGN Computer system architecture
INSTRUCTION PROCESSOR DESIGN Computer system architectureINSTRUCTION PROCESSOR DESIGN Computer system architecture
INSTRUCTION PROCESSOR DESIGN Computer system architecture
VidhyaB10
 
Disk Scheduling in OS computer deals with multiple processes over a period of...
Disk Scheduling in OS computer deals with multiple processes over a period of...Disk Scheduling in OS computer deals with multiple processes over a period of...
Disk Scheduling in OS computer deals with multiple processes over a period of...
VidhyaB10
 
Unit 2 digital fundamentals boolean func.pptx
Unit 2 digital fundamentals boolean func.pptxUnit 2 digital fundamentals boolean func.pptx
Unit 2 digital fundamentals boolean func.pptx
VidhyaB10
 
Digital Fundamental - Binary Codes-Logic Gates
Digital Fundamental - Binary Codes-Logic GatesDigital Fundamental - Binary Codes-Logic Gates
Digital Fundamental - Binary Codes-Logic Gates
VidhyaB10
 
unit 5-files.pptx
unit 5-files.pptxunit 5-files.pptx
unit 5-files.pptx
VidhyaB10
 
Python_Unit1_Introduction.pptx
Python_Unit1_Introduction.pptxPython_Unit1_Introduction.pptx
Python_Unit1_Introduction.pptx
VidhyaB10
 
Preprocessing - Data Integration Tuple Duplication
Preprocessing - Data Integration Tuple DuplicationPreprocessing - Data Integration Tuple Duplication
Preprocessing - Data Integration Tuple Duplication
VidhyaB10
 
Major Tasks in Data Preprocessing - Data cleaning
Major Tasks in Data Preprocessing - Data cleaningMajor Tasks in Data Preprocessing - Data cleaning
Major Tasks in Data Preprocessing - Data cleaning
VidhyaB10
 
Applications ,Issues & Technology in Data mining -
Applications ,Issues & Technology in Data mining -Applications ,Issues & Technology in Data mining -
Applications ,Issues & Technology in Data mining -
VidhyaB10
 
Python Visualization API Primersubplots
Python Visualization  API PrimersubplotsPython Visualization  API Primersubplots
Python Visualization API Primersubplots
VidhyaB10
 
Python_Functions_Modules_ User define Functions-
Python_Functions_Modules_ User define Functions-Python_Functions_Modules_ User define Functions-
Python_Functions_Modules_ User define Functions-
VidhyaB10
 
Datamining - Introduction - Knowledge Discovery in Databases
Datamining  - Introduction - Knowledge Discovery in DatabasesDatamining  - Introduction - Knowledge Discovery in Databases
Datamining - Introduction - Knowledge Discovery in Databases
VidhyaB10
 
INSTRUCTION PROCESSOR DESIGN Computer system architecture
INSTRUCTION PROCESSOR DESIGN Computer system architectureINSTRUCTION PROCESSOR DESIGN Computer system architecture
INSTRUCTION PROCESSOR DESIGN Computer system architecture
VidhyaB10
 
Disk Scheduling in OS computer deals with multiple processes over a period of...
Disk Scheduling in OS computer deals with multiple processes over a period of...Disk Scheduling in OS computer deals with multiple processes over a period of...
Disk Scheduling in OS computer deals with multiple processes over a period of...
VidhyaB10
 
Unit 2 digital fundamentals boolean func.pptx
Unit 2 digital fundamentals boolean func.pptxUnit 2 digital fundamentals boolean func.pptx
Unit 2 digital fundamentals boolean func.pptx
VidhyaB10
 
Digital Fundamental - Binary Codes-Logic Gates
Digital Fundamental - Binary Codes-Logic GatesDigital Fundamental - Binary Codes-Logic Gates
Digital Fundamental - Binary Codes-Logic Gates
VidhyaB10
 
unit 5-files.pptx
unit 5-files.pptxunit 5-files.pptx
unit 5-files.pptx
VidhyaB10
 
Python_Unit1_Introduction.pptx
Python_Unit1_Introduction.pptxPython_Unit1_Introduction.pptx
Python_Unit1_Introduction.pptx
VidhyaB10
 
Ad

Recently uploaded (20)

How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
National Information Standards Organization (NISO)
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti MpdBasic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Ad

Python _dataStructures_ List, Tuples, its functions

  • 1. Python Programming – Unit 3 Part – 2 Datastructures 1 https://www.slideshare.net/slideshow/python_funct ions_modules_-user-define-functions/275958501 Dr.VIDHYA B ASSISTANT PROFESSOR & HEAD Department of Computer Technology Sri Ramakrishna College of Arts and Science Coimbatore - 641 006 Tamil Nadu, India
  • 3. Python Data Structures - Is a group of data elements that are put together under one name - Defines a particular way of storing and organizing data in a computer so that it can be used efficiently
  • 4. 1. LIST - It is a sequence in which elements are written as a list of comma separated values. - Elements can be of different data types - It takes the form list_variable = [val1, val2, val3,…,valn]
  • 5. List (Examples) list1=[1,2,3,4,5] Output: print(list1)  [1,2,3,4,5] list2 =[‘A’, ‘B’, ‘C’, ‘d’, ‘e’] Output: print(list2)  [‘A’, ‘B’, ‘C’, ‘d’, ‘e’] list3=[“Apple”, “Banana”] Output: print(list3)  [‘Apple’, ‘Banana’] list4 = [1, ‘a’, “Dog”] Output: print(list4)  [1, ‘a’, ‘Dog’]
  • 6. Accessing Values in List - Similar to strings, lists can also be sliced and concatenated - square brackets are used to slice along with the index/indices to get values stored at that index. -For example: seq = list[start:stop:step] seq = list[::2] #get every other element, starting with index 0 seq = list[1::2] #get every other element, starting with index 1
  • 7. Accessing Values in List (Example) l1 = [1,2,3,4,5,6,7,8,9,10] print(“List is:”, l1) print(“First element is:”, l1[0]) print(“Index 2 – 5th element:”, l1[2:5]) print(“From 0th index, skip one element:”, l1[ : : 2]) print(“From 1st index, skip two elements:”, l1[1::3]) Output: List is: [1,2,3,4,5,6,7,8,9,10] First element is: 1 Index 2 – 5th element: [3,4,5] From 0th index, skip one element: [1,3,5,7,9] From 1st index, skip two elements: [2,5,8]
  • 8. Updating Values in List - A list can be updated by giving the slice on the left-hand side of the assignment operator - New values can be appended in the list with the method append( ). The input to this method will be appended at the end of the list - Existing values can be removed by using the del statement. Value at the specified index will be deleted.
  • 9. Updating Values in List (Example) l1 = [1,2,3,4,5,6,7,8,9,10] print(“List is:”, l1) l1[5] = 100 print(“After update:”, l1) l1.append(200) print(“After append”, l1) del l1[3] print(“After delete:”, l1) del l1[2:4] print(“After delete:”, l1) del l1[:] print(“After delete:”, l1) Output: List is: [1,2,3,4,5,6,7,8,9,10] After update: [1,2,3,4,5,100,7,8,9,10] After append: [1,2,3,4,5,100,7,8,9,10, 200] After delete: [1,2,3,5,100,7,8,9,10, 200] After delete: [1,2,100,7,8,9,10, 200] After delete: [ ]
  • 10. Slice Operation on List #insert a list in another list using slice operation li=[1,9,11,13,15] print(“Original List:”, li) li[2]=[3,5,7] print(“After inserting another list, the updated list is:”, li) Output: Original List: [1,9,11,13,15] After inserting another list, the updated list is: [1,9,[3,5,7],13,15]
  • 11. Nested List -List within another list Example: l1 = [1, ‘a’, “abc”, [2,3,4,5], 8.9] i = 0 while i<(len[l1]): print(“l1[“ , i , “] =“, l[i]) i+ = 1 Output: l1[0] = 1 l1[1] = a l1[2] = abc l1[3] = [2,3,4,5] l1[4] = 8.9
  • 12. Cloning List Example: def Cloning(li1): li_copy = li1[:] return li_copy li1 = [4, 8, 2, 10, 15, 18] li2 = Cloning(li1) print("Original List:", li1) print("After Cloning:", li2) Output: Original List: [4,8,2,10,15,18] After Cloning : [4,8,2,10,15,18] Cloning If there is a need to modify a list and also to keep a copy of the original list, then a separate copy of the list must be created
  • 13. Basic List Operations 1 2 Operation len Concatenation Description Returns length of the list Joins two list Example len([1,2,3,4,5,6,7,8,9,10]) [1,2,3,4,5] + [6,7,8,9,10] Output 10 [1,2,3,4,5,6,7,8,9,10] 3 4 Operation Repetition in Description Repeats elements in the list Checks if the values is present in the list Example “Hello”, “World” *2 ‘a’ in [‘a’, ‘e’, ‘i’, ‘o’, ‘u’] Output [‘Hello’, ‘World’, ‘Hello’, ‘World’] True
  • 14. Basic List Operations (Continued…) 5 6 Operation not in max Description Checks if the value is not available in the list Returns maximum value in the list Example 3 not in [0,2,4,6,8] n=[2,3,4] print(max(n)) Output True 4 7 8 Operation min sum Description Returns minimum value in the list Adds the values in the list that has number Example n=[2,3,4] print(min(n)) n=[2,3,4] print(“Sum:”, sum(n)) Output 2 9
  • 15. Basic List Operations (Continued…) 9 10 Operation all any Description Returns true if all elements of the list are true (or if the list is empty) Returns true if any elements of the list are true . If empty False Example n=[0,1,2,3] print(all(n)) n=[0,1,2,3] print(any(n)) Output False True 11 12 Operation list sorted Description Converts an iterable (tuple, string, set, dictionary) to a list Returns a new sorted list. The original list is not sorted Example list1=list(“HELLO”) print(list1) list1=[1,5,3,2] list2 = sorted(list1) print(list2) Output [‘H’, ‘E’, ‘L’, ‘L’, ‘O’] [1,2,3,5]
  • 16. Basic List Operations (Continued…) Few more examples on Indexing, Slicing and other operations list_a =[“Hello”, “World”, “Good”, “Morning”] print(list_a[2]) #index starts at 0 print(list_a[-3]) #3rd element from the end print(list_a[1:]) # Prints all elements starting from index 1 Output: Good World [ “World”, “Good”, “Morning”]
  • 17. List Methods 1 2 Operation append( ) count() Description Appends an element to the list Counts the number of times an element appears in the list Example n=[6,3,7,0,1,2,4,9] append(10) print(n) n=[6,3,7,0,1,2,4,9] print(n.count(4)) Output [6,3,7,0,1,2,4,9,10] 1 3 4 Operation index() insert( ) Description Returns the lowest index of obj in the list Inserts obj at the specified index in the list Example n = [6,3,7,0,1,2,4,9] print(n.index(7)) n = [6,3,7,0,1,2,4,9] n.insert(3,100) print(n) Output 2 n = [6,3,7,100,1,2,4,9]
  • 18. List Methods (Continued…) 5 6 Operation pop( ) remove( ) Description Removes the element in the specified index. If no index is specified, last element will be removed Removes the specified element from the list. Example n = [6,3,7,0,1,2,4,9] print(n.pop( )) print(n) n = [6,3,7,0,1,2,4,9] print(n.remove(0)) print(n) Output 9 [6,3,7,0,1,2,4] [6,3,7,1,2,4,9] 7 8 Operation reverse( ) sort( ) Description Reverses the elements in the list Sorts the elements in the list Example n = [6,3,7,0,1,2,4,9] n.reverse( ) print(n) n = [6,3,7,0,1,2,4,9] n.sort( ) print(n) Output [9,4,2,1,0,7,3,6,] [0, 1, 2, 3, 4, 6, 7, 9]
  • 19. List Methods (Continued…) 9 Insert() Remove() Sort() The above methods only modify the list and do not return any value Operation extend Description Adds the element in a list to the end of the another list Example n1 = [1,2,3,4,5] n2 = [6,7,8,9,10] n1.extend(n2) print(n1) Output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • 20. February 23, 2025 20 Using Lists as Stacks - Stack is a DS which stores its elements in an ordered manner. - Eg: Pile of Plates - Stack is a linear DS uses the same principle (ie) elements are added and removed only from one end. - Hence Stack follows LIFO (Last in First Out)
  • 21. February 23, 2025 21 Using Lists as Stacks - Stacks in computer science is used in function calls.
  • 22. February 23, 2025 22 Using Lists as Stacks
  • 23. February 23, 2025 23 Using Lists as Stacks - Stack supports 3 operations: PUSH: Adds an element at the end of the stack. POP: Removes the element from the stack. PEEP: Returns the value of the last element from the stack(without deleting it). In python list methods are used to perform the above operation - PUSH- append () method - POP- pop() method - PEEP- slicing operation is used
  • 24. February 23, 2025 24 Using Lists as Stacks
  • 25. February 23, 2025 25 Using Lists as Queues - Queue is a DS which stores its elements in an ordered manner. - Eg: *People moving in escalator *People waiting for bus *Luggage kept at conveyor belt. *Cars lined at toll bridge. Stack is a linear DS uses the same principle (ie) elements are added at one end and removed from other end. - Hence Queue follows FIFO (First in First Out)
  • 26. February 23, 2025 26 Using Lists as Queues
  • 27. February 23, 2025 27 List Comprehensions Python supports computed lists called List Comprehensions Syntax: List= [expression for variable in sequence] - Beneficial to make new list where each element is obtained by applying some operations to each member of another sequence or iterable. - Also used to create a subsequence of those elements that satisfy certain conditions. An iterable is an object that can be used repeatedly in a subsequent loop statements. Eg: For loop
  • 28. February 23, 2025 28 List Comprehensions
  • 29. February 23, 2025 29 Looping in Lists Python’s for and in construct useful when working with lists, easy to access each element in a list.
  • 30. February 23, 2025 30 Looping in Lists Multiple ways to access a List: Iterator Function: Loop over the elements when used with next() method. Uses built-in iter()function
  • 32. February 23, 2025 32 What is Functional Programming ? Functional Programming decomposes a problem into set of functions Map( ), filter( ), reduce( )
  • 33. February 23, 2025 33 Filter Function - Filter function constructs a list from those elements of the list for which a function returns True. - filter(function, sequence) - If sequence is a string, Unicode or a tuple, then the result will be of same type otherwise it is always a list.
  • 34. February 23, 2025 34 Filter Function (Example) def check(x): if (x % 2 == 0 or x % 4 = =0): return 1 events = list(filter(check, range(2, 22))) print(events) Output: [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20] Explanation: The filter function returns True or False. Functions that returns a Boolean value called as Predicates. Only those values divisible by 2 or 4 is included in newly created list.
  • 35. February 23, 2025 35 Map Function - Map() function applies a particular function to every element of a list. Its syntax is same as the filter function. - map(function, sequence) - After applying the specified function on the sequence the map() function returns the modified list. - It calls function(item)for each item in a sequence a returns a list of return values.
  • 36. February 23, 2025 36 Map Function (Example) Explanation: Map() calls add_2() which adds 2 to every element in the list
  • 37. February 23, 2025 37 Map Function (Example) def add(x,y): return x+y List1 = [1,2,3,4,5] List2 = [6,7,8,9,10] List3 = list(map(add, list1, list2)) Print(“sum of” , list1, “ and”, list2, “ =“, list3) Output: sum of [1,2,3,4,5] and [6,7,8,9,10] = [7,9,11,13,15] Explanation: Here more than one sequence is passed in map(). - Function must have as many as arguments as there are sequences. - Each argument is called with corresponding item from each sequence
  • 38. February 23, 2025 38 Reduce Function - Reduce() function returns a single value generated by calling the function. - reduce(function, sequence)
  • 39. February 23, 2025 39 Reduce Function (Example)