This tutorial aims to introduce you to the wide range of operators available in Python. Operators are special symbols that carry out arithmetic or logical computations. They are the backbone of any programming logic, and Python provides a variety of these operators categorized into Arithmetic, Assignment, Comparison, Logical, Identity, Membership, and Bitwise operators.
By the end of this tutorial, you should be able to understand these different types of operators and apply them effectively in Python programming. The only prerequisite for this tutorial is a basic understanding of Python syntax.
These are used to perform mathematical operations like addition, subtraction, multiplication, etc.
# Example
x = 10
y = 2
print('x + y =',x+y) # Addition
print('x - y =',x-y) # Subtraction
print('x * y =',x*y) # Multiplication
print('x / y =',x/y) # Division
print('x % y =',x%y) # Modulus
print('x ** y =',x**y) # Exponentiation
print('x // y =',x//y) # Floor division
These are used to assign values to variables.
# Example
a = 10 # Assignment
a += 10 # Addition assignment
a -= 5 # Subtraction assignment
a /= 5 # Division assignment
a *= 2 # Multiplication assignment
a %= 3 # Modulo assignment
a **= 2 # Exponentiation assignment
a //= 3 # Floor division assignment
These are used to compare values. It either returns True or False according to the condition.
# Example
x = 10
y = 12
print('x > y is',x>y) # Greater than
print('x < y is',x<y) # Less than
print('x == y is',x==y) # Equal to
print('x != y is',x!=y) # Not equal to
print('x >= y is',x>=y) # Greater than or equal to
print('x <= y is',x<=y) # Less than or equal to
Let's look at some practical examples for the remaining operator types - Logical, Identity, Membership, and Bitwise operators.
Logical operators are used to combine conditional statements.
x = True
y = False
print('x and y is',x and y) # Logical AND
print('x or y is',x or y) # Logical OR
print('not x is',not x) # Logical NOT
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print('x is z is',x is z) # returns True because z is the same object as x
print('x is y is',x is y) # returns False because x is not the same object as y, even if they have the same content
print('x == y is',x == y) # to demonstrate the difference betweeen "is" and "==": this comparison returns True because x is equal to y
Membership operators are used to test whether a value or variable is found in a sequence.
x = ["apple", "banana"]
print('banana' in x) # returns True because a sequence with the value "banana" is in the list
print('pineapple' not in x) # returns True because a sequence with the value "pineapple" is not in the list
Bitwise operators are used to compare binary numbers.
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print("a & b = ", c)
c = a | b; # 61 = 0011 1101
print("a | b = ", c)
c = a ^ b; # 49 = 0011 0001
print("a ^ b = ", c)
c = ~a; # -61 = 1100 0011
print("~a = ", c)
c = a << 2; # 240 = 1111 0000
print("a << 2 = ", c)
c = a >> 2; # 15 = 0000 1111
print("a >> 2 = ", c)
In this tutorial, we covered the different types of Python operators and their uses with practical examples. These operators are fundamental in Python programming and understanding them will help you write more efficient and effective Python code.
For further practice, try to build more complex logic using these operators. Happy Coding!