Build your mother tongue with 'Operators' vocabulary


"It’s often said that the harder you try to avoid a problem, the quicker it comes chasing after you." Have you ever come across this phrase in your life until today? Well, I have, though I had heard it quite frequently, yet I never understood the real meaning behind this saying until very recently, when I glanced at the syllabus of my master’s program. Well, nothing was wrong with the syllabus, lekin yaar usme maths thi! I was that one child who always ran away from “Maths” as if it were a ghost, but how did I get through it? I had to give it a fresh start, step by step, learning the basics and climbing up the ladder. Take the hint, people, take that chance, and give your wand that magical swing.
Chalo, Story time over. Now, let’s get back to studying Python. Whenever we talk about the basics of a programming language, we have to begin with the topic of operators.
What are Operators
In mathematics and computer programming, an operator is a character that represents a specific mathematical or logical action or process. It is a symbol, instructing the computer to perform a specific operation on values or variables. Think of it as a command to the computer to do something with the data. Operators are essential for manipulating data and performing calculations, comparisons, and other logical operations within a program. Let’s understand it better with the help of an image below.
In the above image, a generic mathematical expression that consists of 3 variables and 2 symbols, out of which Number1 and Number2 are known as operands, on which the operation is to be will happen, whereas ‘=’ and ‘+’ are known as operators, which indicate what action needs to be performed
Types of operators in Python
Let’s quickly go through all the categories of operators that Python provides us.
Arithmetic
Comparison
Logical
Bitwise
Assignment
Membership
Identity
Arithmetic Operator
An arithmetic operator is one or more symbols that tell the computer what mathematical operation it should perform on values or variables. They are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Symbol | Operation | Explanation |
+ | Addition (a+b) | This operation adds both operands on either side of the + operator. |
- | Subtraction (a-b) | This operation subtracts the right-hand operand from the left. |
* | Multiplication (a*b) | This operation multiplies both operands. |
/ | Float Division (a/b) | This operation divides the left-hand operand by the operand on the right. |
% | Modulus (a%b) | This operation returns the remainder after dividing the left-hand operand by the right operand. |
// | Floor division (a//b) | This operator divides the first operand by the second |
** | Exponentiation (a**b) | This operator is used to raise the first operand to the power of the second. |
Come, let’s understand all of them with the help of examples!
a = 20
b = 10
print(a + b) #30
print(a - b) #10
print(a * b) #200
print(a / b) #2
print(a // b) #2
print(a ** b) #1.024e+13
print(a % b) #0
print(5.0//2) #2.0
print("Hello" + "Python") # HelloPython
print("Hello" * 2) # HelloHelloHello
print("Hello" + 3) # this statement will give you error. Find out why
Comparison Operator
These operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters (lexicographic order).
Be cautious when comparing floating-point numbers due to potential precision issues. Consider using a small tolerance value for comparisons instead of strict equality.
Symbol | Operation | Explanation |
\== | Equal (a==b) | This operator checks if the values of both operands are equal. If yes, the condition becomes TRUE. |
!= | Not equal (a!=b) | This operator checks if the values of both operands are equal. If not, the condition becomes TRUE. |
\> | Greater than (a>b) | This operator checks if the left operand value is greater than the right operand value. If yes, the condition becomes TRUE. |
< | Less than (a<b) | This operator checks if the left operand is less than the right operand. If yes, the condition becomes TRUE. |
\>= | Greater than or equal (a>=b) | This operator checks if the left operand value is greater than or equal to the value of the right operand. If either condition is satisfied, the operator returns a TRUE value. |
<= | Less than or equal (a<=b) | This operator checks if the left operand value is less than or equal to the value of the right operand. If either condition is satisfied, the operator returns a TRUE value. |
Come, let’s understand all of them with the help of examples
a = 20
b = 10
print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)
'''
Output:
True
False
False
True
True
False
'''
Logical Operator
Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions.
Symbol | Operation | Explanation |
and | Logical AND (a and b) | This operator returns TRUE only if both operands are TRUE or if both conditions are satisfied. If you do not, it returns FALSE. |
or | Logical OR (a or b) | This operator returns TRUE if either operand is TRUE. It also returns TRUE if both operands are TRUE. If neither operand is true, it returns FALSE. |
not | Logical NOT (not a) | This unary operator returns TRUE if the operand is FALSE and vice versa. It is used to reverse the logical state of its (single) operand. |
Come, let’s understand all of them with the help of a Truth table and examples
#example
a, b, c = True, False, True
# AND: Both conditions must be True
if a and c:
print("Both a and c are True (AND condition).")
# OR: At least one condition must be True
if b or c:
print("Either b or c is True (OR condition).")
# NOT: Reverses the condition
if not b:
print("b is False (NOT condition).")
Assignment Operator
These operators are used to perform operations on values and variables. They are the special symbols that carry out arithmetic, logical, and bitwise computations. Assignment Operators are used to assign values to variables. This operator is used to assign the value of the right side of the expression to the left side operand.
Symbol | Operation | Explanation |
\= | Assignment (a=b) | This operator assigns the value of the right operand to the left operand (variable). |
+= | Add and assign (a+=b) | This operator adds the right operand and the left operand and assigns the result to the left operand. |
-= | Subtract and assign (a-=b) | This operator subtracts the right operand from the left operand and assigns the result to the left operand. |
*= | Multiply and assign (a*=b) | This operator multiplies the right operand and the left operand and assigns the result to the left operand. |
/= | Divide and assign (a/=b) | This operator divides the left operand and the right operand and assigns the result to the left operand. |
%= | Modulus and assign (a%=b) | This operator performs the modulus operation on the two operands and assigns the result to the left operand. |
Come, let’s understand all of them with the help of examples
a = 20
b = 3
#performing assignment operators
a += b
a -= b
a *= b
a /= b
a %= b
Bitwise Operator
These operators are used to perform bitwise calculations on integers. The integers are first converted into binary, and then operations are performed on each bit or corresponding pair of bits, hence the name bitwise operators. The result is then returned in decimal format.
Note: The truth table combination we saw in logical operators stands true for the bitwise AND, OR, and NOT, too.
Yaad rakhna… Bitwise operators and Logical operators are same different. Logical operators work with Boolean values, whereas Bitwise operators only works with integer values.
Symbol | Operation | Explanation |
& | Bitwise AND (a&b) | This operator copies a bit to the result if it exists in both operands. So, the result is 1 only if both bits are 1. |
Bitwise OR (a | ||
^ | Bitwise XOR (a^b) | This operator copies a bit to the result if it exists in either operand. So, even if one of the operands is TRUE, the result is TRUE. However, if neither operand is TRUE, the result is FALSE. |
~ | Bitwise NOT (~a) | This unary operator flips the bits (1 to 0 and 0 to 1). |
<< | Bitwise left shift (a<<b) | Shifts the bits of the number to the left and fills 0 on voids right as a result. Similar effect as of multiplying the number with some power of two. |
\>> | Bitwise right shift (a>>b) | Shifts the bits of the number to the right and fills 0 on voids left( fills 1 in the case of a negative number) as a result. Similar effect as of dividing the number with some power of two. |
Come, let’s understand all of them with the help of examples
a = 10
b = 4
# Print bitwise AND operation
print("a & b =", a & b) # Output -> a & b = 0
''' Behind the scenes
in binary a = 10 becomes -> 1010
in binary b = 4 becomes -> 0100
1010
& 0101
----
0000 <--- a
Similary we can check other bitwise operators as well
'''
Note to self: Create a dedicated article on bitwise operators, especially on left and right shifts
Membership Operator
These operators test for the membership of an object in a sequence, such as strings, lists, or tuples. Python offers two membership operators to check or validate the membership of a value.
Symbol | Operation | Explanation |
IN | a in list1 | The in operator is used to check if a character/substring/element exists in a sequence or not. Evaluate to True if it finds the specified element in a sequence, otherwise False. |
NOT IN | a not in list1 | The ‘not in’ Python operator evaluates to true if it does not find the variable in the specified sequence and false otherwise. |
Come, let’s understand all of them with the help of examples
List1 = [1, 2, 'operator']
a = 5
print('i' in "Shiro") #output -> True
print(a not in List1) # output -> True
Identity Operator
These Operators are used to compare objects to determine if they are of the same data type and share the same memory location.
Symbol | Operation | Explanation |
is | (a is b) | This operator returns True if both variables are the same object |
is not | (a is not b) | This operator returns True if both variables are not the same object |
Come, let’s understand all of them with the help of examples
a = 10
b = 20
# in this example currently a and b are point to 2 different object values
print(a is b)
# Let's point both the variables to same object using assignment operator and check again
b = 10
print(a is b) # Output -> True
print(a is not b) # Output -> False
Programmers must use a unique function ‘id(var_name)’ using which they can fetch the memory address of the object.
Note: All the operators that we have discussed above their precedence determines the order in which operators are evaluated (PEMDAS/BODMAS rule), and associativity determines the direction of evaluation for operators with the same precedence (left-to-right for most, right-to-left for exponentiation).
Conclusion
Operators are building blocks of any programming language, and they demand to be understood well! Though this article has tried to cover all the basics and other important details around operators, it is highly recommended that you get your hands dirty in the mud too. Things might get a bit overwhelming at first, and in those times, you must remember that it’s okay and normal. Happy learning :)
Thanks a ton for taking the time to read through—it means a lot!
Special mentions: Hitesh Choudhary , #PriyaBhatia, #chaicode #geeksforgeeks #W3Schools
Previous article
Next article
Subscribe to my newsletter
Read articles from Sonali Shiromani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
