SlideShare ist ein Scribd-Unternehmen logo
1 von 22
http://www.skillbrew.com
/SkillbrewTalent brewed by the
industry itself
Python Lists and references
Pavan Verma
@YinYangPavan
Python Programming Essentials
© SkillBrew http://skillbrew.com
Python lists
 List is an ordered collections of items
 Item could be any Python data type (strings, number, other
lists, tuple, dictionary)
 Similar to arrays in (C / JAVA / C++)
 Lists are the simplest data structure in Python
colors = [‘red’, ‘blue’, ‘green’]
2
© SkillBrew http://skillbrew.com
List creation
Use square brackets [] to create a list
3
>>> colors = ['red', 'blue', 'green']
>>> colors
Output:
['red', 'blue', 'green']
© SkillBrew http://skillbrew.com
Access List Elements
list[index]
4
>>> colors = ['red', 'blue', 'green']
>>> colors
['red', 'blue', 'green']
>>> colors[0]
'red'
>>> colors[1]
'blue'
>>> colors[2]
'green'
© SkillBrew http://skillbrew.com
Negative Indexes
list[-index]
5
>>> colors = ['red', 'blue', 'green']
>>> colors
['red', 'blue', 'green']
>>> colors[-1]
'green'
>>> colors[-2]
'blue'
>>> colors[-3]
'red'
© SkillBrew http://skillbrew.com
List operations
 length
 append
 insert
 remove
 delete
 pop
 Slicing
6
© SkillBrew http://skillbrew.com
Length of a list
Use len()function to get the length of list
7
>>> colors = ['red', 'blue', 'green']
>>> len(colors)
Output:
3
© SkillBrew http://skillbrew.com
Append an element
 Use append() to add an element to the list
 append adds the element at the end of a list
8
>>> colors = ['red', 'blue', 'green']
>>> colors.append('orange')
>>> colors
['red', 'blue', 'green', 'orange']
© SkillBrew http://skillbrew.com
Insert an element at an index
 list.insert(index, value)
 Use insert to insert an element at a particular index
9
>>> colors ['red', 'blue', 'green', 'orange']
>>> colors.insert(1, 'black')
>>> colors
['red', 'black', 'blue', 'green', 'orange']
© SkillBrew http://skillbrew.com
Remove an element from list
remove(x) removes the first item from the list whose
value is x
10
>>> colors
['red', 'black', 'blue', 'green', 'orange']
>>> colors.remove('black')
>>> colors
['red', 'blue', 'green', 'orange']
© SkillBrew http://skillbrew.com
Remove an element from list (2)
If you try to remove an element which is not there in the list a
ValueError is raised
11
>>> colors
['red', 'blue', 'green', 'orange']
>>> colors.remove('yellow')
Traceback (most recent call last): File
"<pyshell#24>", line 1, in <module>
colors.remove('yellow')
ValueError: list.remove(x): x not in list
© SkillBrew http://skillbrew.com
Delete an element from list
del list[index]
Use del operator to delete an element from a list
12
>>> colors
['red', 'blue', 'green', 'orange']
>>> del colors[1]
>>> colors
['red', 'green', 'orange']
© SkillBrew http://skillbrew.com
Sorting a list
 sort()sorts the list in place
 In place means that sort works on the original
list rather than giving back a new list
13
>>> colors
['red', 'green', 'orange']
>>> colors.sort()
>>> colors
['green', 'orange', 'red']
© SkillBrew http://skillbrew.com
Reverse a list
 reverse()reverses the list in place
 In place means that reverse()works on the
original list rather than giving back a new list
14
>>> colors
['green', 'orange', 'red']
>>> colors.reverse()
>>> colors
['red', 'orange', 'green']
© SkillBrew http://skillbrew.com
Using list as stack
 A stack is a list with just two operations:
• Push
• Pop
15
© SkillBrew http://skillbrew.com
Stack – push
40
30
20
10
16
50
40
30
20
10
stack.append(50)
>>> stack = [10, 20, 30, 40]
>>> stack.append(50)
>>> stack
[10, 20, 30, 40, 50]
© SkillBrew http://skillbrew.com
Stack – pop
17
50
40
30
20
10
stack.pop( )
>>> stack
[10, 20, 30, 40, 50]
>>> stack.pop()
50
>>> stack
[10, 20, 30, 40]
40
30
20
10
© SkillBrew http://skillbrew.com
List slicing
 Slicing: Extracting parts of list
 Syntax:
list[start:end]
list[start:]
list[end:]
list[:]
 start inclusive and excluding end
 Slicing returns a new list
18
© SkillBrew http://skillbrew.com
List slicing (2)
19
>>> colors = ['yellow', 'red', 'blue', 'green', 'black']
>>> colors[0:]
['yellow', 'red', 'blue', 'green', 'black']
>>> colors[:4]
['yellow', 'red', 'blue', 'green']
>>> colors[1:3]
['red', 'blue']
>>> colors[:]
['yellow', 'red', 'blue', 'green', 'black']
© SkillBrew http://skillbrew.com
Summary
 What is a list
 List creation
 Access elements of a list
 Basic List operations
 Sorting and reversing a list
 Using list as stack
 Slicing
