Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Similar presentations


Presentation on theme: "Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See"— Presentation transcript:

1 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. Python

2 Slicing Lists, strings, and tuples are all sequences

3 PythonSlicing Lists, strings, and tuples are all sequences Can be indexed by integers in the range 0…len(X)-1

4 PythonSlicing 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 indices

5 PythonSlicing 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 indices >>> element = 'uranium' >>> uranium 01234567 -7-6-5-4-3-2

6 PythonSlicing 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 indices >>> element = 'uranium' >>> print element[1:4] ran >>> uranium 01234567 -7-6-5-4-3-2

7 PythonSlicing 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 indices >>> element = 'uranium' >>> print element[1:4] ran >>> print element[:4] uran >>> uranium 01234567 -7-6-5-4-3-2

8 PythonSlicing 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 indices >>> element = 'uranium' >>> print element[1:4] ran >>> print element[:4] uran >>> print element[4:] ium >>> uranium 01234567 -7-6-5-4-3-2

9 PythonSlicing 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 indices >>> element = 'uranium' >>> print element[1:4] ran >>> print element[:4] uran >>> print element[4:] ium >>> print element[-4:] nium >>> uranium 01234567 -7-6-5-4-3-2

10 PythonSlicing Python checks bounds when indexing

11 PythonSlicing Python checks bounds when indexing But truncates when slicing

12 PythonSlicing Python checks bounds when indexing But truncates when slicing >>> element = 'uranium' >>> uranium 01234567 -7-6-5-4-3-2

13 PythonSlicing Python checks bounds when indexing But truncates when slicing >>> element = 'uranium' >>> print element[400] IndexError: string index out of range >>> uranium 01234567 -7-6-5-4-3-2

14 PythonSlicing Python checks bounds when indexing But truncates when slicing >>> element = 'uranium' >>> print element[400] IndexError: string index out of range >>> print element[1:400] ranium >>> uranium 01234567 -7-6-5-4-3-2

15 PythonSlicing Python checks bounds when indexing But truncates when slicing >>> element = 'uranium' >>> print element[400] IndexError: string index out of range >>> print element[1:400] ranium >>> uranium 01234567 -7-6-5-4-3-2 "A foolish consistency is the hobgoblin of little minds." —Ralph Waldo Emerson

16 PythonSlicing Python checks bounds when indexing But truncates when slicing >>> element = 'uranium' >>> print element[400] IndexError: string index out of range >>> print element[1:400] ranium >>> uranium 01234567 -7-6-5-4-3-2 "A foolish consistency is the hobgoblin of little minds." —Ralph Waldo Emerson "Aw, you're kidding me!" —programmers

17 PythonSlicing So text[1:3] is 0, 1, or 2 characters long

18 PythonSlicing So text[1:3] is 0, 1, or 2 characters long '' 'a''' 'ab''b' 'abc''bc' 'abcdef''bc'

19 PythonSlicing For consistency, text[1:1] is the empty string

20 PythonSlicing For consistency, text[1:1] is the empty string –From index 1 up to (but not including) index 1

21 PythonSlicing 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 string

22 PythonSlicing 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 string –Not the reverse of text[1:3]

23 PythonSlicing 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 string –Not the reverse of text[1:3] But text[1:-1] is everything except the first and last characters

24 PythonSlicing Slicing always creates a new collection

25 PythonSlicing Slicing always creates a new collection Beware of aliasing

26 PythonSlicing Slicing always creates a new collection Beware of aliasing >>> points = [[10, 10], [20, 20], [30, 30], [40, 40]] >>>

27 PythonSlicing Slicing always creates a new collection Beware of aliasing >>> points = [[10, 10], [20, 20], [30, 30], [40, 40]] >>> middle = points[1:-1] >>>

28 PythonSlicing Slicing always creates a new collection Beware of aliasing >>> points = [[10, 10], [20, 20], [30, 30], [40, 40]] >>> middle = points[1:-1] >>> middle[0][0] = 'whoops' >>>

29 PythonSlicing Slicing always creates a new collection Beware of aliasing >>> points = [[10, 10], [20, 20], [30, 30], [40, 40]] >>> middle = points[1:-1] >>> middle[0][0] = 'whoops' >>> middle[1][0] = 'aliasing' >>>

30 PythonSlicing Slicing always creates a new collection Beware of aliasing >>> 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]] >>>

31 PythonSlicing Slicing always creates a new collection Beware of aliasing >>> 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 [[10, 10], ['whoops', 20], ['aliasing', 30], [40, 40]] >>>

32 PythonSlicing 10 20 30 40 points

33 PythonSlicing 10 20 30 40 points middle

34 PythonSlicing 10 20 30 40 points middle 'whoops'

35 PythonSlicing 10 20 30 40 points middle 'whoops' 'aliasing'

36 October 2010 created by Greg Wilson Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.


Download ppt "Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See"

Similar presentations


Ads by Google