61A Lecture 2. Friday, August 28, 2015

Similar documents
61A Lecture 2. Wednesday, September 4, 2013

61A Lecture 3. Friday, September 5

! The simplest building blocks of a language. ! Compound elements are built from simpler ones

61A Lecture 4. Monday, September 9

Lecture 4. Defining Functions

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

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

CSE : Python Programming

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

DRAWING ENVIRONMENT DIAGRAMS

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

CONTROL AND ENVIRONMENTS 1

RLISTS AND THE ENVIRONMENT MODEL 9

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

61A LECTURE 1 FUNCTIONS, VALUES. Steven Tang and Eric Tzeng June 24, 2013

EXCEPTIONS, CALCULATOR 10

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

Lecture #3: Environments

LECTURE 2. Python Basics

Key Differences Between Python and Java

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

C ONTROL AND H IGHER O RDER F UNCTIONS

Chapter 2 Programming in Python

SCHEME AND CALCULATOR 5b

CS1 Lecture 3 Jan. 18, 2019

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

Python for Non-programmers

Recap: Functions as first-class values

CS1 Lecture 3 Jan. 22, 2018

S206E Lecture 19, 5/24/2016, Python an overview

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

Getting Started Values, Expressions, and Statements CS GMU

Intro to Python Programming

Introduction to Bioinformatics

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Python for C programmers

Getting Started with Python

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

Argparse Tutorial Release 2.7.9

Not-So-Mini-Lecture 6. Modules & Scripts

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Midterm Exam 1 Solutions

Abstracting with Functions

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

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

A Little Python Part 1. Introducing Programming with Python

Control Structures 1 / 17

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

MEIN 50010: Python Strings

61A Lecture 9. Friday, September 20

MUTABLE LISTS AND DICTIONARIES 4

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Fundamentals of Programming (Python) Getting Started with Programming

Lecture 16: Static Semantics Overview 1

A Functional Evaluation Model

COMP1730/COMP6730 Programming for Scientists. Functions

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

Python for loops. Girls Programming Network School of Information Technologies University of Sydney. Mini-lecture 7

CS100 Spring 2012 Midterm 1 Practice

MEIN 50010: Python Flow Control

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

61A Lecture 28. Friday, November 4

ENVIRONMENT DIAGRAMS AND RECURSION 2

Defining Functions III: Nested Definitions

Hello, Functions! We define a function using the def keyword:

CS1 Lecture 4 Jan. 23, 2019

61A Lecture 6. Friday, September 7

University of Washington CSE 140 Introduction to Data Programming Winter Midterm exam. February 6, 2013

Environment Diagrams. Administrivia. Agenda Step by Step Environment Diagram Stuff. The Rules. Practice it s on! What are The Rules?

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

CS61A Notes Week 13: Interpreters

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

Python Programming Exercises 1

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

CS1 Lecture 4 Jan. 24, 2018

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

Python 1: Intro! Max Dougherty Andrew Schmitt

Lecture #23: Conversion and Type Inference

Python in 10 (50) minutes

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Introduction to Python

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Chapter 1. Fundamentals of Higher Order Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Python Intro GIS Week 1. Jake K. Carr

Beyond Blocks: Python Session #1

Impera've Programming

Statements 2. a operator= b a = a operator b

University of Washington CSE 140 Introduction to Data Programming Winter Midterm exam. February 6, 2013

Introduction and Functions Summer 2018 Discussion 0: June 19, 2018

Functions and abstraction. Ruth Anderson UW CSE 160 Winter 2017

Introduction to Python! Lecture 2

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Transcription:

61A Lecture 2 Friday, August 28, 2015

Names, Assignment, and User-Defined Functions (Demo)

Types of Expressions Primitive expressions: 2 add 'hello' Number or Numeral Name String Call expressions: max ( 2, 3 ) Operator Operand Operand An operand can also be a call expression max(min(pow(3, 5), -4), min(1, -2)) 3

Discussion Question 1 What is the value of the final expression in this sequence? >>> f = min >>> f = max >>> g, h = min, max >>> max = g >>> max(f(2, g(h(1, 5), 3)), 4)??? 4

Environment Diagrams

Environment Diagrams Environment diagrams visualize the interpreter s process. Just executed Import statement Name Value Next to execute Assignment statement Code (left): Statements and expressions Frames (right): Each name is bound to a value Arrows indicate evaluation order Within a frame, a name cannot be repeated (Demo) Interactive Diagram 6

Assignment Statements Just executed Next to execute Just executed Execution rule for assignment statements: 1. Evaluate all expressions to the right of = from left to right. 2. Bind all names to the left of = to those resulting values in the current frame. Interactive Diagram 7

Discussion Question 1 Solution (Demo) 3 func min(...) 3 4 f(2, g(h(1, 5), 3)) func max(...) 2 3 3 g(h(1, 5), 3) func min(...) 5 3 h(1, 5) func max(...) 1 5 Interactive Diagram 8

Defining Functions

Defining Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions Function signature indicates how many arguments a function takes >>> def <name>(<formal parameters>): return <return expression> Function body defines the computation performed when the function is applied Execution procedure for def statements: 1. Create a function with signature <name>(<formal parameters>) 2. Set the body of that function to be everything indented after the first line 3. Bind <name> to that function in the current frame 10

Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment Built-in function Original name of function called Local frame User-defined function Formal parameter bound to argument Return value (not a binding!) Interactive Diagram 11

Calling User-Defined Functions Procedure for calling/applying user-defined functions (version 1): 1. Add a local frame, forming a new environment 2. Bind the function's formal parameters to its arguments in that frame 3. Execute the body of the function in that new environment A function s signature has all the information needed to create a local frame Interactive Diagram 12

Looking Up Names In Environments Every expression is evaluated in the context of an environment. So far, the current environment is either: The global frame alone, or A local frame, followed by the global frame. Most important two things I ll say all day: An environment is a sequence of frames. A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. E.g., to look up some name in the body of the square function: Look for that name in the local frame. If not found, look for it in the global frame. (Built-in names like max are in the global frame too, but we don t draw them in environment diagrams.) (Demo) 13

Print and None (Demo)

None Indicates that Nothing is Returned The special value None represents nothing in Python A function that does not explicitly return a value will return None Careful: None is not displayed by the interpreter as the value of an expression The name sixteen is now bound to the value None >>> def does_not_square(x):... x * x No return... >>> does_not_square(4) None value is not displayed >>> sixteen = does_not_square(4) >>> sixteen + 4 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' 15

Pure Functions & Non-Pure Functions Pure Functions just return values -2 Argument abs Return value 2 2, 100 2 Arguments pow 1267650600228229401496703205376 Non-Pure Functions have side effects -2 print None Python displays the output -2 Returns None! A side effect isn't a value; it's anything that happens as a consequence of calling a function 16

Nested Expressions with Print None, None print(...): None Does not get displayed display None None None print(print(1), print(2)) func print(...) None print(1) None print(2) func print(...) 1 func print(...) 2 1 print(...): None 2 print(...): None display 1 display 2 17