Presentation is loading. Please wait.

Presentation is loading. Please wait.

First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.

Similar presentations


Presentation on theme: "First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An."— Presentation transcript:

1 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, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 First Python Program

2 Problem:– Find area of a disk
I.e., area of the part between inner circle and outer circle How to do manually? What value do you use for  ? What if your have a lot of disks? (i.e., with calculator) CS-1004, A-Term 2014 First Python Program

3 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

4 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

5 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

6 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

7 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

8 Questions? CS-1004, A-Term 2014 First Python Program

9 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

10 Assignment operator an action
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 CS-1004, A-Term 2014 First Python Program

11 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

12 Questions? CS-1004, A-Term 2014 First Python Program

13 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

14 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

15 Defining functions Indentation matters! return statement
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

16 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

17 Questions? CS-1004, A-Term 2014 First Python Program

18 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


Download ppt "First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An."

Similar presentations


Ads by Google