61A Lecture 3. Friday, September 5

Similar documents
61A Lecture 4. Monday, September 9

61A Lecture 2. Wednesday, September 4, 2013

61A Lecture 2. Friday, August 28, 2015

61A Lecture 9. Friday, September 20

61A Lecture 7. Monday, September 15

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

CONTROL AND ENVIRONMENTS 1

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

CONTROL AND HIGHER ORDER FUNCTIONS 2

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

C ONTROL AND H IGHER O RDER F UNCTIONS

PREPARING FOR PRELIM 1

61A Lecture 6. Monday, February 2

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

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

CIS192 Python Programming. Robert Rand. August 27, 2015

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

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

CONTROL AND HIGHER ORDER FUNCTIONS 1

CIS192: Python Programming

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 1

CS1 Lecture 3 Jan. 18, 2019

61A LECTURE 7 DATA ABSTRACTION. Steven Tang and Eric Tzeng July 3, 2013

More Coding Basics with Some Turtle

Conditionals and Recursion. Python Part 4

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

CONTROL AND HIGHER ORDER FUNCTIONS 1

Conditionals & Control Flow

Midterm Exam 1 Solutions

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

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

61A Lecture 7. Monday, September 16

61A Lecture 6. Friday, September 7

Conditionals: Making Choices

ENVIRONMENT DIAGRAMS AND RECURSION 2

61A LECTURE 16 TREES, ORDERS OF GROWTH. Steven Tang and Eric Tzeng July 22, 2013

Decisions, Decisions, Decisions. GEEN163 Introduction to Computer Programming

Structure and Interpretation of Computer Programs

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

CS1 Lecture 3 Jan. 22, 2018

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

Genome Sciences 373: Genome Informatics. Quiz section #1 March 29, 2018

Basic Syntax - First Program 1

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

61A Lecture 21. Friday, March 13

Announcements. Homework 1 due Monday 10/12 (today) Homework 2 released next Monday 10/19 is due 11/2

CS 314 Principles of Programming Languages. Lecture 16

CSE : Python Programming

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

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

61A LECTURE 8 SEQUENCES, ITERABLES

61A LECTURE 8 SEQUENCES, ITERABLES. Steven Tang and Eric Tzeng July 8, 2013

CS1150 Principles of Computer Science Boolean, Selection Statements

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Test #2 October 8, 2015

Flow Control. So Far: Writing simple statements that get executed one after another.

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

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Lecture #2: Programming Structures: Loops and Functions

CSC148: Week 1

Conditional Expressions and Decision Statements

61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

CSC Software I: Utilities and Internals. Programming in Python

Lecture 5 Tao Wang 1

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

CS 221 Lecture. Tuesday, 13 September 2011

CMSC201 Computer Science I for Majors

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

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

61A Lecture 19. Wednesday, October 12

CS 1803 Individual Homework 2 Conditionals & Loops Due: Wednesday, September 8th, before 6 PM Out of 100 points

Midterm Review. CS61A Summer Katya Stukalova Jerome Baek

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CIS 110: Introduction to Computer Programming

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

CS Introduction to Programming Fall 2016

EXAMINATION INSTRUCTIONS

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

CS3: Introduction to Symbolic Programming. Lecture 5:

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

CS Lecture 26: Grab Bag. Announcements

Introduction to Python

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Structure and Interpretation of Computer Programs

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

CSE 115. Introduction to Computer Science I

Control Structures. CIS 118 Intro to LINUX

CSE 115. Introduction to Computer Science I

Lecture 4. Defining Functions

Measuring Efficiency

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

KOMAR UNIVERSITY OF SCIENCE AND TECHNOLOGY (KUST)

Programming Languages and Techniques (CIS120)

Transcription:

61A Lecture 3 Friday, September 5

