Winter 2019 CISC101 5/26/2019 CISC101 Reminders

Slides:



Advertisements
Similar presentations
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Advertisements

Lists in Python.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1.
CISC124 - Notices Assn 2 due this Friday. You already know what you need to do this assignment: –Another procedural program. –Uses text file I/O (see Exercise.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
10 - Python Dictionary John R. Woodward.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Containers and Lists CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
Department of Computer Science,
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Assn 3 due Friday, this week.
CMSC201 Computer Science I for Majors Lecture 20 – Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSc 110, Spring 2018 Lecture 33: Dictionaries
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Dictionaries Dictionary: object that stores a collection of data
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assn 3 sample solution is posted.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Dictionary.
Introduction to Computer Science
Presentation transcript:

Winter 2019 CISC101 5/26/2019 CISC101 Reminders Assignment 4 due this Friday. Use lists, sets, dictionaries and design and write functions to solve some data mining exercises. Quiz 3 grading nears completion… Quiz 4 next week! Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

Today Dictionaries. Lists of Lists. Passing by Reference. Next Big Topic – Algorithms. Winter 2019 CISC101 - Prof. McLeod

Dictionaries Or a “dict”. Another built-in collection type in Python. Mutable collection (like lists). A “mapped” data structure – not index based. Uses a set of key : value associations. You cannot use the slice operator with a dictionary, instead you use the key name. The key name goes in a set of [ ], but this is not the slice operator in the context of dictionaries. Winter 2019 CISC101 - Prof. McLeod

Dictionaries, Cont. Create an empty dictionary using: emptyDict = dict() or: emptyDict = { } Add or modify a key : value pair simply as: emptyDict['name'] = "Alan" emptyDict is now {'name': 'Alan'} Each key must be unique and hashable – strings are most common. Winter 2019 CISC101 - Prof. McLeod

Dictionaries, Cont. Add another key : value pair: emptyDict['age'] = 21 emptyDict is now {'name': 'Alan', 'age': 21} key : value pairs are separated by commas. Like sets, you cannot control the order of the key value pairs in the dict – the hashing algorithm controls this. However, since you cannot use index values, the position has no meaning. Winter 2019 CISC101 - Prof. McLeod

A List of Dictionaries – An Example CISC101 A List of Dictionaries – An Example >>> name1 = {'name':'Sam', 'age':18, 'SN':4445555} >>> name2 = {'name':'Boris', 'age':21, 'SN':5554444} >>> name3 = {'name':'Ben', 'age':19, 'SN':5445444} >>> allNames = [name1, name2, name3] >>> allNames [{'age': 18, 'name': 'Sam', 'SN': 4445555}, {'age': 21, 'name': 'Boris', 'SN': 5554444}, {'age': 19, 'name': 'Ben', 'SN': 5445444}] >>> allNames[2]['age'] 19 >>> allNames[1]['name'] 'Boris' Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

Better Yet: >>> allNames = {} >>> allNames['Sam'] = {'age':18, 'SN':4445555} >>> allNames['Boris'] = {'age':21, 'SN':5554444} >>> allNames['Ben'] = {'age':19, 'SN':5445444} >>> allNames {'Boris': {'age': 21, 'SN': 5554444}, 'Ben': {'age': 19, 'SN': 5445444}, 'Sam': {'age': 18, 'SN': 4445555}} >>> allNames['Boris']['age'] 21 Winter 2019 CISC101 - Prof. McLeod

Dictionaries, Cont. A dictionary has a method called keys() that returns an iterable list of key names: >>> name1.keys() dict_keys(['age', 'name', 'SN']) If you apply the sorted() BIF to a dictionary it returns an iterable sorted by key name to use with a for loop. See section 5.5 in the Python Tutorial. Winter 2019 CISC101 - Prof. McLeod

For Loop Example: Winter 2019 CISC101 - Prof. McLeod

Dictionary Methods Look for the Section “Mapping Types - dict” in the Python Standard Library: clear() – empties the dictionary fromkeys(keys, [values]) – returns a new dict get(key) – Obtains a key’s value, but returns None if the key is not in the dictionary. items() – returns a iterable of key, value tuples. keys() – returns an iterable of just keys pop(key) – remove key : value and return value. Winter 2019 CISC101 - Prof. McLeod

Dictionary Creation, Demo See DictionaryCreation.py for demos of the dict() BIF and the fromkeys() method. Winter 2019 CISC101 - Prof. McLeod

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 2019 CISC101 - Prof. McLeod

Another Example: Winter 2019 CISC101 - Prof. McLeod

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 2019 CISC101 - Prof. McLeod

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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

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

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 2019 CISC101 - Prof. McLeod

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 2019 CISC101 - Prof. McLeod

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

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 2019 CISC101 - Prof. McLeod

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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

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 2019 CISC101 - Prof. McLeod

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 2019 CISC101 - Prof. McLeod

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 2019 CISC101 - Prof. McLeod