CS1 Lecture 5 Jan. 25, 2019

Similar documents
CS1 Lecture 5 Jan. 26, 2018

CS1 Lecture 4 Jan. 24, 2018

CS1 Lecture 4 Jan. 23, 2019

CS1 Lecture 3 Jan. 22, 2018

CS1 Lecture 3 Jan. 18, 2019

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

CS1 Lecture 9 Feb. 5, 2018

Lecture Transcript While and Do While Statements in C++

Introduction to Python (All the Basic Stuff)

Problem Solving for Intro to Computer Science

CS1 Lecture 2 Jan. 16, 2019

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

CS1 Lecture 13 Feb. 13, 2019

CS1 Lecture 22 Mar. 6, 2019

Text Input and Conditionals

Lecture Notes, CSE 232, Fall 2014 Semester

Conditional Expressions and Decision Statements

Conditionals & Control Flow

Lecture 3. Input, Output and Data Types

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Divisibility Rules and Their Explanations

CS1 Lecture 11 Feb. 9, 2018

(Refer Slide Time: 01.26)

Intro. Speed V Growth

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

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

These are notes for the third lecture; if statements and loops.

Beyond Blocks: Python Session #1

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

CS Summer 2013

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

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

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

If Statements, For Loops, Functions

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

(I m not printing out these notes! Take your own.)

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

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

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

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

LOOPS. Repetition using the while statement

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

Chapter 1 Getting Started

Functions and Decomposition

(Python) Chapter 3: Repetition

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Repetition Through Recursion

Table of Laplace Transforms

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

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

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

CSCI 141 Computer Programming I. Filip Jagodzinski

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Programming with Python

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Lecture 5: Implementing Lists, Version 1

Lecture 8. Conditionals & Control Flow

Semantic Analysis. Lecture 9. February 7, 2018

Comp Exam 1 Overview.

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

6.001 Notes: Section 15.1

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

6.001 Notes: Section 8.1

Conditionals & Loops /

More Lambda Calculus and Intro to Type Systems

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

ENGR 102 Engineering Lab I - Computation

TUPLES AND RECURSIVE LISTS 5

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Lab 6: P.S. it s a stack Due 11:59 pm Monday, Mar 31, 2008

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

CS 221 Lecture. Tuesday, 13 September 2011

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Programming in C++ Indian Institute of Technology, Kharagpur

Chapter Five: Functions

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Reviewing all Topics this term

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

Statistics 13, Lab 1. Getting Started. The Mac. Launching RStudio and loading data

CS 112: Intro to Comp Prog

Types, Expressions, and States

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

Comp 151. Control structures.

Computer Science 210: Data Structures

Lecture 3: Recursion; Structural Induction

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

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

CS1 Lecture 12 Feb. 11, 2019

2: Complexity and Recursion

SCHEME AND CALCULATOR 5b

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Transcription:

CS1 Lecture 5 Jan. 25, 2019 HW1 due Monday, 9:00am. Notes: Do not write all the code at once before starting to test. Take tiny steps. Write a few lines test... add a line or two test... add another line test...!! the math.ceil function can be helpful calculating the number of hotel nights. (See slide later in this lecture) floor division (//) and modulus (%) can also be useful in HW1 calculations. (See slide later in this lecture) HW2 available Monday

Last time Much of chapter 3 Function calls Math functions (math module) Function composition Defining functions <- super important! Parameters and arguments HW1 information and advice

Ch 3: Defining New Functions def foo(a, b, c): temp = a * b result = temp + c return result IMPORTANT When executing a function call: 1) first, the function s parameter variables are bound to the values of the function call s arguments 2) then, body of the function is executed >>> x = 3 >>> foo(x * x, x + 1, 3) ß foo will be executed with variable a bound to 9, b bound to 4, c bound to 3 foo knows nothing about x. x *isn t* passed in. 9, 4, and 3 are passed into foo.

Ch 3: Defining New Functions Functions can have zero parameters: def giveback1(): return(1) >>> giveback1() 1 You can type the function name at interpreter prompt: >>> giveback1 <function giveback1 at 0x10367cb70> >>> type(giveback1) <class 'function'> Note: function name is just like any other variable. It is bound to a value; in this case that value is a function object. Don t try to use that same name as a simple variable (e.g. print1 = 3). You ll lose your function. (And, you can even break Python )

