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

Similar documents
Arrays. Lecture 9 COP 3014 Fall October 16, 2017

CSI33 Data Structures

Python: common syntax

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

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

Array Elements as Function Parameters

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

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

Do not turn to the next page until the start of the exam.

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Lecture #10: Sequences. Last modified: Mon Feb 22 16:33: CS61A: Lecture #10 1

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

1 Anatomy of a Program 4

C++ for Python Programmers

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

ENGR 102 Engineering Lab I - Computation

Dynamic Memory Allocation

Exam 3 Chapters 7 & 9

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

LECTURE 5 Control Structures Part 2

CSC108: Introduction to Computer Programming. Lecture 11

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

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

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

CPTS 111, Fall 2011, Sections 6&7 Exam 3 Review

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?

Algorithmic Thinking: Computing with Lists

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

Fundamentals of Programming CS-110. Lecture 2

First cut, scope. Defining scope. Find the namespace. Passing argument to parameter. A function s namespace 6/13/2017. chapter 8.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

Largest Online Community of VU Students

Lecture #7: Recursion (and a data structure)

Introduction to Programming using C++

Python Programming: Lecture 2 Data Types

61A Lecture 2. Friday, August 28, 2015

61A Lecture 2. Wednesday, September 4, 2013

python 01 September 16, 2016

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

Homework #3 CS2255 Fall 2012

CS 1110: Introduction to Computing Using Python Lists and Sequences

Programming to Python

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

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

COS 140: Foundations of Computer Science

Ceng 111 Fall 2015 Week 8a

CS1 Lecture 11 Feb. 9, 2018

Programming Languages

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

Lecture 16: Static Semantics Overview 1

Lecture 2: SML Basics

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Lecture 15 CIS 341: COMPILERS

Fall Lecture 3 September 4. Stephen Brookes

COMP 364: Functions II

Sixth lecture; classes, objects, reference operator.

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

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

CSCI-1200 Data Structures Spring 2018 Lecture 15 Associative Containers (Maps), Part 2

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

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

Programming Languages

To figure this out we need a more precise understanding of how ML works

Unit 7. Functions. Need of User Defined Functions

Algorithms and Programming

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

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

Section we will not cover section 2.11 feel free to read it on your own

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

ECOR Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

What is an algorithm?

CMSC330 Fall 2015 Midterm #1 12:30pm/2:00pm/5:00pm

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Shell / Python Tutorial. CS279 Autumn 2017 Rishi Bedi

CMSC 201 Computer Science I for Majors

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

CPSC 427: Object-Oriented Programming

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Python for C programmers

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

COS 140: Foundations of Computer Science

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

CSI33 Data Structures

CS558 Programming Languages. Winter 2013 Lecture 3

CSC 1300 Exam 4 Comprehensive-ish and Structs

Largest Online Community of VU Students

CS558 Programming Languages

Working with Lists 4

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Introduction to Computer Science Midterm 3 Fall, Points

Transcription:

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

Office hours reminder M 1:30-2:45pm (Michael) T 1-2pm (Sam), T 3-5pm (Michael) W 11am-12pm (Sam) R 1-2pm (Sam) F 1:30-2:45pm (Michael)

Syntactic Differences: C++ and Python C++ Python ; end line (most frequently) {} indentation for(i=0; i<10; i++) for i in range(10): if/while(x) if/while x: int fun() def fun(): #include import true/false True/False // # &&,,! and, or, not() i++ i=i+1 cout, cin print, input/raw_input const (no equivalent)

Further differences A variable in Python is a pointer to an object. Python is dynamically typed (as opposed to statically typed). Every type in C++ is mutable unless it s const. In Python, some types are immutable. We ll speak about each of these in turn.

Variables in Python are pointers: example x = [] y = x print y x.append(0) print y

Variables in Python are pointers: example annotated x = [] y = x print y # the empty list is created and stored in memory # = "binds" the pointer x to this place in memory # y is a pointer to the same place in memory # prints the list to which x points # prints the list to which y points x.append(0) print y # 0 is appended to the list to which x points # prints the list to which x points # prints the list to which y points

