Operators are used to perform arithmetic and logical operations on input data. They allow us to manipulate data. They can be in-fix or prefix notations. In-fix ones appear between two operands (these are the values on which the operator works). A prefix operator usually works on one operand. These are also called binary and unary operators.
Operators can be classified into 5 different category. We will take a look at each of them one by one.
Arithmetic Operators
This kind of operators allow us to perform arithmetic operations in Python. These include addition, subtraction, multiplication, division and few more. Below table summarizes these operators in detail.
| Operator | Description |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication of two numbers |
// | Integer division. This returns the quotient of the division operation of two integers |
/ | Division. This returns float result for the division of two numbers. |
% | This returns the remainder for the division of two numbers. This is also called modulo operator |
** | Exponent. |
The easiest way to experiment with these operators is to use Python REPL (Read Evaluate Print Loop). On Command prompt, just type python and your prompt should change like below.
Please, note that in REPL, it outputs the value directly without using any method like
python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
1>>> 2 + 4 # sum of two numbers
26
3>>> 4 - 2 # subtraction
42
5>>> 2 - 5 # subtraction can return negative numbers too
6-3
7>>> 5 * 2 # multiplication
810
9>>> 5 // 2 # integer division, returns quotient
102
11>>> 5 % 2 # modulo, returns remainder of the division
121
13>>> 5 / 2 # division
142.5
15>>> 2 ** 3 # exponent
168
17>>>
Assignment Operators
Assignment operators are used to assign values to a variable. The most common among these is = which we already used.
| Operator | Description |
|---|---|
= | Assign a value to a variable |
+= | Add and assigne a value |
-= | Subtract and assgn a value |
*= | Multiply and assign |
/= | Divide and assign a value |
//= | Divide and assign quotient |
%= | Divide and assign remainder |
| ` | =` |
&= | Bitwise AND and assign |
^= | Bitwise XOR and assign |
>>= | Bitwise Right shift and assign |
<<= | Bitwise Left shift and assign |
1counter = 1
2print(counter)
3
4counter += 1 # 2
5print(counter)
6
7counter += 4 # 6
8print(counter)
9
10counter //= 2 # 3
11print(counter)
12
13counter %= 2 # 1
14print(counter)
11
22
36
43
51
Comparison Operators
These are mathematical comparison operators. It returns boolean result based on the comparison operation being performed.
| Operator | Description |
|---|---|
> | Greater than |
< | Less than |
<= | Less than or equal to |
>= | Greater than or equal to |
!= | Is not Equal to |
== | Equal to |
is | Verifies whether two objects are same |
is not | Verifies whether two objects are different |
python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
1>>> 1 == 1
2True
3>>> 1 != 1
4False
5>>> 4 > 5
6False
7>>> 4 < 5
8True
9>>> 4 >= 4
10True
11>>> 6 <= 4
12False
13>>> 4 is 4 # This gives warning. We should be using variable instead of direct literal values.
14<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
15True
16>>> num1 = 1 # create new variable
17>>> num2 = 1 # create another variable with same value
18>>> num1 is num2
19True
20>>> num1 is not num2
21False
Logical Operators
Logical operators are the ones which work on Boolean operands. These are also used in conditional checks usually along with comparison operators.
| Operator | Description |
|---|---|
and | AND operation |
or | OR operation |
not | Complement operation |
Let’s see an example of these operators
1is_logged_in = True
2is_admin = False
3print(is_logged_in and is_admin) # False
4print(not is_admin) # True
5print(not (not is_admin)) # False
6print(is_logged_in and not is_admin) # True
False
True
False
True
Bitwise Operators
Bitwise operators work on bit level of data values. Each value is stored in the form of bits (0 and 1). These operators work on those bits.
| Operator | Description |
|---|---|
& | Bitwise AND operation |
| ` | ` |
^ | Bitwise XOR operation |
~ | Bitwise NOT operation |
<< | Shift Left operation |
>> | Shift Right operation |
1a = 4 # 0100
2b = 2 # 0010
3c = 5 # 0101
4print(a | b) # 0110 = 6
5print(a & b) # 0000 = 0
6print(a ^ c) # 0001 = 1
7
8print (b << 1) # 0100 = 4
9print (b << 2) # 1000 = 8
10print (c << 1) # 1010 = 10
6
0
1
4
8
10
Conclusion
In this lesson, we looked at what are different types of operators available in Python. We also looked at some of the constructs which can be used for conditional checks. Bitwise operators are used less but can become very useful for efficient algorithms.


Comments