Plan for the week Week 2 Sept 1

  • Slides: 24
Download presentation
Plan for the week: Week 2, Sept 1 -5 l Understanding program structure Ø

Plan for the week: Week 2, Sept 1 -5 l Understanding program structure Ø Defining, testing, calling functions Ø How to run a program, how someone runs yours l Understanding more of Python the language Ø Types: string, int, bool, float Ø Operations on these, e. g. , +, %, [: ], and Ø Control: conditionals and loops (Thursday) Course structure: APTs, labs, assignments Ø Tools for enabling structure l Compsci 101, Fall 2014 3. 1

Examples of functions Compsci 101, Fall 2014 3. 2

Examples of functions Compsci 101, Fall 2014 3. 2

Functions explained l In a calculator, sqrt: number in -> number out Ø What

Functions explained l In a calculator, sqrt: number in -> number out Ø What is domain, what is range? l In MSWord, word count: document -> number Ø Domain is word doc, range is integer l In browser, web: URL -> HTML formatted "page" Ø Domain is valid URL, range is HTML resources l In Python we see similar structure! Compsci 101, Fall 2014 3. 3

Abstracting over code: functions l l l http: //goo. gl/Dfc. Pg. I See snarf

Abstracting over code: functions l l l http: //goo. gl/Dfc. Pg. I See snarf for class work as well These functions do not return values, they print Ø Illustrates problem decomposition, but … Ø Normally have each function return a value Ø Normally use the return value in function call Compsci 101, Fall 2014 3. 4

Part of http: //goo. gl/Dfc. Pg. I (and snarf) def eieio(): print "Ee-igh, oh!"

Part of http: //goo. gl/Dfc. Pg. I (and snarf) def eieio(): print "Ee-igh, oh!" def refrain(): print "Old Mac. Donald had a farm, ", eieio() Lots of commas def had_a(animal): print "And on his farm he had a", animal, ", ", eieio() Compsci 101, Fall 2014 3. 5

Anatomy and Dissection of Print l Print generates output to a console, window, …

Anatomy and Dissection of Print l Print generates output to a console, window, … Ø Depends on how program invoked Ø Basically used for: help with debugging and creating output for copy/paste, view print "hello, ", x, "what's up", y l Space inserted between comma-separated items Ø Can use string concatentation, "hello"+str(x) If statement ends with comma, no newline Ø Print anything that has a string representation… Compsci 101, Fall 2014 Ø 3. 6

Tracing program execution l The def statement defines a function Ø Ø Creates a

Tracing program execution l The def statement defines a function Ø Ø Creates a name that can be used in program Name encapsulates program statements, creates its own environment for running code • Variables, parameters, local to the function Function name and statements part of Python execution environment Ø Can call or invoke the function Ø If parameters needed, must pass values for each Ø Visualize program execution: Python. Tutor, Compsci 101, Fall 2014 brain l 3. 7

Abstraction over barnyards l In Old. Mac. Print we have pig() and fox() …

Abstraction over barnyards l In Old. Mac. Print we have pig() and fox() … What's the same in these? What's different? Ø Capture differences in parameters/variables Create new function: Ø def verse(animal, noise) Ø l l Look at pig() and fox() create new function Ø Call: verse("horse", "neigh") Ø Call: verse("cow", "moo") http: //bit. ly/101 fall 14 -0902 -1 Compsci 101, Fall 2014 3. 8

Nancy Leveson: Software Safety l Mathematical and engineering aspects, invented the discipline Ø Health

Nancy Leveson: Software Safety l Mathematical and engineering aspects, invented the discipline Ø Health care software Ø MS Word, Airbus 360, … “There will always be another software bug; never trust human life solely on software” huffington post? l l Therac 25: Radiation machine Ø http: //en. wikipedia. org/wiki/Ther ac-25, http: //bit. ly/5 q. Ojo. H Software and steam engines Compsci 101, Fall 2014 3. 9

Compsci 101: Running Python l What does it mean to run a program? Ø

Compsci 101: Running Python l What does it mean to run a program? Ø What does clicking on app/program do? Ø How do you run/test APT code, other Python code • Where does program start to execute/run? l Control flow in Python Ø Loops and if statements --- coming on Thursday Ø Essential for writing real programs • But function calls are part of control flow Compsci 101, Fall 2014 3. 10

Functions that return values l Most functions return values Ø Example in Old Mac.

Functions that return values l Most functions return values Ø Example in Old Mac. Donald is "different" Ø Some claim: all functions return values def inch 2 centi(inches): return 2. 54*inches xh = inch 2 centi(72) def pluralize(word): return word + "es" pf = pluralize("fish") Compsci 101, Fall 2014 3. 11

What is an APT? BMI APT l l Automated/Algorithmic Problem Testing Ø Write one

What is an APT? BMI APT l l Automated/Algorithmic Problem Testing Ø Write one function, 2 -30 lines, solve a problem Ø Tested automagically in Eclipse or the browser Ø Test test … Quality of code not an issue Start simple, build toward more complex Ø What is a function? A function call? Ø What is a parameter? Argument? Ø How do you run/execute a program Compsci 101, Fall 2014 3. 12

