Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro2cs Tirgul 4.

Similar presentations


Presentation on theme: "Intro2cs Tirgul 4."— Presentation transcript:

1 Intro2cs Tirgul 4

2 What we will cover today?
Sequences - reminder Tuples Mutable and Immutable Aliasing Shallow and Deepcopy Function Variables

3 README(1) README(s) are simple
They are used everywhere (academy and the market) They are there for the future readers of the code In intro we ask you for quite a simple README

4 README(2) cs username ID real name
Those are my friends that I consulted with regarding the ex Edit the name of the ex and its number This is how to run my program from a terminal Fill here a general description of your program - This program computes the expected time of end of the world It does it based on the Algorithm developed by the Nostradamus I preferred this implementation, since the Hollywood algorithm is boring and too long List of submitted files If needed so (not obligatory)

5 Reminder - Sequences (1)
The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so on.

6 Sequences (2) Python has several types of sequences, for example: list, tuple, str, range. All share the following features: • Each sequence contains a finite number of elements. • The elements are arranged in some order in the sequence. • It is possible to iterate over the elements in the sequence, according to their order. • It is also possible to check if an element is in a sequence.

7 Sequences (3) There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements, as we will see later.

8 Casting empty sequences is always false
bool (0) == False bool (0.0) == False bool ("") == False bool ([]) == False

9 enumerate Enumerate returns a couple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence. Share the advantages of both: for i in range(len(lst)): for item in lst:

10 Enumerate(2) Enumerate returns a couple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence. >>> seasons = ['Spring', 'Summer ', 'Fall', 'Winter'] >>> for (i, season) in enumerate(seasons): ... print ("index: ", i, ", value: ", season) index: 0, value: Spring index: 1, value: Summer index: 2, value: Fall index: 3, value: Winter

11 Mutable and Immutable The value of some objects can be changed.
Objects whose value can be changed are said to be mutable; objects whose value is unchangeable once they are created are called immutable. An object’s mutability is determined by its type; for instance, numbers, strings, ranges and tuples are immutable, while lists are mutable.

12 Python Tuples A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only differences are that tuples can't be changed i.e., tuples are immutable, and tuples use parentheses instead of square brackets in lists.

13 Creating a tuple (1) Creating a tuple is as simple as putting different comma- separated values and optionally you can put these comma- separated values between parentheses also. For example: >>> tup1 = ('physics', 'chemistry', 1997, 2000) >>> tup2 = (1,2,3,4,5) >>> tup3 = "a", "b", "c", "d", "e" >>> print(tup1) ('physics', 'chemistry', 1997, 2000) >>> print(tup2) (1, 2, 3, 4, 5) >>> print(tup3) ('a', 'b', 'c', 'd', 'e')

14 Creating a tuple (2) The empty tuple is written as two parentheses containing nothing: To write a tuple containing a single value you have to include a comma, even though there is only one value: >>> tup1 = () >>> print(tup1) () >>> tup1 = (50,) >>> tup1 (50,)

15 Accessing Values in Tuples
Like string indices, tuple indices start at 0, and tuples can be sliced, concatenated and so on. To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index. >>> tup1 = ('physics', 'chemistry', 1997, 2000) >>> tup2 = (1, 2, 3, 4, 5, 6, 7) >>> print ("tup1[0]: ",tup1[0]) tup1[0]: physics >>> print ("tup2[1:5]: ",tup2[1:5]) tup2[1:5]: (2, 3, 4, 5)

16 Updating Tuples – impossible
Tuples are immutable which means you cannot update or change the values of tuple elements. >>>tup1 = (12, 34.56) >>>tup2 = ('abc', 'xyz' ) >>>tup1[0] = 100 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment

17 However, new assignments are always legal!
Creating a new tuple (or any new mutable item) is always legal: >>>tup1 = (12, 34.56) >>>tup2 = ('abc', 'xyz') >>>tup3 = tup1 + tup2 >>>tup3 (12, 34.56, 'abc', 'xyz')

18 Delete Tuple Elements (1)
Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded. To explicitly remove an entire tuple, just use the del statement. >>>tup1 = (12, 34.56, 'abc', 'xyz') >>> del tup1[1] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object doesn't support item deletion >>> del tup1 >>>tup1 NameError: name 'tup1' is not defined

