Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

Similar documents
Outline. The Python Memory Model A Linked Implementation of Lists In-Class Work. 1 Chapter 4: Linked Structures and Iterators

Collections. Lists, Tuples, Sets, Dictionaries

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2

CMPT 120 Lists and Strings. Summer 2012 Instructor: Hassan Khosravi

CS1 Lecture 12 Feb. 11, 2019

Book keeping. Will post HW5 tonight. OK to work in pairs. Midterm review next Wednesday

CS1 Lecture 3 Jan. 22, 2018

The Dynamic Typing Interlude

CS1 Lecture 5 Jan. 25, 2019

CMSC201 Computer Science I for Majors

Our First Programs. Programs. Hello World 10/7/2013

Lecture 12. Lists (& Sequences)

Algorithms for Bioinformatics

Heap Arrays and Linked Lists. Steven R. Bagley

All programs can be represented in terms of sequence, selection and iteration.

Announcements for this Lecture

CMSC201 Computer Science I for Majors

First of all, it is a variable, just like other variables you studied

CS1 Lecture 3 Jan. 18, 2019

CMSC131. A Simple Problem?

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

CS1 Lecture 13 Feb. 13, 2019

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

Lecture 10. Memory in Python

Lecture 11: Iteration and For-Loops

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

Data Structures. Lists, Tuples, Sets, Dictionaries

CS1 Lecture 5 Jan. 26, 2018

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

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

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

CMSC 201 Computer Science I for Majors

Announcements. Project 2 due next Monday. Next Tuesday is review session; Midterm 1 on Wed., EE 129, 8:00 9:30pm

CSI33 Data Structures

Exam 2, Form A CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Exam 2, Form B CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

THE AUSTRALIAN NATIONAL UNIVERSITY Final Examination November COMP1730 / COMP6730 Programming for Scientists

CIS192 Python Programming. Robert Rand. August 27, 2015

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Introduction to Python

19 File Structure, Disk Scheduling

Decisions, Decisions. Testing, testing C H A P T E R 7

Algorithms for Bioinformatics

ECE 364 Software Engineering Tools Lab. Lecture 8 Python: Advanced I

QUIZ. What are 3 differences between C and C++ const variables?

Heap Arrays. Steven R. Bagley

Sharing, mutability, and immutability. Ruth Anderson UW CSE 160 Spring 2018

QUIZ. What is wrong with this code that uses default arguments?

Intermediate Programming, Spring 2017*

Intro. Scheme Basics. scm> 5 5. scm>

An Introduction to Python

CSE 303: Concepts and Tools for Software Development

Lecture 9. Memory and Call Stacks


FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Vector and Free Store (Vectors and Arrays)

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

Program Planning, Data Comparisons, Strings

CSSE 120 Introduction to Software Development Practice for Test 1 paper-and-pencil part Page 1 of 6

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

Lecture 7. Memory in Python

CA31-1K DIS. Pointers. TA: You Lu

Problem Solving for Intro to Computer Science

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

15 Sharing Main Memory Segmentation and Paging

HW1 due Monday by 9:30am Assignment online, submission details to come

CS Summer 2013

More Loop Examples Functions and Parameters

CIS192: Python Programming

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

CS1 Lecture 4 Jan. 23, 2019

Lecture 4. Defining Functions

Structure of Programming Languages Lecture 10

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

22 File Structure, Disk Scheduling

Programming Languages

CS399 New Beginnings. Jonathan Walpole

Introduction to Scientific Computing

1 Strings (Review) CS151: Problem Solving and Programming

Text Input and Conditionals

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

ORDERS OF GROWTH, DATA ABSTRACTION 4

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Introduction to Python

Programming for Engineers in Python

Pointers and Memory 1

CS 1110: Introduction to Computing Using Python Lists and Sequences

Programming Languages and Techniques (CIS120)

T H E I N T E R A C T I V E S H E L L

16 Sharing Main Memory Segmentation and Paging

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

CSCE 145 Exam 1 Review. This exam totals to 100 points. Follow the instructions. Good luck!

C++ for Java Programmers

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

OCR Pseudocode to Python

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Transcription:

References

Homework notes Homework 2 grades posted on canvas Homework 3 due tomorrow Homework 4 posted on canvas Due Tuesday, Oct. 3

Style notes Comment your code! A short line of comments per logical block of code is sufficient Add whitespace Makes it a lot easier to parse your code, especially if it is logically separated Use meaningful variable names If you use a/b, there s no way to easily find out what they do EVEN YOU WON T REMEMBER IN A FEW DAYS!

Simple values vs. collections >>> a = 5 >>> b = a >>> a = a + >>> b 5 >>> a = [, 2, 3] >>> b = a >>> a[] = 5 >>> b [, 5, 3] What s going on?

Variable contents Think of a variable as a box that can hold a small amount of information In the case of a simple value (number or boolean), the value can fit in the box In the case of a larger value, such as a list, what is put in the box is a reference to the value What we copy to another variable is what is in the box

It s all about memory Variables always hold small values integers, floats, booleans, and references Small values are cheap to copy and pass around They are kept in a special part of memory called the stack All other kinds of values are larger (some of which can grow and shrink, like lists) These larger values are kept in a special part of memory called the heap Larger values are expensive (in both time and memory) to copy, so it doesn t happen automatically

vs. Conceptually, two separate contiguous blocks of memory May not be contiguous on an actual machine Variables are copied on the stack directly a = 5 More complex types contain a pointer on the stack and actual data on the heap my_list = [, 2] 5 #ptr to start of my_list 2

