Math Basics
Written by Anish Ahuja (8/8/20)
Introduction
In this lesson, we will learn the basic math operators in Python. This is an important lesson as math operators are always used in order to perform processes in Python.
Math Operations
Operation
Notation
Example
Addition
+
print (8+3) results in 11
Subtraction
-
print (8-3) results in 5
Multiplication
*
print (8*3) results in 24
Division
/
print (8/3) results in 2.6666...
Integer Division
//
print (11//3) results in 3
Modulus
%
print (11%3) results in 2
Power
**
print (2**3) results in 8
This may seem like a lot to remember at first, but many of these are already basic math concepts and with practice, these will all come more naturally.
As in regular mathematics, restrictions apply. These are dividing by 0, taking the sqrt of a negative number, having a negative inside of a logarithm, and having values outside the domain of inverse trigonometric functions.
Pemdas
It is important to remember that Python follows the rules of PEMDAS or BODMAS (Parentheses, Exponents, Multiply and Divide, Addition and Subtraction). This means that when writing code, it is important to focus on the placement of parentheses and adhere to the rules of PEMDAS.
If this code was written without the parentheses around x+18, the code would output 21 instead of 11.
Practice Problems
With these math operations, we can make more complex functions. As a challenge problem, write the next 2 functions in Python.
a) a times x squared plus b times x plus c
b) 2x minus 5 all divided by 3 times x squared
Imports
Imports are very important in Python because they allow us to expand our options and be able to specialize our program. Importing the math module can be especially useful because it gives us access to more math functions.
Conclusion
In this lesson, we learned basic math operations, which are essential in Python. In a future lesson, we will explore more functions, such as sine, cosine, tangent, and logarithms. We will also examine performing import statements with math operations. In the next lesson, we will focus on comparison operators.
Last updated
Was this helpful?