Today Ch 3: The rest Execution flow of control Variables and parameters are local Stack diagrams Fruitful functions - incl return vs. print Why Functions? Skip to first part of Ch 5: Conditionals Floor division and modulus (useful for HW1) Logical/Boolean expressions Conditional execution if/elif/else

Ch 3: Control Flow A program consists of a list of instructions, executed (except in certain situations) in top-to-bottom order x = 3 ß1 y = x + 13 ß2 z = foo(x, y, 3) ß 3 newval = math.sqrt(z) ß7 result = x + newval ß8 print result ß9 def foo(a, b, c): temp = a + (b * c) temp = temp/3.0 return temp As part of 3, foo parameter a is bound to 3 (not x!) b is bound to 16 (not y!) c is bound to 4 ß 4 ß 5 ß 6

Ch 3: Variables and parameters are local A function s parameters are local variables. That is, they exist only during the execution of that function. Other variables that are assigned-to within a function body are also local (note: we ll see exception to this later) Top-level variables (not within defs) are called global def foo(a, b): >>> foo(3, 4) c = (a + b) * 2 14 return(c) >>> a Error >>> c IMPORTANT! Error Demo examples: lec5.py

Ch3: local/global variables Another way to say this is that each function has its own scope or namespace >>> def f(x): y = 1 x = x + y print(x) return x >>> x = 3 >>> y = 2 >>> z = f(x) 4 >>> x? 3 >>> y? 2 >>> z? 4 Function f s variable x. A local variable that exists only within scope of definition of f Top-level or global variable x Two completely different variables! Hint: think of them as, e.g., x f and x global

Ch3: Stack frames At top level (the interpreter/shell) a symbol table keeps track of all variables (and values bound to them) defined at that level When a function is called, a new symbol table, or stack frame, is created to keep track of variables defined in the function and the values associated with those names. This includes (1) the function s parameters and (2) any other variables assigned to within the function When the function finishes, the stack frame goes away. Note: this isn t just one level, of course. As functions call other functions that call other functions, the stack (of stack frames) grows deeper

Ch3: Stack frames >>> def f(x): y = 1 x = x + y print(x) return x f: x X3 4 y 1 >>> x = 3 >>> y = 2 >>> z = f(x) 4 >>> x >>> y >>> z Will demo/discuss more complex example Monday _main_: x 3 y 2 z 4

Ch 3: Fruitful functions Functions generally produce and return values: def add1(number): result = number + 1 return result Often use a variable to grab / catch the returned value so you can use it in further computations: >>> a = 3 >>> b = add1(5) >>> c = (a + b) ** 2 >>> c 81

Fruitful functions Though not covered in chapter 3, functions can also return more than one value via, e.g. return val1, val2, val3 def minandmax(a, b): if (a<b): return a, b else: return b, a >>> small, big = minandmax(23, 4) >>> small 4 >>> big 23 >>>

Ch 3: Fruitful functions. return vs print print and return are very different (and some students never quite get this!) def add1a(number): result = number + 1 return result def add1b(number): result = number + 1 print(result) Explore difference by using invoking them in shell in a couple of ways.

Ch 3: Why functions? Functions let you name a group of statements that make up a meaningful computation. Make programs easier to read and debug E.g. debug functions one by one, assemble into whole program Make programs shorter (in lines of code) by eliminating duplication. If you start seeing same (or nearly the same) segments of code in various places in your program, consider defining a function and replacing the common segment with function calls. Well designed functions often useful in other programs you write.

Ch 3: A few words on debugging For beginning programmers, I m not a big advocate of using real debuggers (the tools that come with IDEs like Wing and even IDLE). I m a fan of reading code carefully and critically and Using a lot of carefully placed print statements print early, print often - see where things go wrong! in HW1, if printed final results don t seem right, print out all your intermediate variables. Most people have variables for, e.g. driving time, gas needed, gas cost, hotel nights needed, etc. print them all! I am also a fan of baby steps : as I said earlier, write a little bit of code, then test, then a little more, and test again, etc. Small changes to a so-far-working program makes errors much easier to find. More to say on this later

