Carnegie Mellon Worcester Polytechnic Institute First Python Program

  • Slides: 18
Download presentation
Carnegie Mellon Worcester Polytechnic Institute First Python Program Professor Hugh C. Lauer CS-1004 —

Carnegie Mellon Worcester Polytechnic Institute 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 Worcester Polytechnic Institute) CS-1004, A-Term 2014 First Python Program 1

Carnegie Mellon Worcester Polytechnic Institute Problem: – Find area of a disk ¢ ¢

Carnegie Mellon Worcester Polytechnic Institute 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? CS-1004, A-Term 2014 First Python Program 2

Carnegie Mellon Worcester Polytechnic Institute Problem: – Find area of a disk (continued) ¢

Carnegie Mellon Worcester Polytechnic Institute 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” CS-1004, A-Term 2014 First Python Program 3

Carnegie Mellon Worcester Polytechnic Institute IDLE — Integrated Development Environment ¢ ¢ Simple GUI

Carnegie Mellon Worcester Polytechnic Institute 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 CS-1004, A-Term 2014 First Python Program 4

Carnegie Mellon Worcester Polytechnic Institute More terms — Compiler ¢ A computer program that

Carnegie Mellon Worcester Polytechnic Institute 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 CS-1004, A-Term 2014 First Python Program 5

Carnegie Mellon Worcester Polytechnic Institute More terms — Interpreter ¢ A computer program that

Carnegie Mellon Worcester Polytechnic Institute 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 CS-1004, A-Term 2014 First Python Program 6

Carnegie Mellon Worcester Polytechnic Institute Expressions ¢ ¢ ¢ When typed into Python “shell”,

Carnegie Mellon Worcester Polytechnic Institute 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” CS-1004, A-Term 2014 First Python Program 7

Carnegie Mellon Worcester Polytechnic Institute Questions? CS-1004, A-Term 2014 First Python Program 8

Carnegie Mellon Worcester Polytechnic Institute Questions? CS-1004, A-Term 2014 First Python Program 8

Carnegie Mellon Worcester Polytechnic Institute Variables ¢ Definition: – a symbolic name used to

Carnegie Mellon Worcester Polytechnic Institute 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 CS-1004, A-Term 2014 First Python Program 9

Carnegie Mellon Worcester Polytechnic Institute Assignment operator ¢ ¢ ¢ an n o i

Carnegie Mellon Worcester Polytechnic Institute Assignment operator ¢ ¢ ¢ an n o i t ac variable. Name = any_valid_Python_expression Makes variable. Name 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 CS-1004, A-Term 2014 First Python Program 10

Carnegie Mellon Worcester Polytechnic Institute Print statement ¢ Example § print(3 + 2) §

Carnegie Mellon Worcester Polytechnic Institute 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 § … § CS-1004, A-Term 2014 First Python Program 11

Carnegie Mellon Worcester Polytechnic Institute Questions? CS-1004, A-Term 2014 First Python Program 12

Carnegie Mellon Worcester Polytechnic Institute Questions? CS-1004, A-Term 2014 First Python Program 12

Carnegie Mellon Worcester Polytechnic Institute Function ¢ ¢ ¢ Definition: A sequence of Python

Carnegie Mellon Worcester Polytechnic Institute 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! CS-1004, A-Term 2014 First Python Program 13

Carnegie Mellon Worcester Polytechnic Institute Calling (i. e. , invoking) a function ¢ ¢

Carnegie Mellon Worcester Polytechnic Institute 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 CS-1004, A-Term 2014 First Python Program 14

Carnegie Mellon Worcester Polytechnic Institute Defining functions ¢ Indentation matters! § Python uses indentation

Carnegie Mellon Worcester Polytechnic Institute 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 CS-1004, A-Term 2014 First Python Program 15

Carnegie Mellon Worcester Polytechnic Institute Note ¢ ¢ “print” and “input” are functions! Almost

Carnegie Mellon Worcester Polytechnic Institute 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! CS-1004, A-Term 2014 First Python Program 16

Carnegie Mellon Worcester Polytechnic Institute Questions? CS-1004, A-Term 2014 First Python Program 17

Carnegie Mellon Worcester Polytechnic Institute Questions? CS-1004, A-Term 2014 First Python Program 17

Carnegie Mellon Worcester Polytechnic Institute This weekend ¢ ¢ ¢ Install Python 3. 4

Carnegie Mellon Worcester Polytechnic Institute 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 CS-1004, A-Term 2014 First Python Program 18