Carnegie Mellon Worcester Polytechnic Institute Elements of a














- Slides: 14
Carnegie Mellon Worcester Polytechnic Institute Elements of a Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2 nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 Elements of a Python Program 1
Carnegie Mellon Worcester Polytechnic Institute Python Language ¢ ¢ Every input line (plus its continuations, if any) defines some action by Python interpreter § I. e. , line causes interpreter to DO something § I. e. , effect some change in state Examples already encountered: – § Expression: – evaluate the expression and print result on output § Assignment associate a value or object with a name ¢ Three more this week: – § Function definitions § Function calls § Definite loops CS-1004, A-Term 2014 Elements of a Python Program 2
Carnegie Mellon Worcester Polytechnic Institute Function ¢ ¢ A sequence of one or more lines of Python code that can be invoked as a group under a single name A way of encapsulating a subset of a program that is used repeatedly … … so that the user (or using program) § doesn‘t have to write it multiple times § doesn‘t have to think about it works again § doesn’t have to even know how it works at all It is not humanly possible for an ordinary person to hold an entire program in his/her head at one time! § Therefore, we partition it into functions — many small functions! CS-1004, A-Term 2014 Elements of a Python Program 3
Carnegie Mellon Worcester Polytechnic Institute Defining functions Colon denotes next line def function. Name( ): is continuation python statement. . . python statement return <optional expression> Each continuation line is indented one “unit” End of function definition denoted by reversion to previous indentation level ¢ ¢ Note: continuation lines may, themselves, have continuations § Indented two units of continuation In general, continuation of n-level indentation is (n+1) units of indentation CS-1004, A-Term 2014 Elements of a Python Program 4
Carnegie Mellon Worcester Polytechnic Institute “Calling” a function ¢ ¢ ¢ a. k. a. , “invoking” a function Function_name followed by parentheses § Containing zero or more “arguments” Meaning: – § “DO the function” § I. e. , execute its body as if you had typed it into a Python “shell” § Or in an expression ¢ ¢ Definition: BODY (of a function) § The statements following the “: ” § Ending with the last indented line Examples: – § Area of disk § Greet function (p. 11 of textbook) CS-1004, A-Term 2014 Elements of a Python Program 5
Carnegie Mellon Worcester Polytechnic Institute So what’s with the parentheses? ¢ Any function may be designed to take arguments § I. e. , a means of conveying information into a function by caller ¢ Examples: § sin(radians) # Sine of that angle § sqrt(non. Negative. Number) # square root § atan(number) # arctangent CS-1004, A-Term 2014 Elements of a Python Program 6
Carnegie Mellon Worcester Polytechnic Institute So what’s with the parentheses? (continued) ¢ ¢ Every function definition must include zero or more parameters between the parentheses § I. e. , identifiers separated by commas § Place to receive the information provided by caller Example § Area of disk Most functions should have one or more return statements § Means of communicating result back to caller § Needed if function call is part of expression More about parameters and returns later in course CS-1004, A-Term 2014 Elements of a Python Program 7
Carnegie Mellon Worcester Polytechnic Institute So what’s with the parentheses? (continued) Parentheses on function definition: – § Place to put the parameters § Error to leave off parameters Parentheses on function call: – a. Place to put arguments Each argument is expression Separated by commas b. Tells Python: – Evaluate arguments in order Assign to corresponding parameter Suspend whatever it was doing Interpret body of function (using argument values) Deliver (optional) result back to caller Resume whatever it was previously doing Example CS-1004, A-Term 2014 Elements of a Python Program 8
Carnegie Mellon Worcester Polytechnic Institute Questions? CS-1004, A-Term 2014 Elements of a Python Program 9
Carnegie Mellon Worcester Polytechnic Institute Digression ¢ ¢ Homework #2 (and all subsequent assignments) are to be completed by two-person teams Please choose a partner § Send partner information to cs 1004 -staff@cs. wpi. edu ¢ ¢ If you do not have a partner by the time HW 2 is assigned (on Thursday), … … a partner will be assigned to you! CS-1004, A-Term 2014 Elements of a Python Program 10
Carnegie Mellon Worcester Polytechnic Institute Module ¢ ¢ A separate file with. py extension in name Contains function definitions § And possibly other stuff Packaged for convenient organization § And sanity on part of programmer § And programmer’s boss or advisor! Lauer’s advice to rising programmers: – § Any program that is more than a couple of pages in length is too long to wrap your head around! § Divide it into smaller “modules” organized around related groups of functions CS-1004, A-Term 2014 Elements of a Python Program 11
Carnegie Mellon Worcester Polytechnic Institute Reading Assignments ¢ ¢ Study § 1. 7 § Especially range, for-loop § Note specification of a module Chapter 2, § 2. 1– 2. 5 § Need to complete Chapter 2 by end of week! CS-1004, A-Term 2014 Elements of a Python Program 12
Carnegie Mellon Worcester Polytechnic Institute Questions? CS-1004, A-Term 2014 Elements of a Python Program 13
Carnegie Mellon Worcester Polytechnic Institute Laboratory assignment tomorrow ¢ Define and use functions ¢ Including with parameters ¢ Will also use Definite loops § I. e. , a for-loop with a fixed number of iterations § Read § 1. 7 in textbook CS-1004, A-Term 2014 Elements of a Python Program 14