20
© SkillBrew http://skillbrew.com
Resources
 Tutorial on lists
http://www.tutorialspoint.com/Python/Python_lists.htm
21
22

Weitere ähnliche Inhalte

Was ist angesagt?

16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queuesIntro C# Book
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaEdureka!
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluationJeeSa Sultana
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queueRojan Pariyar
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversalIntro C# Book
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array pptsandhya yadav
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonCeline George
 
Queue data structure
Queue data structureQueue data structure
Queue data structureanooppjoseph
 
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
 

Was ist angesagt? (20)

16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Queues
QueuesQueues
Queues
 
List in Python
List in PythonList in Python
List in Python
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
List in Python
List in PythonList in Python
List in Python
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
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
 

Andere mochten auch (8)

Class 7: Programming with Lists
Class 7: Programming with ListsClass 7: Programming with Lists
Class 7: Programming with Lists
 
Python002
Python002Python002
Python002
 
An Introduction To Python - Dictionaries
An Introduction To Python - DictionariesAn Introduction To Python - Dictionaries
An Introduction To Python - Dictionaries
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplPython Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG Maniapl
 
Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
 
Python Programming and GIS
Python Programming and GISPython Programming and GIS
Python Programming and GIS
 
Python Worst Practices
Python Worst PracticesPython Worst Practices
Python Worst Practices
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 

Ähnlich wie Python Programming Essentials - M12 - Lists

Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxMihirDatir1
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slidesaiclub_slides
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsSreedhar Chowdam
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3Abdul Haseeb
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeRamanamurthy Banda
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeRamanamurthy Banda
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxMihirDatir
 
list and control statement.pptx
list and control statement.pptxlist and control statement.pptx
list and control statement.pptxssuser8f0410
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptxYagna15
 
Python elements list you can study .pdf
Python elements list you can study  .pdfPython elements list you can study  .pdf
Python elements list you can study .pdfAUNGHTET61
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...Yashpatel821746
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Yashpatel821746
 

Ähnlich wie Python Programming Essentials - M12 - Lists (20)

Unit - 4.ppt
Unit - 4.pptUnit - 4.ppt
Unit - 4.ppt
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
R Data Structure.pptx
R Data Structure.pptxR Data Structure.pptx
R Data Structure.pptx
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, Exceptions
 
Python Lecture 8
Python Lecture 8Python Lecture 8
Python Lecture 8
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
6. list
6. list6. list
6. list
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
list and control statement.pptx
list and control statement.pptxlist and control statement.pptx
list and control statement.pptx
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
 
PureScript & Pux
PureScript & PuxPureScript & Pux
PureScript & Pux
 
Python Training
Python TrainingPython Training
Python Training
 
R programming
R programmingR programming
R programming
 
Python elements list you can study .pdf
Python elements list you can study  .pdfPython elements list you can study  .pdf
Python elements list you can study .pdf
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
 

Mehr von P3 InfoTech Solutions Pvt. Ltd.

Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentPython Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsPython Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsPython Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsPython Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesPython Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbPython Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesPython Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsPython Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...P3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesPython Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesP3 InfoTech Solutions Pvt. Ltd.
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsP3 InfoTech Solutions Pvt. Ltd.
 

Mehr von P3 InfoTech Solutions Pvt. Ltd. (20)

Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web DevelopmentPython Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M44 - Overview of Web Development
 
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External ProgramsPython Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M40 - Invoking External Programs
 
Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit TestingPython Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M39 - Unit Testing
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
 
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & GeneratorsPython Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M35 - Iterators & Generators
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsPython Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List Comprehensions
 
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M31 - PEP 8Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M31 - PEP 8
 
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and FilesPython Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M29 - Python Interpreter and Files
 
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdbPython Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M28 - Debugging with pdb
 
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging modulePython Programming Essentials - M27 - Logging module
Python Programming Essentials - M27 - Logging module
 
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modulesPython Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M25 - os and sys modules
 
Python Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math modulePython Programming Essentials - M24 - math module
Python Programming Essentials - M24 - math module
 
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime modulePython Programming Essentials - M23 - datetime module
Python Programming Essentials - M23 - datetime module
 
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File OperationsPython Programming Essentials - M22 - File Operations
Python Programming Essentials - M22 - File Operations
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception Handling
 
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and ObjectsPython Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M20 - Classes and Objects
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
 
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and PackagesPython Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M18 - Modules and Packages
 
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - FunctionsPython Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - Functions
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
 

