Python Operator :
Operator are very necessary for any programming language for do mathematical function . With out mathematical function you can not add any logic into programming code .Logic of any programming language is the main portion which is soul of any programming language . There is similar operator in python like other programming language .
- Arithmetic Operator
- : minus
/ : slash
* : asterisk
% : percent
There is another arithmetic operators which are ** , // .
- ** returns exponentiation .
- // returns quotient after discarding fractional part .
- % returns remainder . It is also called modulus operator .
- = is called assignment operator .
Input :
>>>print(5+2) #Adding
>>>print(5-2) #substraction
>>>print(5*3) #multiplication
>>>print(5/3) #division
>>>print(5**3)
>>>print(5//3)
>>>print(5%3)
Output :
>>>7
>>>3
>>>15
>>>1.6666666666666667
>>>125
>>>1
>>>2
- Compound assignment Operator
+= , -= , *= , //= , /= , **=
- a += 3 means a= a+3
- a **=3 means a=a**3
Python Conversion :
We can easily convert one data type to another data type in python . Python provides very useful tool to do this operation like integer to float , integer to string , float to integer and vice-versa .
- int(float) : Convert float number to integer .
- float(int) : Integer to float
Input :
>>>print(int(5.6)) # float to integer
>>>print(float(5)) #integer to float
>>>print(int('2')) #from numeric string to integer
>>>print(int('100',2)) #convert from binary to decimal int
>>>print(int('341',8)) #convert from octal to decimal int
Output :
>>>5
>>>5.0
>>>2
>>>4
>>>225
Share: