Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC101 Reminders Slides have changed from those posted last night…

Similar presentations


Presentation on theme: "CISC101 Reminders Slides have changed from those posted last night…"— Presentation transcript:

1 CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders Slides have changed from those posted last night… Quiz 3 marking underway. Assignment 4 due this Friday. (Consider using a dictionary to hold the word frequencies…) Last quiz next week (to avoid week 12). Worst quiz mark is dropped. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Finish Dictionaries. Lists of Lists. Passing by Reference. - NEW
Start Algorithms. Begin with finding Minimums, Maximums and Sums. Winter 2018 CISC101 - Prof. McLeod

3 Quiz 4 Topics Functions, loops, conditionals, lists – STILL.
New topics: Default and Keyword Arguments. Sets. Strings. Raising Exceptions. Passing by Reference. Dictionaries. os, os.path, sys, urllib.request and exec() BIF not on quiz. Winter 2018 CISC101 - Prof. McLeod

4 Dictionary Methods, Cont.
popitem() – destructively iterates through a dictionary, returning key, value tuples. update(other) – updates existing values with values from other, which can be a dictionary or a collection of key, value tuples. values() – returns an iterable of just values. Winter 2018 CISC101 - Prof. McLeod

5 Another Example: Winter 2018 CISC101 - Prof. McLeod

6 Using Dictionaries These are particularly useful for field-based information, such as that stored in a database. It is easier to code using real field names rather than having to remember which fields are at which index locations. Winter 2018 CISC101 - Prof. McLeod

7 CISC101 Dictionary Example See DictionaryDrawingProgram.py which reads the text file: Drawing.txt. Uses a list of dictionaries, with one dictionary per line in the drawing. Note how key names are much better than indices in identifying the elements of a collection. A list of dictionaries could be a good database structure! Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

8 CISC101 Lists of Lists We know a list can hold anything, and the elements do not even have to be of all the same type: ex1 = [1, 4.0, ‘abc’, 2, ‘hello!’] So, there is no reason that an element cannot be another list (or a tuple, or a dictionary). ex2 = [4.5, [1, 2, ‘abc’], 7, ‘hello’] Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

9 Lists of Lists, Cont. For example: >>> for value in ex2:
print(value) 4.5 [1, 2, 'abc'] 7 hello Winter 2018 CISC101 - Prof. McLeod

10 Lists of Lists, Cont. How can I display the elements in the list at position 1?: >>> for value in ex2[1]: print(value) 1 2 abc Winter 2018 CISC101 - Prof. McLeod

11 Lists of Lists, Cont. Nothing new!
How do I access just the 'abc' string inside the list at position 1?: >>> ex2[1][2] = 'wxyz' >>> ex2 [4.5, [1, 2, 'wxyz'], 7, 'hello'] Winter 2018 CISC101 - Prof. McLeod

12 Lists of Lists, Cont. So, a list of lists can be used represent tabular data: ex3 = [['Sam', 18, ], ['Boris', 21, ], ['Ben', 19, ]] You could do it this way, or (better yet) use a dictionary. Sam 18 Boris 21 Ben 19 Winter 2018 CISC101 - Prof. McLeod

13 Lists of Lists, Cont. Don’t forget that list locations are index based, so you can use the slice operator. List elements stay where you put them! So, while you cannot control the order of key : value pairs within a dictionary, you can control the position of dictionaries within a list. Winter 2018 CISC101 - Prof. McLeod

14 CISC101 Passing by Reference Can a function change something in its parameter list and have the change stay (or “stick”), even when the function is complete? What kinds of arguments can be changed and how? See TestPassingByReference.py Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

15 Passing by Reference, Cont.
Observations: Immutable objects (the int, the string and the tuple) do not stay changed outside the function. (All you can do inside the function is re-assign them.) Re-assigning a mutable object, a list, does not change it. However, element by element changes (using the slice operator only!) or invoking a method belonging to a list does allow the changes to stay after the function is complete. Winter 2018 CISC101 - Prof. McLeod

16 Passing by Reference, Cont.
When you pass a list (or any object) into a function, you do not re-create the entire structure inside the function. That would be wasteful and time-consuming! Instead you just pass a reference (a memory address, or “pointer”) into the function. If the object is mutable, and its elements are changed or deleted inside the function, then that change is made to the structure created outside the function. Winter 2018 CISC101 - Prof. McLeod

17 Passing by Reference, Cont.
We will take advantage of being able to pass mutable objects (especially lists) by reference to simplify code! This also gives you a way to get more than one list out of a function without having to return a tuple of lists. But, if you are doing this maybe your function is not just doing one thing! And, returning multiple things through the parameter list can make for confusing code. Winter 2018 CISC101 - Prof. McLeod

18 Algorithms Let’s take a break from Python syntax for a while!
Instead – use Python code to explore various useful algorithms. Make comparisons based on ease of coding, flexibility and efficiency (speed!). Winter 2018 CISC101 - Prof. McLeod

19 CISC101 What is an Algorithm? An algorithm is a set of instructions to solve a particular problem. A computing algorithm can be expressed in code. In any language, including Python! We are going to learn some simple algorithms that every programmer should know. Focus on Searching and Sorting algorithms. (Avoid recursive algorithms.) Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

20 Finding Min’s, Max’s and Sums
Naturally Python has BIFs for this! min(), max() and sum() Used as min(iter, key=None), or min(arg0, arg1, arg2, …, key=None), and sum(iter[, start]) iter is a list or tuple (or string), or an iterable (Optional key can point to a function that can be used to determine the order of the elements. We won’t use this.) Winter 2018 CISC101 - Prof. McLeod

21 Finding Min’s and Max’s, Cont.
Sometimes you have to do this yourself. (And sometimes your version will work better!) A function that returns the minimum of a simple list: def findMin(aList): min = aList[0] i = 1 while i < len(aList) : if aList[i] < min : min = aList[i] i = i + 1 return min Winter 2018 CISC101 - Prof. McLeod

22 Finding Min’s and Max’s, Cont.
Maybe you want to know the position of the minimum, not the value. Could you use a for loop instead? Would it be safe to say min = 0 ? See FindMinMaxSumDemo.py Winter 2018 CISC101 - Prof. McLeod

23 Finding Min’s and Max’s, Cont.
Note how the functions work with lists of other types. And, note that the built-in sum() works only with lists of numbers – we can modify our sum and our Min/Max functions to work with the other list types or even a mix of types in the same list. Winter 2018 CISC101 - Prof. McLeod

24 Two Questions… Would it be faster to sort the list first and then just get the first or last values as the Min and Max? If you had to write a function that returns the min, the max and the sum at the same time – what is the best way to do it? How many loops? Winter 2018 CISC101 - Prof. McLeod

25 Summary Finding minimums, maximums and making sums are all very similar algorithms. Python has BIFs for these since they are so often used. Writing our own versions allows us to add flexibility to how the code works. Is our code as fast as the BIF version? Winter 2018 CISC101 - Prof. McLeod


Download ppt "CISC101 Reminders Slides have changed from those posted last night…"

Similar presentations


Ads by Google