SlideShare a Scribd company logo
Python Programming-Part 6
Megha V
Research Scholar
Kannur University
15-11-2021 meghav@kannuruniv.ac.in 1
Difference between Method and Function in Python
Function
• A function is a block of code to carry out a specific task, will contain
its own scope and is called by name.
• All functions may contain zero(no) arguments or more than one
arguments.
15-11-2021 meghav@kannuruniv.ac.in 2
Difference between Method and Function in Python
Method
• A method in python is somewhat similar to a function, except it is
associated with object/classes.
• Methods in python are very similar to functions except for two major
differences.
• The method is implicitly used for an object for which it is called.
• The method is accessible to data that is contained within the class.
15-11-2021 meghav@kannuruniv.ac.in 3
List
• Creating a list
• Basic List operations
• Indexing and slicing in Lists
• Built-in functions used on lists
• List methods
• The del statement
15-11-2021 meghav@kannuruniv.ac.in 4
List
• Creating a list
• Lists are used to store multiple items in a single variable.
thislist = ["apple", "banana", "cherry"]
print(thislist)
• We can update lists by using slice[] on the LHS of the assignment operator
Eg:
list=[‘bcd’,147,2.43,’Tom’]
print(“Item at index 2=”,list[2])
list[2]=500
print(“Item at index 2=”,list[2])
Output
2.43
500
15-11-2021 meghav@kannuruniv.ac.in 5
List
• To remove an item from a list
• del statement
• remove() method
list=[‘abcd’,147,2.43,’Tom’,74.9]
print(list)
del list[2]
print(“list after deletion:”, list)
Output
[‘abcd’,147,2.43,’Tom’,74.9]
list after deletion:[‘abcd’,147,’Tom’,74.9]
15-11-2021 meghav@kannuruniv.ac.in 6
• The del keyword can also used to delete the list completely
thislist = ["apple", "banana", "cherry"]
del thislist
• remove()function
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
15-11-2021 meghav@kannuruniv.ac.in 7
Built-in list functions
1. len(list) – Gives the total length of list
2. max(list)- Returns item from list with maximum value
3. min(list)- Returns item from list with minimum value
4. list(seq)- Returns a tuple into a list
5. map(aFunction,aSequence) – Apply an operation to each item and
collect result.
15-11-2021 meghav@kannuruniv.ac.in 8
Example
list1=[1200,147,2.43,1.12]
list2=[213,100,289]
print(list1)
print(list2)
print(len(list1))
print(“Maximum value in list1 is ”,max(list))
print(“Maximum value in list2 is ”,min(list))
Output
[1200,147,2.43,1.12]
[1200,147,2.43,1.12]
4
Maximum value in the list1 is 1200
Minimum value in the list2 is 100
15-11-2021 meghav@kannuruniv.ac.in 9
Example of list() and map() function
tuple = (‘abcd’,147,2.43,’Tom’)
print(“List:”,list(tuple))
str=input(“Enter a list(space separated):”)
lis=list(map(int,str.split()))
print(lis)
Output
List: [‘abcd’,147,2.43,’Tom’]
Enter a list (space separated) : 5 6 8 9
[5,6,8,9]
In this example a string is read from the keyboard and each item is
converted into int using map() function
15-11-2021 meghav@kannuruniv.ac.in 10
Built-in list methods
1. list.append(obj) –Append an object obj passed to the existing list
list = [‘abcd’,147,2.43,’Tom’]
print(“Old list before append:”, list)
list.append(100)
print(“New list after append:”,list)
Output
Old list before append: [‘abcd’,147,2.43,’Tom’]
New list after append: [‘abcd’,147,2.43,’Tom’,100]
15-11-2021 meghav@kannuruniv.ac.in 11
2. list.count(obj) –Returns how many times the object obj appears in a list
list = [‘abcd’,147,2.43,’Tom’]
print(“The number of times”,147,”appears in the
list=”,list.count(147))
Output
The number of times 147 appears in the list = 1
15-11-2021 meghav@kannuruniv.ac.in 12
3. list.remove(obj) – Removes an object
list1 = [‘abcd’,147,2.43,’Tom’]
list.remove(‘Tom’)
print(list1)
Output
[‘abcd’,147,2.43]
4. list.index(obj) – Returns index of the object obj if found
print(list1.index(2.43))
Output
2
15-11-2021 meghav@kannuruniv.ac.in 13
5. list.extend(seq)- Appends the contents in a sequence passed to a list
list1 = [‘abcd’,147,2.43,’Tom’]
list2 = [‘def’,100]
list1.extend(list2)
print(list1)
Output
[‘abcd’,147,2.43,’Tom’,‘def’,100]
6. list.reverse() – Reverses objects in a list
list1.reverse()
print(list1)
Output
[‘Tom’,2.43,147,’abcd’]
15-11-2021 meghav@kannuruniv.ac.in 14
7. list.insert(index,obj)- Returns a list with object obj inserted at the given index
list1 = [‘abcd’,147,2.43,’Tom’]
list1.insert(2,222)
print(“List after insertion”,list1)
Output
[‘abcd’,147,222,2.43,’Tom’]
15-11-2021 meghav@kannuruniv.ac.in 15
8. list.sort([Key=None,Reverse=False]) – Sort the items in a list and returns the list,
If a function is provided, it will compare using the function provided
list1=[890,147,2.43,100]
print(“List before
sorting:”,list1)#[890,147,2.43,100]
list1.sort()
print(“List after sorting in ascending
order:”,list1)#[2.43,100,147,890]
list1.sort(reverse=True)
print(“List after sorting in descending
order:”,list1)#[890,147,100,2.43]
15-11-2021 meghav@kannuruniv.ac.in 16
9.list.pop([index]) – removes or returns the last object obj from a list. we can pop out
any item using index
list1=[‘abcd’,147,2.43,’Tom’]
list1.pop(-1)
print(“list after poping:”,list1)
Output
List after poping: [‘abcd’,147,2.43]
10. list.clear() – Removes all items from a list
list1.clear()
11. list.copy() – Returns a copy of the list
list2=list1.copy()
15-11-2021 meghav@kannuruniv.ac.in 17
Using List as Stack
• List can be used as stack(Last IN First OUT)
• To add an item to the top of stack – append()
• To retrieve an item from top –pop()
stack = [10,20,30,40,50]
stack.append(60)
print(“stack after appending:”,stack)
stack.pop()
print(“Stack after poping:”,stack)
Output
Stack after appending:[10,20,30,40,50,60]
Stack after poping:[10,20,30,40,50]
15-11-2021 meghav@kannuruniv.ac.in 18
Using List as Queue
• List can be used as Queue data structure(FIFO)
• Python provide a module called collections in which a method called deque is designed to
have append and pop operations from both ends
from collections import deque
queue=deque([“apple”,”orange”,”pear”])
queue.append(“cherry”)#cherry added to right end
queue.append(“grapes”)# grapes added to right end
queue.popleft() # first element from left side is removed
queu.popleft() # first element in the left side removed
print(queue)
Output
deque([‘pear’,’cherry’,’grapes’])
15-11-2021 meghav@kannuruniv.ac.in 19
LAB ASSIGNMENTS
• Write a Python program to change a given string to a new string
where the first and last characters have been changed
• Write a Python program to read an input string from user and displays
that input back in upper and lower cases
• Write a program to get the largest number from the list
• Write a program to display the first and last colors from a list of color
values
• Write a program to Implement stack operation using list
• Write a program to implement queue operation using list
15-11-2021 meghav@kannuruniv.ac.in 20

