SlideShare uma empresa Scribd logo
1 de 36
Baixar para ler offline
Python 
Slicing 
Copyright © Software Carpentry 2010 
This work is licensed under the Creative Commons Attribution License 
See http://software-carpentry.org/license.html for more information.
Lists, strings, and tuples are all sequences 
Python Slicing
Lists, strings, and tuples are all sequences 
Can be indexed by integers in the range 0…len(X)-1 
Python Slicing
Lists, strings, and tuples are all sequences 
Can be indexed by integers in the range 0…len(X)-1 
Can also be sliced using aa rraannggee ooff iinnddiicceess 
Python Slicing
Lists, strings, and tuples are all sequences 
Can be indexed by integers in the range 0…len(X)-1 
Can also be sliced using aa rraannggee ooff iinnddiicceess 
>>>>>>>>>>>> element = 'uranium' 
>>>>>>>>>>>> 
0 1 2 3 4 5 6 7 
u r a n i u m 
-7 -6 -5 -4 -3 -2 -1 
Python Slicing
Lists, strings, and tuples are all sequences 
Can be indexed by integers in the range 0…len(X)-1 
Can also be sliced using aa rraannggee ooff iinnddiicceess 
>>>>>>>>>>>> element = 'uranium' 
>>>>>>>>>>>> print element[1:4] 
ran 
>>>>>>>>>>>> 
0 1 2 3 4 5 6 7 
u r a n i u m 
-7 -6 -5 -4 -3 -2 -1 
Python Slicing
Lists, strings, and tuples are all sequences 
Can be indexed by integers in the range 0…len(X)-1 
Can also be sliced using aa rraannggee ooff iinnddiicceess 
>>>>>>>>>>>> element = 'uranium' 
>>>>>>>>>>>> print element[1:4] 
ran 
>>>>>>>>>>>> print element[:4] 
uran 
>>>>>>>>>>>> 
0 1 2 3 4 5 6 7 
u r a n i u m 
-7 -6 -5 -4 -3 -2 -1 
Python Slicing
Lists, strings, and tuples are all sequences 
Can be indexed by integers in the range 0…len(X)-1 
Can also be sliced using aa rraannggee ooff iinnddiicceess 
>>>>>>>>>>>> element = 'uranium' 
>>>>>>>>>>>> print element[1:4] 
ran 
>>>>>>>>>>>> print element[:4] 
uran 
>>>>>>>>>>>> print element[4:] 
0 1 2 3 4 5 6 7 
u r a n i u m 
-7 -6 -5 -4 -3 -2 -1 
ium 
>>>>>>>>>>>> 
Python Slicing
Lists, strings, and tuples are all sequences 
Can be indexed by integers in the range 0…len(X)-1 
Can also be sliced using aa rraannggee ooff iinnddiicceess 
>>>>>>>>>>>> element = 'uranium' 
>>>>>>>>>>>> print element[1:4] 
ran 
>>>>>>>>>>>> print element[:4] 
uran 
>>>>>>>>>>>> print element[4:] 
0 1 2 3 4 5 6 7 
u r a n i u m 
-7 -6 -5 -4 -3 -2 -1 
ium 
>>>>>>>>>>>> print element[-4:] 
nium 
>>>>>>>>>>>> 
Python Slicing
Python checks bounds when indexing 
Python Slicing
Python checks bounds when indexing 
But truncates when slicing 
Python Slicing
Python checks bounds when indexing 
But truncates when slicing 
>>>>>>>>>>>>>>>>>>>>>>>> eelleemmeenntt == ''uurraanniiuumm'' 
>>>>>>>>>>>> 
0 1 2 3 4 5 6 7 
u r a n i u m 
-7 -6 -5 -4 -3 -2 -1 
Python Slicing
Python checks bounds when indexing 
But truncates when slicing 
>>>>>>>>>>>>>>>>>>>>>>>> eelleemmeenntt == ''uurraanniiuumm'' 
>>>>>>>>>>>> print element[400] 
IIIInnnnddddeeeexxxxEEEErrrrrrrroooorrrr:::: ssssttttrrrriiiinnnngggg iiiinnnnddddeeeexxxx oooouuuutttt ooooffff rrrraaaannnnggggeeee 
>>>>>>>>>>>> 
0 1 2 3 4 5 6 7 
u r a n i u m 
-7 -6 -5 -4 -3 -2 -1 
Python Slicing
Python checks bounds when indexing 
But truncates when slicing 
>>>>>>>>>>>>>>>>>>>>>>>> eelleemmeenntt == ''uurraanniiuumm'' 
>>>>>>>>>>>> print element[400] 
IIIInnnnddddeeeexxxxEEEErrrrrrrroooorrrr:::: ssssttttrrrriiiinnnngggg iiiinnnnddddeeeexxxx oooouuuutttt ooooffff rrrraaaannnnggggeeee 
>>>>>>>>>>>> print element[1:400] 
ranium 
>>>>>>>>>>>> 
0 1 2 3 4 5 6 7 
u r a n i u m 
-7 -6 -5 -4 -3 -2 -1 
Python Slicing
Python checks bounds when indexing 
But truncates when slicing 
>>>>>>>>>>>>>>>>>>>>>>>> eelleemmeenntt == ''uurraanniiuumm'' 
>>>>>>>>>>>> print element[400] 
IIIInnnnddddeeeexxxxEEEErrrrrrrroooorrrr:::: ssssttttrrrriiiinnnngggg iiiinnnnddddeeeexxxx oooouuuutttt ooooffff rrrraaaannnnggggeeee 
>>>>>>>>>>>> print element[1:400] 
ranium 
>>>>>>>>>>>> 
0 1 2 3 4 5 6 7 
u r a n i u m 
-7 -6 -5 -4 -3 -2 -1 
"A foolish consistency is 
the hobgoblin of little minds." 
— Ralph Waldo Emerson 
Python Slicing
Python checks bounds when indexing 
But truncates when slicing 
>>>>>>>>>>>>>>>>>>>>>>>> eelleemmeenntt == ''uurraanniiuumm'' 
>>>>>>>>>>>> print element[400] 
IIIInnnnddddeeeexxxxEEEErrrrrrrroooorrrr:::: ssssttttrrrriiiinnnngggg iiiinnnnddddeeeexxxx oooouuuutttt ooooffff rrrraaaannnnggggeeee 
>>>>>>>>>>>> print element[1:400] 
ranium 
>>>>>>>>>>>> 
0 1 2 3 4 5 6 7 
u r a n i u m 
-7 -6 -5 -4 -3 -2 -1 
"A foolish consistency is 
the hobgoblin of little minds." 
— Ralph Waldo Emerson 
"Aw, you're kidding me!" 
— programmers 
Python Slicing
So text[1:3] is 0, 1, or 2 characters long 
Python Slicing
So text[1:3] is 0, 1, or 2 characters long 
'' '' 
''aa'' '''' 
'ab' 'b' 
'abc' 'bc' 
'abcdef' 'bc' 
Python Slicing
For consistency, text[1:1] is the empty string 
Python Slicing
For consistency, text[1:1] is the empty string 
– From index 1 up to (but not including) index 1 
Python Slicing
For consistency, text[1:1] is the empty string 
– From index 1 up to (but not including) index 1 
And text[3:1] is always tthhee eemmppttyy ssttrriinngg 
Python Slicing
For consistency, text[1:1] is the empty string 
– From index 1 up to (but not including) index 1 
And text[3:1] is always tthhee eemmppttyy ssttrriinngg 
– Not the reverse of text[1:3] 
Python Slicing
For consistency, text[1:1] is the empty string 
– From index 1 up to (but not including) index 1 
And text[3:1] is always tthhee eemmppttyy ssttrriinngg 
– Not the reverse of text[1:3] 
But text[1:-1] is everything except the first and 
last characters 
Python Slicing
Slicing always creates a new collection 
Python Slicing
Slicing always creates a new collection 
Beware of aliasing 
Python Slicing
Slicing always creates a new collection 
Beware of aliasing 
>>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] 
>>>>>>>>>>>> 
Python Slicing
Slicing always creates a new collection 
Beware of aliasing 
>>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] 
>>>>>>>>>>>> middle = points[1:-1] 
>>>>>>>>>>>> 
Python Slicing
Slicing always creates a new collection 
Beware of aliasing 
>>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] 
>>>>>>>>>>>> middle = points[1:-1] 
>>>>>>>>>>>> middle[0][0] = 'whoops' 
>>>>>>>>>>>> 
Python Slicing
Slicing always creates a new collection 
Beware of aliasing 
>>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] 
>>>>>>>>>>>> middle = points[1:-1] 
>>>>>>>>>>>> middle[0][0] = 'whoops' 
>>>>>>>>>>>> middle[1][0] = 'aliasing' 
>>>>>>>>>>>> 
Python Slicing
Slicing always creates a new collection 
Beware of aliasing 
>>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] 
>>>>>>>>>>>> middle = points[1:-1] 
>>>>>>>>>>>> middle[0][0] = 'whoops' 
>>>>>>>>>>>> middle[1][0] = 'aliasing' 
>>>>>>>>>>>> print middle 
[['whoops', 20], ['aliasing', 30]] 
>>>>>>>>>>>> 
Python Slicing
Slicing always creates a new collection 
Beware of aliasing 
>>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] 
>>>>>>>>>>>> middle = points[1:-1] 
>>>>>>>>>>>> middle[0][0] = 'whoops' 
>>>>>>>>>>>> middle[1][0] = 'aliasing' 
>>>>>>>>>>>> print middle 
[['whoops', 20], ['aliasing', 30]] 
>>>>>>>>>>>> print points 
[[10, 10], ['whoops', 20], ['aliasing', 30], [40, 40]] 
>>>>>>>>>>>> 
Python Slicing
10 
10 
20 
points 
20 
30 
30 
40 
40 
Python Slicing
10 
10 
20 
points 
20 
30 
30 
middle 
40 
40 
Python Slicing
10 
10 
points 
'whoops' 
20 
30 
30 
middle 
40 
40 
Python Slicing
10 
10 
points 
'whoops' 
20 
30 
middle 
'aliasing' 
40 
40 
Python Slicing
created by 
Greg Wilson 
October 2010 
Copyright © Software Carpentry 2010 
This work is licensed under the Creative Commons Attribution License 
See http://software-carpentry.org/license.html for more information.

Mais conteúdo relacionado

Mais procurados

Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++Jawad Khan
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++Jaspal Singh
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arraysNeeru Mittal
 
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!
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c languageTanmay Modi
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Muhammad Hammad Waseem
 
One Dimensional Array
One Dimensional Array One Dimensional Array
One Dimensional Array dincyjain
 
Union in c language
Union  in c languageUnion  in c language
Union in c languagetanmaymodi4
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 

Mais procurados (20)

Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Linked list
Linked listLinked list
Linked list
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Call by value
Call by valueCall by value
Call by value
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
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
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Arrays
ArraysArrays
Arrays
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Structure in C
Structure in CStructure in C
Structure in C
 
One Dimensional Array
One Dimensional Array One Dimensional Array
One Dimensional Array
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 

Destaque (6)

Tuples
TuplesTuples
Tuples
 
Class 6: Lists & dictionaries
Class 6: Lists & dictionariesClass 6: Lists & dictionaries
Class 6: Lists & 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
 
Lists
ListsLists
Lists
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 

Semelhante a Slicing

Semelhante a Slicing (20)

Libraries
LibrariesLibraries
Libraries
 
Python String Revisited.pptx
Python String Revisited.pptxPython String Revisited.pptx
Python String Revisited.pptx
 
Python course
Python coursePython course
Python course
 
Introduction to Python for Bioinformatics
Introduction to Python for BioinformaticsIntroduction to Python for Bioinformatics
Introduction to Python for Bioinformatics
 
Python 3.3 チラ見
Python 3.3 チラ見Python 3.3 チラ見
Python 3.3 チラ見
 
Basics
BasicsBasics
Basics
 
Python study material
Python study materialPython study material
Python study material
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Python Quick Start
Python Quick StartPython Quick Start
Python Quick Start
 
Python in 90 Minutes
Python in 90 MinutesPython in 90 Minutes
Python in 90 Minutes
 
Python
PythonPython
Python
 
Python
PythonPython
Python
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Python Traning presentation
Python Traning presentationPython Traning presentation
Python Traning presentation
 
Python Loop
Python LoopPython Loop
Python Loop
 
Numpy_Cheat_Sheet.pdf
Numpy_Cheat_Sheet.pdfNumpy_Cheat_Sheet.pdf
Numpy_Cheat_Sheet.pdf
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python language data types
Python language data typesPython language data types
Python language data types
 

Mais de Marieswaran Ramasamy (6)

Handouts lecture slides_lecture6_5
Handouts lecture slides_lecture6_5Handouts lecture slides_lecture6_5
Handouts lecture slides_lecture6_5
 
Functions
FunctionsFunctions
Functions
 
Alias
AliasAlias
Alias
 
Strings
StringsStrings
Strings
 
Input and Output
Input and OutputInput and Output
Input and Output
 
Control Flow
Control FlowControl Flow
Control Flow
 

Último

The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 

Último (20)

The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 

Slicing

  • 1. Python Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.
  • 2. Lists, strings, and tuples are all sequences Python Slicing
  • 3. Lists, strings, and tuples are all sequences Can be indexed by integers in the range 0…len(X)-1 Python Slicing
  • 4. Lists, strings, and tuples are all sequences Can be indexed by integers in the range 0…len(X)-1 Can also be sliced using aa rraannggee ooff iinnddiicceess Python Slicing
  • 5. Lists, strings, and tuples are all sequences Can be indexed by integers in the range 0…len(X)-1 Can also be sliced using aa rraannggee ooff iinnddiicceess >>>>>>>>>>>> element = 'uranium' >>>>>>>>>>>> 0 1 2 3 4 5 6 7 u r a n i u m -7 -6 -5 -4 -3 -2 -1 Python Slicing
  • 6. Lists, strings, and tuples are all sequences Can be indexed by integers in the range 0…len(X)-1 Can also be sliced using aa rraannggee ooff iinnddiicceess >>>>>>>>>>>> element = 'uranium' >>>>>>>>>>>> print element[1:4] ran >>>>>>>>>>>> 0 1 2 3 4 5 6 7 u r a n i u m -7 -6 -5 -4 -3 -2 -1 Python Slicing
  • 7. Lists, strings, and tuples are all sequences Can be indexed by integers in the range 0…len(X)-1 Can also be sliced using aa rraannggee ooff iinnddiicceess >>>>>>>>>>>> element = 'uranium' >>>>>>>>>>>> print element[1:4] ran >>>>>>>>>>>> print element[:4] uran >>>>>>>>>>>> 0 1 2 3 4 5 6 7 u r a n i u m -7 -6 -5 -4 -3 -2 -1 Python Slicing
  • 8. Lists, strings, and tuples are all sequences Can be indexed by integers in the range 0…len(X)-1 Can also be sliced using aa rraannggee ooff iinnddiicceess >>>>>>>>>>>> element = 'uranium' >>>>>>>>>>>> print element[1:4] ran >>>>>>>>>>>> print element[:4] uran >>>>>>>>>>>> print element[4:] 0 1 2 3 4 5 6 7 u r a n i u m -7 -6 -5 -4 -3 -2 -1 ium >>>>>>>>>>>> Python Slicing
  • 9. Lists, strings, and tuples are all sequences Can be indexed by integers in the range 0…len(X)-1 Can also be sliced using aa rraannggee ooff iinnddiicceess >>>>>>>>>>>> element = 'uranium' >>>>>>>>>>>> print element[1:4] ran >>>>>>>>>>>> print element[:4] uran >>>>>>>>>>>> print element[4:] 0 1 2 3 4 5 6 7 u r a n i u m -7 -6 -5 -4 -3 -2 -1 ium >>>>>>>>>>>> print element[-4:] nium >>>>>>>>>>>> Python Slicing
  • 10. Python checks bounds when indexing Python Slicing
  • 11. Python checks bounds when indexing But truncates when slicing Python Slicing
  • 12. Python checks bounds when indexing But truncates when slicing >>>>>>>>>>>>>>>>>>>>>>>> eelleemmeenntt == ''uurraanniiuumm'' >>>>>>>>>>>> 0 1 2 3 4 5 6 7 u r a n i u m -7 -6 -5 -4 -3 -2 -1 Python Slicing
  • 13. Python checks bounds when indexing But truncates when slicing >>>>>>>>>>>>>>>>>>>>>>>> eelleemmeenntt == ''uurraanniiuumm'' >>>>>>>>>>>> print element[400] IIIInnnnddddeeeexxxxEEEErrrrrrrroooorrrr:::: ssssttttrrrriiiinnnngggg iiiinnnnddddeeeexxxx oooouuuutttt ooooffff rrrraaaannnnggggeeee >>>>>>>>>>>> 0 1 2 3 4 5 6 7 u r a n i u m -7 -6 -5 -4 -3 -2 -1 Python Slicing
  • 14. Python checks bounds when indexing But truncates when slicing >>>>>>>>>>>>>>>>>>>>>>>> eelleemmeenntt == ''uurraanniiuumm'' >>>>>>>>>>>> print element[400] IIIInnnnddddeeeexxxxEEEErrrrrrrroooorrrr:::: ssssttttrrrriiiinnnngggg iiiinnnnddddeeeexxxx oooouuuutttt ooooffff rrrraaaannnnggggeeee >>>>>>>>>>>> print element[1:400] ranium >>>>>>>>>>>> 0 1 2 3 4 5 6 7 u r a n i u m -7 -6 -5 -4 -3 -2 -1 Python Slicing
  • 15. Python checks bounds when indexing But truncates when slicing >>>>>>>>>>>>>>>>>>>>>>>> eelleemmeenntt == ''uurraanniiuumm'' >>>>>>>>>>>> print element[400] IIIInnnnddddeeeexxxxEEEErrrrrrrroooorrrr:::: ssssttttrrrriiiinnnngggg iiiinnnnddddeeeexxxx oooouuuutttt ooooffff rrrraaaannnnggggeeee >>>>>>>>>>>> print element[1:400] ranium >>>>>>>>>>>> 0 1 2 3 4 5 6 7 u r a n i u m -7 -6 -5 -4 -3 -2 -1 "A foolish consistency is the hobgoblin of little minds." — Ralph Waldo Emerson Python Slicing
  • 16. Python checks bounds when indexing But truncates when slicing >>>>>>>>>>>>>>>>>>>>>>>> eelleemmeenntt == ''uurraanniiuumm'' >>>>>>>>>>>> print element[400] IIIInnnnddddeeeexxxxEEEErrrrrrrroooorrrr:::: ssssttttrrrriiiinnnngggg iiiinnnnddddeeeexxxx oooouuuutttt ooooffff rrrraaaannnnggggeeee >>>>>>>>>>>> print element[1:400] ranium >>>>>>>>>>>> 0 1 2 3 4 5 6 7 u r a n i u m -7 -6 -5 -4 -3 -2 -1 "A foolish consistency is the hobgoblin of little minds." — Ralph Waldo Emerson "Aw, you're kidding me!" — programmers Python Slicing
  • 17. So text[1:3] is 0, 1, or 2 characters long Python Slicing
  • 18. So text[1:3] is 0, 1, or 2 characters long '' '' ''aa'' '''' 'ab' 'b' 'abc' 'bc' 'abcdef' 'bc' Python Slicing
  • 19. For consistency, text[1:1] is the empty string Python Slicing
  • 20. For consistency, text[1:1] is the empty string – From index 1 up to (but not including) index 1 Python Slicing
  • 21. For consistency, text[1:1] is the empty string – From index 1 up to (but not including) index 1 And text[3:1] is always tthhee eemmppttyy ssttrriinngg Python Slicing
  • 22. For consistency, text[1:1] is the empty string – From index 1 up to (but not including) index 1 And text[3:1] is always tthhee eemmppttyy ssttrriinngg – Not the reverse of text[1:3] Python Slicing
  • 23. For consistency, text[1:1] is the empty string – From index 1 up to (but not including) index 1 And text[3:1] is always tthhee eemmppttyy ssttrriinngg – Not the reverse of text[1:3] But text[1:-1] is everything except the first and last characters Python Slicing
  • 24. Slicing always creates a new collection Python Slicing
  • 25. Slicing always creates a new collection Beware of aliasing Python Slicing
  • 26. Slicing always creates a new collection Beware of aliasing >>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] >>>>>>>>>>>> Python Slicing
  • 27. Slicing always creates a new collection Beware of aliasing >>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] >>>>>>>>>>>> middle = points[1:-1] >>>>>>>>>>>> Python Slicing
  • 28. Slicing always creates a new collection Beware of aliasing >>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] >>>>>>>>>>>> middle = points[1:-1] >>>>>>>>>>>> middle[0][0] = 'whoops' >>>>>>>>>>>> Python Slicing
  • 29. Slicing always creates a new collection Beware of aliasing >>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] >>>>>>>>>>>> middle = points[1:-1] >>>>>>>>>>>> middle[0][0] = 'whoops' >>>>>>>>>>>> middle[1][0] = 'aliasing' >>>>>>>>>>>> Python Slicing
  • 30. Slicing always creates a new collection Beware of aliasing >>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] >>>>>>>>>>>> middle = points[1:-1] >>>>>>>>>>>> middle[0][0] = 'whoops' >>>>>>>>>>>> middle[1][0] = 'aliasing' >>>>>>>>>>>> print middle [['whoops', 20], ['aliasing', 30]] >>>>>>>>>>>> Python Slicing
  • 31. Slicing always creates a new collection Beware of aliasing >>>>>>>>>>>>>>>>>>>>>>>> ppooiinnttss == [[[[1100,, 1100]],, [[2200,, 2200]],, [[3300,, 3300]],, [[4400,, 4400]]]] >>>>>>>>>>>> middle = points[1:-1] >>>>>>>>>>>> middle[0][0] = 'whoops' >>>>>>>>>>>> middle[1][0] = 'aliasing' >>>>>>>>>>>> print middle [['whoops', 20], ['aliasing', 30]] >>>>>>>>>>>> print points [[10, 10], ['whoops', 20], ['aliasing', 30], [40, 40]] >>>>>>>>>>>> Python Slicing
  • 32. 10 10 20 points 20 30 30 40 40 Python Slicing
  • 33. 10 10 20 points 20 30 30 middle 40 40 Python Slicing
  • 34. 10 10 points 'whoops' 20 30 30 middle 40 40 Python Slicing
  • 35. 10 10 points 'whoops' 20 30 middle 'aliasing' 40 40 Python Slicing
  • 36. created by Greg Wilson October 2010 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.