overview questions
play

Overview/Questions What is an algorithm? How do we go about - PDF document

CS101 Lecture 21: Writing Simple Programs: Algorithms, The Software Development Process Python Names, Expressions, Input, and Output Aaron Stevens 18 March 2009 1 Overview/Questions What is an algorithm? How do we go about writing a


  1. CS101 Lecture 21: Writing Simple Programs: Algorithms, The Software Development Process Python Names, Expressions, Input, and Output Aaron Stevens 18 March 2009 1 Overview/Questions – What is an algorithm? – How do we go about writing a program? – What are the elements that make up a program? 2 1

  2. Algorithms Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of time, and using a finite amount of data . For a given set of inputs, the algorithm will always produce the same outputs. 3 Following an Algorithm Grandpa Ron’s Chocolate Kahlua Mudslides Ingredient List:  ½ cup vodka  ½ cup Kahlua  ½ cup Bailey’s Irish Cream  ¼ cup Hershey’s chocolate syrup  1 blender full of ice Process: Fill blender with ice. Pours other ingredients onto ice. Blend until smooth consistency. Pour into glasses and enjoy. 4 2

  3. Following an Algorithm Algorithm for preparing Chocolate Mudslides: If not all ingredients are present Go to store and buy missing ingredients Fill blender with ice Pour other ingredients into the blender While (not smooth) Turn on blender Wait 10 seconds Turn off blender Test consistency with wooden spoon Pour into glasses and serve 5 Pseudo code Pseudo code is a way of expressing algorithms that uses a mixture of English phrases and indention to make the steps in the solution explicit. – There are no grammar rules in pseudocode. – Pseudocode is not case sensitive. Analogy: pseudo code is like an outline for writing a paper -- it contains all of the main ideas, but is not tied to the structure of grammar, etc. 6 3

  4. Software Development Example Problem Description Suppose you’re going to visit Mexico. You want to go shopping, but the prices are in “pesos”, and you need to know if the prices are good deals before you buy. What information do you need to solve this problem? 7 3 Software Development Example Design Algorithm Suppose you observe that a currency exchange offers you 100 pesos for 7 American dollars. We can express this relationship as: 100 P = 7 D 8 3 4

  5. Software Development Example Refine Algorithm This relationship 100 P = 7 D implies a conversion ratio, either pesos or dollars: pesos = 100 / 7 dollars or dollars = 7 / 100 pesos 9 3 Input, Process, Output Design Pattern A reusable model for a program. Input, Process, Output pattern – prompt the user for some input – apply algorithm to the input – display output back to the user 10 5

  6. Input, Process, Output Completed algorithm: – input the price in pesos – calculate dollars = 7 / 100 * pesos – output dollars 11 The Completed Program 12 6

  7. Elements of Programs Comments Lines which are ignored by the interpreter. Programmers use comments to document their programs, and to remove troublesome lines during testing. Any part of a line after the # symbol is a comment Example: # currencyconversion.py # A program to convert a price in pesos to dollars. 13 Elements of Programs Keywords Special words reserved by the language. Keywords are used to perform basic computation functions, and are one of the building blocks of programs. Example: print # The word ‘print’ is a Python keyword print “Hello, world!” 14 7

  8. Elements of Programs Identifiers Programmers use names to identify modules (e.g. currencyconversion) and variables (e.g. pesos) within programs. Variables Give names to values within programs 15 Variables A variable is a spot in memory which can hold a value. – The value is stored using a name we chose. – We can recall it when needed to use the value, or change it to hold something else. x = 42 16 8

  9. Elements of Programs Identifiers in Python follow these rules: – Must begin with a letter or underscore _ – May contain letters, numbers, underscores – May not contain spaces, punctuation, etc. – May not be Python keywords – Are case sensitive 17 Elements of Programs Valid Identifier Examples: x pesos dollars dollarsAndCents dollars_and_cents main Invalid Identifier Examples: 9x print 18 9

  10. Elements of Programs Expressions Any statement which produces a value. Values can be numeric, text, or other types. Variables, too are expressions. Examples (each line is a valid Python statement): input("Enter a price in pesos: ") 7.0 / 100.0 * pesos dollars 19 Arithmetic Operators Python built-in numeric operations: + addition - subtraction * multiplication / division ** exponentiation % remainder (modulo) The order of operations is the familiar PEMDAS. 20 10

  11. Output Statements The simplest form of output from a program is a print statement, which prints text to the console window. print takes a variable number of arguments, separated by commas: print print <expr> print <expr>,<expr>, ... , <expr> print <expr>,<expr>, ... , <expr>, 21 Assignment Statements The process of giving a value to a variable. General form: <variable> = <expr> Examples (each line is an assignment statement): x = 5 dollars = 7.0 / 100.0 * pesos word = “hello” 22 11

  12. Assignment Statements A variable can be assigned many times during a Python program or interactive session (called reassignment). It always holds the most recent value. Examples: x = 5 print x x = x + 1 print x 23 Input Statements An input statement uses the special built-in expression called input . input takes the general form: <variable> = input(<prompt>) Examples: pesos = input(“Enter the price in pesos: ”) name = raw_input(“What is your name? ”) NOTE: input evaluates the input as an expression, whereas raw_input does not. For text input, you will want to use raw_input instead. 24 12

  13. Input Statements When Python encounters an input statement, the interpreter pauses, and waits for the user to input an expression (value), terminated by the <Enter> key. Only after this input does processing continue. 25 The Completed Program (again) 26 13

  14. Take-Away Points – Algorithms – Software Development Process – Design Patterns – Elements of Python Programs  Identifiers  Expressions (and Arithmetic)  Variables  Output  Input 27 Student To Dos –Readings:  Python readings will be taken from a free book called How to Think Like A Computer Scientist: Learning with Python Available online at: http://openbookproject.net//thinkCSpy/  Ch02 (Wednesday), Ch03 (Friday/Monday) –HW08 is due Wednesday 3/18 28 14

Recommend


More recommend