Announcements There's plenty of room in live lecture if you want to come (but videos are still better) Please don't make noise outside of the previous lecture! Homework 1 is due next Wednesday 9/10 at 2pm (Changed from original time!)! Homework is graded on effort, but the bar is high you must make substantial progress! Monday homework parties 3pm-4pm in Wozniak Lounge and 6pm-8pm in 2050 VLSB Take-home quiz released next Wednesday 9/10 at 3pm, due Thursday 9/12 at 11:59pm! 3 points, similar in format to homework, but graded for correctness! If you score 0/3, you will need to talk to the course staff or be dropped! Open-computer: You can use the Python interpreter, watch course videos, etc.! Closed-help: Please don't talk to your classmates, search for answers, etc. Project 1 due Wednesday 9/17 at 11:59pm. 2

Multiple Environments

Life Cycle of a User-Defined Function Def statement: Name >>> def Formal parameter square( x ): Return expression What happens? A new function is created! Def statement return mul(x, x) Body (return statement) Name bound to that function in the current frame Call expression: square(2+2) operator: square function: func square(x) operand: 2+2 argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) Calling/Applying: 4 square( x ): Argument Signature 16 Return value A new frame is created! Parameters bound to arguments Body is executed in that new environment 4

Multiple Environments in One Diagram! square(square(3)) func square(x) square(3) func square(x) 3 Interactive Diagram 5

Multiple Environments in One Diagram! square(square(3)) func square(x) 9 square(3) func square(x) 3 Interactive Diagram 6

Multiple Environments in One Diagram! 1 2 2 1 81 square(square(3)) 1 func square(x) 9 square(3) An environment is a sequence of frames. func square(x) 3 The global frame alone A local, then the global frame Interactive Diagram 7

Names Have No Meaning Without Environments 1 2 2 1 Every expression is evaluated in the context of an environment. 1 A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. An environment is a sequence of frames. The global frame alone A local, then the global frame Interactive Diagram 8

Names Have Different Meanings in Different Environments A call expression and the body of the function being called are evaluated in different environments 1 2 1 Every expression is evaluated in the context of an environment. A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. Interactive Diagram 9

Miscellaneous Python Features Operators Multiple Return Values Docstrings Doctests Default Arguments (Demo)

Conditional Statements

Statements A statement is executed by the interpreter to perform an action Compound statements: Statement Clause The first header determines a statement s type <header>: <statement> <statement>... <separating header>: <statement> <statement>...... Suite The header of a clause controls the suite that follows def statements are compound statements 12

Compound Statements Compound statements: <header>: <statement> <statement>... <separating header>: <statement> <statement>...... Suite A suite is a sequence of statements To execute a suite means to execute its sequence of statements, in order Execution Rule for a sequence of statements: Execute the first statement Unless directed otherwise, execute the rest 13

Conditional Statements (Demo) 1 statement, 3 clauses, 3 headers, 3 suites def absolute_value(x):! """Return the absolute value of x."""! if x < 0:! return -x! elif x == 0:! return 0! else:! return x Execution Rule for Conditional Statements: Syntax Tips: Each clause is considered in order. 1. Evaluate the header's expression. 2. If it is a true value, execute the suite & skip the remaining clauses. 1. Always starts with "if" clause. 2. Zero or more "elif" clauses. 3. Zero or one "else" clause, always at the end. 14

Boolean Contexts def absolute_value(x):! """Return the absolute value of x."""! if x < 0:! return -x! elif x == 0:! return 0! else:! return x George Boole 15

Boolean Contexts def absolute_value(x):! """Return the absolute value of x."""! if x < 0:! return -x! elif x == 0:! return 0! else:! return x Two boolean contexts George Boole False values in Python: False, 0, '', None (more to come) True values in Python: Anything else (True) Read Section 1.5.4! Reading: http://composingprograms.com/pages/15-control.html#conditional-statements 16

Iteration

While Statements (Demo) 1 2 3 1 3 6 George Boole Execution Rule for While Statements: 1. Evaluate the header s expression.! 2. If it is a true value, execute the (whole) suite, then return to step 1.! 18