your first python program - wpiweb.cs.wpi.edu/.../week0_firstpythonprogram.pdfalmost everything you...

18
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) First Python Program CS-1004, A-Term 2014 1

Upload: others

Post on 05-Aug-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

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, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute)

First Python Program CS-1004, A-Term 2014 1

Page 2: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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?

What value do you use for ?

What if your have a lot of disks?

First Python Program CS-1004, A-Term 2014 2

(i.e., with calculator)

Page 3: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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”

First Python Program CS-1004, A-Term 2014 3

Page 4: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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

First Python Program CS-1004, A-Term 2014 4

Page 5: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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

First Python Program CS-1004, A-Term 2014 5

Page 6: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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

First Python Program CS-1004, A-Term 2014 6

Page 7: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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”

First Python Program CS-1004, A-Term 2014 7

Page 8: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute

Questions?

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

Page 9: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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

First Python Program CS-1004, A-Term 2014 9

Page 10: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

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

First Python Program CS-1004, A-Term 2014 10

Page 11: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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

First Python Program CS-1004, A-Term 2014 11

Page 12: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute

Questions?

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

Page 13: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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!

First Python Program CS-1004, A-Term 2014 13

Page 14: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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

First Python Program CS-1004, A-Term 2014 14

Page 15: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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

First Python Program CS-1004, A-Term 2014 15

Page 16: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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!

First Python Program CS-1004, A-Term 2014 16

Page 17: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute

Questions?

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

Page 18: Your First Python Program - WPIweb.cs.wpi.edu/.../Week0_FirstPythonProgram.pdfAlmost everything you do in Python will be part of a function Almost every program your submit for homework

Carnegie Mellon Worcester Polytechnic Institute 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

First Python Program CS-1004, A-Term 2014 18