Operators in python play very important role to compute mathematical, relational or logical expression. Operators in Python are tokens and when they apply on variables they provoke some computation.
I-Unary Operators-
These operators require only one variable.
Examples-
+ (Unary Plus)
– (Unary Minus)
not ( Negation)
~ ( Bit-wise Negation)
II-Binary Operators-
Binary operators in Python require two operands to perform computation.
1-Arithmetic Operators-
a-Addition-
+ symbol is used for addition of two numbers.
For example
a=6
b=5
c = a + b
print(c)
The output would be 11
b-Subtraction-
a=6
b=5
c = a – b
print(c)
The output would be 1
c-Multiplication-
a=6
b=5
c = a * b
print(c)
The output would be 30
d-Division-
a=17
b=5
c = a / b
print(c)
e-Modulus-
a=17
b=5
c = a % b
print(c)
The output would be 2
f-Exponent-
a=2
b=3
c = a ** b
print(c)
The output would be 8
g-Floor Division-
a=17
b=5
c = a / /b
print(c)
The output would be 3
2-Relational Operators-
Relational operators are used to compare two values, a relational operator returns True or False.
x=5
y=3
z = x
a-Less Than-
< symbol is used less than relational operator
x=5
y=3
z = y < x
print(z)
It would print True
because 3 < 5
See another example
x=15
y=23
z = y < x
print(z)
It would print False
because 23 < 15 is false.
b-Greater Than-
x=25
y=23
z = x > y
It would print True
print(z)
c-Less Than or Equal To-
<= symbol is used less than or equal relational operator
x=5
y=3
z = y <= x
print(z)
It would print True
d-Greater Than or Equal To-
=> symbol is used greater than or equal relational operator
x=25
y=23
z = x => y
It would print True
print(z)
e-Equal To-
== symbol is used for equal relational operator
x=25
y=23
z = x == y
print(z)
It would print False, since x is not equal to y.
See another example
x=25
y=25
z = x == y
print(z)
It would print True, since x equal to y.
f-Not Equal To-
!= symbol is used for not equal to relational operator
x=25
y=23
z = x != y
print(z)
It would print True, since x is not equal to y.
See another example
x=25
y=25
z = x != y
print(z)
It would print False, since x is equal to y.
3-Logical Operators-
Logical operators connect relational operators.
a-Logical AND
“and” is used for AND operator.
Example-
x=8
y=6
z=7
r= x > y and x > z
print(r)
The output would be True
b-Logical OR
“and” is used for AND operator.
Example-
x=8
y=6
z=7
r= x > y or x < z
print(r)
The output would be True
4-Bitwise Operators-
Bit-wise operators perform logical operations at bit level.
a-Bit-wise AND
& is used for bit-wise AND.
x=6
y=5
z= x & y
print(z)
The output would be 4
To understand see the image below
b-Bit-wise OR
| is used for bit-wise OR
x=6
y=5
z= x | y
print(z)
The output would be 7
c-Bit-wise Exclusive OR
^ is used for bit-wise EX-OR
x=6
y=5
z= x ^ y
print(z)
The output would be 3
5-Shift Operators-
Shift operators work at the bit level and shift bits specified by programmer.
a-Shift Left-
<< symbol is used for left shift operator, see an example.
x=5
y=x<<1
print(y)
The output would be 10.
See how it works
First 5 is converted into binary number which is 101
then x<<1 statement shifts one bit left and put 0 at the right place
i.e.
1010
Which is 10 in decimal
See another example
x=5
y=x<<2
print(y)
The output would be 20.
First 5 is converted into binary number which is 101
then x<<2 statement shifts two bits left and put 00 at the right place
i.e.
10100
Which is 20 in decimal.
b-Shift Right-
>> symbol is used for right shift operator, see example
x=40
y=x>>1
print(y)
The output would would be 20.
First 40 is converted into binary number which is 101000
then x>>1 statement shifts 1 bit right and put 0 at the left place
i.e.
010100
Which is 20
See the another example
x=40
y=x>>2
print(y)
The output would would be 10.
First 40 is converted into binary number which is 101000
then x>>2 statement shifts 2 bits right and put 00 at the left place
i.e.
001010
Which is 10 in decimal number system.
6-Membership Operators-
Membership operators check whether an object is in a sequence (list, tuple and set) or not.
a- in Operator-
“in” operator checks whether an object is in a sequence or not if it is in sequence expression then True will be returned.
For example see the example-
x=50
l=[20,50,70,80]
b= x in l
print(b)
It would print True
On the other hand
x=60
l=[20,50,70,80]
b= x in l
print(b)
It would print False
b- not in Operator
“not in” operator checks whether an object is in a sequence or not if it is in sequence expression then False will be returned.
x=50
l=[20,50,70,80]
b= x not in l
print(b)
It would print True
7-Assignment Operators
Assignment operators are used to define variables in Python.
a-Assignment
= is used to assign a number, and right side number is assigned to left side variable.
For example-
x=5 then x refers to 5
b-Assign Quotient
x/=5 it means x=x/5
c-Assign Sum
x+=5 it means x=x+5
d-Assign Product
x*=5 it means x=x*5
e-Assign Remainder
x%=5 it means x=x%5
f-Assign Difference
x-=5 it means x=x-5
g-Assign Exponent
x**=5 it means x=x**5
h-Assign Floor Division
x//=5 it means x=x//5
8-Identity Operators-
Identity operators are used to check whether two variables are referencing the same memory.
a- is Operator
“is” is used to check whether two variables are referring the same memory address.
x=5
y=5
If you check
z= x is y
print(z)
The output would be True
But if
x=int(input(“Enter a number”))
y=5
If you check
z= x is y
The output would be False
b- is not Operator
“is not” is is used to check whether two variables are not referring the same memory address.
x=5
y=5
If you check
z= x is not y
print(z)
The output would be False
Conclusion-
To sum up, I have explained about operators in Python, you should practice and apply in applications. Then you programming skills will be better.