More Related Content

PPTX
Parts of python programming language
Megha V
 
PPTX
Python programming- Part IV(Functions)
Megha V
 
PPTX
Python programming –part 7
Megha V
 
PPTX
Python programming -Tuple and Set Data type
Megha V
 
PPTX
Python programming –part 3
Megha V
 
PPTX
Python programming: Anonymous functions, String operations
Megha V
 
PDF
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
PDF
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Parts of python programming language
Megha V
 
Python programming- Part IV(Functions)
Megha V
 
Python programming –part 7
Megha V
 
Python programming -Tuple and Set Data type
Megha V
 
Python programming –part 3
Megha V
 
Python programming: Anonymous functions, String operations
Megha V
 
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 

What's hot (20)

PDF
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
PDF
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
PDF
Arrays in python
moazamali28
 
PPTX
Python- Regular expression
Megha V
 
PDF
Map, Reduce and Filter in Swift
Aleksandras Smirnovas
 
PDF
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
PPTX
Functional Programming in Swift
Saugat Gautam
 
PPTX
Java Foundations: Lists, ArrayList<T>
Svetlin Nakov
 
PDF
Python Workshop Part 2. LUG Maniapl
Ankur Shrivastava
 
PPTX
Java Foundations: Maps, Lambda and Stream API
Svetlin Nakov
 
