Skip to main content

Command Palette

Search for a command to run...

πŸ”₯ Python Operators – Full Easy Explanation

Published
β€’2 min readβ€’View as Markdown
S

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

TypeWorks OnExampleCode Example
Unary1 operand-a, not aprint(-5)
Binary2 operandsa + bprint(5 + 3)
Ternary3 thingsa if cond else bAdult if age >=18 else Minor

πŸ“š Bonus: Short Examples and Easy Way to Remember

Operator TypeExample CodeHindi-Style Easy Meaning
Unary-a, not a"One person working"
Binarya + b, a * b"Two people working together"
Ternaryx 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")


More from this blog

Untitled Publication

136 posts