Python Fundamentals for Everybody — Operators and Operands
This is the third article on Python Fundamentals for Everyone, a python tutorial series which focuses on python fundamentals.
You can refer to previous article written in this series below.
At the end of this article you will have knowledge on what are operators, operands and how we can use it.
Operators and Operands
Operators are basically a symbols like +,-,>,< etc., which are used to perform operations on values and variables. These values and variables are known as operands.
Operators are used to manipulate the operands.
For example: In this example, ‘a’ and ‘b’ are operands where as ‘+’ is the operator.
Python supports the following Operators:
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Let us look into each of these one by one along with examples.
Arithmetic Operators
They are used to perform mathematical operations like addition, subtraction, multiplication etc.
Assume a = 2 and b = 3, and go through the following two images with example to understand better.
Comparison (Relational) Operators
These operators are used to compare the operand values. It returns True or False depending upon the condition. They are also called as relational operators.
Assume a = 10 and b = 20, and go through the following two images with example to understand better.
Assignment Operators
Assignment operators (symbol ‘=’) are operators used to assign values to the variables, in a program.
Assume a = 10 and b = 20, and go through the following two images with example to understand better.
Logical Operators
They are operators which are used to control the flow of the program. Most often, logical operators are combined with Boolean expressions. Sometimes, Logical Operators are also called as Boolean Operators. It holds the value of 0 or 1, where 0 means False and 1 means True.
Boolean/Logical Expressions are expressions that evaluate to either True or False.
Assume a = False and b = True for the following example
Bitwise Operators
Bitwise Operators are similar to logical operators but, it manipulates directly on bits.
In the above example, the value of a << 2 is 40, since the binary representation of a = 10 is 00001010(2), which when we shift left two spaces, and fill up the empty spots with zeroes, we get 00101000(2) = 40(10).
The right shift operator works the opposite way, the value of a >> 2 is 2, since the binary representation of a = 10 is 1010(2), when we shift right two spaces by filling up values with zeroes, the rightmost two bits fall. Therefore, we get 00000010(2) = 2.
Membership Operators
Python has two membership operators namely: in and not in.
These operators are used to test if the value or a variable is present or not. It is applied to strings, tuples, list and dictionary.
Identity Operator
Identity Operators are used to compare the memory location of objects. Python has two identity operators namely, is and is not.
Membership Operator is used to check the contents inside the list etc., where as Identity Operator compares the memory location.
Phew! That was quite a lot. Since you have reached here, might as well go read about operator precedence & associativity.
Python Operator Precedence & Associativity
If the expression is single, then there exists no problem in evaluating the solution, but what if there are more than one operations?
In most cases, when the expression is mixed i.e., it contains several operations then, how would Python know which operation to perform first?
Well, operator precedence and associativity, helps python decide the priorities of the operator.
Now, you have knowledge on various types of are Python operators and its priorities.
In the next article, we will look at the difference between Type Conversion and Type Coercion.