First 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 ) 1
Problem: Find area of a disk I.e., area of the part between inner circle and outer circle How to do manually? (i.e., with calculator) What value do you use for? What if your have a lot of disks? 2
Problem: Find area of a disk (continued) What about Excel? This is, in fact, a form of programming! Change value of cell Excel reacts by changing values of all cells depending on that one! A functional language 3
IDLE Integrated Development Environment Simple GUI for Python programmers Python shell I.e., command interpreter Intelligent file editor Understands Python syntax and formatting Debugger Single-step View values of variables 4
More terms Compiler A computer program that reads another program line-by-line, character-by-character analyzes it, and converts it into internal machine instructions and data 5
More terms Interpreter A computer program that reads one or more lines of another program analyzes just those lines and executes them in place Much in common with compiler The Python shell is really an interpreter 6
Expressions When typed into Python shell, evaluated immediately Print Result Much like (the right side of) any scientific or engineering equation Operators +, -, *, /, ** Special operator: // Example: Area of disk Inner radius = 3, outer radius = 5 What value to use for? import Python s own value called pi 7
Questions? 8
Variables Definition: a symbolic name used to refer to a value or to some computational object Starts with a letter or underscore Followed by any sequence of letters, digits, and underscore Case sensitive May not be reserved word i.e., keyword See Table 2.1 (for all 33 keywords) Subtly different from variables in C, Java, etc. Python variable refers to or points to a value or object C/Java variable is name of a location where value or object is stored Using a variable Anywhere in an expression or Python statement where the value could have been used instead Examples 9
Assignment operator variablename = any_valid_python_expression Makes variablename refer to the value of that expression A specific action that happens at a specific time I.e., when the Python interpreter gets to that particular statement Variable retains what it refers to until changed (by another assignment operator) Like C and Java Unlike Excel Example Inner and outer radii of disk 10
Print statement Example print(3 + 2) print( The sum of 3 and 2 is, 3 + 2) May be any sequence of values Converted into printable strings Printed on output device E.g., The IDLE window 11
Questions? 12
Function Definition: A sequence of Python statements that takes zero or more arguments and (optionally) returns a value that can be used in a Python expression Useful when you need to repeat the same computation on different values Example: area of disk Also useful when you want to solve a (sub) problem and then put it out of your mind while working on another problem Especially useful when sharing code With teammates, colleagues, etc. With people you don t know! 13
Calling (i.e., invoking) a function Function name used in Python expression or statement Meaning: Suspend whatever you were doing Execute the function with the argument values in place of parameters When function returns, use its return value at the place where it was called. Examples Area of disk 14
Defining functions Indentation matters! Python uses indentation to recognize when still reading part of function vs whatever comes later! return statement Tells Python interpreter to stop interpreting the function and go back to what it was previously doing (Optionally) delivers a value back to caller to be used at the point where function was called 15
Note print and input are functions! Almost everything you do in Python will be part of a function Almost every program your submit for homework will consist of one or more functions Functions can (and usually do) call each other! 16
Questions? 17
This weekend Install Python 3.4 on your own computer or laptop See course website for cookbook DO IT TODAY! GET HELP IF YOU NEED IT Homework #1 Available on Course web-site Need WPI password to access Start ASAP Due next Thursday, 6:00 PM Read Chapter 1 Responsible for entire contents of chapter in quizzes 18