Skip to Ch 5: Conditional execution Recall list of key programming components: Expressions, variables and assignment, functions, if-then-else (conditional execution), iteration We ve covered three of them! Can t easily compute a lot yet. Need the last two. Conditional execution today, iteration next week.

Ch5: Floor Division and Modulus But first, Ch5 has oddly placed section of two math ops (not really related to rest of Chapter!) Convenient right now, though, since useful for HW1 //: floor division operator. Divides two numbers and truncates (rounds down) to whole number 14 // 3 yields 4 8.1 // 2 yields 4.0 %: modulus operator. Yields remainder 11 % 3 yields 2 % can be very useful for, e.g., odd/even tests ((14 // 3) * 3) + (11 % 3) == 14 and in general (except for possible floating point issues), ((n // d) * d) + (n % d) == n math.ceil(x) yield nearest integer equal or greater than x. E.g. math.ceil(23.01) -> 24 and math.ceil(16.0) -> 16

Ch5: Boolean expressions and Logical Operators Demonstrated in Lec 3. Boolean expressions: have True/False as value E.g. 3 == (5 x) Boolean/relational operators: <, >, >=, <=,!= E.g. (x + y) < 4 Logical operators and, or, not E.g. (x < 5) or (z == foo ) Understand these well. Critical for conditional execution, decision making in programs!

Ch5: Condition execution - if The most basic conditional statement is if if (Boolean expression): lines of code that execute when Boolean expression evaluates to True more lines of code. These execute whether or not Boolean expression evaluated to True

Ch5: Condition execution if-else if (Boolean expression): code that executes when Boolean expr evaluates to True else: code that executes when Boolean not true code that executes after if-else statement, whether Boolean expression was True or not

print( before if ) if (a < b): print( a is smaller ) else: print( a is not smaller ) print( after if ) print( goodbye ) Ch5: if-else NOTE: indentation semantically important. Has execution effect.

HW1 Take baby steps: write a line or two test repeat! While debugging, use lots of print statements! Sometimes even worth printing values after every line of code! (remember to comment out/remove such debugging statements before submitting your homework) Consider using math.ceil() function for computing nights needed Some people have asked me about lastdayfraction in my HW1 suggested outline. There are many ways to do things. That s just how I think about it. Once the number of nights needed is known, what costs can we compute? Hotel costs breakfast cost, dinner cost Lunch cost I calculate a value lastdayfraction to enable lunch cost calculation string format method makes it easy to print nice complicated strings and to control number of decimal places in printed numbers: >>> item = bicycle >>> x = 423.1274123 >>> print( the cost of the {} is {:.2f}.format(item, x)) https://docs.python.org/3.7/library/string.html?highlight=string%20format#format-stringsyntax

HW1: function tripcost def tripcost(distancekm, vehspeedkph, kwhoursper100km, electricitycostperkwh, hotelcostpernight, breakfastcostperday, lunchcostperday, dinnercostperday) : totalcost = 0 # compute length of trip in hours hours = # compute kilowatt-hours needed kwhoursneeded = # compute electricity cost electricitycost = # compute number of nights (hotel stays) needed nights = This should be an integer! # perhaps compute a number (0.0->1) representing fraction of final day lastdayfraction = # compute breakfast, lunch, and dinner costs breakfastcost = # (lunchcost might involve more than one line and an if statement) dinnercost = # sum costs totalcost = # print results print(.)

Next time Finish conditional execution part of Chapter 5 nested conditionals, if-elif-else Note: the recursion section of Chapter 5 is very important but we will return to it later) start iteration (the last of the five key programming components). Read Ch 7

side note: be careful with floats! >>> v1 = 2 ** 64 >>> v2 = 2.0 ** 64 >>> v1 == v2 True >>> (v1-1) == (v2-1) False >>> (2.0 **64) == (2.0**64) + 1 True >>> (2.0**53) + 1 + 1 == (2.0**53) + 2 False Those might seem esoteric but consider: >>> (2.2 * 3) == 6.6 False >>>.1 +.1 +.1 ==.3 False Floating point representations are often not exact. It probably won t be an issue in this class, but keep in mind for the future. Be careful when comparing floating point numbers for equality with each other or with 0.0! Instead of x == 0.0 use comparisons like x <= abs(epsilon) for some suitably small value epsilon