Your SlideShare is downloading. ×
Python & Perl: Lecture 04
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Python & Perl: Lecture 04

971
views

Published on

1) Types in Python …

1) Types in Python
2) Built-in Sequnces
3) Python Lists

Published in: Technology, Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
971
On Slideshare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Python & Perl Lecture 04 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Types in Python – Dynamic and strong typing – Duck typing – Checking object types ● Built-in Sequences – Types and Operations ● List: Pythons Workhorsewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 3. Types in Pythonwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 4. Strong and Dynamic Typing ● Python is both strongly and dynamically typed ● Strong typing means that every object has a specific type ● Variables contain references to objects of different types ● Dynamic typing means that types of objects pointed to by variables are inferred at run time ● There is no contradiction b/w strong and dynamic typing: they describe two different features of programming lan- guageswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 5. Duck Typing ● Sometimes Python is called a duck typing language ● The gist of duck typing: real types of objects do not mat- ter so long as specific operations can be performed on them ● Objects themselves “know” whether they can be added, multiplied, concatenated, etc ● If and when objects cannot perform a requested opera- tion, a run-time error occurswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 6. Checking Object Types ● There are two methods in Python for checking object types: type(<object>) and isinstance(<object>, <type>) >>> type([1, 2]) <type list> >>> isinstance([1, 2], list) True >>> type((1, 2)) <type tuple> >>> isinstance((1, 2), tuple) True >>> isinstance((1, 2), type((1, 2))) Truewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 7. Built-In Sequenceswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 8. Data Structures ● A data structure is a collection of data items organized in a specific way ● A data structure = data items + operations to manipu- late them ● The most basic Python data structure is the sequence ● Each element of a sequence is assigned its position in the sequence (aka the elements index)www.youtube.com/vkedco www.vkedco.blogspot.com
  • 9. Python Built-In Sequences ● There are six sequences in Python: – list – tuple – string (str) – unicode string (unicode) – buffer – xrangewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 10. Side Note on Sequence Indexing ● In Python sequences, elements can be indexed left to right and right to left ● If s is a sequence, then the leftmost element is s[0] while the rightmost element is s[-1] ● In general, if s is a sequence of n elements, then s[0] == s[-n], s[1] == s[-n+1], …, s[n-1] == s[-1]www.youtube.com/vkedco www.vkedco.blogspot.com
  • 11. Sequence Indexing Example >>> s = [a, b, c, d, e] ### s is a list of 5 characters >>> s[0] == s[-5] ### s[0] == s[-5] == a True >>> s[1] == s[-5+1] == s[-4] ### s[1] == s[-4] == b True >>> s[2] == s[-5+2] == s[-3] ### s[2] == s[-3] == c True >>> s[3] == s[-5+3] == s[-2] ### s[3] == s[-2] == d True >>> s[4] == s[-5+4] == s[-1] ### s[4] == s[-1] == e Truewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 12. Sequence Operations ● All sequences support the following operations: – Indexing – Membership – Concatenation – Multiplication – Slicing – Length – Minimum/Maximum elementwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 13. List Pythons Workhorsewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 14. General Facts ● Lists are mutable type sequences ● Lists contain 0 or more references to objects ● Lists can contain references to different types of objectswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 15. Construction# lst1 is an empty list>>> lst1 = []>>> lst1[]# lst2 is a list of integers.>>> lst2 = [1, 2, 3, -4, 0, -5, 7]>>> lst2[1, 2, 3, -4, 0, -5, 7] www.vkedco.blogspot.com
  • 16. Construction # lst3 is a mixed list. >>> lst3 = [1, a, rock violin, 3.14, "math"] >>> lst3 [1, a, rock violin, 3.14, math] # lst4 is a nested list. >>> lst4 = [1, [2, 3], 4, 5] # lst5 is another nested list. >>> lst5 = [1, lst3] >>> lst5 [1, [1, a, rock violin, 3.14, math]]www.youtube.com/vkedco www.vkedco.blogspot.com
  • 17. Construction ● list() function can be used as a constructor to make empty lists or convert other sequences to lists >>> x = list() >>> x [ ] >>> y = list(“abc”) >>> y [a, b, c]www.youtube.com/vkedco www.vkedco.blogspot.com
  • 18. Indexing ● Use the square brackets [ and ] to index into lists ● Since lists a sequences, left-to-right indexing starts at 0 >>> lst = [1, 2, 3] >>> lst[0] 1 >>> lst[1] 2 >>> lst[2] 3www.youtube.com/vkedco www.vkedco.blogspot.com
  • 19. Indexing ● Right-to-left indexing starts with -1 and ends with -n, where n is the number of elements in the list >>> lst = [1, 2, 3] >>> lst[-1] # 1st element from right 3 >>> lst[-2] # 2nd element from right 2 >>> lst[-3] # 3rd element from right 1www.youtube.com/vkedco www.vkedco.blogspot.com
  • 20. Out of Range Indexing ● Both positive and negative indices result in errors if they go off on either side of the list >>> lst = [1, 2, 3] >>> lst[3] out of range error >>> lst[-4] out of range errorwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 21. Membership ● If x is an object and lst is a list, then x in lst returns True if x is an element of lst, else False is returned >>> lst = [10, eggs, 3] >>> 10 in lst True >>> eggs in lst True >>> buzz in lst Falsewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 22. Membership ● If x is an object and lst is a list, then x not in lst returns True if x is not an element of lst, else False >>> lst = [10, eggs, 3] >>> 11 not in lst True >>> eggs not in lst False >>> buzz not in lst Truewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 23. Membership ● Membership can be tested on nested lists >>> lst = [[one, 1], [two, 2], [three, 3]] >>> [one, 1] in lst True >>> [two, 2] in lst True >>> [three, 3] in lst Truewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 24. Side Note On NOT ● If you want to test if the negation of a boolean ex- pression is true or false, you can use not in front that expression >>> not 1 == 2 True >>> not 1 == 1 False >>> not abc == efg True >>> 1 != 2 ## this works as wellwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 25. Concatenation ● If x and y are lists, then x + y is their concatena- tion, i.e. the list that consists of xs elements fol- lowed by ys elements >>> x = [10, eggs, 3] >>> y = [12, buzz, 5] >>> z = x + y >>> z [10, eggs, 3, 12, buzz, 5]www.youtube.com/vkedco www.vkedco.blogspot.com
  • 26. Multiplication ● If x is a list and n is an integer, then x * n and n * x is the list that consists of n shallow copies (copies of references to x) of x >>> x = [1, 2] >>> y = [a, b] >>> z = [x, y] >>> z [[1, 2], [a, b]] >>> z2 = z * 2 >>> z2 [[1, 2], [a, b], [1, 2], [a, b]]www.youtube.com/vkedco www.vkedco.blogspot.com
  • 27. Slicing ● Slicing is an operation that accesses a range of ele- ments in a sequence ● When the length of the range is 1, slicing is equiva- lent to indexing ● A Slice is defined by two indexes: the start index is the index of the first element; the end index is the index of the first element that immediately follows the last element of the slice ● The start index is inclusive and the end index is ex- clusivewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 28. Slicing Example >>> lst = [a, b, c, d, e, f, g] >>> lst[0:1] [a] >>> lst[0:2] [a, b] >>> lst[1:2] [b] >>> lst[3:7] [d, e, f, g]www.youtube.com/vkedco www.vkedco.blogspot.com
  • 29. Slicing ● Omission of both indexes slices the entire list >>> lst = [a, b, c, d, e, f, g] >>> lst[:] ## same as lst[0:7] [a, b, c, d, e, f, g] ● Omitted start index defaults to 0 >>> lst[:3] ## same as lst[0:3] [a, b, c] ● Omitted end index defaults to the one right after the last index in the sequence >>> lst[3:] ## same as lst[3:7] [d, e, f, g]www.youtube.com/vkedco www.vkedco.blogspot.com
  • 30. Regular and Irregular Slicing ● In regular slicing, slices are constructed by moving from one element to the next (step of 1) ● In irregular slicing, slices are constructed by moving from one element to every n-th element where n is the length of the step >>> lst = [a, b, c, d, e, f, g] >>> lst[0:7:2] ## every 2-nd element beginning from a [a, c, e, g] >>> lst[0:7:3] ## every 3-rd element beginning from a [a, d, g]www.youtube.com/vkedco www.vkedco.blogspot.com
  • 31. Length, Minimum, Maximum ● These are self explanatory >>> lst = [a, b, c] >>> len(lst) 3 >>> min(lst) a >>> max(lst) cwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 32. Multi-Dimensional Lists ● It is possible to construct multi-dimensional lists ● Here is an example of a 2D list (aka matrix) >>> matrix = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] >>> matrix[0] [0, 1, 2] >>> matrix[1] [3, 4, 5] >>> matrix[0][0] 0 >>> matrix[0][2] 2www.youtube.com/vkedco www.vkedco.blogspot.com
  • 33. Slice Assignment ● It is possible to assign into slices >>> lst = list(abcde) >>> lst[2:2] = [1, 2, 3] >>> lst [a, b, 1, 2, 3, c, d, e] >>> lst[2:4] = X * 5 >>> lst [a, b, X, X, X, X, X, 3, c, d, e]www.youtube.com/vkedco www.vkedco.blogspot.com
  • 34. Deleting Elements ● Delete individual element >>> lst = [1, 2, 3, 4, 5] >>> del lst[1] ## deleting 2 >>> lst [1, 3, 4, 5] ● Delete a slice >>> del lst[1:3] >>> lst [1, 5] ● Assign an empty list to a slice to delete it >>> lst[0:2] = []www.youtube.com/vkedco www.vkedco.blogspot.com
  • 35. Reading & References ● www.python.org ● http://docs.python.org/library/stdtypes.html#typesseq ● doc.python.org/howto/unicode.html ● Ch 02, M. L. Hetland. Beginning Python From Novice to Professional, 2nd Ed., APRESSwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 36. Feedback Bugs, comments to vladimir dot kulyukin at usu dot eduwww.youtube.com/vkedco www.vkedco.blogspot.com