PDF
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
PPTX
List in Python
Siddique Ibrahim
 
PDF
7 Habits For a More Functional Swift
Jason Larsen
 
PDF
List,tuple,dictionary
nitamhaske
 
PPTX
Iteration
Pooja B S
 
PDF
Reasoning about laziness
Johan Tibell
 
PDF
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
PPTX
Whiteboarding Coding Challenges in Python
Andrew Ferlitsch
 
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Arrays in python
moazamali28
 
Python- Regular expression
Megha V
 
Map, Reduce and Filter in Swift
Aleksandras Smirnovas
 
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Functional Programming in Swift
Saugat Gautam
 
Java Foundations: Lists, ArrayList<T>
Svetlin Nakov
 
Python Workshop Part 2. LUG Maniapl
Ankur Shrivastava
 
Java Foundations: Maps, Lambda and Stream API
Svetlin Nakov
 
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
List in Python
Siddique Ibrahim
 
7 Habits For a More Functional Swift
Jason Larsen
 
List,tuple,dictionary
nitamhaske
 
Iteration
Pooja B S
 
Reasoning about laziness
Johan Tibell
 
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
Whiteboarding Coding Challenges in Python
Andrew Ferlitsch
 
Ad

Similar to Python programming Part -6 (20)

PPT
STACK_Imp_Functiosbbsbsbsbsbababawns.ppt
Farhana859326
 
PPT
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
divijareddy0502
 
PPT
GF_Python_Data_Structures.ppt
ManishPaul40
 
PPTX
PYTHON-PROGRAMMING-UNIT-III.pptx kghbg kfhjf jruufg jtuuf
DeepakRattan3
 
PPTX
Unit 4 python -list methods
narmadhakin
 
PPTX
Programming with Python_Unit-3-Notes.pptx
YugandharaNalavade
 
PPTX
Python _dataStructures_ List, Tuples, its functions
VidhyaB10
 
PPTX
Python list
ArchanaBhumkar
 
PPTX
List and Dictionary in python
Sangita Panchal
 
PPTX
Chapter 15 Lists
Praveen M Jigajinni
 
PPTX
PROBLEM SOLVING AND PYTHON PROGRAMMING PPT
MulliMary
 
PPTX
Python for the data science most in cse.pptx
Rajasekhar364622
 
DOCX
List Data Structure.docx
manohar25689
 
PDF
Unit 1-Part-4-Lists, tuples and dictionaries.pdf
Harsha Patil
 
PDF
python_avw - Unit-03.pdf
AshaWankar1
 
PPTX
Python - List, Dictionaries, Tuples,Sets
Mohan Arumugam
 
PPTX
listtupledictionary-2001okdhfuihcasbdjj02082611.pptx
meganathan162007
 
PPTX
Python programming
sirikeshava
 
STACK_Imp_Functiosbbsbsbsbsbababawns.ppt
Farhana859326
 
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
divijareddy0502
 
GF_Python_Data_Structures.ppt
ManishPaul40
 
PYTHON-PROGRAMMING-UNIT-III.pptx kghbg kfhjf jruufg jtuuf
DeepakRattan3
 
Unit 4 python -list methods
narmadhakin
 
Programming with Python_Unit-3-Notes.pptx
YugandharaNalavade
 
Python _dataStructures_ List, Tuples, its functions
VidhyaB10
 
Python list
ArchanaBhumkar
 
List and Dictionary in python
Sangita Panchal
 
Chapter 15 Lists
Praveen M Jigajinni
 
PROBLEM SOLVING AND PYTHON PROGRAMMING PPT
MulliMary
 
Python for the data science most in cse.pptx
Rajasekhar364622
 
List Data Structure.docx
manohar25689
 
Unit 1-Part-4-Lists, tuples and dictionaries.pdf
Harsha Patil
 
python_avw - Unit-03.pdf
AshaWankar1
 
Python - List, Dictionaries, Tuples,Sets
Mohan Arumugam
 
listtupledictionary-2001okdhfuihcasbdjj02082611.pptx
meganathan162007
 
Python programming
sirikeshava
 
Ad

More from Megha V (19)

PPTX
Soft Computing Techniques_Part 1.pptx
Megha V
 
PPTX
JavaScript- Functions and arrays.pptx
Megha V
 
PPTX
Introduction to JavaScript
Megha V
 
PPTX
Python Exception Handling
Megha V
 
PPTX
File handling in Python
Megha V
 
PPTX
Python programming
Megha V
 
PPTX
Strassen's matrix multiplication
Megha V
 
PPTX
Solving recurrences
Megha V
 
PPTX
Algorithm Analysis
Megha V
 
PPTX
Algorithm analysis and design
Megha V
 
PPTX
Genetic algorithm
Megha V
 
PPTX
UGC NET Paper 1 ICT Memory and data
Megha V
 
PPTX
Seminar presentation on OpenGL
Megha V
 
PPT
Msc project_CDS Automation
Megha V
 
PPTX
Gi fi technology
Megha V
 
PPTX
Digital initiatives in higher education
Megha V
 
PPTX
Information and Communication Technology (ICT) abbreviation
Megha V
 
PPTX
Basics of internet, intranet, e mail,
Megha V
 
PPTX
Information and communication technology(ict)
Megha V
 
Soft Computing Techniques_Part 1.pptx
Megha V
 
JavaScript- Functions and arrays.pptx
Megha V
 
Introduction to JavaScript
Megha V
 
Python Exception Handling
Megha V
 
File handling in Python
Megha V
 
Python programming
Megha V
 
Strassen's matrix multiplication
Megha V
 
Solving recurrences
Megha V
 
Algorithm Analysis
Megha V
 
Algorithm analysis and design
Megha V
 
Genetic algorithm
Megha V
 
UGC NET Paper 1 ICT Memory and data
Megha V
 
Seminar presentation on OpenGL
Megha V
 
Msc project_CDS Automation
Megha V
 
Gi fi technology
Megha V
 
Digital initiatives in higher education
Megha V
 
Information and Communication Technology (ICT) abbreviation
Megha V
 
Basics of internet, intranet, e mail,
Megha V
 
Information and communication technology(ict)
Megha V
 

Recently uploaded (20)

PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Understanding operators in c language.pptx
auteharshil95
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 