How to solve an APT l Two very, very important steps 1. How to

How to solve an APT l Two very, very important steps 1. How to solve the problem without computer Paper, Pencil, (Calculator) 2. l l How to translate problem-solving to Python Both steps can be hard, vocabulary and language are initially a real barrier Ø More Python experience, easier step 2 becomes Ø With experience, step 2 can influence step 2 Step 1 is key, without it you won’t get anywhere Compsci 101, Fall 2014 3. 13

Anatomy of a Python function def name(params): body l Define a function: name, parameters,

Anatomy of a Python function def name(params): body l Define a function: name, parameters, body Ø How do we decide on these? Ø Do we need parameters? Ø What does body of function do l Functions provide a named abstraction over code Ø Huh? math. factorial(5)"hello". upper() Compsci 101, Fall 2014 3. 14

Functions: BMI (Body Mass Index) l What is formula? How to use it? Ø

Functions: BMI (Body Mass Index) l What is formula? How to use it? Ø For one person can simply print the BMI • Make sure units are correct, formula right Ø Ø What if we want to validate data? What if we want to notify folks who might need guidance? call replaced by return value, why use function? def bmi(weight, height): return 703. 07 * weight/(height*height) if bmi(170, 72) < 18. 5: print "underweight" Compsci 101, Fall 2014 3. 15

What does return statement do? l Programs execute one line at a time Ø

What does return statement do? l Programs execute one line at a time Ø After one statement finishes, the next executes Ø Calling a function causes its code to execute • What happens in the code that calls the function? l The value returned replaces the function call Ø print math. sqrt(25. 0) Ø if bmi(170, 72) < 18. 5: print "underweight" l What if nothing returned? Ø None by default in Python Compsci 101, Fall 2014 3. 16

Re-use: Counting words in file def word_count(filename): f = open(filename) all = f. read()

Re-use: Counting words in file def word_count(filename): f = open(filename) all = f. read() words = all. split() return len(words) if __name__ == "__main__": name = "/data/romeo. txt" print "# words in", name, print "=", word. Count(filename) Compsci 101, Fall 2014 3. 17

Running Python Program/Module l l Python is an interpreter, platform specific Ø So is

Running Python Program/Module l l Python is an interpreter, platform specific Ø So is Java, so is Android, … contrast compilers Ø Python can execute a. py file, need a "launch point" Convention in Python and other languages Ø Start with section labeled __main__, that's run if __name__ == "__main__": statements here l Boilerplate, don't memorize, let Eclipse do work! Compsci 101, Fall 2014 3. 18

Function calls: what is an API? http: //www. enotes. com/shakespeare-quotes/vasty-deep Compsci 101, Fall 2014

Function calls: what is an API? http: //www. enotes. com/shakespeare-quotes/vasty-deep Compsci 101, Fall 2014 3. 19

Eclipse Particulars l Supports many languages: we care about Python Ø Py. Dev perspective:

Eclipse Particulars l Supports many languages: we care about Python Ø Py. Dev perspective: Windows>Open Perspective>Other>… Ø Also use console: Windows>Show View>Console Ø Use Py. Dev console (right click console icon) l Creating projects, Python Module Ø Illustrated with examples in class Submitting and check via Ambient Ø Illustrated with examples in class l Compsci 101, Fall 2014 3. 20

A-Z, Soup to Nuts, APT all the way l Where do we find what

A-Z, Soup to Nuts, APT all the way l Where do we find what APTs are due this week? Ø Web pages, Sakai v Google v bookmark l Testing code for APTs supplied by 101 staff Ø Snarf the project that provides testing harnass Ø Don't call us, ETester. py will call you (your code) l Refresh to see results. html Ø Repeat until finished Submit using Ambient, Duke CS Eclipse plugin l Compsci 101, Fall 2014 3. 21

Summary of Today l Functions help in program/problem decomposition Ø Each function does one

Summary of Today l Functions help in program/problem decomposition Ø Each function does one thing only Ø Functions typically return values • Song printing functions don't, they print l Names, parameters, arguments, return values Ø Functions execute, return replaces call point Ø Calling code picks up and continues after call l We'll see loops and conditionals on Thursday Compsci 101, Fall 2014 3. 22

Grace Murray Hopper (1906 -1992) l “third programmer on world's first large-scale digital computer”

Grace Murray Hopper (1906 -1992) l “third programmer on world's first large-scale digital computer” Ø l US Navy: Admiral “It's better to show that something can be done and apologize for not asking permission, than to try to persuade the powers that be at the beginning” ACM Hopper award given for contributions before 35 2010: Craig Gentry: http: //www. youtube. com/watch? v=qezm. Ho. PW 30 Compsci 101, Fall 2014 2011: Luis von Ahn 2013: Pedro Felzenszwalb 3. 23

Duke Compsci: Grace Hopper 2013 Compsci 101, Fall 2014 3. 24

Duke Compsci: Grace Hopper 2013 Compsci 101, Fall 2014 3. 24