Kürzlich hochgeladen

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Python Programming Essentials - M12 - Lists

  • 1. http://www.skillbrew.com /SkillbrewTalent brewed by the industry itself Python Lists and references Pavan Verma @YinYangPavan Python Programming Essentials
  • 2. © SkillBrew http://skillbrew.com Python lists  List is an ordered collections of items  Item could be any Python data type (strings, number, other lists, tuple, dictionary)  Similar to arrays in (C / JAVA / C++)  Lists are the simplest data structure in Python colors = [‘red’, ‘blue’, ‘green’] 2
  • 3. © SkillBrew http://skillbrew.com List creation Use square brackets [] to create a list 3 >>> colors = ['red', 'blue', 'green'] >>> colors Output: ['red', 'blue', 'green']
  • 4. © SkillBrew http://skillbrew.com Access List Elements list[index] 4 >>> colors = ['red', 'blue', 'green'] >>> colors ['red', 'blue', 'green'] >>> colors[0] 'red' >>> colors[1] 'blue' >>> colors[2] 'green'
  • 5. © SkillBrew http://skillbrew.com Negative Indexes list[-index] 5 >>> colors = ['red', 'blue', 'green'] >>> colors ['red', 'blue', 'green'] >>> colors[-1] 'green' >>> colors[-2] 'blue' >>> colors[-3] 'red'
  • 6. © SkillBrew http://skillbrew.com List operations  length  append  insert  remove  delete  pop  Slicing 6
  • 7. © SkillBrew http://skillbrew.com Length of a list Use len()function to get the length of list 7 >>> colors = ['red', 'blue', 'green'] >>> len(colors) Output: 3
  • 8. © SkillBrew http://skillbrew.com Append an element  Use append() to add an element to the list  append adds the element at the end of a list 8 >>> colors = ['red', 'blue', 'green'] >>> colors.append('orange') >>> colors ['red', 'blue', 'green', 'orange']
  • 9. © SkillBrew http://skillbrew.com Insert an element at an index  list.insert(index, value)  Use insert to insert an element at a particular index 9 >>> colors ['red', 'blue', 'green', 'orange'] >>> colors.insert(1, 'black') >>> colors ['red', 'black', 'blue', 'green', 'orange']
  • 10. © SkillBrew http://skillbrew.com Remove an element from list remove(x) removes the first item from the list whose value is x 10 >>> colors ['red', 'black', 'blue', 'green', 'orange'] >>> colors.remove('black') >>> colors ['red', 'blue', 'green', 'orange']
  • 11. © SkillBrew http://skillbrew.com Remove an element from list (2) If you try to remove an element which is not there in the list a ValueError is raised 11 >>> colors ['red', 'blue', 'green', 'orange'] >>> colors.remove('yellow') Traceback (most recent call last): File "<pyshell#24>", line 1, in <module> colors.remove('yellow') ValueError: list.remove(x): x not in list
  • 12. © SkillBrew http://skillbrew.com Delete an element from list del list[index] Use del operator to delete an element from a list 12 >>> colors ['red', 'blue', 'green', 'orange'] >>> del colors[1] >>> colors ['red', 'green', 'orange']
  • 13. © SkillBrew http://skillbrew.com Sorting a list  sort()sorts the list in place  In place means that sort works on the original list rather than giving back a new list 13 >>> colors ['red', 'green', 'orange'] >>> colors.sort() >>> colors ['green', 'orange', 'red']
  • 14. © SkillBrew http://skillbrew.com Reverse a list  reverse()reverses the list in place  In place means that reverse()works on the original list rather than giving back a new list 14 >>> colors ['green', 'orange', 'red'] >>> colors.reverse() >>> colors ['red', 'orange', 'green']
  • 15. © SkillBrew http://skillbrew.com Using list as stack  A stack is a list with just two operations: • Push • Pop 15
  • 16. © SkillBrew http://skillbrew.com Stack – push 40 30 20 10 16 50 40 30 20 10 stack.append(50) >>> stack = [10, 20, 30, 40] >>> stack.append(50) >>> stack [10, 20, 30, 40, 50]
  • 17. © SkillBrew http://skillbrew.com Stack – pop 17 50 40 30 20 10 stack.pop( ) >>> stack [10, 20, 30, 40, 50] >>> stack.pop() 50 >>> stack [10, 20, 30, 40] 40 30 20 10
  • 18. © SkillBrew http://skillbrew.com List slicing  Slicing: Extracting parts of list  Syntax: list[start:end] list[start:] list[end:] list[:]  start inclusive and excluding end  Slicing returns a new list 18
  • 19. © SkillBrew http://skillbrew.com List slicing (2) 19 >>> colors = ['yellow', 'red', 'blue', 'green', 'black'] >>> colors[0:] ['yellow', 'red', 'blue', 'green', 'black'] >>> colors[:4] ['yellow', 'red', 'blue', 'green'] >>> colors[1:3] ['red', 'blue'] >>> colors[:] ['yellow', 'red', 'blue', 'green', 'black']
  • 20. © SkillBrew http://skillbrew.com Summary  What is a list  List creation  Access elements of a list  Basic List operations  Sorting and reversing a list  Using list as stack  Slicing 20
  • 21. © SkillBrew http://skillbrew.com Resources  Tutorial on lists http://www.tutorialspoint.com/Python/Python_lists.htm 21
  • 22. 22

Hinweis der Redaktion

  1. Here the intention is to implement a stack using lists
  2. Here we have a stack with 10, 20, 30, 40 already present append is equivalent to a PUSH operation in a stack
  3. Stack after append operation has 50 on top pop will remove 50 from top, just like POP in a stack Data Structure
  4. These have been covered in detail in strings hence just a brief overview of slicing is presented here