Operators are used to perform operations on variables and values.
Python divides the operators in the following groups:
1. Arithmetic operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Operator |
Name of Operator |
Example |
+ |
Addition |
a+b |
- |
Subtraction |
a-b |
* |
Multiplication |
a*b |
/ |
Division |
a/b |
% |
Modulus |
a%b |
** |
Exponentiation |
a**b |
// |
FLoor Division |
a//b |
2. Assignment Operators
Assignment operators are used to assign values to variables. Some of the assignment operators are:
Operator |
Example |
Same as |
= |
x=10 |
x=10 |
+= |
x+=5 |
x=x+5 |
-= |
x-=5 |
x=x-5 |
*= |
x*=5 |
x=x*5 |
/= |
x/=5 |
x=x/5 |
%= |
x%=5 |
x=x%5 |
**= |
x**=5 |
x=x**5 |
//= |
x//=5 |
x=x//5 |
Comparison operators are used to compare two values:
Operator |
Name of Operator |
Example |
== |
Equal to |
x==y |
!= |
Not Equal to |
x!=y |
> |
Greater Than |
x>y |
< |
Less Than |
x<y |
>= |
Greater than or Equal to |
x>=y |
<= |
Less than or Equal to |
x<=y |
Operator |
Description |
Example |
and |
Returns True if both statements are true |
x < 5 and x < 10 |
or |
Returns True if one of the statements is true |
x < 5 or x < 4 |
not |
Reverse the result, returns False if the result is true |
not(x < 5 and x < 10) |
Operator |
Name |
Description |
& |
AND |
Sets each bit to 1 if both bits are 1 |
| |
OR |
Sets each bit to 1 if one of two bits is 1 |
^ |
XOR |
Sets each bit to 1 if only one of two bits is 1 |
~ |
NOT |
Inverts all the bits |
<< |
Zero fill left shift |
Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> |
Signed right shift |
Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
Example:
Write a program to find the square root of a given number
x=int(input("Enter an integer number : "))
sq_root=x**0.5
print("Square Root of",x,"is : ",sq_root)
#Output
Enter an integer number : 4
Square Root of 4 is : 2.0