Slide 1
Input, Processing, and Output
Joan Boone
jpboone@email.unc.edu
Summer 2020
Input, Processing, and Output Joan Boone jpboone@email.unc.edu - - PowerPoint PPT Presentation
INLS 560 Programming for Information Professionals Input, Processing, and Output Joan Boone jpboone@email.unc.edu Summer 2020 Slide 1 Topics Part 1 Program design and development Variables and print statement Part 2 Reading
Slide 1
Joan Boone
jpboone@email.unc.edu
Summer 2020
Slide 2
Slide 3
Slide 4
Source: Starting Out with Python by Tony Gaddis Correct runtime and logic errors Design the program Write the code Correct syntax errors Test the program
Slide 5
– Methods for documenting an algorithm:
pseudocode and flowcharts Example: Calculate and display the gross pay
Source: Starting Out with Python by Tony Gaddis
Pseudocode
pay rate
Flowchart
Slide 6
hours_worked = 40 hourly_pay_rate = 25.00 gross_pay = hours_worked * hourly_pay_rate print(gross_pay)
1000.0
Slide 7
message = “some useful text goes here” voting_age = 18 pi = 3.1415926535897932
Slide 8
Two approaches for multi-word variables
– Use underscore, _, to separate multiple words, e.g.,
miles_per_gallon (snake_case)
– Use mixed case, e.g., milesPerGallon (camelCase)
totals_for_2019
are different variables
Slide 9
# Create two variables: top_speed and distance top_speed = 160 distance = 300 # Display the value referenced by the variables print('The top speed is') print(top_speed) print('The distance traveled is') print(distance) # Variables can appear on both sides # of an assignment statement counter = 0 counter = counter + 1 print('The counter value is') print(counter)
variable_demo1.py The top speed is 160 The distance traveled is 300 The counter value is 1 Output
Slide 10
# Create two variables: top_speed and distance top_speed = 160 distance = 300 # Display the value referenced by the variables print('The top speed is', top_speed) print('The distance traveled is', distance) # Variables can appear on both sides of an assignment statement counter = 0 counter = counter + 1 print('The counter value is', counter)
variable_demo2.py The top speed is 160 The distance traveled is 300 The counter value is 1 Output
Slide 11
character that is not visible, but causes output to start on a new line.
print('One') print('Two') print('Three')
print('One', 'Two', 'Three')
print('One','Two','Three', sep=', ') One Two Three One Two Three One, Two, Three
Gaddis: Section 2.8
Slide 12
Python documentation on Lexical Analysis
print('One\nTwo\nThree')
One Two Three
print('Three\tFour\tFive')
Three Four Five
print('Monty Python\'s Flying Circus')
Monty Python's Flying Circus
Slide 13
PEP 8 - Style Guide for Python Code
– Why important?
don't over-comment
– Bad comments are worse than no comments – Also useful for debugging: comment out sections of code to try
different approaches to solving a problem
– Multi-line comments (delimited with triple quotes) ''' Very lengthy, verbose, run-on comment goes here. '''
Slide 14
Slide 15
input(prompt)is a built-in Python function that reads input typed by the user on the keyboard. Example
first_name = input('Enter your first name: ') last_name = input('Enter your last name: ') print('Hello', first_name, last_name)
Example output: Hello Monty Python
How it works: when the input statement is executed by Python
something
variable as a string
string_input.py
Slide 16
# Prompt for hours worked hours_worked = input('Enter hours worked: ') # Prompt for hourly pay rate hourly_pay_rate = input('Enter hourly pay rate: ') # Calculate gross pay and display result gross_pay = hours_worked * hourly_pay_rate print('Gross pay: ', gross_pay)
gross_pay_calc.py Enter hours worked: 40 Enter hourly pay rate: 25 Traceback (most recent call last): File ".../gross_pay_calc.py", line 8, in <module> gross_pay = hours_worked * hourly_pay_rate
TypeError: can't multiply sequence by non-int of type 'str'
Process finished with exit code 1 Output in the PyCharm Run Window
Slide 17
float(item)converts item to a float (number w/decimal point)
days_in_week = int('7')
pi = float('3.1416')
number, otherwise an error (exception) is generated
Slide 18
hours_worked = input('Enter hours worked: ') hours_worked = float(hours_worked)
hours_worked = float(input('Enter hours worked: '))
After the input function executes, it will return the value you entered, as a string. That value is saved (temporarily) by Python, and then is passed as an argument to the float function which converts it to a number.
Slide 19
Enter hours worked: 40 Enter hourly pay rate: 25 Gross pay: 1000.0 Process finished with exit code 0
Output in the PyCharm Run Window
Update gross_pay_calc.py so that it converts the user inputs to float before calculating the gross pay amount.
Slide 20
Symbol Operation Description
+
Addition Adds two numbers
Subtracts one number from another
*
Multiplication Multiplies one number by another
/
Division Divides one number by another and gives the result as a floating point number
//
Integer division Divides one number by another and gives the result as an integer
%
Remainder Divides one number by another and gives the remainder
**
Exponent Raises a number to a power
Slide 21
# Prompt for the price
# Calculate the sale_price with a 20% discount # Calculate the total_price = sale_price + tax # Display sale and total prices print('The sale price is ', sale_price) print('The total price is', total_price) sale_price.py Enter the item's original price: 100.00 The sale price is 80.0 The total price is 83.8
Sample output
Slide 22
converted exactly
decimal version looks like 0.3333333333333333
16, but you can control how many digits are displayed Specify a minimum field width with a format specifier (Section 2.8)
some_number = 12345.6789 print(format(some_number, '12.2f')) Result: 12345.68
Displays the number in a field of 12 spaces, with 2 decimal places
Slide 23
Slide 24
# This program displays the following floating-point numbers # in a column with their decimal points aligned. num1 = 127.899 num2 = 3465.148 num3 = 3.776 num4 = 264.821 num5 = 88.081 num6 = 799.999 # Display each number in a field # of 7 spaces with 2 decimal places. print(format(num1, '7.2f')) print(format(num2, '7.2f')) print(format(num3, '7.2f')) print(format(num4, '7.2f')) print(format(num5, '7.2f')) print(format(num6, '7.2f')) 127.90 3465.15 3.78 264.82 88.08 800.00
Result:
Slide 25
– Exponentiation: ** – Multiplication, division, and remainder: * / // % – Addition and subtraction: + -
Slide 26
– When both operands are int, the result is an int – When both operands are float, the result is a float – When one operand is an int and the other is a float,
the result is a float
– 12 * 2 → 24 – 12.0 * 2.0 → 24.0 – 12 * 2.0 → 24.0
Slide 27
Algebraic Expression Python Statement
Area of a triangle
A = 1/2bh
area_of_triangle = base * height / 2 Simple interest
I = prt
simple_interest = principal * rate * time Compound interest A = P (1 + R)n amount = principal * (1 + rate)**time Distance between 2 points A = P (1 + R)n distance = ((x2 - x1)**2 + (y2 - y1)**2)**0.5
Some Math Formulas can be simplified with the use of Python built-in Math functions
Slide 28
Enter number of pennies: 573 Here is the amount in dollars and coins Dollars: 5 Quarters: 2 Dimes: 2 Nickels: 0 Pennies: 3
coin_conversion.py
Slide 29
Enter number of seconds: 123456 Hours: 34 Minutes: 17 Seconds: 36
print(format(hours,'02d'), format(minutes,'02d'), format(remaining_seconds,'02d'), sep=':')
'02d' displays the number with 2 decimal digits, and inserts a leading 0
if there is only 1 digit