27
An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana http://mark.goadrich.com NWLAPCUG - May 15th 2008

An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana NWLAPCUG - May 15th 2008

Embed Size (px)

Citation preview

Page 1: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

An Introduction toPython Programming

Dr. Mark Goadrich

Centenary College of Louisiana

http://mark.goadrich.com

NWLAPCUG - May 15th 2008

Page 2: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

I’m Thinking of a Number…

• Can we guess the computer’s secret number between 0 and 100?

• Can the computer guess our secret number between 0 and 100?

• We need a common language to communicate.

Page 3: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Guess A Number Pseudocode

Pick a random number for the computer

Ask the user to guess the computer’s number

While they guess wrong,

Give feedback if number is too high or low

Ask for another guess from the user

Tell the user they are correct

Page 4: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Language of Programming

• English is a natural language– casual, slang, vague– “I went to the bank.” (money or river?)

• Computers need formal languages– strict, specific, clear, unambiguous

• Many language choices: C++, Java, Prolog, Scheme, Perl, Fortran, Cobol, Ruby, etc…

Page 5: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Python Programming

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 6: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

How to Talk to your Computer

Application CPU

User

Programmer

Page 7: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Python Interpreter

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 8: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Python and IDLE

Page 9: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Programming Components

• Five basic pieces to all programs – Data– User Interaction– Decisions– Repetition– Libraries

Page 10: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Data Storage and Memory

• Numbers903.141592 + 3 / 4 * 5.6

• Strings"The quick brown fox""$5.48"

• Variablesage = 31cat = "Felix"length = 5width = 10area = length * width

Page 11: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

User Interaction

• We need to ask the user questions

age = input("How old are you? ")name = raw_input("What is your name? ")

• We want to tell the user the results

print "In dog years, you are " + str(age * 7)

Page 12: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Decisions

• Relate variables with logic (True, False)

temperature > 90legs == 2 and not tall

• Logic decides program path

if temperature > 90:print "Must be summer again . . ."

else:print "Looks like good weather."

?

Page 13: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Repetition

• Repeats commands whilea condition is true

count = 10while count > 0:

print countcount = count - 1

print "Blastoff!"

?

Page 14: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Including Libraries

• Import functions from other placesimport randomimport math

• Use these functions to help our programif (random.random() < 0.5):

print "Heads"else:

print "Tails"

Page 15: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Guess A Number Translation

Pick a random number for the computer

Ask the user to guess the computer’s number

While they guess wrong,

Give feedback if number is too high or low

Ask for another guess from the user

Tell the user they are correct

Page 16: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Guess A Number Translation

import randomnum = random.randrange(100)Ask the user to guess the computer’s number

While they guess wrong,

Give feedback if number is too high or low

Ask for another guess from the user

Tell the user they are correct

Page 17: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Guess A Number Translation

import randomnum = random.randrange(100)guess = input("Try to guess my number 0-99: ")

While they guess wrong,

Give feedback if number is too high or low

Ask for another guess from the user

Tell the user they are correct

Page 18: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Guess A Number Translation

import randomnum = random.randrange(100)guess = input("Try to guess my number 0-99: ")

while num != guess:

Give feedback if number is too high or low

Ask for another guess from the user

Tell the user they are correct

Page 19: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Guess A Number Translation

import randomnum = random.randrange(100)guess = input("Try to guess my number 0-99: ")

while num != guess:if guess < num:

print "Too Low!"else:

print "Too High!"Ask for another guess from the user

Tell the user they are correct

Page 20: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Guess A Number Translation

import randomnum = random.randrange(100)guess = input("Try to guess my number 0-99: ")

while num != guess:if guess < num:

print "Too Low!"else:

print "Too High!"guess = input("Guess again: ")

Tell the user they are correct

Page 21: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Guess A Number Translation

import randomnum = random.randrange(100)guess = input("Try to guess my number 0-99: ")

while num != guess:if guess < num:

print "Too Low!"else:

print "Too High!"guess = input("Guess again: ")

print "Correct!"

Page 22: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Running our Code

• Go to IDLE

• Open guess1.py

• Press F5 to run the program

Page 23: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Part II - Computer Guesses

• How can the computer guess our number?

• The same way we guessed– Start out with a range of numbers– Each guess, split the answers in half– Eventually, the range will be one number

• We’re now moving past programming to computer science . . .

Page 24: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Guess A Number II

Print instructions to the userInitialize high and low boundaries and status of guess

While guess is incorrect,Formulate a new guess halfway between boundariesAsk for user feedback on guessIf guess too high, reset upper boundary

If guess too low, reset lower boundary If correct, update status of guess

Otherwise ask for valid input from the user

When guess is correct, tell the user and exit

Page 25: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Guess A Number II

print "Pick a number between 0 and 99, I will guess it."print "If I am high, type H, low, type L, and correct type C."low, high, correct = 0, 100, Falsewhile not correct:

guess = (high + low) / 2answer = raw_input("I guess " + str(guess) + ", HLC? ")if answer == "H": high = guesselif answer == "L": low = guesselif answer == "C": correct = Trueelse: print "I don't understand, please enter H, L or C."

print "I found it!"

Page 26: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Other Examples

• Chaos and Fractals

• Can’t Stop the Monkeys

Page 27: An Introduction to Python Programming Dr. Mark Goadrich Centenary College of Louisiana  NWLAPCUG - May 15th 2008

Further References

• Python Home Pagehttp://python.org

• How to Think Like a (Python) Programmerhttp://thinkpython.com

• Graphics Package for Pythonhttp://cs1graphics.org