In this part of the Python Basics tutorial, we will learn about the basic operators.
There are many different kinds of operators based on their function.
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Arithematic Operators in Python
Operators used with numeric values to perform common mathematical operations are called Arithmetic operators. (For example, addition)
|+||Addition||x + y|
|–||Subtraction||x – y|
|*||Multiplication||x * y|
|/||Division||x / y|
|%||Modulus||x % y|
|**||Exponent||x ** y|
|//||Floor division||x // y|
Comparison Operators in Python
Comparison operators are used to compare two values.
|==||Equal||x == y|
|!=||Not equal||x != y|
|>||Greater than||x > y|
|<||Lesser than||x < y|
|>=||Greater than or equal to||x >= y|
|<=||Lesser than or equal to||x <= y|
They return Boolean values. They return True if the condition holds true. Otherwise, they return False.
For example, consider the following code block.
>> x = 5 >> y = 5 >> x == y True
Logical Operators in Python
Logical operators are used to combine conditional statements.
Logical operators return boolean values.
|not||not x=y||Returns opposite of|
the actual result
|and||x=y and y<z||Return true if both |
expressions are True
|or||x=y and y<z|| Return true if any one of the |
expressions is True
Bitwise Operators in Python
Bitwise operators are used to compare (binary) numbers:
Membership Operators in Python
To test if a sequence is present in an object, we use membership operators.
Membership operators return boolean values.
|in||5 in [1,2,3,4,5]||True|
|not in||‘a’ not in ‘python’||True|
Identity Operators in Python
To compare different objects, we use Identity operators. They are not used to compare values. They tell if the objects are the same i.e. point to the same memory location.
Identity operators return boolean value.
Returns True if both objects point to the same memory location.
Returns False if both objects do not point to the same memory location.
|is||x is y|
|is not||x is not y|
We now know the basic operators used in python and their usage. In the next part of this tutorial, we will learn about conditional statements.