Gaddis Python 4e Chapter 02 PT 1
Gaddis Python 4e Chapter 02 PT 1
Input,
Processing,
and Output
Topics
◦ Designing a Program
◦ Input, Processing, and Output
◦ Displaying Output with print Function
◦ Comments
◦ Variables
◦ Reading Input from the Keyboard
◦ Performing Calculations
◦ More About Data Output
Designing a Program
◦ Programs must be designed before they are written
◦ Program development cycle:
◦ Design the program
◦ Write the code
◦ Correct syntax errors
◦ Test the program
◦ Correct logic errors
Designing a Program (cont’d.)
◦ Design is the most important part of the program
development cycle
◦ Understand the task that the program is to perform
◦ Work with customer (user) to get a sense what the program is
supposed to do
◦ Ask questions about program details
◦ Create one or more software requirements
◦ In college course make sure you understand your professor’s
requirements
Designing a Program (cont’d.)
◦ Determine the steps that must be taken to perform the task
◦ Break down required task into a series of steps
◦ Create an algorithm, listing logical steps that must be taken
◦ Algorithm: set of well-defined logical steps that must be
taken to perform a task
Algorithms
◦ Set of well-defined logical steps
◦ Must be performed in order to perform a task
1-7
Pseudocode
◦ Pretend code
◦ Informal language that has no syntax rule
◦ Not meant to be compiled or executed
◦ Used to create model program
◦ No need to worry about syntax errors, can focus on program’s design
◦ Can be translated directly into actual code in any programming language
Flowcharts
◦ Diagram that graphically depict the steps in a program
◦ Ovals are terminal symbols
Examples:
print("Kate Austen")
print('''Cara O'Brian''' "Cara O’Brian")
print('Asheville, NC 28899')
Comments
◦ Notes of explanation within a program
◦ Ignored by Python interpreter
◦ Intended for a person reading the program’s code
◦ Begin with a # character
◦ End-line comment: appears at the end of a line of code
◦ Typically explains the purpose of that line
Output:
Hello Python world!
Hello world!
Displaying Multiple Items with the
print Function
◦ Python can display multiple items on one line
◦ Items to print are separated by commas
◦ The items are passed as arguments
◦ Displayed in order they are passed to print function
◦ Items are automatically separated by a space when displayed on
screen
Output:
I had $ 2.75 in my account.
But now I have $ 99.95 in my account!
Data Types
Output:
Enter your first name: Anthony
Enter your last name: Manno
Hello Anthony Manno
Reading Numbers with input
◦ input function always returns a string
◦ Need to convert strings to numbers (type conversion)
◦ int(item) converts item to an int
◦ float(item) converts item to a float
◦ This requires a Nested Function Call where you get input from
the user AND convert it to a number in same line
◦ General format:
◦ function1(function2(argument))
◦ value returned by function2 is passed to function1
◦ Example: int( input("what is the temperature? "))
◦ This gets what they user typed and sends it immediately to function that
converts it to an integer
◦ Type conversion only works if item is valid numeric value,
otherwise, gives error
Numeric Input Example
# Get the user's name, age, and income.
name = input('What is your name? ')
age = int(input('What is your age? '))
income = float(input('What is your income? '))
# Display the data.
print('Here is the data you entered:')
print('Name:', name)
print('Age:', age)
print('Income:', income)
Output:
What is your name? Anthony
What is your age? 20
What is your income? 10000
Here is the data you entered:
Name: Anthony
Age: 20
Income: 10000.0
Performing Calculations
◦ Math expression: performs calculation and gives a value
◦ Math operator: tool for performing calculation Operator Description
◦ Resulting value typically assigned to variable
◦ Example: 12 + 2 + Addition
◦ Value returned is 14 - Subtraction
◦ Operands: values surrounding operator * Multiplication
◦ Variables can be used as operands
◦ Example with variables “PayRate” and “HoursWorked” / or // Division
◦ Salary = PayRate * HoursWorked
◦ Two types of division:
◦ / operator performs floating point division
◦ // operator performs integer division
◦ Positive results truncated, negative rounded away from zero
Math Program
# Assign a value to the salary variable.
salary = 2500.0
# Assign a value to the bonus variable.
bonus = 1200.0
Output:
Your pay is $ 3700.0
Exponentiation and Remainder
◦ Exponent operator (**): Raises a number to a power
◦ x ** y = xy
◦ Remainder operator (%): Performs division and returns the
remainder
◦ a.k.a. modulus operator
◦ Ex: 4%2=0, 5%2=1
◦ Example uses
◦ Convert times and distances
◦ Detect odd or even numbers
Operator Precedence
◦ Python operator precedence
1. Operations enclosed in parentheses
◦ Forces operations to be performed before others
2. Exponentiation (**)
3. Multiplication (*), division (/ and //), and remainder (%)
4. Addition (+) and subtraction (-)
◦ Higher precedence performed first
◦ Same precedence operators execute from left to right
Zen of Python
◦ Guiding principles for writing code with Python to write well-
designed, elegant and efficient code
◦ Beautiful is better than ugly
◦ Explicit is better than implicit
◦ Simple is better than complex
◦ Complex is better than complicated
◦ Readability counts
◦ In the face of ambiguity, refuse the temptation to guess
◦ Now is better than never.
◦ Although never is often better than right now
Exercises
◦ Do exercises for lab 2a
◦ It is due next class