Tags:
create new tag
view all tags

Python Activity 3: Arithmetic Operations and Assignment Statements

Learning Objectives
Students will be able to:
Content:
  • Explain each Python arithmetic operator
  • Explain the meaning and use of an assignment statement
  • Review string literals and print statements
  • 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
Material covered in previous Activities
Critical Thinking Questions:

1. Execute the print statements in the Python program. What is the output for each statement?

Screen_Shot_2018-07-05_at_3.17.30_PM.png

2.State the arithmetic operation each symbol represents:

  • a. +
  • b. -
  • c. *
  • d. **
  • e. /
  • f. //
  • g. %
FYI: 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 lefthand side.
3. Enter and execute the following two lines of Python code:
age = 15
print("Your age is", age)
  • a. What does the assignment statement : age = 15 do?
  • b. What happens if you replace the comma (,) in the print statement with a plus sign (+) and execute the code again?
4.What is stored in memory after each assignment statement is executed?
Assignment Statement   Assignment Statement
answer = 6 ** 2 + 3 * 4 // 2   answer  
final = answer % 4   final  
5. Test the following program to see what happens if you try to use the “+” with strings instead of numbers.

Screen_Shot_2018-07-05_at_3.36.56_PM.png

  • a. The third line of code contains an assignment statement. What is stored in the variable fullNamewhen the line is executed?
  • b. How can you fix the output so that the words are separated?
  • c. What is the output of the following code? Why?
Screen_Shot_2018-07-05_at_3.37.06_PM.png
FYI: The “+” concatenates the two strings stored in the variables into one string. “+” can only be used when both operators are strings
6. Before entering the following code into the Python interpreter, try to figure out what you think the statement should print. Then execute it. What does it do? Is this what you thought it would do?

Screen_Shot_2018-07-05_at_3.37.11_PM.png

  • What you think it does:
  • What it really does:
7. Let’s take a look at a program that prompts the user for two numbers and subtracts them.

Screen_Shot_2018-07-05_at_3.37.24_PM.png

a. What output do you expect?

b. Execute the code. What is the actual output?

c. Revise the program in the following manner: Between lines 2 and 3 add the following lines of code:

num1 = int(firstNumber) 
num2 = int(secondNumber

Next, replace the statement:

difference = firstNumber - secondNumber
</sticky> with the statement:
difference = num1 - num2
</sticky>

Execute the program again. What output did you get?

d. Explain the purpose of the function int().

Application Questions: Use the Python Interpreter to check your work

1. Write the line of Python code that calculates and prints the answer to the following arithmetic expressions:

a. 8 to the 4th power

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

3. Assume:

courseLabel = "CMSC"
courseNumber = "190"

Write a line of Python code that concatenates the label with the number and stores the result in the variable courseName. Be sure that there is a space between the course label and the course number when they are concatenated.

4. Create a program the outputs the total cost of a lunch order. Users should be prompted to input the number of hamburgers, fries, and drinks they want and the program should print the total cost of the order. The hamburgers cost 2.00, fries cost 1.50, and drinks cost 1.00. Be creative and professional in prompting the user for the information and in displaying the output.

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng Screen_Shot_2018-07-05_at_3.17.30_PM.png r1 manage 25.0 K 2018-07-05 - 19:17 JimSkon  
PNGpng Screen_Shot_2018-07-05_at_3.36.56_PM.png r1 manage 19.2 K 2018-07-05 - 19:38 JimSkon  
PNGpng Screen_Shot_2018-07-05_at_3.37.06_PM.png r1 manage 23.9 K 2018-07-05 - 19:38 JimSkon  
PNGpng Screen_Shot_2018-07-05_at_3.37.11_PM.png r1 manage 10.2 K 2018-07-05 - 19:38 JimSkon  
PNGpng Screen_Shot_2018-07-05_at_3.37.24_PM.png r1 manage 34.6 K 2018-07-05 - 19:38 JimSkon  
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2018-07-06 - JimSkon
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback