Lecture 04 – Models of memory Mutable and immutable data.

Slides:



Advertisements
Similar presentations
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Advertisements

ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Introduction to Programming Lecture 39. Copy Constructor.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Lists Introduction to Computing Science and Programming I.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Computer Science 210 Computer Organization Pointers.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Introduction. 2COMPSCI Computer Science Fundamentals.
Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
CSC 107 – Programming For Science. The Week’s Goal.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
CIT 590 Intro to Programming Lecture 6 (dictionaries)
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Pointers *, &, array similarities, functions, sizeof.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
CSC Programming for Science Lecture 5: Actual Programming.
Input, Output and Variables GCSE Computer Science – Python.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Midterm preview.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Agenda Introduction Computer Programs Python Variables Assignment
Python Variable Types.
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Chapter 8 Namespaces and Memory Realities
Fundamentals of Functional Programming Languages
Basic notes on pointers in C
CSC115 Introduction to Computer Programming
Introduction to the C Language
Microsoft Visual Basic 2005 BASICS
© A+ Computer Science - VARIABLES © A+ Computer Science -
Guide to Programming with Python
Chapter 8 Namespaces and Memory Realities
References.
Reference semantics, variables and names
Chapter 8 Namespaces and Memory Realities
CSE 231 Lab 7.
Variables and Constants
Objects Management.
Hardware is… Software is…
Python fundamental.
Presentation transcript:

Lecture 04 – Models of memory Mutable and immutable data

 Memory  Memory consists of boxes with numeric addresses  Each box holds a single number  Variable identifiers  Labels for these boxes  Interpreter maintains tables linking label to address 2COMPSCI Computer Science Fundamentals LabelAddress X Y456849

 What is the output produced by each of the following programs?  Discuss your answers 3COMPSCI Computer Science Fundamentals x = '4' y = x y += '5' print(y) print(x) x = [4] y = x y += [5] print(y) print(x)

 Data  data is stored directly in the box  good model for simple data  need a more complex model for more complex data 4COMPSCI Computer Science Fundamentals

 Linking variables with data  Data is stored in the memory  Variables hold a reference to the location of the data 5COMPSCI Computer Science Fundamentals

 Assignment statements copy the value on the right to the variable on the left 6COMPSCI Computer Science Fundamentals y = x

 Assignment statements copy the value on the right to the variable on the left 7COMPSCI Computer Science Fundamentals y = x

 Assignment statements copy the value on the right to the variable on the left 8COMPSCI Computer Science Fundamentals y = x

 Does that explain the behaviour of this code?  What would you expect to see? 9COMPSCI Computer Science Fundamentals x = '4' y = x y += '5' print(y) print(x) x = [4] y = x y += [5] print(y) print(x)

 Mutable  A type of variable in which the contents can be changed  lists, dictionaries, most complex data types  Immutable  A type of variable in which the contents cannot be changed  int, float, boolean, string, tuple 10COMPSCI Computer Science Fundamentals

 In place operators use different code to normal operators  With immutable types, they both perform the same function  With mutable types, the in place operator modifies the contents referred to by x, but the normal operator + creates a new object. 11COMPSCI Computer Science Fundamentals x = x + 4 x += 4 These are not the same operator

 What is the output of the following code? 12COMPSCI Computer Science Fundamentals data = [1, 2, 3, 4] backup = data while len(data) > 0: element = data.pop() print(element, data) print(data) print(backup)

 Value equality  Reference equality 13COMPSCI Computer Science Fundamentals X y [1, 2, 3, 4, 5] X y Two different objects that store the same information. Two different references / names for the same object.

 ==  Calls a method of the object  Programmer who defined the object decides how to determine equality  Typically involves checking the contents of the objects  We should always use this kind of equality unless you need to check references  is  Checks the references of the objects  Evaluates to True if they are the same object 14COMPSCI Computer Science Fundamentals

 What is the output from each of the examples below? Explain. 15COMPSCI Computer Science Fundamentals x = 500 y = 500 print(x == y, x is y) x = 100 y = 100 print(x == y, x is y) x = 2.5 y = 2.5 print(x == y, x is y) x = 'Hello World' y = 'Hello World' print(x == y, x is y)

 Lists and dictionaries have a copy method  data.copy() 16COMPSCI Computer Science Fundamentals x = [1, 2, 3, 4, 5] y = x.copy() print( x is y ) a = [ [11], [22], [33] ] b = a.copy() print( a is b ) print( a[0] is b[0] )

 New object created  Contents of the original object are copied  If the contents are references, then the references are copied 17COMPSCI Computer Science Fundamentals y[,, ] x [11][22][33]

 New object created  Contents of the original object are copied  If the contents are references, then the copy the objects referred to 18COMPSCI Computer Science Fundamentals x[,, ] [11][22][33] y[,, ] [11][22][33]

 Use the module copy  new_copy = copy.copy( original )  new_copy = copy.deepcopy( original ) 19COMPSCI Computer Science Fundamentals import copy a = [ [11], [22], [33] ] b = copy.deepcopy(a) print( a is b ) print( a[0] is b[0] )

 Variables store references to the objects, not the actual objects  When you assign a variable, a reference is copied, not the object  There are two kinds of equality  Equality of content (value equality) can be tested with ==  Equality of identity (reference equality) can be tested with is  When a copy is created, it can be a shallow or deep copy  A shallow copy copies the references  A deep copy recursively copies the objects referred to 20COMPSCI Computer Science Fundamentals