Math Expressions Homework
Quick practice with math expressions
Math Expressions Homework
๐ Homework: Multiple Choice
1) What will the following Python code output?
x = 10
y = 3
print(x % y + x // y)
2) Which of the following best describes a mathematical expression in programming?
3) What is the value of result
in the following code?
num1 = 8
num2 = 3
result = num1 * num2 - num1 / num2
4) What will this code output?
a = 15
b = 4
print(a // b)
5) Which of the following best represents a % b
in Python?
6) What is the value of result
?
x = 2
y = 5
result = x ** y % 3
7) What will this print?
num = 7
print((num + 3) * 2 / 5)
8) Which expression correctly calculates the distance between points (x1, y1) and (x2, y2)?
๐ Homework: Free Response
1) Write a function in Python that takes two numbers as inputs and returns the result of:
(xยฒ + yยฒ) รท (x + y)
Be sure to handle the case when x + y = 0
.
2) Explain how order of operations (PEMDAS) affects the evaluation of:
result = 6 + 4 * 2 % 5
- Show the step-by-step evaluation.
- Write the final answer.
3) Write a Python function called calculate_mod_sum
that takes three numbers a, b, and c, and returns the sum of a % b and b % c. Make sure your function handles cases where b or c is zero.
4) Create a Python program that calculates the area of a trapezoid. The formula for the area is:
area = ((base1 + base2) / 2) * height
Ask the user for base1
, base2
, and height
, compute the area, and print the result.
5) Suppose a Fibonacci-like sequence starts with 3 and 5 (instead of 0 and 1). Write a Python function that returns the nth term of this sequence.
6) Explain step-by-step how Python evaluates the following expression using order of operations (PEMDAS):
result = 8 + 12 / 4 * 2 - 5 % 3
Include the intermediate steps and the final value of result
.
7) Write a Python function distance_3d
that calculates the distance between two points in 3D space: (x1, y1, z1) and (x2, y2, z2). Use the formula:
distance = sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)
Make sure your function accepts six arguments and returns the distance.
8) Consider the expression:
x = 7
y = 2
z = 3
result = x + y * z % 4
Explain step-by-step how Python calculates result
and give the final value.