Operators in Python
Operators are symbols or keywords used to perform operations on values and variables. They tell Python what action to perform, such as adding numbers, comparing values, or checking conditions. Without operators, a program would not be able to calculate, decide, or manipulate data.
Every Python program uses operators to transform input into meaningful output.
Types of Operators in Python
Python provides several types of operators, each serving a specific purpose.
Arithmetic Operators
These operators are used to perform mathematical calculations.
| Operator | Meaning |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (remainder) |
** | Power |
// | Floor division |
Example:
a = 10
b = 3
print(a + b)
print(a % b)Comparison (Relational) Operators
These operators compare two values and return either True or False.
| Operator | Meaning |
== | Equal to |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater or equal |
<= | Less or equal |
Example:
x = 10
y = 20
print(x < y)Logical Operators
These operators are used to combine conditions.
| Operator | Meaning |
and | True if both are true |
or | True if at least one is true |
not | Reverses the result |
Example:
age = 20
print(age > 18 and age < 30)Assignment Operators
These operators assign values to variables.
| Operator | Meaning |
= | Assign |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
Example:
x = 10
x += 5Membership Operators
Used to check whether a value exists in a sequence.
| Operator | Meaning |
in | Found in sequence |
not in | Not found |
Example:
fruits = ["apple", "banana"]
print("apple" in fruits)Identity Operators
Used to compare memory location of objects.
| Operator | Meaning |
is | Same object |
is not | Different object |
Example:
a = [1,2]
b = a
print(a is b)Conclusion
Operators are the tools that make Python useful. They allow programs to calculate, compare, and make decisions. Without operators, Python would only store data—it would never do anything meaningful with it.
