1 / 26

An Overview of Programming in Python

An Overview of Programming in Python. CSC 161: The Art of Programming Prof. Henry Kautz 9 /9/ 2009. Slides stolen shamelessly from Dr. Mark Goadrich , Centenary College of Louisiana. Assignment s. Read the first chapter of the textbook Should be done! Assignment 1: Using IDLE

boris
Download Presentation

An Overview of Programming in Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary College of Louisiana

  2. Assignments Why Computer Science • Read the first chapter of the textbook • Should be done! • Assignment 1: Using IDLE • Turn in assignment sheet today or Monday • Only required to do steps 1 – 5 • See revised steps 6 – 7 on the course web page • Follow links from Calendar of Lectures, Workshops, & Labs • Doing these steps is optional • We will learn more about modules later in the course • Workshops start: September 13/14/15 • Required labs start: September 15/17

  3. 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.

  4. 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

  5. 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 • Our choice: Python • In principle, all programming languages can do the same things • 1 line of Python = 5 lines of Java = 20 lines of C = 100 lines of machine code • Pseudocode • Simple, (mostly) unambiguous English • A way of getting from an English description to a real programming language

  6. Elements of Programming • Five basic pieces to all programs • Data • User Interaction • Decisions • Repetition • Libraries

  7. Data Storage and Memory • Numbers 90 3.14159 2 + 3 / 4 * 5.6 • Strings "The quick brown fox" "$5.48" • Variables • age = 31 • cat = "Felix" • length = 5 • width = 10 • area = length * width

  8. 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)

  9. Decisions • Relate variables with logic (True, False) temperature > 90 legs == 2 and not human • Logic decides program path if temperature > 90: print "Must be summer again . . ." else: print "Looks like good weather." ?

  10. Repetition • Repeats commands whilea condition is true count = 10 while count > 0: print count count = count - 1 print "Blastoff!" ?

  11. Including Libraries • Import functions from other places import random import math • Use these functions to help our program if (random.random() < 0.5): print "Heads" else: print "Tails"

  12. 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

  13. Guess A Number Translation import random num = 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

  14. Guess A Number Translation import random num = 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

  15. Guess A Number Translation import random num = 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

  16. Guess A Number Translation import random num = 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

  17. Guess A Number Translation import random num = 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

  18. Guess A Number Translation import random num = random.randrange(100) guess = input("Try to guess my number 0-99: ”) whilenum != guess: if guess < num: print "Too Low!" else: print "Too High!" guess = input("Guess again: ”) print "Correct!"

  19. 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 . . .

  20. Guess A Number II Print instructions to the user Initialize high and low boundaries and status of guess While guess is incorrect, Formulate a new guess halfway between boundaries Ask for user feedback on guess If 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

  21. Elements of Python Why Computer Science • Literals • Numbers • 3. 14159265 • 42 • Strings • “Henry Kautz” • Variables • Holders for numbers, strings, lists, and other kinds of information • Must begin with a letter, followed by letters, numbers, or underscore _ characters • GradePointAverage • grade_point_average • gradePointAverage

  22. Expressions We use <Type> to indicate any thing that is of that type Why Computer Science • Made out of • Literals • Variables • Operators • (MidtermGrade + FinalGrade) / 2.0 • FirstName + “ “ + LastName • Functions (we’ll see one soon…) • Assignment Statements • <Variable> = <Expression> • AverageGrade = (MidtermGrade + FinalGrade) / 2.0 • FullName = FirstName+ “ “ + LastName

  23. How Assignments Work the first function we have seen today Why Computer Science • Evaluate the right-hand (expression) side • Put the value in the left hand (variable) side Count = 1 Count = Count + 1 what is the value of Count? • Assigning Input • <variable> = input(<prompt>) • guess = input(“What is your guess?”)

  24. Conditional Execution Why Computer Science ifguess < num: print "Too Low!” else: print "Too High!” General form: if <expression>: <indented lines of code> else: <indented lines of code>

  25. Loops Why Computer Science while num != guess: if guess < num: print "Too Low!” else: print "Too High!” General form: while <expression>: <indented lines of code>

  26. For Next Class Why Computer Science • Read Chapter 2 • Pre-read Chapter 3 • Finish Assignment 1 parts 1-5 if not already done • Try new version of parts 6-7 if you like • Remember: • Lab sessions become mandatory starting next week • Workshops start Sunday (or Mon/Tue)

More Related