Lecture Agenda. Objects. But First... Immutable Types and Nesting. Immutable Types and Nesting

Similar documents
COMP10001 Foundations of Computing Functions

Announcements. Lecture Agenda. Class Exercise. Hashable. Mutability. COMP10001 Foundations of Computing Iteration

Reminders. Lecture Outline. Lecture Agenda. Lecture Outline. Remember This? COMP10001 Foundations of Computing Testing and Debugging

COMP10001 Foundations of Computing Testing and Debugging

Lists, Tuples and Dictionaries. HORT Lecture 10 Instructor: Kranthi Varala

Sequences and iteration in Python

Computer Sciences 368 Scripting for CHTC Day 3: Collections Suggested reading: Learning Python

PIC 16, Fall Variables, Dynamically-typed, Mutable, Immutable, Functions. Michael Andrews. October 1, Department of Mathematics UCLA

Python and Bioinformatics. Pierre Parutto

Randomized Algorithms, Hash Functions

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

Reminders. Lecture Outline. Lecture Agenda. Namespaces II. Namespaces I. COMP10001 Foundations of Computing PEP8, Modules and Files

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Notebook. March 30, 2019

Data Structures. Lists, Tuples, Sets, Dictionaries

Programming in Python

MEIN 50010: Python Data Structures

MUTABLE LISTS AND DICTIONARIES 4

Collections. Lists, Tuples, Sets, Dictionaries

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

CIS192 Python Programming

Scheme as implemented by Racket

Data Structures. Dictionaries - stores a series of unsorted key/value pairs that are indexed using the keys and return the value.

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

ENGR 102 Engineering Lab I - Computation

Today: Revisit some objects. Programming Languages. Key data structure: Dictionaries. Using Dictionaries. CSE 130 : Winter 2009

Python Programming: Lecture 2 Data Types

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CSI33 Data Structures

Lecture 12 Hash Tables

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Python for Non-programmers

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Turn in a printout of your code exercises stapled to your answers to the written exercises at 2:10 PM on Thursday, January 13th.

Randomized Algorithms: Element Distinctness

Extended Introduction to Computer Science CS1001.py Lecture 15: The Dictionary Problem Hash Functions and Hash Tables

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

CSC326 Python Sequences i. CSC326 Python Sequences

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

LECTURE 3 Python Basics Part 2

Sets and Dictionaries

Lecture 12 Notes Hash Tables

AAL 217: DATA STRUCTURES

The Practice of Computing Using PYTHON

Python. Karin Lagesen.

A Little Python Part 1. Introducing Programming with Python

Dictionaries. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region. Based on CBSE Curriculum Class -11. Neha Tyagi, KV 5 Jaipur II Shift

Programming Languages

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Lecture 18. Collision Resolution

This quiz is open book and open notes, but do not use a computer.

TUPLES AND RECURSIVE LISTS 5

Functional Programming and Haskell

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Lecture 16 More on Hashing Collision Resolution

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Lecture 7: Python s Built-in. in Types and Basic Statements

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

(IUCAA, Pune) kaustubh[at]iucaa[dot]ernet[dot]in.

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

A Little Python Part 2

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

STA141C: Big Data & High Performance Statistical Computing

Ceng 111 Fall 2015 Week 8a

1 Strings (Review) CS151: Problem Solving and Programming

Compound Data Types 1

Quiz 1 Practice Problems

The dictionary problem

CMSC 341 Hashing. Based on slides from previous iterations of this course

Introduction to Python

[301] Objects/References. Tyler Caraza-Harter

Extended Introduction to Computer Science CS1001.py

Recap from last Python lecture

CSC148 Week 2. Larry Zhang

The following content is provided under a Creative Commons license. Your support

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Python - Variable Types. John R. Woodward

Lecture 16: Binary Search Trees

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

A Brief Python Tutorial

ENGR (Socolofsky) Week 07 Python scripts

Programming Languages

Algorithms for Bioinformatics

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Python Tutorial. Day 1

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Script language: Python Data structures

CSE : Python Programming

