292x Filetype PDF File size 0.79 MB Source: ioct.tech
Python Activity 3: Arithmetic Operations and Assignment Statements
"Get the program to compute that!"
"How do I assignment values to variables?"
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)
Critical Thinking Questions:
1. Draw a line between each flowchart symbol and its corresponding line of Python code. Make note of
any problems.
2. Execute the print statements in the previous Python program.
a. Next to each print statement above, write the output.
b. What is the value of the following line of code?
print((16//3)*3+(16%3))
c. Predict the values of 17%3 and 18%3 without using your computer.
3. Explain the purpose of each arithmetic operation:
a. + ____________________________
b. - ____________________________
c. * ____________________________
d. ** ____________________________
e. / ____________________________
f. // ____________________________
g. % ____________________________
Information: Assignment Statements
An assignment statement is a line of code that uses a "=" sign. The statement stores the result
of an operation performed on the right-hand side of the sign into the variable memory location
on the left-hand side.
4. Enter and execute the following lines of Python code in the editor window of your IDE (e.g. Thonny):
MethaneMolMs = 16 Python Program 1
EthaneMolMs = 30
print("The molecular mass of methane is", MethaneMolMs)
print("The molecular mass of ethane is", EthaneMolMs)
a. What are the variables in the above python program?
b. What does the assignment statement: MethaneMolMs = 16 do?
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?
5. What is stored in memory after each assignment statement is executed?
Assignment Statement Computer Memory
answer = 6 ** 2 + 3 * 4 // 2 answer
final = answer % 4 final
Information: Concatenating Strings in Python
The "+" concatenates the two strings stored in the variables into one string. "+" can only be
used when both operators are strings.
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?
firstName = "Monty" Python Program 2
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!
Python Program 3 Python Program 4
addressNumber = 1600 addressNumber = "1600"
streetName = "Pennsylvania Ave" streetName = "Pennsylvania Ave"
streetAddress= addressNumber + streetName streetAddress= addressNumber + streetName
print(streetAddress) print(streetAddress)
e. Run the programs above and see if you were correct.
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.
no reviews yet
Please Login to review.