Fundamentals | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.3 Mathematical Expressions
Student led teaching on Mathematical Expressions. Learn how mathematical expressions involve using arithmetic operators (like addition, subtraction, multiplication, and division) to perform calculations
Mathematical Expressions:
- Algorithm: a finite set of instructions that accomplish a specific task.
- Ex: when you go to bake cookies you use specific steps
- Getting ready in the morning: shirt, pants, breakfast
- Algorithm is like a list of steps that occur in a specific/particular order.
- Sequencing: things are done in order
- Selection
- Iteration
- Selection: do first step, make decision(yes or no), step true or false( if or else)
- Iteration/repetition→ First step, second step, continue condition, Yes or no, step to do if true
- Ways to write algorithm: flow chart(use arrows to tell decision)
- Pseudo code→ fake code. writing . verbal instructions/comments on what each section is doing. </span>
def factorial(n):
# Selection
if n < 0:
return "Factorial is not defined for negative numbers"
# in case negative numbers are inputted...
elif n == 0 or n == 1:
# "else if" sets a condition to be met, and if it is met, the code should return 1
return 1
else:
result = 1
for i in range(2, n + 1):
#iterates through the list, from 2 to n inclusive - 1 is not counted, n*1 = n
result *= i
#multiplies the current value of result by i
# i is sort of like the index, or the number assigned to each value in the list, starting from 0
return result
def main():
try:
# Get user input for the numbers as a comma-separated list
user_input = input("Enter a list of non-negative integers (comma-separated): ")
numbers = [int(num.strip()) for num in user_input.split(',')]
# Calculate and print factorial for each number
for number in numbers:
# Calculate factorial for each number, iterates through the list
result = factorial(number)
if isinstance(result, int):
print(f"The factorial of {number} is: {result}")
# Selection, makes sure the result is actually an integer before printing
else:
print(f"The factorial for {number} is not defined (negative input).")
except ValueError:
print("Invalid input. Please enter a valid list of non-negative integers.")
if __name__ == "__main__":
main()
Example 1
Scenario: You’re in your math test and you forgot how to graph the parent function of 3(x+2)-4. Input a list of x-values to figure out the y-values and print the results of what you get so you can graph the function and get an A on your math test!
# list of x coordinates which need to be plugged into the equation to get y values
nums = [2, 5, 7, 10]
# for loop to iterate through the list of x coordinates
for num in nums:
print((3 * ( num + 2)) - 4)
3.3 Continued - Mathematical Operations
- Addition: a + b
- Subtraction: a - b
- Multiplication: a * b
- Division: a / b
- Modulus (remainder of a/b): a MOD b
- For python it is %
- Math in order of operations
- MOD is held to the value of multiplication + division in PEMDAS
num1 = 40
num2 = num1 / 2 # division
num3 = 5 * num2 + 3 # pemdas --> multiplication then additon
result = num2 % 3 * num1 + 4 - num3 / 2 # pemdas --> modulus, then multiplication, then subtraction, then division
print (result)
Example #2
What will the code below print?
</span>
num1 = 20
num2 = num1 /2
num3 = num2 * 10 + 3
print(num3)
103.0
Hacks
Review each of the sections above and produce …
- A function that takes in 2 variables and adds, subtracts, multiplies, dividies, and mods the two numbers
- Use an algorithm to find certain values of the Fibonacci sequence. For example, your code should output the nth index in the sequence. An example of how the sequence works is 0,1,1,2,3,5,8,13. The pattern is you add the previous 2 numbers together to get the new number.
Challenge: Try to implement more than one algorithm for the Fibonacci sequence