Exceptions & a Taste of Declarative Programming in SQL

Comp 335 File Structures. Hashing

Ceng 111 Fall 2015 Week 7a

Programming to Python

Lecture #12: Mutable Data. map rlist Illustrated (III) map rlist Illustrated. Using Mutability For Construction: map rlist Revisited

Python Basics. 1 of 7 9/5/2018, 8:51 AM. txt1 = "ada lovelace, english mathematician and writer" print(txt1)

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Chapter 20 Hash Tables

Quiz 1 Practice Problems

Recall that strings and tuples are immutable datatypes, while lists are mutable datatypes. What does this mean?

Transcription:

COMP10001 Foundations of Computing Objects and Types: A Closer Look (Advanced Lecture) Semester 1, 2017 Tim Baldwin & Egemen Tanin Lecture Agenda This lecture: Making sense of strings, lists and functions 9am Revision Lecture in Herbert Wilson Theatre, Doug McDonell Bldg NB: All advanced lectures are unexaminable version: 1083, date: March 17, 2017 2017 The University of Melbourne But First Seat rearragement: Move to the seat (x,y) defined as follows: x = ( age + day_of_your_birthday ) % 27 y = ( min ( dentist_visits_in_last_year, 5) + cousins ) % 12 For example (11,3): Immutable Types and Nesting Hang on, hang on, explain the following then: >>> mytup = (0, []) >>> mytup [0] = 1 Traceback ( most recent call last ): File " <stdin >", line 1, in <module > TypeError : >>> mytup [1] append (" hey there!") >>> print ( mytup ) (0, ['hey there!']) Objects Everything in Python is an object, with an unchangeable type and a value There are two basic categories of type: mutable types, where the value of the object is changeable (eg list): >>> lst = [0] >>> lst [0] = " COMP10001 " >>> print ( lst ) ['COMP10001 '] immutable types, where the value of the object is unchangeable (eg int, str) Immutable Types and Nesting Hang on, hang on, explain the following then: >>> mytup = (0, []) >>> mytup [0] = 1 Traceback ( most recent call last ): File " <stdin >", line 1, in <module > TypeError : >>> mytup [1] append (" hey there!") >>> print ( mytup ) (0, ['hey there!']) The answer is that the values of immutable types can t be changed directly, but that doesn t stop us changing the values of mutable types nested within them

Object Identity Internally on the computer, every object exists in memory and is accessible via its identity, defining its location in memory; this identity is also unchangeable, even for mutable types: >>> lst = [] >>> id(lst ) 140016396283848 >>> lst append ('newval ') >>> id(lst ) 140016396283848 is is a relational operator that checks whether two objects have the same identity Objects and Variables II although there are some optimisations that confuse things slightly for immutable types: >>> str1 = " woodchuck " >>> str2 = " woodchuck " >>> str1 == str2 # same value? >>> str1 is str2 # same object? >>> lst1 = [] >>> lst2 = [] >>> lst1 == lst2 >>> lst1 is lst2 False A Basic Intro to Dictionaries As powerful as lists are, if items are associated with a (unique) key, we want to be able to look them up directly, rather than search through the values in a list; dictionaries provide this, eg: >>> lst1 = [( 'tim ', 'duck '), ('egemen ', ' hamster ')] >>> for i in lst1 : if i [0] == 'tim ': print (i [1]) duck >>> dict1 = {} # initialise a new dictionary >>> dict1 ['tim '] = ' duck ' # add an item >>> dict1 [' egemen '] = ' hamster ' # add another item >>> print ( dict1 ['tim ']) # look up an item duck Objects and Variables I When we construct an object and assign it to a variable, the variable is simply assigned the identity of the new object; when we assign a variable to a new variable, therefore, the new variable is simply given the identity of the existing object >>> int1 = 10001 >>> int2 = int1 >>> int2 is int1 # same object? >>> int2 = 10001 >>> int2 is int1 # same object? False Aside: Functions are also Objects All well and good, but what about functions? Functions are also just objects: >>> type ( print ) <class ' builtin_function_or_method '> >>> id( print ) 140443419192584 >>> myprint = print >>> myprint (" Hello world ") Hello world >>> del ( print ) # can only delete user - defined objects Traceback ( most recent call last ): File " <stdin >", line 1, in <module > NameError : name ' print ' is not defined >>> del ( myprint ) >>> print = len # note : user - defined object >>> print (" Hello world ") 11 >>> del ( print ) # can delete user - defined objects >>> print (" Hello world ") Hello world Add item Indexing Lookup

