Python Operators
In the previous lesson, we learned about Input and Output operations in Python. Today, we will learn about Operators, which are used to perform operations on variables and values.
Operators are important in programming because they help perform calculations, comparisons, logical decisions, and data manipulation.
What are Operators?
Operators are special symbols used to perform operations on operands (values or variables).
Example
x = 10
y = 5
print(x + y)
Output
15
In this example:
- "+" is the operator
- "x" and "y" are operands
Types of Operators in Python
Python provides several types of operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic Operators are used for mathematical calculations.
Operator| Meaning| Example
"+"| Addition| "x + y"
"-"| Subtraction| "x - y"
""| Multiplication| "x * y"
"/"| Division| "x / y"
"%"| Modulus| "x % y"
""| Exponent| "x * y"
"//"| Floor Division| "x // y"
Example
x = 20
y = 3
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
print(x ** y)
print(x // y)
Output
23
17
60
6.666666666666667
2
8000
6
2. Assignment Operators
Assignment Operators are used to assign values to variables.
Operator| Example
"="| "x = 5"
"+="| "x += 3"
"-="| "x -= 3"
"*="| "x *= 3"
"/="| "x /= 3"
Example
x = 10
x += 5
print(x)
Output
15
3. Comparison Operators
Comparison Operators compare two values and return either "True" or "False".
Operator| Meaning
"=="| Equal to
"!="| Not equal to
">"| Greater than
"<"| Less than
">="| Greater than or equal to
"<="| Less than or equal to
Example
x = 10
y = 20
print(x == y)
print(x != y)
print(x < y)
Output
False
True
True
4. Logical Operators
Logical Operators are used to combine conditions.
Operator| Meaning
"and"| Returns True if both conditions are True
"or"| Returns True if at least one condition is True
"not"| Reverses the result
Example
x = 5
print(x > 2 and x < 10)
print(x > 10 or x < 10)
print(not(x > 2))
Output
True
True
False
5. Identity Operators
Identity Operators compare memory locations of objects.
Operator| Meaning
"is"| Returns True if both variables are the same object
"is not"| Returns True if variables are not the same object
Example
x = [1, 2, 3]
y = x
print(x is y)
Output
True
6. Membership Operators
Membership Operators check whether a value exists in a sequence.
Operator| Meaning
"in"| Returns True if value exists
"not in"| Returns True if value does not exist
Example
python
languages = ["Python", "Java", "C++"]
print("Python" in languages)
print("PHP" not in languages)
Output
plaintext
True
True
7. Bitwise Operators
Bitwise Operators perform operations on binary numbers.
Operator| Meaning
"&"| AND
|
"^"| XOR
"~"| NOT
"<<"| Left Shift
">>"| Right Shift
Example
x = 5
y = 3
print(x & y)
Output
1
Operator Precedence
Python follows a specific order when evaluating expressions.
Example
result = 5 + 2 * 3
print(result)
Output
11
Multiplication happens before addition because it has higher precedence.
Why Operators are Important
Operators help programmers:
- Perform calculations
- Compare values
- Make decisions
- Build conditions
- Manipulate data
- Create dynamic programs
Without operators, programming logic would not work effectively.
Conclusion
Operators are essential building blocks in Python programming. They allow programs to perform mathematical calculations, comparisons, logical decisions, and many other operations.
Understanding operators is important because they are used in almost every Python program.
Practice using different operators to become more comfortable with Python expressions and logic.
Top comments (0)