< Prev
Next >

# Python Arithmetic Operators

## Arithmetic Operators

There are 6 kinds of arithmetic operators in Python -
• - operator(subtraction).
• * operator(multiplication).
• / operator(floating-point division).
• // operator(integer division).
• % operator(modulus).

• ## Arithmetic operators(+, -, *)

• In the upcoming example, we are going to display the use of arithmetic operators like + - * in Python.

Note :
• The arithmetic addition + operator is overloaded as when it is applied on two variables carrying integer values, it adds them and when it is applied to two variables carrying string values, it concatenates(adds) them as well.

• The arithmetic multiplication * operator is also overloaded in a way that if one of its operands is a string value and the other is an integer value, the * operator duplicates the string value to the number of times of its integer operand value.

• Unlike + operator which allowed both operands to be a string value to concatenate them, both the operands of an * operator cannot be a string value because it wouldn't make sense to multiple two string values.

``````#Arithmetic Operators Example in Python

x=2				# variable1
y=10                            # variable2
s1 = 'hi'			# variable3
s2 = ' there'			# variable4

sum = x + y

s1 = s1 + s2

# subtraction of two integers
subtract = x - y

# multiplication of two integers
multiply = x * y

# Overloading * operator performing multipliction of an integer with a string
result = 2 * s2

print("Sum of two integers = ", sum)
print("Concatenation of two strings = ", s1)
print("Subtraction of two integers = ", subtract)
print("Multplication of two integers = ", multiply)
print("Multplication of an int with a string = ", result)
``````

## Output is

``````Sum of two integers =  12
Concatenation of two strings =  hi there
Subtraction of two integers =  -8
Multplication of two integers =  20
Multplication of an int with a string =   there there``````

## floating-point division operator /, Modulus operator % and integer division operator //

• floating-point division operator / gives us a floating-point quotient value after dividing two numbers(irrespective of the numbers being integer or float).
• Modulus operator % gives us the remainder value after dividing two numbers.
• Integer division operator // gives us an integer quotient value after dividing two integers and it gives a floating-point quotient after dividing a floating-point number with an integer value or vice versa.

Note : The // integer division operator is not found in C, C++ and Java languages.

``````# Division and Modulus operator in Python

i=40;
j=10;

floating_quotient = i/j
integer_quotient = i//j
remainder = i%j

print("Floating Quotient value after dividing 40 by 10 : ", floating_quotient)
print("Integer Quotient value after dividing 40 by 10 : ", integer_quotient )
print("Remainder after diviiding 40 by 10 : ", remainder)

a = 70
b = 20

floating_quotient = a/b
integer_quotient = a//b
floating_remainder = a%b

print("Floating Quotient value after dividing 70 by 20 : ", floating_quotient)
print("Integer Quotient value after dividing 70 by 20 : ", integer_quotient )
print("Remainder after dividing 70 by 20 : ", remainder)

k=20;
l=2.0

floating_quotient = k/l
integer_quotient = k//l
remainder = k%l

print("Floating Quotient value after dividing 40 by 10 : ", floating_quotient)
print("Integer Quotient value after dividing 40 by 10 : ", integer_quotient )
print("Remainder after diviiding 40 by 10 : ", remainder)
``````

## Output-

``````Floating Quotient value after dividing 40 by 10 :  4.0
Integer Quotient value after dividing 40 by 10 :  4
Remainder after diviiding 40 by 10 :  0
Floating Quotient value after dividing 70 by 20 :  3.5
Integer Quotient value after dividing 70 by 20 :  3
Remainder after dividing 70 by 20 :  0
Floating Quotient value after dividing 40 by 10 :  10.0
Integer Quotient value after dividing 40 by 10 :  10.0
Remainder after diviiding 40 by 10 :  0.0```
```

## Priorities of arithmetic operators in arithmetic expressions.

• + and - operator have the least priority in an arithmetic expression.
• *, / and % operators have an equal highest priority in an arithmetic expression.

## Calculating arithmetic expressions with equal and unequal priorities.

• Calculation of an arithmetic expression involving operators with unequal priorities starts with the calculation of a sub-expression that involves an operator with the highest priority.
• Calculation of an arithmetic expression involving operators with equal priorities is made by evaluating expressions from left to right.

For example, when calculating an arithmetic expression 3+2x4-2, sub-expression involving * operator will be calculated first, because operator * has the highest priority. Hence -
• In 3+2*4-2, first, 2*4 will be calculated, which results in the next expression i.e. 3+8-2.
• Next in 3+8-2, now + and - operator have an equal priority, so the expression will be calculated from left to right, which results in 11-2.
• Next, 11-2, results in 9.

Let's understand with a code -:
``````# Arithmetic expressions with equal and unequal priority operators

result = 3+2-4+2;		#  + - operators have equal priorities
print("Result of 3+2-1+2 is : ", result)

result = 3+2*4+2;		#  + *  have unequal priorities
print("Result of 3+2*4+2 is : ", result)

result = 3+2*4/2;		#  + * / operators have unequal priorities
print("Result of 3+2*4/2 is : ", result)

result = 3%2*4/2;		#  % * / operators have equal priorities
print("Result of 3%2*4/2 is : ", result)
``````

## Output-

``````Result of 3+2-1+2 is :  3
Result of 3+2*4+2 is :  13
Result of 3+2*4/2 is :  7.0
Result of 3%2*4/2 is :  2.0``````

## Program Analysis

• First arithmetic expression 3+2-4+2, is calculated left to the right as + - operators have equal priorities.
• The * operator have more priority than +, hence in the second expression 3+2*4+2, 2*4 is calculated first.
• * / operators have more priority than +, hence in the third expression 3+2*4/2, hence 2*4 is calculated first.
• % * / operators have an equal priority, hence in the fourth expression 3%2*4/2, , hence expression is calculated from left to the right, starting with 3%2.

## Arithmetic expressions with sub-expressions in parenthesis ( ) will always be executed first

``````#Arithmetic operator example

result = (3+2)*4+2;
print("Result of (3+2)*4+2 is : ", result);

result = 3%(2*4)/2;
print("Result of 3%(2*4)/2 is : ", result);
``````

## Output-

``````Result of (3+2)*4+2 is : 22
Result of 3(2*4)/2 is : 1.5``````

## Program Analysis

• In the first expression (3+2)*4+2, * has more priority than + but the sub-expression in () has the most priority, hence (3+2) is calculated first.
• In the second expression 3%(2*4)/2, * % / have equal priority so this expression could be calculated left to right but the sub-expression in () has most priority, hence (2*4) is calculated first.   