Python Operators and Expressions
What are Operators?
An operator in Python is a special symbol that tells the computer to perform a specific action on values or variables.
In simple words, an operator works like a tool that helps us do tasks such as addition, comparison, or checking conditions.
Example:
5 + 3 # Here, + is an operator that adds 5 and 3
Types of Operators in Python
-
Arithmetic Operators → Used for basic math
x = 10
y = 3
print(x + y) # Addition → 13
print(x - y) # Subtraction → 7
print(x * y) # Multiplication → 30
print(x / y) # Division → 3.33
print(x % y) # Modulus (remainder) → 1
print(x ** y) # Exponentiation → 1000
print(x // y) # Floor Division → 3
-
Comparison Operators → Compare values (return True/False)
a = 5
b = 10
print(a == b) # Equal? → False
print(a != b) # Not Equal? → True
print(a > b) # Greater? → False
print(a < b) # Smaller? → True
print(a >= b) # Greater or Equal? → False
print(a <= b) # Smaller or Equal? → True
-
Logical Operators → Combine conditions
x = 5
print(x > 2 and x < 10) # True (both conditions true)
print(x > 2 or x > 10) # True (at least one true)
print(not(x > 2 and x < 10)) # False (negates result)
-
Assignment Operators → Assign values to variables
x = 10
x += 5 # x = x + 5 → 15
x -= 3 # x = x - 3 → 12
x *= 2 # x = x * 2 → 24
x /= 4 # x = x / 4 → 6.0
Expressions in Python
-
An expression is a combination of variables, values, and operators that produces a result.
result = (2 + 3) * 4
print(result) # 20
Summary
-
Operators are symbols to perform calculations or comparisons.
-
Types: Arithmetic, Comparison, Logical, Assignment.
-
Expressions combine operators and values to give results.
