slicing
DESCRIPTION
Python - SlicingTRANSCRIPT
![Page 1: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/1.jpg)
Slicing
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.
![Page 2: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/2.jpg)
Lists, strings, and tuples are all sequences
Python Slicing
![Page 3: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/3.jpg)
Lists, strings, and tuples are all sequences
Can be indexed by integers in the range 0…len(X)-1
Python Slicing
![Page 4: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/4.jpg)
Lists, strings, and tuples are all sequences
Can be indexed by integers in the range 0…len(X)-1
Can also be sliced using a range of indicesCan also be sliced using a range of indices
Python Slicing
![Page 5: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/5.jpg)
Lists, strings, and tuples are all sequences
Can be indexed by integers in the range 0…len(X)-1
Can also be sliced using a range of indicesCan also be sliced using a range of indices
>>>>>>>>>>>> element = 'uranium'
>>>>>>>>>>>>
u r a n i u m
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
Python Slicing
-7 -6 -5 -4 -3 -2 -1
![Page 6: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/6.jpg)
Lists, strings, and tuples are all sequences
Can be indexed by integers in the range 0…len(X)-1
Can also be sliced using a range of indicesCan also be sliced using a range of indices
>>>>>>>>>>>> element = 'uranium'
>>>>>>>>>>>> print element[1:4]
ran
>>>>>>>>>>>>u r a n i u m
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
Python Slicing
-7 -6 -5 -4 -3 -2 -1
![Page 7: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/7.jpg)
Lists, strings, and tuples are all sequences
Can be indexed by integers in the range 0…len(X)-1
Can also be sliced using a range of indicesCan also be sliced using a range of indices
>>>>>>>>>>>> element = 'uranium'
>>>>>>>>>>>> print element[1:4]
ran
>>>>>>>>>>>> print element[:4]
uran
>>>>>>>>>>>>
u r a n i u m
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
Python Slicing
>>>>>>>>>>>> -7 -6 -5 -4 -3 -2 -1
![Page 8: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/8.jpg)
Lists, strings, and tuples are all sequences
Can be indexed by integers in the range 0…len(X)-1
Can also be sliced using a range of indicesCan also be sliced using a range of indices
>>>>>>>>>>>> element = 'uranium'
>>>>>>>>>>>> print element[1:4]
ran
>>>>>>>>>>>> print element[:4]
uran
>>>>>>>>>>>> print element[4:]
u r a n i u m
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
Python Slicing
>>>>>>>>>>>> print element[4:]
ium
>>>>>>>>>>>>
-7 -6 -5 -4 -3 -2 -1
![Page 9: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/9.jpg)
Lists, strings, and tuples are all sequences
Can be indexed by integers in the range 0…len(X)-1
Can also be sliced using a range of indicesCan also be sliced using a range of indices
>>>>>>>>>>>> element = 'uranium'
>>>>>>>>>>>> print element[1:4]
ran
>>>>>>>>>>>> print element[:4]
uran
>>>>>>>>>>>> print element[4:]
u r a n i u m
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
Python Slicing
>>>>>>>>>>>> print element[4:]
ium
>>>>>>>>>>>> print element[-4:]
nium
>>>>>>>>>>>>
-7 -6 -5 -4 -3 -2 -1
![Page 10: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/10.jpg)
Python checks bounds when indexing
Python Slicing
![Page 11: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/11.jpg)
Python checks bounds when indexing
But truncates when slicing
Python Slicing
![Page 12: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/12.jpg)
Python checks bounds when indexing
But truncates when slicing
>>>>>>>>>>>> element = 'uranium'>>>>>>>>>>>> element = 'uranium'
>>>>>>>>>>>>
u r a n i u m
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
Python Slicing
-7 -6 -5 -4 -3 -2 -1
![Page 13: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/13.jpg)
Python checks bounds when indexing
But truncates when slicing
>>>>>>>>>>>> element = 'uranium'>>>>>>>>>>>> element = 'uranium'
>>>>>>>>>>>> print element[400]
IndexError: string index out of rangeIndexError: string index out of rangeIndexError: string index out of rangeIndexError: string index out of range
>>>>>>>>>>>>
u r a n i u m
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
Python Slicing
-7 -6 -5 -4 -3 -2 -1
![Page 14: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/14.jpg)
Python checks bounds when indexing
But truncates when slicing
>>>>>>>>>>>> element = 'uranium'>>>>>>>>>>>> element = 'uranium'
>>>>>>>>>>>> print element[400]
IndexError: string index out of rangeIndexError: string index out of rangeIndexError: string index out of rangeIndexError: string index out of range
>>>>>>>>>>>> print element[1:400]
ranium
>>>>>>>>>>>>u r a n i u m
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
Python Slicing
-7 -6 -5 -4 -3 -2 -1
![Page 15: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/15.jpg)
Python checks bounds when indexing
But truncates when slicing
>>>>>>>>>>>> element = 'uranium'>>>>>>>>>>>> element = 'uranium'
>>>>>>>>>>>> print element[400]
IndexError: string index out of rangeIndexError: string index out of rangeIndexError: string index out of rangeIndexError: string index out of range
>>>>>>>>>>>> print element[1:400]
ranium
>>>>>>>>>>>>u r a n i u m
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
Python Slicing
-7 -6 -5 -4 -3 -2 -1
"A foolish consistency is
the hobgoblin of little minds."
— Ralph Waldo Emerson
![Page 16: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/16.jpg)
Python checks bounds when indexing
But truncates when slicing
>>>>>>>>>>>> element = 'uranium'>>>>>>>>>>>> element = 'uranium'
>>>>>>>>>>>> print element[400]
IndexError: string index out of rangeIndexError: string index out of rangeIndexError: string index out of rangeIndexError: string index out of range
>>>>>>>>>>>> print element[1:400]
ranium
>>>>>>>>>>>>u r a n i u m
0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1
Python Slicing
-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
![Page 17: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/17.jpg)
So text[1:3] is 0, 1, or 2 characters long
Python Slicing
![Page 18: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/18.jpg)
So text[1:3] is 0, 1, or 2 characters long
'' ''
'a' '''a' ''
'ab' 'b'
'abc' 'bc'
'abcdef' 'bc'
Python Slicing
![Page 19: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/19.jpg)
For consistency, text[1:1] is the empty string
Python Slicing
![Page 20: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/20.jpg)
For consistency, text[1:1] is the empty string
– From index 1 up to (but not including) index 1
Python Slicing
![Page 21: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/21.jpg)
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 the empty stringAnd text[3:1] is always the empty string
Python Slicing
![Page 22: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/22.jpg)
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 the empty stringAnd text[3:1] is always the empty string
– Not the reverse of text[1:3]
Python Slicing
![Page 23: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/23.jpg)
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 the empty stringAnd text[3:1] is always the empty string
– Not the reverse of text[1:3]
But text[1:-1] is everything except the first and
last characters
Python Slicing
![Page 24: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/24.jpg)
Slicing always creates a new collection
Python Slicing
![Page 25: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/25.jpg)
Slicing always creates a new collection
Beware of aliasing
Python Slicing
![Page 26: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/26.jpg)
Slicing always creates a new collection
Beware of aliasing
>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]
>>>>>>>>>>>>
Python Slicing
![Page 27: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/27.jpg)
Slicing always creates a new collection
Beware of aliasing
>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]
>>>>>>>>>>>> middle = points[1:-1]
>>>>>>>>>>>>
Python Slicing
![Page 28: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/28.jpg)
Slicing always creates a new collection
Beware of aliasing
>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]
>>>>>>>>>>>> middle = points[1:-1]
>>>>>>>>>>>> middle[0][0] = 'whoops'
>>>>>>>>>>>>
Python Slicing
![Page 29: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/29.jpg)
Slicing always creates a new collection
Beware of aliasing
>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]
>>>>>>>>>>>> middle = points[1:-1]
>>>>>>>>>>>> middle[0][0] = 'whoops'
>>>>>>>>>>>> middle[1][0] = 'aliasing'
>>>>>>>>>>>>
Python Slicing
![Page 30: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/30.jpg)
Slicing always creates a new collection
Beware of aliasing
>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]
>>>>>>>>>>>> middle = points[1:-1]
>>>>>>>>>>>> middle[0][0] = 'whoops'
>>>>>>>>>>>> middle[1][0] = 'aliasing'
>>>>>>>>>>>> print middle
[['whoops', 20], ['aliasing', 30]]
>>>>>>>>>>>>
Python Slicing
![Page 31: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/31.jpg)
Slicing always creates a new collection
Beware of aliasing
>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]>>>>>>>>>>>> points = [[10, 10], [20, 20], [30, 30], [40, 40]]
>>>>>>>>>>>> middle = points[1:-1]
>>>>>>>>>>>> middle[0][0] = 'whoops'
>>>>>>>>>>>> middle[1][0] = 'aliasing'
>>>>>>>>>>>> print middle
[['whoops', 20], ['aliasing', 30]]
>>>>>>>>>>>> print points
Python Slicing
[[10, 10], ['whoops', 20], ['aliasing', 30], [40, 40]]
>>>>>>>>>>>>
![Page 32: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/32.jpg)
10
10
20
points
20
30
30
Python Slicing
40
40
![Page 33: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/33.jpg)
10
10
20
points
20
30
30
middle
Python Slicing
40
40
middle
![Page 34: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/34.jpg)
10
10
points
'whoops'
20
30
30
middle
Python Slicing
40
40
middle
![Page 35: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/35.jpg)
10
10
points
'whoops'
20
30
middle
'aliasing'
Python Slicing
40
40
middle
![Page 36: Slicing](https://reader033.vdocument.in/reader033/viewer/2022042815/55652afcd8b42a47678b5347/html5/thumbnails/36.jpg)
October 2010
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.