Mathematical Expressions Homework - Python
Categories: Aneesh (Individual Homeworks)Quick practice with Mathemetical Expressions
π 3.3 Homework: Math Expressions & Algorithms in Python
Learning Objectives
- Write a Python program that follows a step-by-step algorithm.
- Use sequencing correctly in Python.
- Use arithmetic operators and variables to calculate results.
Part 1: Sequencing & Variables
Write a Python program that performs the following steps in order:
- Create a variable
grade1and assign it a value of90. - Create a variable
grade2and assign it a value of75. - Calculate the average of
grade1andgrade2and store it in a variable calledaverageGrade. - Display the value of
averageGrade.
Example Output:
The average grade is 82.5
grade1 = 90
grade2 = 75
averageGrade = (grade1 + grade2) / 2
print("The average grade is", averageGrade)
Part 2: Arithmetic Expressions
Calculate the following in Python using variables and arithmetic operators:
num1 β 10num2 β 5num3 β num1 + num2 * 2num4 β (num1 + num2) * 2num5 β num3 / 3 + num4- Display the values of
num3,num4, andnum5.
Hint: Use
+,-,*,/, and parentheses to control order of operations.
num1 = 10
num2 = 5
num3 = num1 + num2 * 2
num4 = (num1 + num2) * 2
num5 = num3 / 3 + num4
print("num3 =", num3)
print("num4 =", num4)
print("num5 =", num5)
Part 3: Step-by-Step Algorithm Practice
Write Python code for this algorithm without using loops or if statements:
Algorithm:
- Set
itemto7 - Create three variables:
num1 = 3,num2 = 7,num3 = 9 - Check each variable manually to see if it equals
item(just write it as a sequence of assignments and displays) - Display
"Item found"if a variable matchesitem - At the end, display
"Item not found"if none match
Hint: Since you havenβt learned
ifstatements, just write a sequence that shows the comparisons step by step with comments.
item = 7
num1 = 3
num2 = 7
num3 = 9
print("Checking num1:", num1 == item)
print("Checking num2:", num2 == item)
print("Checking num3:", num3 == item)
print("Item found")
print("Item not found")
Part 4: Mixed Expressions
Write a Python program that:
- Create three variables:
a = 8,b = 4,c = 10 - Calculate:
result1 = a + b * cresult2 = (a + b) * cresult3 = a + (b / c)
- Display all three results.
This will help you practice order of operations and arithmetic in Python.
a = 8
b = 4
c = 10
result1 = a + b * c
result2 = (a + b) * c
result3 = a + (b / c)
print("Result 1:", result1)
print("Result 2:", result2)
print("Result 3:", result3)