19 Basic Tuples Operations
Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too. In fact, tuples respond to all of the general sequence operations we used on strings. Python Expression Results Description len((1, 2, 3)) 3 Length (1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation ('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') Repetition 3 in (1, 2, 3) True Membership for x in (1, 2, 3): print x, 1 2 3 Iteration

20 Indexing and Slicing Because tuples are sequences, indexing and slicing work the same way for tuples as they do for strings. Assuming following input: Python Expression Results Explanation L[2] 'SPAM!' Offsets start at zero Negative: count from the right L[-2] 'Spam' L[1:] ['Spam', 'SPAM!'] Slicing fetches sections

21 Built-in Tuple Functions
len(tuple) Gives the total length of the tuple. max(tuple) Returns item from the tuple with max value. min(tuple) Returns item from the tuple with min value. tuple(seq) Converts a list into tuple.

22 No Enclosing Delimiters (1)
Any set of multiple objects, comma-separated, written without identifying symbols (i.e., brackets for lists, parentheses for tuples, etc.), default to tuples. x, y == 1, 2 is exactly the same as x, y = (1, 2) >>> a = 'abc', e37, j, 'xyz' >>> print(a) ('abc', e+37, (18+6.6j), 'xyz') >>> type(a) <class 'tuple'>

23 No Enclosing Delimiters (2)
An example in python shell: >>> x, y = 1, 2 >>> x, y (1, 2) >>> type(_) <class 'tuple'> Underscore (‘_’) gets the last answer in the python shell

24 Function returns multiple variables? Also a tuple
We saw previously that we may return few variables from a functions Actually python returns only one variable - A tuple! >>> def foo(): ... return 3, 4 >>> type(foo) <class ‘function'> >>> type(foo()) <class 'tuple'> >>> type(foo()[0]) <class 'int'>

25 Mutable and Immutable types
object Mutable or Immutable? numbers string range tuple lists Immutable Immutable Immutable Immutable mutable

26 Immutable/Mutable containers – a comment
Objects within an immutable object can be mutable And vice versa! >>> tup1 = (1, 2, [3, 4, 5]) >>> tup1[0] = 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> tup1[2][0] = 6 >>> tup1 (1, 2, [6, 4, 5])

27 Immutable/Mutable containers – a comment
Are those legal in python? >>> tup1 = (1, 2, [3, 4, 5]) >>> L1 = ['a', 'b', ('c', 'd', 'e')] >>> tup1[0] = 3 >>> tup1[2][0] = 6 >>> tup1[2] = 3 >>> L1[0] = 'g' >>> L1[2][0] = ‘g' >>> L1[2] = 'g'

28 Immutable/Mutable containers – a comment
Are those legal in python? >>> tup1 = (1, 2, [3, 4, 5]) >>> L1 = ['a', 'b', ('c', 'd', 'e')] >>> tup1[0] = 3 >>> tup1[2][0] = 6 >>> tup1[2] = 3 >>> L1[0] = 'g' >>> L1[2][0] = 'g' >>> L1[2] = 'g'

29 Aliases An alias is a second name for a piece of data. Often easier (and more useful) than making a second copy If the data is immutable, aliases don't matter because the data can't change But if data can change, aliases can result in a lot of hard-to-find bugs

30 Aliases Aliasing happens whenever one variable's value is assigned to another variable >>> first = 'isaac' >>> second = first variable value first second 'isaac'

31 id function Id(object)
Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. In most of the computers id function return the address of the object in memory.

32 Aliasing immutable items – same identity
When we copy immutable items, the two items have the same id: >>> first = 'isaac' >>> second = first >>> id(first) >>> id(second) variable value first second 'isaac'

33 Any new assignment will result in a different identity
But new assignment will always result in a new identity >>> first = 'isaac' >>> second = first >>> id(first) >>> id(second) >>> first = first + ' newton' variable value first second 'isaac' 'isaac newton'

34 Aliasing mutable items is a completely different story!
As mutable items are allowed to change, they behave very differently in aliasing For example: >>> L1 = [1, 2, 3] >>> L2 = L1 >>> print(L2) [1, 2, 3] >>> id(L1) >>> id(L2) >>> L3 = L1[:] >>> print(L3) >>> id(L3) # != id (L1) Huh?!

35 Copying with the equal sign copying a reference
Assign a list to another - Both point to the same place in memory! >>> colours1 = ["red", "green"] >>> colours2 = colours1 variable value colours1 colours2 'red' 'green'

36 Copying with the equal sign copying a reference
Assign a list to another - Both point to the same place in memory! >>> colours1 = ["red", "green"] >>> colours2 = colours1 >>> colours2[1] = "blue" >>> print(colours2) ['red', 'blue'] >>> print(colours1) ['red', ‘blue'] variable value colours1 colours2 'red' 'blue'

37 New assignments are new references
On the other hand – any new assignment will always lead to a new identity variable value >>> colours1 = ["red", "green"] >>> colours2 = colours1 >>> colours2 = ["pink", "yellow"] >>> colours1 ["red", "green"] colours1 colours2 'red' 'green' variable value colours1 colours2 'red' 'green' 'pink' 'yellow'

38 Copy with the equal operator Same identity!
>>> nums1 = [1, 2] >>> nums2 = nums1 >>> id(nums1) == id(nums2) True variable value >>> nums2.append(3) >>> print(nums2) [1, 2, 3] >>> print(nums1) >>> id(nums1) == id(nums2) True nums1 nums2

39 Shallow and Deep Copy Shallow copy
In the process of shallow copying A, B will copy all of A's field values. If the field value is a memory address it copies the memory address, and if the field value is a object that is not containers: int, float, bool (primitive type) - it copies the value of it. Deep copy In deep copy the data is actually copied over. The result is different from the result a shallow copy gives. The advantage is that A and B do not depend on each other, but at the cost of a slower and more expensive copy.

40 Copy with the slice operator
Simple data types >>> list1 = ['a', 'b', 'c', 'd'] >>> list3 = list1[:] >>> id(list1) == id(list3) False >>> list3[1] = 'x' >>> print(list3) ['a', 'x', 'c', 'd'] >>> print(list1) ['a', 'b', 'c', 'd']

41 Copy with the slice operator – shallow copy
Compound data types - copy the reference! >>> lst1 = ['a', 'b', ['ab', 'ba']] >>> lst2 = lst1[:] >>> lst2[0] = 'c' >>> lst2[2][1] = ‘d’ >>> print(lst1) ['a', 'b', ['ab', 'd']] >>> id(lst1)==id(lst2) False >>> id(lst1[0])==id(lst2[0]) >>> id(lst1[2])==id(lst2[2]) True

42 deepcopy from the Module copy
Create a new version of the same values but with new references! >>> from copy import deepcopy >>> lst1 = ['a', 'b', ['ab', 'ba']] >>> lst2 = deepcopy(lst1) >>> lst2[2][1] = 'd' >>> lst1[0] = "c" >>> print(lst1) ['c', 'b', ['ab', 'ba']] >>> print(lst2) ['a', 'b', ['ab', 'd']] >>> id(lst1) == id(lst2) False >>> id(lst1[2]) == id(lst2[2])

43 Quick Summary - immutable items
An alias is a second name for the same piece of data In immutable items every copy is an alias >>> a = (1,2,[3,4]) >>> b = a >>> id(a) == id(b) True >>> c = a[:] >>> id(a) == id(c)

44 Quick Summary - Mutable items
In mutable items we distinguish between three cases: Assignment (=): Assign a second name to the same identity Shallow copy ([:]): Copy all the fields (values for simple types and references for memory addresses ) Deepcopy (copy.deepcopy) Copy (recursively) all the values in one list to another

45 is Vs. == In python we have two functions to compare objects: is and the equal operator is test for identity and is similar for all types Equal is a function that may be implemented differently (wait for the cool stuff in OOP) In Lists == works similar to deepcopy and compare the values of two lists

46 is Vs. == example(1) >>> L1 = [1, 2, 3]
>>> L1 == L2 True >>> L1 is L2 # similar to id(L1) == id(L2) False

47 is Vs. == example(2) >>> tup1 = (3, 4)
>>> tup1 is tup2 False >>> tup1 == tup2 True >>> L1 = [1, 2, tup1] >>> L2 = [1, 2, tup2] >>> L2 [1, 2, (3, 4)] >>> L1 >>> L1 == L2 >>> L1 is L2

48 Function scopes How can we determine what belongs to a function and what to the module? Scopes Function Scope – Everything between the function definition and the end of the function. Python functions have no explicit begin or end, and no curly braces to mark where the function code starts and stops. The only delimiter is a colon (:) and the indentation of the code itself. Everything inside the scope of a function “belongs” to it – local variables, local functions and more

49 Functions have their own variables
Reminder: >>> a = 3 >>> def print_func(): ... a = 12 ... print('inside the func: a = ', a) ... >>> print_func() inside the func: a = 12 >>> print('outside the func: a = ', a) outside the func: a = 3

50 Local variables Functions have a special type of variable called local variables These variables only exist while the function is running. Local variables are not accessible from outside the function When a local variable has the same name as another variable (such as a global variable), the local variable hides the other.

51 Local variables - example(1)
>>> def func_a(): ... a = 3 >>> def func_b(): ... b = a -2 >>> func_b() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in func_b NameError: name 'a' is not defined

52 Local variables – example(2)
def print_func(): a = 3 print('in the func a =', a) b = a + 100 print('in the func b =', b) d = 2 * a print('in the func d =', d) print('in the func e =', e) return b + 10 c = print_func()

53 Local variables – example(2)
def print_func(): a = 3 print('in the func a =', a) b = a + 100 print('in the func b =', b) d = 2 * a print('in the func d =', d) print('in the func e =', e) return b + 10 c = print_func() # output: in the func a = 3 in the func b = 103 in the func d = 6 in the func e = 4 print('a =',a) a = 5 print('b =',b) b = 3 print('c =',c) c = 113 print('d =',d) NameError: name 'd' is not defined

54 The global variable a = 3 def print_func(): global a a = 12
print('inside the func: a = ', a) print_func() print('outside the func: a = ', a) inside the func: a = 12 outside the func: a = 12 When possible, avoid the use of global variables – global variable can potentially be modified from anywhere, and any part of the program may depend on it.

55 Mutable items as variables
>>> def func_a(L): ... L[1] = 2 >>> L1 = [4,5,6] >>> func_a(L1) >>> L1 [4, 2, 6] Huh?!?! What happened to local variables?

56 Mutable items are passed as references!
>>> def func_a(L): ... L[1] = 2 >>> L1 = [4,5,6] >>> func_a(L1) >>> L1 [4, 2, 6]

57 Let’s test you (1) What will happen?
L = (1,2,3) def foo(L): L[2] = 4 >>>L What will happen?

58 Let’s test you (1) What will happen?
L = (1, 2, 3) def foo(L): L[2] = 4 >>>foo(L) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in foo TypeError: 'tuple' object does not support item assignment What will happen?

59 Let’s test you (2) What will happen?
L = [1, 2, 3] def foo(L): L[2] = 4 >>>foo(L) >>>L What will happen?

60 Let’s test you (2) What will happen?
L = [1, 2, 3] def foo(L): L[2] = 4 >>>foo(L) >>>L [1, 2, 4] What will happen?

61 Let’s test you (3) What will happen?
L = [1, 2, 3] def foo(L): L = [1, 2, 4] >>>foo(L) >>>L What will happen?

62 Let’s test you (3) What will happen?
L = [1, 2, 3] def foo(L): L = [1, 2, 4] >>>foo(L) >>>L What will happen?

63 Let’s test you (4) What will happen?
def foo(L): L2 = [1, 2] L[2] = L2 L = [1, 2, 3] >>>foo(L) >>>L What will happen?

64 Let’s test you (4) What will happen?
def foo(L): L2 = [1, 2] L[2] = L2 L = [1, 2, 3] >>>foo(L) >>>L L = [1, 2, [1, 2]] What will happen?


Download ppt "Intro2cs Tirgul 4."

Similar presentations


Ads by Google