Input, Processing and Output Bonita Sharif 1 Review A program is a set of instructions a computer follows to perform a task The CPU is responsible for running and executing programs A set of instructions that a CPU can decode is called the CPU s instruction set Main memory is where data and programs are stored Data is stored using the binary numbering system CPU goes through the fetch-decode-execute cycle to execute programs 2 Review Every language has keywords, operators and syntax rules A program consists of a set of statements A statement consists of programming elements such as key words and operators arranged in the proper sequence to form an operation An interpreter is a program that can read programming statements and execute them Review Syntax errors occur if you violate the rules a language has for writing program statements Python can be used in the interactive and script mode 3 4 Typical Software Development cycle 1. Design the program 2. Write the code 3. Correct syntax errors 4. Test the program 5. Correct logic errors (debug) A logic error is an error in a program that produces incorrect results. The program will still run. Designing a Program 1. Understand the task that the program is to perform (Requirements collection) 2. Determine steps that must be taken to perform the task (algorithm) Pseudocode and flow charts are used to create models of a program 5 6 1
Algorithm An ordered well-defined set of steps taken to perform a task Example: Calculate and display gross pay for an hourly paid employee Pseudocode (algorithm written in pseudocode) Input hours worked Input hourly pay rate Calculate gross pay as hours worked * hourly pay rate Display the gross pay Flowcharts A flowchart is a diagram that graphically shows the steps in a program Four types of symbols connected by arrows Start/Stop Processing Input and Output Decision 7 8 Flowchart Example Start Input hours worked Input hourly pay Gross pay = Hours worked * hourly pay Three Basic Steps in a Program Input Data a program receives while it is running Process Output Display gross pay End Input Process Output 9 10 Displaying Output The print statement displays output on the screen. E.g., print This is what I want to output. E.g., print This is the first line print This is the second line print This is the third line Statements in a program are executed in order from top to down Introduction to Data Types Programs usually work on data that are of a particular type Examples of data types Strings (e.g., This is a string ) Numeric A string literal is the name we use when a string is used in code 11 12 2
More on Strings Comments Using single quotes Used if the string you want to display has double quotes in it E.g., I like reading Goodnight Moon Using double quotes Used if the string you want to display has single quotes in it or if it has an apostrophe. E.g, Don t fear! or Hello World Using triple single or double quotes Used if the string you want to display has single or double quotes in it Also used if the string is a multi line string 13 An important part of any program is making sure you document it so you or someone else can understand it later Documentation involves writing comments about the program statements you write You can comment each line or sections of a program In python, comment lines start with a # These lines are not interpreted Comments can also be placed at the end of a line 14 Commenting Guidelines Variables At the top of your program, you should briefly describe in comments what the program is about In the program, comment input, process and output. The process i.e, calculations on input might not be immediately obvious in bigger programs. Comments help alleviate some of the burden in understanding code especially if it is not your own. A variable is a name that represents a value stored in memory. Programs use variables to access and manipulate data stored in memory. E.g., if your hourly pay rate was $10, you can use a variable payrate to represent the data value 10. payrate references the value 10. 15 16 Creating Variables Variable Names Done using an assignment statement Variable = expression E.g., payrate = 10 What is the effect of this assignment? assignment operator (=) A variable called payrate is created that references a location in memory that contains the value 10 10 payrate Make it descriptive but not too long Don t use 1 letter variable names Don t use abbreviations that only you understand It is not easy to come up with a good variable name. 17 18 3
Expressions Expressions are Values (e.g., strings, numbers ) Code that results in a value String expression This is a test Assignment Statements Variable = expression A variable can be used only after you have assigned a value to it E.g., #This is a variable testing program room =115 print We are in room print room # displays the contents of the variable room print room # displays the string 19 20 Naming Conventions for Variables Python key words cannot be used as variables Spaces are not allowed The first character must be a letter or a _ Variables are case sensitive Rate is not the same as rate Choose a variable name based on what it is used for Use an _ in multiword variable names such as pay_rate Camel casing naming convention The word looks like a camel s hump pay_rate would be payrate in the camel casing naming convention The first letter is lowercase The first letter in subsequent words are uppercase 21 22 Displaying multiple pieces of information How do I display more than 1 piece of data in a print statement. Separate them with commas E.g., room = 115 print I am in room, room The above print statement displays a string literal and a variable that references the value 115 Notice the space added by the python interpreter. This is done for you when a comma is used 23 Variable Reassignment The values of variables can be changed. E.g., pay_rate = 10 print pay_rate pay_rate = 20 print pay_rate Draw picture depicting this in memory. 24 4
Numeric Data types Integers int E.g., 2, 101, 45, -3 Reals float E.g., 2.44, 1.51 Non-numeric data types str E.g., room, This is a test Determining Data Types Python determines the data type of variables you assign values to A number written in code is called a numeric literal. room = 115 Python will determine that 115 is an int since it is a whole number with no decimal point. 115 will be stored in memory as an int 25 26 More on Data Types You need to know the data types of your variables since the same operation might behave differently for different data types We will talk about this more as we go through examples during the course of the semester Examples # This is an example of three variables with different data types room_one = 115 room_two = 121 print We are either in, room_one, or, room_two pay_rate = 10 Pi = 3.14 27 28 What is the output of this program? val = 99 print The value is, val print The value is, val print The value is, 99 50shoes = 130 print I wonder what this does?. Key words in Python and as assert break class continue def del elif else except exec finally for from global If import in is lambda not or pass print raise return try while with yiel 29 30 5
Using python in script mode From the python shell, select File -> New Window and type in your program. Don t forget to save it. This is called the script window. Or Write your program in a file and save it with a.py extension and then open it using the Open command from the File Menu To run your program, select Run->Run Module 31 6