Tuesday, 14 March 2017

Python Operators:Arithmetic Operators

Operators:
  • Operators are the constructs which are used to perform the operations on the data of the operands.
  • Python supports some other language operators (not all operators) and some own python operators.
  • Python supports auto boxing and auto un-boxing.
  • Unboxing: getting back the data from the object.
  • Boxing: after the operation completion, store/put the data into the object

Python supports below operators
  • Arithmetic
  • Comparison
  • Logical
  • Bit-wise
  • Assignment
  • Special Operators.

Arithmetic Operators:

  • These are used to perform mathematical/arithmetic operations by addition, subtraction and so on.


Different arithmetic operators which are supported by the Python are:


All arithmetic operators perform int values, float values and bool values.

Example 1: int values

x=10
y=3
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x%y)
print(x//y)
print(x**y)

Output:

13
7
30
3.3333333333333335
1
3
1000

Example2: Float values

x=2.3
y=4.2
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x%y)
print(x//y)
print(x**y)

output:

6.5
-1.9000000000000004
9.66
0.5476190476190476
2.3
0.0
33.056503228217146

Example 3: bool values

x=False
y=True
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x%y)
print(x//y)
print(x**y)

Output:

1
-1
0
0.0
False
0
0

Floor arithmetic and modulo arithmetic operators not perform complex values.

Example: complex values

x=3+4j
y=5+3j
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x**y)
print(x%y)
print(x//y)

Output:

(8+7j)
(-2+1j)
(3+29j)
(0.7941176470588236+0.3235294117647059j)
(-193.35023234931506-7.740434815019787j)
Traceback (most recent call last):
  File "E:/python_practice/operaters.py", line 38, in <module>
    print(x%y)
TypeError: can't mod complex numbers.

String multiplication and concatenation support.

In string multiplication----> one operand is int, another operand is string.

Example:

s1="siva"
s2=3
print(s1*s2)

Output:

sivasivasiva


In string concatenation -----> both operands are string

Example:

s1="siva"
s2="krishna"
print(s1+s2)

output:

sivakrishna.


No comments:

Post a Comment