How To Do Math in Python 3 with Operators?
Last Updated :
18 Apr, 2022
Python3 provides us data types like integer and float along with various operators to perform mathematical calculations for graph plotting, machine learning algorithms, Statistical, data analytical purposes. An operator is a symbol that performs specific operations, which is very useful if one is frequently dealing with numbers. Operator precedence specifies the order in which operators are evaluated when two or more operators with different precedence are adjacent in an expression. Below is a quick reference table for Unary Operators and Arithmetic Operator.
Operators in Python
Type | Operator | Name | Example | Description |
---|
Urinary Operator | - | Minus | - x | Negates the numeric argument |
| + | Plus | + x | numeric argument unchanged. |
| ~ | Invert | - (x + 1) | bit-wise inversion of x |
Arithmetic Operator | + | Addition | x + y | Add two operands using '+' operator in between |
| - | Subtraction | x - y | subtract two operands using the '-' operator in between |
| * | Multiplication | x * y | Multiply two operands using '*' operator in between |
| / | Division | x / y | Divide left operand with right operand using '/' operator |
| // | Floor Division | x // y |
Divide left operand with right operand using '//' operator
and provide the only quotient as an output
|
| ** | Exponentiation | x ** y | Exponentiation (power) x to the power y |
| % | Modulo | x % y |
Divide left operand with right operand using '%' operator
and provide the only remainder as an output
|
Operator Precedence
Operator | Meaning | Associativity |
---|
** | Exponent | Right-to-left |
~x | Bitwise NOT (Invert) | Left-right |
+x, -x | Unary plus, Unary minus | Left-right |
*, /, //, % | Multiplication, Division, Floor division, Modulus | Left-right |
+, - | Addition, Subtraction | Left-right |
Example 1: Unary Operators.
Python3
a = 2.202
b = -2.202
# This inverts the sign
# for both integer as
# well as float type
c = -a
print("Minus operator value 1:", c)
c = -b
print("Minus operator value 2:", c)
# This does not inverts the sign
# for both integer as well
# as float type
c = +a
print("Plus operator value 1:", c)
c = +b
print("Plus operator value 2:", c)
a = 2
b = -2
# This inverts the sign only
# for integer type as perform
# operation as per this '-(x + 1)'.
c = ~a # -(2 + 1)
print("Invert operator value 1:", c)
c = ~b # -(-2 + 1)
print("Invert operator value 2:", c)
Output:
Minus operator value 1: -2.202
Minus operator value 2: 2.202
Plus operator value 1: 2.202
Plus operator value 2: -2.202
Invert operator value 1: -3
Invert operator value 2: 1
Example 2: Addition Operator.
Python3
a = 4
b = -5
# This + operator performs
# addition of two operands
# or numbers
d = a + b
print("Addition value 1:", d)
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
d = []
for j in range(len(a)):
d.append(a[j] + b[j])
print("Addition value 2:", d)
Output:
Addition value 1: -1
Addition value 2: [7, 9, 11, 13, 15]
Example 3: Subtraction Operator.
Python3
a = 4
b = -5
# This - operator performs
# subtraction of two operands
# or numbers
d = a - b
print("Subtraction value 1:",d)
a = [ 1 ,4,5]
b = [1, 2, 3]
print("Subtraction values:")
for i in range(len(a)) :
print(a[i] - b[i])
Output:
Subtraction value 1: 9
Subtraction values:
0
2
2
Example 4: Multiplication Operator.
Python3
a = 4
b = -5
c = 5.02
# This * operator performs
# Multiplication of two
# operands or numbers
d = a * b
print("Multiplication value 1:", d)
d = a * c
print("Multiplication value 2:", d)
OutputMultiplication value 1: -20
Multiplication value 2: 20.08
Example 5: Division Operator.
Python3
a = 20
b = -5
c = 5.02
# This '/' operator performs
# Division of two operands
# or numbers
d = a / b
print("Division value 1:", d)
d = a / c
print("Division value 2:", d)
Output:
Division value 1: -4.0
Division value 2: 3.9840637450199208
Example 6: Floor Division Operator.
Python3
a = 20
b = -5
c = 5.02
# This // operator performs
# Floor Division of two
# operands or numbers
d = a // b
print("Floor Division value 1:", d)
d = a // c
print("Floor Division value 2:", d)
Output:
Floor Division value 1: -4
Floor Division value 2: 3.0
Example 7: Exponential Operator.
Python3
a = 5
b = 3
c = -3
# This ** operator performs
# Exponential operation of two
# operands or numbers
d = a ** b
print("Exponent value 1:", d)
d = a ** c
print("Exponent value 2:", d)
Output:
Exponent value 1: 125
Exponent value 2: 0.008
Example 8: Modulo Operator.
Python3
a = 12
b = 5
c = 3
# This % operator performs Modulus
# of two operands or numbers and
# return the remainder
d = a % b
print("Modulus value 1:", d)
d = c % b
print("Modulus value 2:", d)
OutputModulus value 1: 2
Modulus value 2: 3
Similar Reads
What does the Double Star operator mean in Python?
The ** (double star)operator in Python is used for exponentiation. It raises the number on the left to the power of the number on the right. For example:2 ** 3 returns 8 (since 2³ = 8)It is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python and is also known as Power Operator.Prec
2 min read
Why Are There No ++ and -- Operators in Python?
Python does not include the ++ and -- operators that are common in languages like C, C++, and Java. This design choice aligns with Python's focus on simplicity, clarity, and reducing potential confusion. In this article, we will see why Python does not include these operators and how you can achieve
3 min read
Python := Walrus Operator in Python 3.8
The Walrus Operator is a new addition to Python 3.8 and higher. In this article, we're going to discuss the Walrus operator and explain it with an example. Walrus Operator allows you to assign a value to a variable within an expression. This can be useful when you need to use a value multiple times
2 min read
Python 3 - Logical Operators
Logical Operators are used to perform certain logical operations on values and variables. These are the special reserved keywords that carry out some logical computations. The value the operator operates on is known as Operand. In Python, they are used on conditional statements (either True or False
4 min read
Division Operators in Python
Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. There are two types of division operators: Float divisionInteger division( Floor division)When an in
5 min read
Python Logical Operators
Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions. These Python operators, alongside arithmetic operators, are special symbols used to carry out computations on values and variables. In this article, we will discuss l
6 min read
Comparison Operators in Python
Python operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters
4 min read
How to Do Calculus with Python ?
Calculus is a branch of mathematics focused on limits, functions, derivatives, integrals, and infinite series. We will use SymPy library to do calculus with python. SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the c
4 min read
Operator Functions in Python | Set 1
Python has predefined functions for many mathematical, logical, relational, bitwise etc operations under the module "operator". Some of the basic functions are covered in this article. 1. add(a, b) :- This function returns addition of the given arguments. Operation - a + b. 2. sub(a, b) :- This func
5 min read
Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...)
Python in its definition provides methods to perform inplace operations, i.e. doing assignments and computations in a single statement using an operator module. Example x += y is equivalent to x = operator.iadd(x, y) Inplace Operators in PythonBelow are some of the important Inplace operators in Pyt
3 min read