Arithmetic Operators in Python

We all have been using Arithmetic operators for a very long time and these works the same way in programming languages as well. Let’s learn about these operators and try with some examples.

 

Addition Operator (+)

Addition operators are used to adding two values. Simple maths, right?

Let’s try it out!

a = 10
b = 20

 

# Addition
# Adds values on either side of the operator.

"""
output:

a+b
30

"""

But there is more to it in Python than just doing simple addition. It can concatenate two strings as well.

Sound amazing, right? Let’s see one more example!

firstName = "Co-learning "
lastName = "Lounge"

firstName+lastName

"""
output:

'Co-learning Lounge'

"""

 

Subtraction Operator (-)

The subtractionOperator, as you expect, does the subtraction. It subtracts the right operand from the left operand.

# Subtraction
# Subtracts right-hand operand from left-hand operand.

a = 10
b = 20 


"""
output:

a-b
-10

"""

 

Multiplication Operator (*)

The multiplication operator is used to get the product of two values. It is denoted by an asterisk(*). It is slightly different from what we use in maths (x).😀

# Multiplication
# Multiplies values on either side of the operator

a = 10 
b = 20 


"""
output:

a * b
200

"""

 

Division Operator (/)

The divisionoperator (/) is used to divide two numbers. It divides the left operand by the right operand and returns the quotient.

Let’s try it out!

# Division
# Divides left-hand operand by right-hand operand

a = 10
b = 20

"""
output:

b/a
2.0

"""

If you have noticed one thing; it returns float every time, even if both numbers are integers.

We can verify this with the type function!

type(b/a)

"""
output:

float

"""

Hence this can be used for typecasting. Don’t panic because of something that sounds so meaningful. Typecasting is a way to convert values from one data type to another.

The value of a is 10 and we want to convert it to float. let’s see how the division operator is helpful.

a = 10
type(a)

"""
output:

int

"""

 

# We will divide a by 1 and assign the value back to a.

a = a/1
type(a)


"""
output:

float

"""

See how a is now a float without any changes in its original value.

Modulus Operator (%)

Modules operator is used to find the remainder when the left operand is divided by the right operand.

Let’s have a look at one example!

# Modulus
# Divides left-hand operand by right-hand operand and returns the remainder

c = 5
d = 2

c % d

"""
output:

1

"""

 

Exponent Operator (**)

The exponent operator is used to finding to the power of the left operand to the right operand. It is denoted by two asterisks. (**)

# Exponent
# Performs exponential (power) calculation on operators

a**b

"""
output:

100000000000000000000

"""

 

Floor Division (//)

The division of operands where the result is the quotient in which the digits after The decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity).

9//2

"""
output:

4

"""

 

9.0//2.0

"""
output:

4.0

"""

 

-11//3

"""
output:

-4

"""

 

-11.0//3

"""
output:

-4.0

"""

 

#Example
x = 15
y = 4
print(f"x  = {x} and y = {y}")

# Output: x + y = 19
print('x + y =',x+y)

# Output: x - y = 11
print('x - y =',x-y)

# Output: x * y = 60
print('x * y =',x*y)

# Output: x / y = 3.75
print('x / y =',x/y)

# Output: x // y = 3
print('x // y =',x//y)

# Output: x ** y = 50625
print('x ** y =',x**y)


"""
output:

x  = 15 and y = 4
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625

"""

We have seen some of the obvious arithmetic operators with some examples. Arithmetic operators are very useful in programming as they help us construct logic.

One more thing to understand here is ‘what if there are multiple operators in one line?’ or which operator will take precedence in a nested calculation? Here also just like in mathematics, the interpreter follows some rules.

We will learn about the precedence of operators in another blog.