Programming Lessons
Programming Fundamentals | 3.1 | 3.2 | 3.3 | 3.12 |
Mathematical Operations in Javascript and Python - Lesson
Learn how your computer can become a powerful calculator using mathematical operations in both Javascript and Python programming languages.
3.3 Mathematical Expressions
Basics of mathematical expressions in Python
Addition : a + b
Subtraction: a - b
Multiplication: a * b
Division: a / b
Modulus: a % b
- Modulus is like a remainder
- 9 % 2 = 1
Exponents: a ** b
Basics of mathematical expressions in JavaScript
Addition: a + b
Subtraction: a - b
Multiplication: a * b
Division: a / b
Modulus/remainder: a % b
exponents: a ** b
Algorithms
“An Algorithm is a finite set of instructions used to accomplish a task”
What does an algorithm consist of?
Building an Algorithm: !
Sequencing
Sequencing is a method used to ensure that all tasks are completed in a specific order which instructions/commands/code is provided in.
Examples:
grade1 = 10
grade2 = grade1
averageGrade = (grade1 + grade2) / 2
print(averageGrade)
10.0
Selection
Selections provide choices between separate, specific outcomes which are executed depending on conditions.
Examples:
Check if it is raining. If it is raining, grab an umbrella. If it is not raining, leave umbrella at home.
Iterations
Iterations describes the process of creating a loop which continues to repeat as long as some condition stands to be true. This condition stops once it is false or doesn’t apply anymore.
Example:
Tweaking a recipe until all requirements like taste and texture are satisfied.
Flowcharts
Flowcharts are effective ways to represent an algorithm. On a flowchart, you can represent sequencing, selection, and iteration.
The example demonstrates sequencing by following a specific pattern for each step, not bouncing from one step to the next.
Selection is shown when the user is able to make a choice between either adding another number and operation OR ending the calculation.
Iteration is shown when the user is able to add as many numbers and operations as they want to the calculation.
Pseudocode
Pseudocode is a way to represent code that is easy to follow for even those who do not code.
ex:
grade = 82
avg(1, 7, 24) = 10 2/3