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:-

Line Code Meaning
1 total = 9 total is assigned the value of 9.
2 total += 10 total 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.
3 total -= 10 total is assigned the value of itself minus 10.
This is the shortcut operator for subtraction.
total now has the value of 9.
4 total *= 3 total is assigned the value of itself multiplied by 3.
This is the shortcut operator for multiplication.
total now has the value of 27.
5 total /= 2 total 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.
6 total = 27
7 from __future__ import division
8 total /= 2 total 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
9 total = ’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.
10 total += 10 total, 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
11 total = 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.
12 total += .5 total 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
13 total -= .5 total 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
14 total = int(total) total is converted to an integer.
total now has the value of 9.

Related Posts

  • 53
    While executing an arithmetic statement, which has two or more operators, we may have some problems as to how exactly does it get executed. For example, does the expression 2 * x - 3 * y correspond to (2x)-(3y) or to 2(x-3y)? Similarly, does A / B * C correspond…
    Tags: operation, operators, performed, example, arithmetic, python
  • 48
    Example: !/usr/bin/python a = 21 b = 10 c = 0 c = a + b print "Line 1 - Value of c is ", c c += a print "Line 2 - Value of c is ", c c *= a print "Line 3 - Value of c is…
    Tags: example, python, operators
  • 48
    Example: #!/usr/bin/python a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print "Line 1 - a is available in the given list" else: print "Line 1 - a is not available in the given list" if ( b not…
    Tags: python, operators, example
  • 48
    Example: #!/usr/bin/python a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011…
    Tags: example, operators, python
  • 47
    Example:  #!/usr/bin/python x = True y = False print('x and y is',x and y) print('x or y is',x or y) print('not x is',not x) Output: x and y is False x or y is True not x is False Few more example: 1. if (five == 5) AND (two ==…
    Tags: example, operators, python

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

Your email address will not be published. Required fields are marked *

%d bloggers like this: