SlideShare a Scribd company logo
1 of 20
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

What's hot

Arrays in python
Arrays in pythonArrays in python
Arrays in pythonmoazamali28
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expressionMegha V
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in SwiftSaugat Gautam
 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Svetlin Nakov
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplPython Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplAnkur Shrivastava
 
Java Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APIJava Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APISvetlin Nakov
 
The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88Mahmoud Samir Fayed
 
7 Habits For a More Functional Swift
7 Habits For a More Functional Swift7 Habits For a More Functional Swift
7 Habits For a More Functional SwiftJason Larsen
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionarynitamhaske
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about lazinessJohan Tibell
 
Whiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonWhiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonAndrew Ferlitsch
 

What's hot (20)

Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
 
Map, Reduce and Filter in Swift
Map, Reduce and Filter in SwiftMap, Reduce and Filter in Swift
Map, Reduce and Filter in Swift
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplPython Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG Maniapl
 
Java Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream APIJava Foundations: Maps, Lambda and Stream API
Java Foundations: Maps, Lambda and Stream API
 
The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.3 book - Part 13 of 88
 
List in Python
List in PythonList in Python
List in Python
 
7 Habits For a More Functional Swift
7 Habits For a More Functional Swift7 Habits For a More Functional Swift
7 Habits For a More Functional Swift
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Iteration
IterationIteration
Iteration
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
Whiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonWhiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in Python
 

Similar to Python programming Part -6

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 Pythonkvpv2023
 
Data type list_methods_in_python
Data type list_methods_in_pythonData type list_methods_in_python
Data type list_methods_in_pythondeepalishinkar1
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methodsnarmadhakin
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queueRai University
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonLifna C.S
 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of DataIHTMINSTITUTE
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear listsToniyaP1
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonCeline George
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queueRai University
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 
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.pdfMCCMOTOR
 
Python Assignment 1 Answers (1).docx
Python Assignment 1 Answers (1).docxPython Assignment 1 Answers (1).docx
Python Assignment 1 Answers (1).docxShubhamTripathi290909
 

Similar to Python programming Part -6 (20)

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
 
Data type list_methods_in_python
Data type list_methods_in_pythonData type list_methods_in_python
Data type list_methods_in_python
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of Data
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear lists
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Groovy
GroovyGroovy
Groovy
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Queue
QueueQueue
Queue
 
Groovy
GroovyGroovy
Groovy
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
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
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Python Assignment 1 Answers (1).docx
Python Assignment 1 Answers (1).docxPython Assignment 1 Answers (1).docx
Python Assignment 1 Answers (1).docx
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
Python list
Python listPython list
Python list
 

More from Megha V

Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxMegha V
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxMegha V
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptMegha V
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception HandlingMegha V
 
File handling in Python
File handling in PythonFile handling in Python
File handling in PythonMegha V
 
Python programming
Python programmingPython programming
Python programmingMegha V
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplicationMegha V
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesMegha V
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm AnalysisMegha V
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and designMegha V
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithmMegha V
 
UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data  UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data Megha V
 
Seminar presentation on OpenGL
Seminar presentation on OpenGLSeminar presentation on OpenGL
Seminar presentation on OpenGLMegha V
 
Msc project_CDS Automation
Msc project_CDS AutomationMsc project_CDS Automation
Msc project_CDS AutomationMegha V
 
Gi fi technology
Gi fi technologyGi fi technology
Gi fi technologyMegha V
 
Digital initiatives in higher education
Digital initiatives in higher educationDigital initiatives in higher education
Digital initiatives in higher educationMegha V
 
Information and Communication Technology (ICT) abbreviation
Information and Communication Technology (ICT) abbreviationInformation and Communication Technology (ICT) abbreviation
Information and Communication Technology (ICT) abbreviationMegha V
 
Basics of internet, intranet, e mail,
Basics of internet, intranet, e mail,Basics of internet, intranet, e mail,
Basics of internet, intranet, e mail,Megha V
 
Information and communication technology(ict)
Information and communication technology(ict)Information and communication technology(ict)
Information and communication technology(ict)Megha V
 

More from Megha V (19)

Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptx
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python programming
Python programmingPython programming
Python programming
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data  UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data
 
Seminar presentation on OpenGL
Seminar presentation on OpenGLSeminar presentation on OpenGL
Seminar presentation on OpenGL
 
Msc project_CDS Automation
Msc project_CDS AutomationMsc project_CDS Automation
Msc project_CDS Automation
 
Gi fi technology
Gi fi technologyGi fi technology
Gi fi technology
 
Digital initiatives in higher education
Digital initiatives in higher educationDigital initiatives in higher education
Digital initiatives in higher education
 
Information and Communication Technology (ICT) abbreviation
Information and Communication Technology (ICT) abbreviationInformation and Communication Technology (ICT) abbreviation
Information and Communication Technology (ICT) abbreviation
 
Basics of internet, intranet, e mail,
Basics of internet, intranet, e mail,Basics of internet, intranet, e mail,
Basics of internet, intranet, e mail,
 
Information and communication technology(ict)
Information and communication technology(ict)Information and communication technology(ict)
Information and communication technology(ict)
 

Recently uploaded

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

Python programming Part -6

  • 1. Python Programming-Part 6 Megha V Research Scholar Kannur University 15-11-2021 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 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 meghav@kannuruniv.ac.in 20