What is an Operator in Python?
Operators in Python are special symbols that perform arithmetic or logical computations. The value that the operator works on is called the “operand.” This lesson will teach you about the many types of operators in Python, their syntax, and how to utilize them with examples.
Table of contents
Different Types of Operators in Python
Here are the different types of operators in Python:
- Arithmetic Operators
- Logical Operators
- Comparison (Relational) Operators
- Bitwise Operators
- Assignment Operators
- Membership Operators
- Identity Operators
Python Arithmetic Operators
Arithmetic operators are used in mathematics to accomplish operations such as addition, subtraction, multiplication, and so on.
Operator | Meaning | Example |
---|---|---|
+ | Add two operands or unary plus | a + b + 2 |
– | Subtract the right operand from the unary minus. | a – b – 2 |
* | Multiplies the values on either side of the operator. | a * b |
/ | Division Operators divide numbers and return a quotient. | a / b |
% | Modulus – the remainder of the division of the left operand by the right operand | a % b (remainder of a/b) |
// | Floor division – division that returns a whole number moved to the left on the number line | a // b |
** | Exponent – left operand raised to the power of right | a**b (a to the power b) |
Arithmetic Operator Example:
a = 15
b = 4
# Output: a + b = 19
print('a + b =',a+b)
# Output: a - b = 11
print('a - b =',a-b)
# Output: a * b = 60
print('a * b =',a*b)
# Output: a / b = 3.75
print('a / b =',a/b)
# Output: a // b = 3
print('a // b =',a//b)
# Output: a ** b = 50625
print('a ** b =',a**b)
Output:
a + b = 19
a - b = 11
a * b = 60
a / b = 3.75
a // b = 3
a ** b = 50625
Python Logical Operators
The operators and, or, and not are logical operators.
Operator | Meaning | Example |
---|---|---|
and | If both operands are true, then this is true. | a and b |
or | True if one of the operands is true. | a or b |
not | If the operand is false, the condition is true (complements the operand) | not a |
Logical Operator Example:
a = True
b = False
print('a and b is',a and b)
print('a or b is',a or b)
print('not a is',not a)
Output:
a and b is False
a or b is True
not a is False
Truth Table for Logical Operator
The Truth Table for Logical Operators is shown below.
and, or, not
The logical operators in Python are and, or, and not. and will only produce a True result if both operands are True. Below is the truth table for and:
A | B | A and B |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
If any of the operands is True, or will produce True. Below is the truth table for or:
A | B | A or B |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
The truth value is inverted using the not operator. Below is the truth table for not:
A | not A |
---|---|
True | False |
False | True |
Example:
>>> True and False
False
>>> True or False
True
>>> not False
True
Comparison Operators in Python
Value comparison is done using comparison operators. Depending on the criteria, it returns True or False.
Operator | Meaning | Example |
---|---|---|
> | Greater than – True if left operand is greater than the right | a > b |
< | Less than – True if left operand is less than the right | a < b |
== | Equal to – True if both operands are equal | a == b |
!= | Not equal to – True if operands are not equal | a != b |
>= | Greater than or equal to – True if left operand is greater than or equal to the right | a >= b |
<= | Less than or equal to – True if left operand is less than or equal to the right | a <= b |
Comparison Operator Example:
a = 10
b = 12
# Output: a > b is False
print('a > b is',a>b)
# Output: a < b is True
print('a < b is',a<b)
# Output: a == b is False
print('a == b is',a==b)
# Output: a != b is True
print('a != b is',a!=b)
# Output: a >= b is False
print('a >= b is',a>=b)
# Output: a <= b is True
print('a <= b is',a<=b)
Output:
a > b is False
a < b is True
a == b is False
a != b is True
a >= b is False
a <= b is True
Python Bitwise Operators
Bitwise operators in Python operate bit by bit and operate on bits.
For example a = 60 and b = 13.
In this case, their values in binary form would be a = 0011 1100 and b = 0000 1101, respectively.
Operator | Meaning | Example |
---|---|---|
& | Bitwise AND | a & b = 0 (0000 0000 ) |
| | Bitwise OR | a | b = 14 (0000 1110 ) |
~ | Bitwise NOT | ~a = -11 (1111 0101 ) |
^ | Bitwise XOR | a ^ b = 14 (0000 1110 ) |
>> | Bitwise right shift | a >> 2 = 2 (0000 0010 ) |
<< | Bitwise left shift | a << 2 = 40 (0010 1000 ) |
Bitwise Operator Example:
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print ("Line 1 - Value of c is ", c)
c = a | b; # 61 = 0011 1101
print ("Line 2 - Value of c is ", c)
c = a ^ b; # 49 = 0011 0001
print ("Line 3 - Value of c is ", c)
c = ~a; # -61 = 1100 0011
print ("Line 4 - Value of c is ", c)
c = a << 2; # 240 = 1111 0000
print ("Line 5 - Value of c is ", c)
c = a >> 2; # 15 = 0000 1111
print ("Line 6 - Value of c is ", c)
Output:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
Assignment Operators in Python
In Python, assignment operators are used to give variable values.
Using the simple assignment operator a = 5, the variable a on the left is given the value 5 on the right.
There are many different types of compound operators in Python. For example, a += 5 adds 5 to a variable and then assigns it. It has the same meaning as a = a + 5.
Operator | Example | Equivalent to |
---|---|---|
= | a = 5 | a = 5 |
+= | a += 5 | a = a + 5 |
-= | a -= 5 | a = a – 5 |
*= | a *= 5 | a = a * 5 |
/= | a /= 5 | a = a / 5 |
%= | a %= 5 | a = a % 5 |
//= | a //= 5 | a = a // 5 |
**= | a **= 5 | a = a ** 5 |
&= | a &= 5 | a = a & 5 |
|= | a |= 5 | a = a | 5 |
^= | a ^= 5 | a = a ^ 5 |
>>= | a >>= 5 | a = a >> 5 |
<<= | a <<= 5 | a = a << 5 |
Assignment Operator Example:
a = 25
b = 5
c = 0
c = a + b
print ("Line 1 - Value of c is ", c)
c += a
print ("Line 2 - Value of c is ", c)
c *= a
print ("Line 3 - Value of c is ", c)
c /= a
print ("Line 4 - Value of c is ", c)
c = 2
c %= a
print ("Line 5 - Value of c is ", c)
c **= a
print ("Line 6 - Value of c is ", c)
c //= a
print ("Line 7 - Value of c is ", c)
Output:
Line 1 - Value of c is 30
Line 2 - Value of c is 55
Line 3 - Value of c is 1375
Line 4 - Value of c is 55.0
Line 5 - Value of c is 2
Line 6 - Value of c is 33554432
Line 7 - Value of c is 1342177
Membership Operators
The membership operators in Python are in and not in. They are used to determine if a value or variable can be found in a sequence (string, list, tuple, set, and dictionary).
In a dictionary, we can only check if the key is there, not if the value is there.
Operator | Meaning | Example |
---|---|---|
in | True if the value or variable is in the sequence. | 5 in a |
not in | True if value or variable is not found in the sequence | 5 not in a |
Membership Operator Example:
a = 'Hello world'
b = {1:'a',2:'b'}
# Output: True
print('H' in a)
# Output: True
print('hello' not in a)
# Output: True
print(1 in b)
# Output: False
print('a' in b)
Output:
True
True
True
False
Here, ‘H’ is in a, but ‘hello’ is not in a. (remember, Python is case sensitive). In the same way, 1 is the key, and ‘a’ is the value in dictionary b. So, ‘a’ in b returns False.
Identity Operators
In Python, the identity operators are is and is not. They are used to see if two values (or variables) are located on the same part of memory. Even if two variables are identical, this does not indicate that they represent the same object.
Operator | Meaning | Example |
---|---|---|
is | True if the operands are identical (refer to the same object) | a is True |
is not | True if the operands are not identical (do not refer to the same object) | a is not True |
Identity Operator Example:
a1 = 5
b1 = 5
a2 = 'Hello'
b2 = 'Hello'
a3 = [1,2,3]
b3 = [1,2,3]
# Output: False
print(a1 is not b1)
# Output: True
print(a2 is b2)
# Output: False
print(a3 is b3)
Output:
False
True
False
Here, we can see that a1 and b1 are both integers with the same value. This means that they are both equal and the same. The same is true for a2 and b2 (strings).
But a3 and b3 are both lists. They are the same, but not the same because the interpreter puts them in different places in his or her memory, even though they are the same.
Python Operators Precedence
The table below displays operator precedence from most to least important.
Operator | Description |
---|---|
** | Exponentiation (raise to the power) |
~ + – | Complement, unary plus and minus (method names for the last two are [email protected] and [email protected]) |
* / % // | Multiply, divide, modulo and floor division |
+ – | Addition and subtraction |
>> << | Right and left bitwise shift |
& | Bitwise ‘AND’td> |
^ | | Bitwise exclusive `OR’ and regular `OR’ |
<= < > >= | Comparison operators |
<> == != | Equality operators |
= %= /= //= -= += *= **= | Assignment operators |
is is not | Identity operators |
in not in | Membership operators |
not or and | Logical operators |
Summary
In summary, Operators in Python are what make Python work. Operators can be used to do a lot of different things, like add two numbers or give a variable a value. And in this tutorial, we covered the types of operators in Python.
Now that we know what operators are, we are one step closer to writing better and more efficient codes. So, start using all the operators you’ve learned today in your code.
In the next post, we’ll learn how to use Python to make decisions based on things that can happen while a program is running.
Inquiries
If you’re into machine learning you can visit our article about Top 40 Machine Learning Projects with Source Code.
Finally, if you have any questions or suggestions regarding the Types of Operators in Python, please do not hesitate to contact me. Simply leave a comment on this page or send me a message using the contact page of our website.
Python Variable Types
Python Decision-Making Statements