Add item lappend() d[key] = VAL Indexing Lookup Add item lappend() d[key] = VAL Lookup Add item lappend() d[key] = VAL Lookup VAL in s VAL in l VAL in t KEY in d Add item lappend() d[key] = VAL Lookup VAL in s VAL in l VAL in t KEY in d s[i:j] l[i:j] t[i:j] Add item lappend() d[key] = VAL Lookup VAL in s VAL in l VAL in t KEY in d s[i:j] l[i:j] t[i:j] Y Y Y N

Mutation is weird! (or is it ) Strings: Key Idea Mutation is weird! (or is it ) Dictionary keys must be (recursively) immutable Contiguous array of fixed objects (characters = str of length one) a Additional bookkeeping to store the length of the string (avoid overrunning the ends of the string) a This is a slight over-simplification, but it gives you the basic idea Lists: Key Idea Contiguous array of pointers to arbitrary objects a Additional bookkeeping to store the length of the list (avoid overrunning the ends of the list) a Again, this is a slight over-simplification Dictionaries: Key Idea Use a hash function to map objects onto integers with which to index a list Build in some mechanism to handle hash collisions (cf the birthday paradox )

Hash Functions Our hash function must: return an int value for all types be deterministic given a key be cheap return a value in a given range and ideally should: distribute keys evenly irrespective of the key source Our hash table must: not use excessive storage be able to handle collisions efficiently support incremental additions and deletions allow dynamic growth Hash functions in the wild? Open Hashing The simplest form of a hashing is open hashing, where each cell in the hash table takes the form of a list, and collisions are resolved simply by appending to the end of the list Advantages: simple, table can t ever fill Disadvantage: slow if lots of collisions Closed Hashing A more sophisticated form of hashing with better behaviour when there is a high collision rate is closed hashing : collisions resolved by probing within hash table for an empty cell Advantages: faster lookup Disadvantage: complexity; table can fill up Roughly what Python uses in practice Answers to The Quirks of Assignment Based on what we have learned about the different types, let s see if we can make sense of the following: >>> lst1 = biglist () >>> lst2 = lst1 >>> lst1 [0] ' tzrqbqawuslkbubkiwwajawbstje_ol ' >>> lst2 [0] = -1 >>> lst1 [0] -1 >>> del ( lst1 ) >>> lst2 [0] -1 If we think about it in terms of pointers (a la the internals of strings), hopefully it s less baffling Answers to again, this is a direct function of the implementation details

Answers to again, this is a direct function of the implementation details Mutation is weird! (or is it ) given that variables are just pointers, hopefully considerably less weird verging on sensible For the Brave at Heart Answers to again, this is a direct function of the implementation details Mutation is weird! (or is it ) given that variables are just pointers, hopefully considerably less weird verging on sensible Dictionary keys must be (recursively) immutable what would happen if we allowed the key to be mutated, in terms of its location in the dictionary? Lecture Summary If you want to find out more about the internal implementation details of Python objects, strings and lists, the following are a good starting point: https://docspythonorg/3/reference/ datamodelhtml http://wwwlaurentlucecom/posts/ python-string-objects-implementation/ http://wwwlaurentlucecom/posts/ python-list-implementation/ http://wwwlaurentlucecom/posts/ python-dictionary-implementation/ but expect to have to go away and read up on some of the back-end algorithms What are objects? What are the basic behaviours of string, lists, tuples and dictionaries? What data structures underlie each? How does assignment work and why? What s the deal with mutation? What s the big deal about immutable keys and dictionaries?