Python is dynamically typed Python variables point to various objects. There is no need to declare a variable or require it to always point to the same type of object. This is the sense in which people say Python is dynamically typed. x = 1 print type(x) x = 'hello' print type(x) x = [1, 2, 3] print type(x) # x points to an int # see! # now x points to a string # see! # now x points to a list # see!

Variant of previous example: more about assignment x = [] y = x print y x = [1] print y

Variant of previous example annotated x = [] y = x print y x = [1] print y # the empty list is created and stored in memory # = binds the pointer x to this place in memory # y is a pointer to the same place in memory # prints the list to which x points # prints the list to which y points # the list [1] is created and stored in memory # = binds the pointer x to this place in memory # prints the list to which x points # prints the list to which y points

Related example about assignment y = [] z = [1] x = y y = z, y, z

Related example annotated y = [] z = [1] x = y y = z # the empty list is created and stored in memory # = binds the pointer y to this place in memory # the list [1] is created and stored in memory # = binds the pointer z to this place in memory # x is binded to where y currently points # y is binded to where z currently points, y, z

Variant of the example before last: lists ints x = 0 y = x print y x = 1 print y # the int 0 is "created" and stored in memory # = binds the pointer x to this place in memory # y is a pointer to the same place in memory # prints the int to which x points # prints the int to which y points # the int 1 is "created" and stored in memory # = binds the pointer x to this place in memory # prints the list which x points to # prints the list which y points to

Mutability The first example on page 6 illustrates a subtlety concerning python variables arising from the fact they are pointers. The variation on page 9 emphasizes thinking about assignment as binding : x = [1] does not change the content of the object to which x points originally; it creates a new object and changes where x points. The variation on page 12 is a simple tweak from lists to ints. We cannot create an example like the first one using ints. While x = 1 means x points to the int 1, there is no way of changing the object 1. The only way to make x point to a different integer is to create a new integer and have x point somewhere else. ints are immutable, whereas lists are mutable. strings are also immutable.

Passing variables to functions Passing a variable to a function is like passing a pointer by value in C++. We create a copy of the pointer insider the function. We do not create a copy of the object being pointed to.

Passing variables to functions: Example 1 def f(l): L = L + [0] x = [] f(x)

Passing variables to functions: Example 1 annotated def f(l): L = L + [0] # a NEW list is stored in memory: # the result of COPYING the list that L points to, # and then APPENDING 0. # = binds L to this new list. # L is disgarded. x = [] f(x) # x points to the empty list. # a new pointer called L is created. # L points to where x points. # the above 4 comments apply. # none of these edit the list to which x points.

Passing variables to functions: Example 2 def g(l): L.append(0) # 0 is appended to the list to which L points. # L is disgarded. x = [] f(x) # x points to the empty list. # a new pointer called L is created. # L points to where x points. # the above 2 comments apply.

Mutable default parameters def f(x, L=[]): L.append(x) return L print f(1) print f(2) print f(3) print f(88,[8,18]) print f(4)

Mutable default parameters annotated def f(x, L=[]): L.append(x) return L # when you define f, # f is added to the global scope, # and L is added to the local scope of f, # L points to memory storing []. # this only happens once. print f(1) # hopefully this makes print f(2) # sense of the first 3 lines. print f(3) print f(88,[8,18]) # this probably feels correct too. print f(4) # this line might still be confusing; # it'll make more sense once # we look at class variables.

Another weird thing: not lectured def create_multipliers(): m = [] for i in range(5): def multiplier_i(x): return i * x m.append(multiplier_i) return m multipliers = create_multipliers() print multipliers[1](11)

Another weird thing: not lectured def create_multipliers(): m = [] for i in range(5): def multiplier_i(x, i=i): # used default parameter return i * x m.append(multiplier_i) return m multipliers = create_multipliers() print multipliers[1](11)