Call by value When we call a function, the arguments in the call are evaluated, and the values are put into the parameters If the parameters are changed within the function (which isn t good style), the changes are not put back into the arguments Besides, the arguments are not necessarily simple variables; they could be literal numbers, or expressions We refer to this by call by value IDLE example: callbyvalue.py

Call by value revisited a = 5 5 def alter(x): x = x + def main(): a = 5 alter(a) print(a) main()

Call by value revisited a = 5 alter(a) 5 5 def alter(x): x = x + def main(): a = 5 alter(a) print(a) main()

Call by value revisited a = 5 alter(a) x = x + 5 65 def alter(x): x = x + def main(): a = 5 alter(a) print(a) main()

Call by value revisited a = 5 alter(a) x = x + print(a) 5 def alter(x): x = x + def main(): a = 5 alter(a) print(a) main()

Call by reference When a function is called with something other than a number or boolean, the same rules apply, but something different happens What is in the argument is a reference, and that is what is passed to the function The argument continues to refer to the same list but the list has been altered This is call by reference IDLE example: callbyreference.py

Call by reference revisited b = [, 2, 3] #ptr to start of b def alter(x): x[] = 99 # changes contents of b x = [4, 5, 6] # does not affect b def main(): b = [, 2, 3] alter(b) print(b) main() 3 2

Call by reference revisited b = [, 2, 3] alter(b) #ptr to start of b #x = ptr to start of b def alter(x): x[] = 99 # changes contents of b x = [4, 5, 6] # does not affect b def main(): b = [, 2, 3] alter(b) print(b) main() 3 2

Call by reference revisited b = [, 2, 3] alter(b) x[] = 99 #ptr to start of b #x = ptr to start of b def alter(x): x[] = 99 # changes contents of b x = [4, 5, 6] # does not affect b def main(): b = [, 2, 3] alter(b) print(b) main() 3 99

Call by reference revisited b = [, 2, 3] alter(b) x[] = 99 x = [4, 5, 6] #ptr to start of b #x = ptr to new list 6 def alter(x): x[] = 99 # changes contents of b x = [4, 5, 6] # does not affect b def main(): b = [, 2, 3] alter(b) print(b) main() 5 4 3 99

Call by reference revisited b = [, 2, 3] alter(b) x[] = 99 x = [4, 5, 6] print(b) #ptr to start of b def alter(x): x[] = 99 # changes contents of b x = [4, 5, 6] # does not affect b def main(): b = [, 2, 3] alter(b) print(b) main() 3 99

Collections as parameters A variable is a box that can hold a small amount of information Four bytes of actual data, but there is associated information to describe the type of the data Dictionaries and sets are mutable: You can change the values in them Dictionaries and sets are not small, so like lists, a variable whose value appears to be a dictionary or set, is actually a reference to that dictionary or set Python keeps this straight, so you don t (usually) have to Bottom line: Dictionaries and sets are like lists; when you pass one into a function, You can change the values in the dictionary or set, but You can t change it to be a different dictionary or set

Strings as parameters Are strings passed to functions by value or by reference? Answer: Strings are passed by reference, but since they are immutable, nothing you do in the function will change the original string IDLE example: stringparam.py

Operations on lists: making a list You can enter a list directly: >>> [, 2, 3] [, 2, 3] You can give a sequence to the list function: >>> list(range(, 4)) [, 2, 3] >>> list({'one', 'two', 'three'}) ['two', 'three', one'] >>> list({'one':, 'two':2}) ['two', one'] You can multiply a list: >>> ['A'] * 3 ['A', 'A', A'] >>> ['a', 'b', 'c'] * 2 ['a', 'b', 'c', 'a', 'b', 'c']

List multiplication gone wrongs >>> m = [['a', 'b', 'c']] * 3 >>> m [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', c']] >>> m[][2] = '*' >>> m [['a', 'b', '*'], ['a', 'b', '*'], ['a', 'b', '*']] Explanation:. In the first line above, the list contains a reference to a list 2. The *3 made copies of the reference, not copies of the list itself 3. This gets assigned to m, which has three copies of the same reference 4. The second assignment above changes the one referenced list

Copying collections Two ways to copy: A shallow copy of an object makes a copy of an object that includes all the small values (including references) in the original object A deep copy of an object makes a copy of that object that includes deep copies of all the referenced objects in the original object IDLE example. copytypes.py

m = [, [2, 3], 4] copy and deepcopy #ptr to start of m 3 2 4 #ptr to list in m

m = [, [2, 3], 4] m2 = copy.copy(m) copy and deepcopy 4 #ptr to start of m #ptr to start of m2 #ptr to list in m 3 2 4 #ptr to list in m

m = [, [2, 3], 4] m2 = copy.copy(m) m3 = copy.deepcopy(m) copy and deepcopy 3 2 4 #ptr to list in m3 4 #ptr to start of m #ptr to list in m 3 #ptr to start of m2 #ptr to start of m3 2 4 #ptr to list in m

m = [, [2, 3], 4] m2 = copy.copy(m) m3 = copy.deepcopy(m) m[][] = 99 copy and deepcopy 3 2 4 #ptr to list in m3 4 #ptr to start of m #ptr to list in m 99 #ptr to start of m2 #ptr to start of m3 2 4 #ptr to list in m

m = [, [2, 3], 4] m2 = copy.copy(m) m3 = copy.deepcopy(m) m[][] = 99 m.append(5) #ptr to start of m #ptr to start of m2 #ptr to start of m3 copy and deepcopy 3 2 4 #ptr to list in m3 4 #ptr to list in m 99 2 5 4 #ptr to list in m