Example of Arithmetic Operators in Python
Division Operators
In Python programming language 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 division
- Floor division
Float division
The quotient returned by this operator is always a float number, no matter if two numbers are integers. For example:
Example: The code performs division operations and prints the results. It demonstrates that both integer and floating-point divisions return accurate results. For example, β10/2β² results in β5.0β, and β-10/2β results in β-5.0β.
Python
print(5/5)
print(10/2)
print(-10/2)
print(20.0/2)
Output:
1.0
5.0
-5.0
10.0
Integer division( Floor division)
The quotient returned by this operator is dependent on the argument being passed. If any of the numbers is float, it returns output in float. It is also known as Floor division because, if any number is negative, then the output will be floored. For example:
Example: The code demonstrates integer (floor) division operations using the // in Python operators. It provides results as follows: β10//3β² equals β3β, β-5//2β equals β-3β, β5.0//2β² equals β2.0β, and β-5.0//2β equals β-3.0β. Integer division returns the largest integer less than or equal to the division result.
Pythons
print(10//3)
print (-5//2)
print (5.0//2)
print (-5.0//2)
Output:
3
-3
2.0
-3.0
Precedence of Arithmetic Operators in Python
The precedence of Arithmetic Operators in Python is as follows:
- P β Parentheses
- E β Exponentiation
- M β Multiplication (Multiplication and division have the same precedence)
- D β Division
- A β Addition (Addition and subtraction have the same precedence)
- S β Subtraction
The modulus of Python operators helps us extract the last digit/s of a number. For example:
- x % 10 -> yields the last digit
- x % 100 -> yield last two digits
Arithmetic Operators With Addition, Subtraction, Multiplication, Modulo and Power
Here is an example showing how different Arithmetic Operators in Python work:
Example: The code performs basic arithmetic operations with the values of βaβ and βbβ. It adds (β+β), subtracts (β-β), multiplies (β*β), computes the remainder (β%β), and raises a to the power of βb (**)β. The results of these operations are printed.
Python
a = 9
b = 4
add = a + b
sub = a - b
mul = a * b
mod = a % b
p = a ** b
print(add)
print(sub)
print(mul)
print(mod)
print(p)
Output:
13
5
36
1
6561
Note: Refer to Differences between / and // for some interesting facts about these two Python operators.
Python Operators
In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators.
- OPERATORS: These are the special symbols. Eg- + , * , /, etc.
- OPERAND: It is the value on which the operator is applied.
Contact Us