Python programming Part -6

  • 1. Python Programming-Part 6 Megha V Research Scholar Kannur University 15-11-2021 [email protected] 1
  • 2. Difference between Method and Function in Python Function • A function is a block of code to carry out a specific task, will contain its own scope and is called by name. • All functions may contain zero(no) arguments or more than one arguments. 15-11-2021 [email protected] 2
  • 3. Difference between Method and Function in Python Method • A method in python is somewhat similar to a function, except it is associated with object/classes. • Methods in python are very similar to functions except for two major differences. • The method is implicitly used for an object for which it is called. • The method is accessible to data that is contained within the class. 15-11-2021 [email protected] 3
  • 4. List • Creating a list • Basic List operations • Indexing and slicing in Lists • Built-in functions used on lists • List methods • The del statement 15-11-2021 [email protected] 4
  • 5. List • Creating a list • Lists are used to store multiple items in a single variable. thislist = ["apple", "banana", "cherry"] print(thislist) • We can update lists by using slice[] on the LHS of the assignment operator Eg: list=[‘bcd’,147,2.43,’Tom’] print(“Item at index 2=”,list[2]) list[2]=500 print(“Item at index 2=”,list[2]) Output 2.43 500 15-11-2021 [email protected] 5
  • 6. List • To remove an item from a list • del statement • remove() method list=[‘abcd’,147,2.43,’Tom’,74.9] print(list) del list[2] print(“list after deletion:”, list) Output [‘abcd’,147,2.43,’Tom’,74.9] list after deletion:[‘abcd’,147,’Tom’,74.9] 15-11-2021 [email protected] 6
  • 7. • The del keyword can also used to delete the list completely thislist = ["apple", "banana", "cherry"] del thislist • remove()function thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist) 15-11-2021 [email protected] 7
  • 8. Built-in list functions 1. len(list) – Gives the total length of list 2. max(list)- Returns item from list with maximum value 3. min(list)- Returns item from list with minimum value 4. list(seq)- Returns a tuple into a list 5. map(aFunction,aSequence) – Apply an operation to each item and collect result. 15-11-2021 [email protected] 8
  • 9. Example list1=[1200,147,2.43,1.12] list2=[213,100,289] print(list1) print(list2) print(len(list1)) print(“Maximum value in list1 is ”,max(list)) print(“Maximum value in list2 is ”,min(list)) Output [1200,147,2.43,1.12] [1200,147,2.43,1.12] 4 Maximum value in the list1 is 1200 Minimum value in the list2 is 100 15-11-2021 [email protected] 9
  • 10. Example of list() and map() function tuple = (‘abcd’,147,2.43,’Tom’) print(“List:”,list(tuple)) str=input(“Enter a list(space separated):”) lis=list(map(int,str.split())) print(lis) Output List: [‘abcd’,147,2.43,’Tom’] Enter a list (space separated) : 5 6 8 9 [5,6,8,9] In this example a string is read from the keyboard and each item is converted into int using map() function 15-11-2021 [email protected] 10
  • 11. Built-in list methods 1. list.append(obj) –Append an object obj passed to the existing list list = [‘abcd’,147,2.43,’Tom’] print(“Old list before append:”, list) list.append(100) print(“New list after append:”,list) Output Old list before append: [‘abcd’,147,2.43,’Tom’] New list after append: [‘abcd’,147,2.43,’Tom’,100] 15-11-2021 [email protected] 11
  • 12. 2. list.count(obj) –Returns how many times the object obj appears in a list list = [‘abcd’,147,2.43,’Tom’] print(“The number of times”,147,”appears in the list=”,list.count(147)) Output The number of times 147 appears in the list = 1 15-11-2021 [email protected] 12
  • 13. 3. list.remove(obj) – Removes an object list1 = [‘abcd’,147,2.43,’Tom’] list.remove(‘Tom’) print(list1) Output [‘abcd’,147,2.43] 4. list.index(obj) – Returns index of the object obj if found print(list1.index(2.43)) Output 2 15-11-2021 [email protected] 13
  • 14. 5. list.extend(seq)- Appends the contents in a sequence passed to a list list1 = [‘abcd’,147,2.43,’Tom’] list2 = [‘def’,100] list1.extend(list2) print(list1) Output [‘abcd’,147,2.43,’Tom’,‘def’,100] 6. list.reverse() – Reverses objects in a list list1.reverse() print(list1) Output [‘Tom’,2.43,147,’abcd’] 15-11-2021 [email protected] 14
  • 15. 7. list.insert(index,obj)- Returns a list with object obj inserted at the given index list1 = [‘abcd’,147,2.43,’Tom’] list1.insert(2,222) print(“List after insertion”,list1) Output [‘abcd’,147,222,2.43,’Tom’] 15-11-2021 [email protected] 15
  • 16. 8. list.sort([Key=None,Reverse=False]) – Sort the items in a list and returns the list, If a function is provided, it will compare using the function provided list1=[890,147,2.43,100] print(“List before sorting:”,list1)#[890,147,2.43,100] list1.sort() print(“List after sorting in ascending order:”,list1)#[2.43,100,147,890] list1.sort(reverse=True) print(“List after sorting in descending order:”,list1)#[890,147,100,2.43] 15-11-2021 [email protected] 16
  • 17. 9.list.pop([index]) – removes or returns the last object obj from a list. we can pop out any item using index list1=[‘abcd’,147,2.43,’Tom’] list1.pop(-1) print(“list after poping:”,list1) Output List after poping: [‘abcd’,147,2.43] 10. list.clear() – Removes all items from a list list1.clear() 11. list.copy() – Returns a copy of the list list2=list1.copy() 15-11-2021 [email protected] 17
  • 18. Using List as Stack • List can be used as stack(Last IN First OUT) • To add an item to the top of stack – append() • To retrieve an item from top –pop() stack = [10,20,30,40,50] stack.append(60) print(“stack after appending:”,stack) stack.pop() print(“Stack after poping:”,stack) Output Stack after appending:[10,20,30,40,50,60] Stack after poping:[10,20,30,40,50] 15-11-2021 [email protected] 18
  • 19. Using List as Queue • List can be used as Queue data structure(FIFO) • Python provide a module called collections in which a method called deque is designed to have append and pop operations from both ends from collections import deque queue=deque([“apple”,”orange”,”pear”]) queue.append(“cherry”)#cherry added to right end queue.append(“grapes”)# grapes added to right end queue.popleft() # first element from left side is removed queu.popleft() # first element in the left side removed print(queue) Output deque([‘pear’,’cherry’,’grapes’]) 15-11-2021 [email protected] 19
  • 20. LAB ASSIGNMENTS • Write a Python program to change a given string to a new string where the first and last characters have been changed • Write a Python program to read an input string from user and displays that input back in upper and lower cases • Write a program to get the largest number from the list • Write a program to display the first and last colors from a list of color values • Write a program to Implement stack operation using list • Write a program to implement queue operation using list 15-11-2021 [email protected] 20