Operators in Python Tutorial: A Complete Beginner’s Guide
#Operars in Python Tutorial: A Complete Beginner’s Guide
Introduction
Operators in Python are special symbols or keywords used to perform operations on variables and values. They are the foundation of programming logic, allowing developers to manipulate data and build efficient programs.
In this tutorial, we will cover the different types of operators in Python with examples to help you understand how they work.
Python provides several categories of operators:
Arithmetic Operators
Relational (Comparison) Operators
Logical Operators
Assignment Operators
Bitwise Operators
Identity Operators
Membership Operators
Arithmetic operators are used for mathematical calculations.
Operator | Description | Example (a=10, b=5 ) | Output |
---|---|---|---|
+ | Addition | a + b | 15 |
- | Subtraction | a - b | 5 |
* | Multiplication | a * b | 50 |
/ | Division | a / b | 2.0 |
% | Modulus (remainder) | a % b | 0 |
** | Exponentiation | a ** b | 100000 |
// | Floor Division | a // b | 2 |
These compare values and return a Boolean (True
/False
).
a = 10
b = 5
print(a > b) # True
print(a == b) # False
print(a != b) # True
Logical operators combine conditional statements.
Operator | Description | Example | Output |
---|---|---|---|
and | Returns True if both are true | (a > 5 and b < 10) | True |
or | Returns True if one is true | (a < 5 or b < 10) | True |
not | Reverses the result | not(a > b) | False |
Used to assign values to variables.
x = 10
x += 5 # x = x + 5 → 15
x *= 2 # x = x * 2 → 30
Operate on binary numbers (bit-level operations).
Operator | Description | Example (a=10, b=4 ) | Output |
---|---|---|---|
& | AND | a & b | 0 |
` | ` | OR | `a |
^ | XOR | a ^ b | 14 |
~ | NOT | ~a | -11 |
<< | Left Shift | a << 2 | 40 |
>> | Right Shift | a >> 2 | 2 |
Used to check if two variables point to the same object.
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True (same object)
print(a is c) # False (different objects, same values)
Used to check if a value exists in a sequence.
x = "Python"
print("P" in x) # True
print("Java" not in x) # True
Operators in Python are essential for performing calculations, making decisions, and building logic in programs. Understanding how arithmetic, relational, logical, bitwise, identity, and membership operators work will help you write efficient Python code.
✅ Keep practicing these operators in real-world examples to strengthen your programming logic.
🔑 Target SEO Keywords:
Python operators, operators in Python tutorial, Python arithmetic operators, logical operators in Python, relational operators Python, Python assignment operators, membership operators in Python
👉 Do you want me to also prepare a comparison infographic (Types of Operators in Python) that you can embed in your tutorial for better SEO and Google Discover ranking?