Python Arithmetic Operators Example

Python Arithmetic Operators Example

Here is example to show how arithmetic operation can be performed :-

#!/usr/bin/python

a = 21
b = 10
c = 0

c = a + b
print “Line 1 – Value of c is “, c

c = a – b
print “Line 2 – Value of c is “, c

c = a * b
print “Line 3 – Value of c is “, c

c = a / b
print “Line 4 – Value of c is “, c

c = a % b
print “Line 5 – Value of c is “, c

a = 2
b = 3
c = a**b
print “Line 6 – Value of c is “, c

a = 10
b = 5
c = a//b
print “Line 7 – Value of c is “, c

Output:-

Line 1 – Value of c is 31

Line 2 – Value of c is 11

Line 3 – Value of c is 210

Line 4 – Value of c is 2

Line 5 – Value of c is 1

Line 6 – Value of c is 8

Line 7 – Value of c is 2

Lets see below another example:-

LineCodeMeaning
1total = 9total is assigned the value of 9.
2total += 10total is assigned the value of itself plus 10.
This is the “shortcut” operator for addition. It is easier to type than “total = total + 10”.
total now has the value of 19.
3total -= 10total is assigned the value of itself minus 10.
This is the shortcut operator for subtraction.
total now has the value of 9.
4total *= 3total is assigned the value of itself multiplied by 3.
This is the shortcut operator for multiplication.
total now has the value of 27.
5total /= 2total is assigned the value of itself divided by 2.
This is the shortcut operator for division.
total now has the value of 13, not 13.5, because / returns the integer portion of the division.
6total = 27
7from __future__ import division
8total /= 2total is assigned the value of itself divided by 2, preceded by an import from the __future__ module.
What this line says is that in a future release of Python (Python 3000, to be exact), division will include the fractional portion of the result.
total is now 13.5
9total = ’9’total is assigned the value of the character ’9’.
This ’9’ is not an integer, but a string data type. Even though it looks the same as the integer 9, the program cannot use it for arithmetic calculations.
10total += 10total, with a string value of ’9’ is assigned the value of itself plus the integer 10.
This causes a compiler error, because you cannot add a string and a number
11total = int(total)total is converted to an integer.
This only works if the string can convert to an integer.
total now has the value of the integer 9.
12total += .5total is assigned the value of itself plus a floating point number.
The datatype of total now becomes float in order to accommodate the floating point total now has the value of 9.5
13total -= .5total is assigned the value of itself minus a floating point number.
The datatype of total is still a float, even though its decimal fraction is 0. total now has a value of 9.0
14total = int(total)total is converted to an integer.
total now has the value of 9.

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply