Py 03 Thonny Arithmetic
Py 03 Thonny Arithmetic
Learning Objectives
Students will be able to:
Content:
Explain each Python arithmetic operator
Explain the meaning and use of an assignment statement
Explain the use of "+" and "*" with strings and numbers
Use the int() and float() functions to convert string input to numbers for computation
Incorporate numeric formatting into print statements
Recognize the four main operations of a computer within a simple Python program
Process:
Create input statements in Python
Create Python code that performs mathematical and string operations
Create Python code that uses assignment statements
Create Python code that formats numeric output
Prior Knowledge
Understanding of Python print and input statements
Understanding of mathematical operations
Understanding of flowchart input symbols
Further Reading
https://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/Hello,_World
https://en.wikibooks.org/wiki/Non-
Programmer%27s_Tutorial_for_Python_3/Who_Goes_There%3F
Model 1: Arithmetic Operators in Python
Python includes several arithmetic operators: addition, subtraction, multiplication, two types of division,
exponentiation and mod.
Flowchart Python Program
# Programmer: Monty Python
# Date: Sometime in the past
# Description: A program
# explores arithmetic operators
print(16+3)
print(16-3)
print(16*3)
print(16**3)
print(16/3)
print(16//3)
print(16.0/3)
print(16.0//3)
print(16%3)
c. Predict the values of 17%3 and 18%3 without using your computer.
4. Enter and execute the following lines of Python code in the editor window of your IDE (e.g. Thonny):
Python Program 1
MethaneMolMs = 16
EthaneMolMs = 30
print("The molecular mass of methane is", MethaneMolMs)
print("The molecular mass of ethane is", EthaneMolMs)
c. What happens if you replace the comma (,) in the print statements with a plus sign (+) and
execute the code again? Why does this happen?
6. Run the following program in the editor window of your IDE (e.g. Thonny) to see what happens
if you try to use the "+" with strings instead of numbers?
Python Program 2
firstName = "Monty"
lastName = "Python"
fullName = firstName + lastName
print(fullName)
print(firstName,lastName)
a. The third line of code contains an assignment statement. What is stored in fullName when the
line is executed?
b. What is the difference between the two output lines?
c. How could you alter your assignment statements so that print(fullName)gives the same
output as print(firstName,lastName)
d. Only one of the following programs will work. Which one will work, and why doesn’t the other
work? Try doing this without running the programs!
f. The program that worked above results in no space between the number and the street name. How
can you alter the code so that it prints properly while using a concatenation operator?
7. Before entering the following code into the Python interpreter (Thonny IDE editor window), predict
the output of this program.
Python Program 5 Predicted Output
myNumber = "227" * 10
print(myNumber)
myWord = "Cool!" * 10
print(myWord)
Now execute it. What is the actual output? Is this what you thought it would do? Explain.
8. Let’s take a look at a python program that prompts the user for two numbers and subtracts them.
Execute the following code by entering it in the editor window of Thonny.
Python Program 6
firstNumber = input("Enter a number: ")
secondNumber = input("Enter another number: ")
difference= firstNumber - secondNumber
print("*" * 10)
print("Difference = ", difference)
g. Explain how the changes in the program produced the desired output.
b. What happens to the number if you tell it to display less decimals than are in the number,
regardless of formatting method used?
10. Execute the following code by entering it in the editor window of Thonny.
Python Program 7
numLaptops = 7
laptopCost = 599.50
price = numLaptops* laptopCost
print("Total cost of laptops: $", price)
a. Does the output look like standard output for something that has dollars and cents
associated with it?
02.5 ___________________________________________________
08.2 ___________________________________________________
03.1 ___________________________________________________
e. Explain what each part of the format function: format(variable, "%n.nf") does in a print
statement where n.n represents a number.
f. Revise the print statement by changing the "f" to "d" and laptopCost = 600. Execute the
statements and explain the output format.
print("Total cost of laptops: %2d" % price)
print("Total cost of laptops: %10d" % price)
g. Explain how the function format(var,'10d') formats numeric data. var represents
a whole number.
Reminder:
Computers perform four main operations on data:
Input data into a computer
Output data to a screen or file
Process data using arithmetic, logical, searching or sorting operations
Store data
11. Use the following program and output to answer the questions below.
a. From the code and comments in the previous program, explain how the four main
operations are implemented in this program.
b. There is one new function in this sample program. What is it? From the
corresponding output, determine what it does.
b. The sum of 5 and 6 multiplied by the quotient of 34 and 7 using floating point arithmetic
2. Write an assignment statement that stores the remainder obtained from dividing 87 and 8 in the
variable leftover
b. Write one line of code that prints the total cost with a label, a dollar sign, and exactly two
decimal places. Sample output: Total cost: $22.50
1. Write a Python program that prompts the user for two numbers, and then gives the sum and product of
those two numbers. Your sample output should look like this:
Your program must contain documentation lines that include your name, the date, a line that states
"PA3 Homework question 1" and a description line that indicates what the program is supposed to do.
Take a screen shot of the program and the output and attach the files to homework python activity 3
in Moodle. You must also attach the file that ends in .py to the assignment. (5 points)
2. Write a program that calculates the molarity of a solution. Molarity is defined as numbers of moles per
liter solvent. Your program will calculate molarity and must ask for the substance name, its molecular
weight, how many grams of substance you are putting in solution, and the total volume of the solution.
Report your calculated value of molarity to 3 decimal places. Your output should also be separated from
the input with a line containing 80 asterixis.
Assuming you are using sodium chloride, your input and output should look like:
See your class instructions for how to submit the homework