Chapter 2: Input, Processing, and Output Flashcards
Stages of “The Program Development Cycle”:
- Design the Program
- Write the Code
- Correct Syntax Errors
- Test the Program
- Correct Logic Errors
What is a SYNTAX ERROR?
Mistake such as: misspelled key word, missing punctuation, or incorrect use of an operator
What is a LOGIC ERROR?
Mistake that does not prevent the program from running but causes it to produce incorrect results.
What is PSEUDOCODE?
An informal language that has no syntax rules and is not meant to be compiled or executed.
Used by programmers to create models, or “mock-ups” of programs.
What is a FLOWCHART?
Diagram that graphically depicts the steps that take place in a program
Computer programs typically perform a 3 Step Process:
- INPUT is received
- Some PROCESS is performed on the input
- OUTPUT is produced
Print Function
Used to display output on the screen.
Print function SYNTAX:
STRING/STRING LITERAL (str) must be enclosed in a pair of quote marks-
print(‘argument’)
> > > print(‘Hello World’)
Hello world
comments
Notes of explanation that document lines or sections of a program.
- part of the program, but ignored by Python interpreter
- intended for people reading source code
Variable
A name that references a value in the computer’s memory.
Assignment statement
& operator
Used to create variable and make it reference a piece of data
variable = expression
print(the value stored in a variable)
>>> width = 10 >>> length = 5 >>> print(width) 10 >>> print(length) 5 >>> print(length, width) 10 5
VARIABLE Naming Rules
- Cannot use a Python key word
- Cannot contain spaces
- First character must be: a-z, A-Z, or an underscore_
- After first char., may use: a-z, A-Z, 0-9, or underscores_
- Upper & lower case char.’s are distinct
camelCase naming convention
variableName begins with lower case letters & the first char. of the second and subsequent words written in uppercase-
camelCaseNamingConvention
Python allows display of multiple items with one call to the print function by:
Separating items with a comma-
firstName = ‘Jane’
lastName = ‘Doe’
»> print(firstName, lastName)
Jane Doe
A number written into a program’s code is called a NUMERIC LITERAL.
Because TYPES of numbers are stored and manipulated in different ways, Python uses data types to categorize values in memory-
3 data types:
- int - a numeric literal written as whole number with no decimal is considered as an integer. (7, 124, -9)
- float - a numeric literal written with a decimal is considered a float. (1.5, 5.0)
- str - used for storing strings in memory. ( number = 3 or number = ‘three’ or number = ‘The number 3 is spelled, three’
Variable reassignment
>>> number = 3 >>> print(number) 3 >>> number = ‘three’ >>> print(number) three
Input function
Programs commonly need to read input typed by the user on the keyboard.
Normally used as in an assignment statement, following the general format:
variable = input(prompt)
> > > name = input(‘Enter your name: ‘)
Enter your name: Jane
print(name)
Jane
User INPUT is returned to the program as a STRING
To perform math operations on numeric data entered by the user with the input() you need to first, convert the data from str, to int or float:
> > > stringValue = input(‘How many hours did you work? ‘)
hours = int(stringValue)
Or more efficiently, use the NESTED FUNCTION -
»> hours = int(input(‘How many hours did you work? ‘))
Write a program that asks the user to enter 3 different data types- their name (str), age (int), & income (float) and returns the user entered data as:
Here is the data you entered:
Name: Jane
Age: 25
Income: 7500.0
> > > name = input(‘Enter your name: ‘)
age = int(input(‘Enter your age: ‘))
income = float(input(‘Enter your income: ‘))
print(‘Here is the data you entered: ‘)
print(‘Name: ‘, name)
print(‘Age: ‘, age)
print(‘Income: ‘, income)
What result is returned on:
> > > 3*3
> > > ‘3’ *3
> > > 3*3
9
‘3’ *3
333
In the following, which are the math expression, the operands, and the operator?
> > > 12 + 2
math expression: 12+2
operands: 12 and 2
operator: +
Python Math Operators
+ Addition
- Subtraction
* Multiplication
/ Division (gives result as a float)
// Integer Division (gives result as a whole number)
% Remainder (divides #s and gives the remainder)
** Exponent
Operator Precedence:
First: operations enclosed in ( ) are performed
Next: exponents **
Then: multiplication, division, and remainder * / // % (from L to R)
Last: addition and subtraction + - (from L to R)