Depending on the type of operations they do, operators are classified as –
- Assignment operators
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators
Lets see them one-by-one.
In programming, = sign is known as an assignment sign. It means we are assigning the value on the right side of the = sign to the variable on the left.
The statements x = y and y = x have very different meanings in programming.
Confused? Don’t worry have a look on below example we are here to clear your confusion 😉
Write the following code into your IDLE editor, and save it.
x = 5
y = 10
x = y
print(“x =”), x)
print(“y =”), y)
now run this program and you will get this output.
x = 10
y = 10
Here, x has an initial value of 5 (declared on the first line), the third line x = y assigns the value of y to x, hence changing the value of y remains unchanged.
Now, change the third line from x = y to y = x.
Mathematically, x = y and y = x mean the same thing, but not so in programming.
Run this modified program and you will get below output
x = 5
y = 5
here, the x value remains as 5, but it changes the value of y to 5, because the statement y = x assigns the value of x to y and y becomes 5 while x remains unchanged as 5.
There are a few more assignment operators in python. Operators like += , -= and *=.
For example, we have the variable x, with an initial value of 10. If we want to increase the value of x by 2, we can write
x = x+2
This program will firstly evaluate the expression on the right (x+2) and assign the value to the left. So the statement becomes x= 12.
Instead of writing above statement, we can also write
x += 2
this combines the assignment sign with addition operator.
Similarly, if we want to do a substraction, we can write
x -= 2
this works for all the arithmetic operators.
These operators are used to perform mathematical operations like addition, substraction, multiplication, etc.
|+||Add two operands or unary plus||x + y|
|–||Subtract right operand from the left or unary minus||x – y|
|*||Multiply two operands||x * y|
|/||Divide left operand by the right one (always results into float)||x / y|
|%||Modulus – remainder of the division of left operand by the right||x % y (remainder of x/y)|
|//||Floor division – division that results into whole number adjusted to the left in the number line||x // y|
|**||Exponent – left operand raised to the power of right|
x = 5, y = 2
x + y = 7
x – y = 3
x * y = 10
x/y = 2.5
x // y = 2 (rounds down the answer to the nearest whole number)
x % y = 1 (gives the remainder when 5 is divided by 2)
x ** y = 25 (5 to the power of 2).
Relational operators are symbols that perform operations on data and return a result as true or false depending on the comparison conditions. Thus, they are certain functionalities that do something with your variables.
Let’s take a look at the different types of relational operators in python. The following table gives you a list of all the relational operators:
|>=||Greater than or equal to|
|<=||Less than or equal to|
|! =||Not equal to|
Relational operators can be used to compare numbers as well as strings.
#Less than > print 50 < 60 True # Less or equals to > print 50 <= 60 False # Greater than > print 50 > 60 True # Greater or equals to > print 50 >= 60 False # Equals to >print 50 == 60 False > print 50 == 50 True # Does not equal to > print 50 != 50 False > print 50 != 60 True
There are three kinds of logical operations in Python. Each of them operates on one or more bool type and returns a bool type. They are listed as below:
- and – Output is True, if all the operands are True, else False
- or – Output is True, if any one of the operands is True, else False
- not – Output is True if operand is False, else False.
#Less than print 50 < 60 #Less or equals to print 50 <= 60 False # Greater than > print 50 > 60 True # Greater or equals to > print 50 >= 60 False # Equals to >print 50 == 60 False > print 50 == 50 True # Does not equal to > print 50 != 50 False > print 50 != 60 True
We have come across different kinds of operands and operators, but these operators are special ones. Bitwise operators process bits or bit patterns, unlike other operators. Basic bitwise operations are listed as below:
|&||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|
a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print "Value of c is ", c c = a | b; # 61 = 0011 1101 print "Value of c is ", c c = a ^ b; # 49 = 0011 0001 print "Value of c is ", c c = ~a; # -61 = 1100 0011 print "Value of c is ", c c = a << 2; # 240 = 1111 0000 print "Value of c is ", c c = a >> 2; # 15 = 0000 1111 print "Value of c is ", c
When you execute the above program, it produces the following result −
Value of c is 12 Value of c is 61 Value of c is 49 Value of c is -61 Value of c is 240 Value of c is 15
In this article, we learned about various operators in Python. Please let us know about your views and opinions in the comment section below and stay tuned. Thank you.😊