π₯ Python Operators β Full Easy Explanation
Hi! My name is Safia Khatoon. I am complete my Bachelors in Technology from RTC Institute Of Technology. My specialisation in Computer Science and Engineering.I love contributing to Open Source with the help of the skills I gain.
Also, I'm working on my YouTube Channel as well where I teach about DevOps tools and make technical content. You can have a look at it through my profile.
Feel free to reach out to me! I'd be happy to connect with you.
First of all, what is an "Operator"?
An operator is a symbol or sign that performs an operation between values (operands).
Example:+,-,*,/are all operators.
π§ Types of Operators:
1. Unary Operator (Works on Single Value)
Job: Operates on only one operand.
Examples:
-5(makes it negative),+5(shows positive),not True(reverses a boolean)
π₯ Example:
a = 5
print(-a) # Output: -5
b = True
print(not b) # Output: False
Understand:
One value + one operation = Unary Operator β
2. Binary Operator (Works on Two Values)
Job: Performs operation between two operands.
Examples: Addition (
+), Subtraction (-), Multiplication (*), Division (/)
π₯ Example:
x = 10
y = 5
print(x + y) # Output: 15
print(x * y) # Output: 50
Understand:
Two values + one operation = Binary Operator β
3. Ternary Operator (Decision Between Three Things)
Job: Checks a condition and chooses result β all in one line!
Also called a Conditional Expression.
Syntax:
value_if_true if condition else value_if_false
π₯ Example:
age = 18
status = "Adult" if age >= 18 else "Minor"
print(status) # Output: Adult
Understand:
If condition is true β pick first value.
If condition is false β pick second value.
β Ternary = 3 parts (Condition, True result, False result)
β‘ Full Chart β Super Easy
| Type | Works On | Example | Code Example |
| Unary | 1 operand | -a, not a | print(-5) |
| Binary | 2 operands | a + b | print(5 + 3) |
| Ternary | 3 things | a if cond else b | Adult if age >=18 else Minor |
π Bonus: Short Examples and Easy Way to Remember
| Operator Type | Example Code | Hindi-Style Easy Meaning |
| Unary | -a, not a | "One person working" |
| Binary | a + b, a * b | "Two people working together" |
| Ternary | x if cond else y | "Think and decide" |
π― Quick Recap
Unary = Single operand (Example:
-5,not True)Binary = Two operands (Example:
5 + 3,10 * 2)Ternary = Three parts (Condition, True Result, False Result) β (Example:
"Adult" if age>=18 else "Minor")