Python - Lecture 2 Fixed
Transcript of Python - Lecture 2 Fixed
-
8/9/2019 Python - Lecture 2 Fixed
1/83
Python
67557Lecture 2
CS.HUJIFall 2014 -ה"תשע
Dr. Arie Schlesinger
-
8/9/2019 Python - Lecture 2 Fixed
2/83
Containers of data
Motivation: keep data and processing
tools ‘close by’,
11/8/2014 10:37:29 PM 2
-
8/9/2019 Python - Lecture 2 Fixed
3/83
Some classifications
• Simple objects, ףס נ ק ריפל םינתינ :ל
int, float, bool (non iterable)
• Containers of objects : (iterable)
str, list, tuple, range, set, frozenset, dict…
11/8/2014 10:37:29 PM 3
-
8/9/2019 Python - Lecture 2 Fixed
4/83
Order• Sequences: ordered Containers
• A sequence is a container whose elements are ordered
and can be accessed by their position index, starting
from 0. Example, the 1st element in the string:
>>> s=‘abc’ >>> s[0]
‘a’
• Containers: strings, lists, tuples, range,..
• Not Sequences-unordered Containers
- Can not index their elements: sets, frozensets, dictionaries11/8/2014 10:37:29 PM 4
-
8/9/2019 Python - Lecture 2 Fixed
5/83
Freedom
• Mutable: list, set, dictionary
- Contents can be changed :
- add , remove, change elements.
• Immutable: strings, tuples, frozenset, range
- No changes can be made
(Not containers: int, float, bool )
11/8/2014 10:37:29 PM 5
l
-
8/9/2019 Python - Lecture 2 Fixed
6/83
The two titles every containerholds
there are more
• Sequence, Mutable: list
• Sequence, Immutable: string, tuple, range
• Not sequence, Mutable : set, dictionary
• Not sequence, Immutable : frozenset
11/8/2014 10:37:29 PM 6
-
8/9/2019 Python - Lecture 2 Fixed
7/83
What about the titles of thenon containers ?
• No Sequence, Immutable : int, float, bool
(no order, no freedom to change)
11/8/2014 10:37:29 PM 7
-
8/9/2019 Python - Lecture 2 Fixed
8/83
sequences
strings, lists, tuples, range
11/8/2014 10:37:29 PM 8
-
8/9/2019 Python - Lecture 2 Fixed
9/83
SequencesTuples
Contain 0 or more items of mixed types, including othercontainers – Immutable
Strings
Contain a seq of 0 or more single chars – Immutable
Lists
Contain a seq of 0 or more items of mixed types, includingother containers – Mutable
Ranges
Contain a seq of 0 or more ordered integers ! - immutable11/8/2014 10:37:29 PM 9
-
8/9/2019 Python - Lecture 2 Fixed
10/83
Similar Syntax
•The three sequences types : tuples, strings, andlists, have similar syntax and functionality
Therefore most operations on all sequences, can be
demonstrated on one seq, to illustrate the others
11/8/2014 10:37:29 PM 10
-
8/9/2019 Python - Lecture 2 Fixed
11/83
Sequences• Tuples : defined using parentheses, and commas.
>>> tup = (12, 'bcd', 3.45, (1,2), 'efg')
• Lists : defined using square brackets, and commas.
>>> liz = ["bcd", 23, 3.23, 12]
• Strings are defined using quotes (", ', or """).
>>> st = "abc"
>>> st = 'abc'
>>> st = """an exampleof a multi-line string that uses triple quotes."""
• Empty seq: [], (), ''
11/8/2014 10:37:29 PM 11
-
8/9/2019 Python - Lecture 2 Fixed
12/83
Sequence indexing
11/8/2014 10:37:29 PM 12
-
8/9/2019 Python - Lecture 2 Fixed
13/83
Indexing• You can access individual elements of a tuple, list, or string
using square bracket indexing notation, ( 0 based…)>>> tup = (12, ‘bcd’, 3.45, (1,2), ‘efg’)
>>> tup[1] # 2nd element in the tuple.
‘bcd’
>>> liz = [“bcd”, 23, 3.23, 12]
>>> liz[1] # 2nd element in the list.
23
>>> st = “bcd”
>>> st[1] # 2nd char in string.
‘c’
The index is an expression that evaluates to
an int11/8/2014 10:37:29 PM 13
-
8/9/2019 Python - Lecture 2 Fixed
14/83
Positive and negative indices
>>> tup = (12, ‘bcd’, 3.45, (1,2), ‘efg’)
• Positive index: count from the left , starting with 0.
>>> tup[1]‘bcd’
• Negative index:count from right , starting with the lastelement, represented by index: -1
>>> tup[-3] # the 3rd from the end
3.4511/8/2014 10:37:29 PM 14
-
8/9/2019 Python - Lecture 2 Fixed
15/83
Sequence slicing
11/8/2014 10:37:29 PM 15
-
8/9/2019 Python - Lecture 2 Fixed
16/83
Slicing>>> tup = (12, ‘bcd’, 3.45, (1,2), ‘efg’)
>>> t[1:4]
(‘bcd’, 3.45, (1,2))
• Return a same-class-container, with a subset of the
original members.• It starts copying from the first index, and stops
copying before the second index.
• negative indices can be used :
>>> t[1:-1]
(‘bcd’, 3.45, (1,2))
11/8/2014 10:37:29 PM 16
-
8/9/2019 Python - Lecture 2 Fixed
17/83
Slicing defaults
>>> tup = (12, ‘bcd’, 3.45, (1,2), ‘efg’)
• Without a first index, it starts from position 0:
>>> t[:2]
(12, ‘bcd’)
• Without the second index, it starts at the first index
until end of the container.
>>> t[2:](3.45, (1,2), ‘efg’)
11/8/2014 10:37:29 PM 17
-
8/9/2019 Python - Lecture 2 Fixed
18/83
What indexing, and slicing return
Strings return strings:
Indexing: returns the one-char string at index pos.
Slicing : returns the substring specified (a new string)
Lists, tuples return:
Indexing: returns the element at index positionSlicing : returns a new seq as specified
(element can be simple or compound: int,string, list ,…)
11/8/2014 10:37:29 PM 18
-
8/9/2019 Python - Lecture 2 Fixed
19/83
indexing slicing doc
http://docs.python.org/2/library/stdtypes.html#
sequence-types-list-tuple-range
11/8/2014 10:37:29 PM 19
-
8/9/2019 Python - Lecture 2 Fixed
20/83
Copying the Whole Sequence• [:] makes a copy of an entire sequence.
>>> tup[:](12, ‘bcd’, 3.45, (1,2), ‘efg’)
>>> list1=[1,[2,3],4]• Two diff names of one seq:
>>> list2 = list1 # 2 names refer to 1 ref
# NEW: Changing one affects both Shallow copy : Two diff names of two diff seq
>>> list3 = list1[:]
(copying will b back..) 11/8/2014 10:37:29 PM 20
-
8/9/2019 Python - Lecture 2 Fixed
21/83
The ‘in’ Operator
•membership test , returns True or False :
>>> t = [1, 2, 4, 5]
>>> 3 in t
False>>> 4 in t
True
>>> 4 not in t
False
11/8/2014 10:37:29 PM 21
-
8/9/2019 Python - Lecture 2 Fixed
22/83
Membership for strings• tests for substrings of 1 or more adjacent chars
>>> a = 'abcde'>>> 'c' in a
True
>>> 'cd' in a
True
>>> 'ac' in a
False
• Careful: the in keyword is also used in the syntax of for loopsand list comprehensions.11/8/2014 10:37:29 PM 22
-
8/9/2019 Python - Lecture 2 Fixed
23/83
The + Operator• The + operator produces a new tuple, list, or
string whose value is the concatenation of itsarguments.
>>> (1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> “The” + “ ” + “Beatles”
‘The Beatles’
11/8/2014 10:37:29 PM 23
-
8/9/2019 Python - Lecture 2 Fixed
24/83
-
8/9/2019 Python - Lecture 2 Fixed
25/83
Mutability:
Tuples vs. Lists
11/8/2014 10:37:29 PM 25
T l I t bl
-
8/9/2019 Python - Lecture 2 Fixed
26/83
Tuples: Immutable
>>> tup = (12, ‘bcd’, 3.45, (1,2), ‘efg’)
>>> tup[2] = 2…TypeError: object doesn't support item assignment
You can’t change a tuple.
You can make a fresh tuple and assign its
reference to a previously used name.
>>> tup = (12, ‘bcd’, 2, (1,2), ‘efg’)
After the assignment, tup is now the name of the new
tuple11/8/2014 10:37:29 PM 26
-
8/9/2019 Python - Lecture 2 Fixed
27/83
Lists: Mutable• Lists are mutable: it is possible to change their
content (strings are immutable)
• Assign to a legal position:
>>> liz = [‘abc’, 12, 4.34, 23]
>>> liz[1] = 35 # assignment
>>> liz[‘abc’, 35, 4.34, 23]
11/8/2014 10:37:29 PM 27
-
8/9/2019 Python - Lecture 2 Fixed
28/83
More examples
>>> myList
[1, 'a', -2.7]
>>> myList[0]='Iosi'
>>> myList
['Iosi', 'a', -2.7]
• We can change lists in place.
• myList is still the name of the same container
at the same address ,when we’re done.11/8/2014 10:37:29 PM 28
-
8/9/2019 Python - Lecture 2 Fixed
29/83
Assignment to slices…1
• Assignment to slices can even change the size of
the list or clear it entirely:
>>> letters =['a’,'b’,'c’,'d’,'e’,'f’,'g']
# replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
11/8/2014 10:37:29 PM 29
-
8/9/2019 Python - Lecture 2 Fixed
30/83
Assignment to slices…2# now remove them
>>> letters[2:5] = []>>> letters
['a', 'b', 'f', 'g']
# clear the list by replacing all the elements with anempty list
>>> letters[:] = []
>>> letters[]
([] is the ‘destroyer tool’)
11/8/2014 10:37:29 PM 30
-
8/9/2019 Python - Lecture 2 Fixed
31/83
len() works with all sequences
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
11/8/2014 10:37:29 PM 31
nesting lists
-
8/9/2019 Python - Lecture 2 Fixed
32/83
nesting lists
• Create lists containing other lists:
>>> a = ['a', 'b', 'c']>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c'] # list a>>> x[0][1]
'b'
11/8/2014 10:37:29 PM 32
For your lists Only 1
-
8/9/2019 Python - Lecture 2 Fixed
33/83
For your lists Only..1
>>> liz1 = [1, 1, 3, 4, 5]>>> liz1.append(‘x’) # a method of lists
>>> liz1
[1, 1, 3, 4, 5, ‘x’]
>>> liz1.insert(2, ‘y’)
>>>liz1 [1, 1, ‘y’, 3, 4, 5, ‘x’]
11/8/2014 10:37:29 PM 33
The extend method vs the
-
8/9/2019 Python - Lecture 2 Fixed
34/83
The extend method vs theoperator +
• + creates a fresh list (with a new memoryreference)
• extend operates on liz1 in place.
>>> liz1.extend([9, 8, 7])
>>> liz1
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7]
11/8/2014 10:37:29 PM 34
-
8/9/2019 Python - Lecture 2 Fixed
35/83
Append
>>> x = [1, 2, 3]
>>> x.append([4, 5])
>>> x # appends its arg packed as it comes
[1, 2, 3, [4, 5]]
11/8/2014 10:37:29 PM 35
-
8/9/2019 Python - Lecture 2 Fixed
36/83
Extend
>>> x = [1, 2, 3]
>>> x.extend([4, 5])
>>> x # extend unpacks its arg
[1, 2, 3, 4, 5]
11/8/2014 10:37:29 PM 36
For your lists Only 2
-
8/9/2019 Python - Lecture 2 Fixed
37/83
For your lists Only ..2
>>> liz4 = [‘a’, ‘b’, ‘c’, ‘b’]
>>> liz4.index(‘b’)
1
>>> liz4.count(‘b’)
2
>>> liz4.remove(‘b’)
>>> liz4[‘a’, ‘c’, ‘b’]
11/8/2014 10:37:29 PM 37
For your lists Only 3
-
8/9/2019 Python - Lecture 2 Fixed
38/83
For your lists Only ..3>>> li = [5, 2, 6, 8]
>>> li.reverse() # reverse the list in place>>> li
[8, 6, 2, 5]
>>> li.sort() # sort the list in place
>>> li
[2, 5, 6, 8]
11/8/2014 10:37:29 PM 38
l
-
8/9/2019 Python - Lecture 2 Fixed
39/83
Tuples vs. Lists
• Lists can be modified, and they have lots of
handy operations we can perform on them.
• Tuples are immutable and have fewer
features.
• To convert between tuples and lists use the
list() and tuple() functions:
>>> li = list(tu)>>> tu = tuple(li)
11/8/2014 10:37:29 PM 39
M li t() t l ()
-
8/9/2019 Python - Lecture 2 Fixed
40/83
More on list(),tuple() • list(iterable), tuple(iterable) are constructors that
decompose their arg and create a list/tuple from itselements:
>>> list('abc')
['a', 'b', 'c']
>>> list([1,2])
[1, 2]
>>> list((11,22))
[11, 22]
>>> list(12)
…TypeError: 'int' object is not iterable
(other types have similar constructors: set(), dict()…
which decompose some iterable and use its elems)
-
8/9/2019 Python - Lecture 2 Fixed
41/83
The magic of tuples
-
8/9/2019 Python - Lecture 2 Fixed
42/83
The Magic of Tuples
>>> a=(1,2)
>>> b=1,2 # a tuple without parens
>>> a
(1, 2)
>>> b
(1, 2)
42
-
8/9/2019 Python - Lecture 2 Fixed
43/83
From Tuples Unpacking to Swap
>>> x,y=(1,2) # tuple unpacking>>> x
1
>>> y
2
>>> x,y=11,22 # also tuple unpacking
>>> x
11
>>> y
22
43
-
8/9/2019 Python - Lecture 2 Fixed
44/83
Swap
>>> x,y=y,x # swap is a tuple unpacking !!>>> x
22
>>> y11
>>> x,y # just writing like that, creates a tuple(22,11)
44
-
8/9/2019 Python - Lecture 2 Fixed
45/83
-
8/9/2019 Python - Lecture 2 Fixed
46/83
From Swap de luxe to .. Fibonacci
>>> a=0; b=1
>>> for i in range(5):
a, b = b, a+b
print b,
1 2 3 5 8
46
-
8/9/2019 Python - Lecture 2 Fixed
47/83
The range() Function
11/8/2014 10:37:29 PM 47
http://docs.python.org/3/library/stdtypes.html%23rangehttp://docs.python.org/3/library/stdtypes.html%23range
-
8/9/2019 Python - Lecture 2 Fixed
48/83
range,xrange• The built-in function range([i,]j [,step])
creates an object that represents a range of integersk such that i
-
8/9/2019 Python - Lecture 2 Fixed
49/83
-
8/9/2019 Python - Lecture 2 Fixed
50/83
‘Les Iterables’
Iterable: An object(container)
capable of returning its
members one at a time.
11/8/2014 10:37:29 PM 50
Wh i it bl ?
-
8/9/2019 Python - Lecture 2 Fixed
51/83
Who is iterable?
• all sequence types (like list, str, tuple, range)
• some non-sequence types like set,
frozenset, dict, file objects,
• Can be used in a for loop and in other places
where sequences are employed
11/8/2014 10:37:29 PM 51
http://docs.python.org/3/library/stdtypes.html%23listhttp://docs.python.org/3/library/stdtypes.html%23strhttp://docs.python.org/3/library/stdtypes.html%23tuplehttp://docs.python.org/3/library/stdtypes.html%23dicthttp://docs.python.org/3/glossary.html%23term-file-objecthttp://docs.python.org/3/reference/compound_stmts.html%23forhttp://docs.python.org/3/reference/compound_stmts.html%23forhttp://docs.python.org/3/glossary.html%23term-file-objecthttp://docs.python.org/3/library/stdtypes.html%23dicthttp://docs.python.org/3/library/stdtypes.html%23tuplehttp://docs.python.org/3/library/stdtypes.html%23strhttp://docs.python.org/3/library/stdtypes.html%23list
-
8/9/2019 Python - Lecture 2 Fixed
52/83
Iterators
• An iterator represents a stream of data
• the built-in function iter(),
- takes an iterable object as argument- returns an iterator for the object. ( 'של' )
• to get successive items in the stream.
- use the built-in function next()
- is good for one pass over the set of values.
11/8/2014 10:37:29 PM 52
Iterator examples
http://docs.python.org/3/library/functions.html%23iterhttp://docs.python.org/3/library/functions.html%23nexthttp://docs.python.org/3/library/functions.html%23nexthttp://docs.python.org/3/library/functions.html%23iter
-
8/9/2019 Python - Lecture 2 Fixed
53/83
Iterator examples>>> s='abc' # s is iterable
>>> its=iter(s) # iter(s) creates an iterator to s
>>> its.next() # next(its) returns the next item in s
'a'
>>> its.next()
'b'>>> its.next()
'c’
>>> its.next() # iterator is exausted - no more elements
…Error…StopIteration
To do it again repeat(refill) its=iter(…)
11/8/2014 10:37:29 PM 53
-
8/9/2019 Python - Lecture 2 Fixed
54/83
-
8/9/2019 Python - Lecture 2 Fixed
55/83
References and Copies
About assignments
-
8/9/2019 Python - Lecture 2 Fixed
56/83
About assignments
• Magic integers (יור ק ע ):-5
256 ..singletons)
>>> x=256
>>> y=256
>>> print id(x),id(y)
28190492 28190492
-
8/9/2019 Python - Lecture 2 Fixed
57/83
Noיור ק ע for floats
>>> x=256.0
>>> y=256.0
>>> print id(x),id(y)
36792004 36791924
nor for ints out of
-
8/9/2019 Python - Lecture 2 Fixed
58/83
nor for ints out of[-5 …256]
>>> x=257
>>> y=257
>>> print id(x),id(y)
11733080 28387720
Reference alias
-
8/9/2019 Python - Lecture 2 Fixed
59/83
Reference, alias
• When a program makes an assignment suchas a = b, a new reference to b is created.
>>> b = 257
>>> a = b
>>> print id(a), id(b)
28387672 28387672
>>> a is b # are the ids equal ?True
>>> a==b # are the values equal ?
True
-
8/9/2019 Python - Lecture 2 Fixed
60/83
What abo t fo st i gs?
http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413
-
8/9/2019 Python - Lecture 2 Fixed
61/83
What aboutיור ק ע for strings?
>>> x='a'
>>> y='a'
>>> print id(x),id(y)
20008472 20008472
>>> x='ojuwriuwrvpiurvpibpihv'
>>> y='ojuwriuwrvpiurvpibpihv'>>> print id(x),id(y)
32475184 32475184
A d h t b t li t t l
http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413http://moodle.cs.huji.ac.il/cs13/mod/forum/discuss.php?d=6413
-
8/9/2019 Python - Lecture 2 Fixed
62/83
And what about lists, or tuples
>>> x=[1,2,3]
>>> y=[1,2,3]
>>> print id(x),id(y)
32502248 32489384
>>> x=(1,2,3)
>>> y=(1,2,3)>>> print id(x),id(y)
32503248 32502368
-
8/9/2019 Python - Lecture 2 Fixed
63/83
Mutable objects behaviour
• The behavior is different for mutable objects such as lists and
dictionaries. Example:
>>> a = [1,2,3,4]
>>> b = a # b is a reference to a
>>> b is a
True
>>> a == b
True
Changes in an ‘alias’ copy
-
8/9/2019 Python - Lecture 2 Fixed
64/83
Changes in an alias copy
>>> b[2] = -100 # Change an element in b
>>> a # Notice how a also changed
[1, 2, -100, 4]
>>> b
[1, 2, -100, 4]
>>> a is b
True
>>> a==b
True
What about real copies
-
8/9/2019 Python - Lecture 2 Fixed
65/83
What about real copies
•Here a and b refer to the same object, so a changemade to the obj thru one of the vars is seen thru theother.
We need copies that can be changed without changing
the original: not a new reference
shallow , deep copies
-
8/9/2019 Python - Lecture 2 Fixed
66/83
shallow , deep copies• There are 2 diff types of copy operations for container
objects, such as lists and dictionaries: shallow copy,
deep copy.
• A shallow copy:
- creates a new object
- fills it with references to the items contained in the originalobject . Example:
>>> a = [ 1, 2, [3,4] ]
>>> b = list(a)
# Create a shallow copy of a.
>>> b is a
False
Shallow copies ‘merits’
-
8/9/2019 Python - Lecture 2 Fixed
67/83
Shallow copies merits
>>> b.append(100) # Append element to b.
>>> b[1, 2, [3, 4], 100]
>>> a # Notice that a is unchanged[1, 2, [3, 4]]
>>> b[2][0] = -100 # Modify an element inside b
>>> b
[1, 2, [-100, 4], 100]
>>> a # Notice the change inside a
[1, 2, [-100, 4]]
How it works
-
8/9/2019 Python - Lecture 2 Fixed
68/83
How it works
• In this case, a and b are separate list objects, but
the elements they contain are shared.
• Therefore, a modification to one of the elements
of a also modifies an element of b, as shown.
• A deep copy creates a new object and recursively
copies all the objects it contains.
• There is no built-in operation to create deep
copies of objects.
copy.deepcopy()
-
8/9/2019 Python - Lecture 2 Fixed
69/83
copy deepcopy()
• The copy.deepcopy() function in copy module doesit, example:
>>> import copy
>>> a = [1, 2, [3, 4]]
>>> b = copy.deepcopy(a)
>>> print a is b, a==b # False, True
>>> b[2][0] = -100
>>> b
[1, 2, [-100, 4]]
>>> a
[1, 2, [3, 4]] # a is not changed(problems: when some element recursively points to itself)
-
8/9/2019 Python - Lecture 2 Fixed
70/83
Three ways to shallow
• Each of the follows produces a shallow copy of
iterable x:
y=list(x)
y=x[:]
y=copy.copy(x)
-
8/9/2019 Python - Lecture 2 Fixed
71/83
First class objects
First-Class Objects
-
8/9/2019 Python - Lecture 2 Fixed
72/83
j
• All objects in Python are“first class”,
meaning that:
- all objects that can be named by an identifier,
have equal status.
- all objects that can be named, can be treated
as data.
First class Example
-
8/9/2019 Python - Lecture 2 Fixed
73/83
pA simple dictionary with two values:
items = { 'number' : 42
'text' : "Hello World"}
Let’s add some ‘special’ items to this dictionary.
items["func"] = abs # Add the abs() functionimport math
items["mod"] = math # Add a module
items["error"] = ValueError # Add an exception type
nums = [1,2,3,4]
items["append"] = nums.append # Add a meth of another object
First class example
-
8/9/2019 Python - Lecture 2 Fixed
74/83
First class example
• The items dictionary contains: a function, a module, an
exception, and a method of another object .
• Use the keys as the prev items, Example:
>>> items["func"](-5) # Executes abs(-5)
5
>>> items["mod"].sqrt(9) # Executes math.sqrt(9)
3.0
>>>try:
x = int("Iosi")
except items["error"] as e: # Same as except ValueError as e
print "Couldn't convert"
Couldn't convert
i l l
-
8/9/2019 Python - Lecture 2 Fixed
75/83
First class Example cont.
>>> items["append"](‘Ety’)
>>> nums
[1, 2, 3, 4, ‘Ety’]
•Writing very compact code is possible because
everything in Python is first-class.
h i l l
-
8/9/2019 Python - Lecture 2 Fixed
76/83
Another First class example
• Given a line of text such as:
"Iosi, 1, 3.14, Malka"
let’s convert it into a list of elements with the right
type-conversion.
- create a list of types (which are first-class objects)
- execute a few simple list processing operations:
C
-
8/9/2019 Python - Lecture 2 Fixed
77/83
Cont. >>> line = "Iosi, 1, 3.14, Malka"
>>> field_types = [str, int, float, str]
>>> raw_fields = line.split(',')
>>> fields = [ty(val) for ty,val in
zip(field_types,raw_fields)]
>>> fields
['Iosi', 1, 3.14, 'Malka']
-
8/9/2019 Python - Lecture 2 Fixed
78/83
Aliasing function names
Did you know ?
-
8/9/2019 Python - Lecture 2 Fixed
79/83
Did you know ?
You can bind a name to a ‘known respectable’
function, and use it instead :
>>> Iosi=len
>>> Iosi('abc')3
>>> Ety = type
>>> Ety(2)
(Just don’t forget the new names)11/8/2014 10:37:29 PM 79
Write your functions
-
8/9/2019 Python - Lecture 2 Fixed
80/83
y>>> def mySquare(x) : # function declaration
return x*x
>>> mySquare(7) # function call
49
>>> def mySquarePlusAnything(x,anything):
return x*x+mySquare(anything)
>>> mySquarePlusAnything(6,4) # function call
50
11/8/2014 10:37:29 PM 80
-
8/9/2019 Python - Lecture 2 Fixed
81/83
elsei ng with the loops
In python
Loops (for,while)may havean else part
Else and the loops
-
8/9/2019 Python - Lecture 2 Fixed
82/83
Else and the loops
• The else clause of a loop is executed only if :
- the loop ends naturally, or if
- the loop did not start/execute at all.
• If the loop is un-naturally terminated by a
break statement (or by some exception),
- the else clause is skipped.
Why: As a simple signal of what happened in the
loop.11/8/2014 10:37:29 PM 82
l l
-
8/9/2019 Python - Lecture 2 Fixed
83/83
else example
while value < limit:
if value == x:
print "Found it!"
break
value+=y
else: # see the else indentationprint ”x it’s not there ! "