Operators in Python

If variables are the nouns of Python, then operators are the verbs. They help Python perform actions — add two numbers, compare values, or combine conditions.

how to use operators in python

Understanding operators is crucial to writing logic that works. Whether you’re building a calculator, validating passwords, or making games — you’ll be using Python’s operators everywhere.

In this guide, we’ll walk through:

  • What are operators?
  • Different types of operators in Python
  • Real-life examples for each
  • A few gotchas to avoid
  • A mini practice task at the end

What are Operators?

An operator is a special symbol or keyword that performs a specific operation on values or variables.

For example:

a = 10
b = 5
print(a + b)  # ➝ 15

Here, + is an operator that adds a and b.


1. Arithmetic Operators

These are the basic math operators:

OperatorMeaningExampleOutput
+Addition3 + 25
-Subtraction5 - 23
*Multiplication4 * 312
/Division10 / 25.0
//Floor Division10 // 33
%Modulus10 % 31
**Exponentiation2 ** 38

Example:

amount = 1050
tax = 0.18
total = amount + (amount * tax)
print("Total payable:", total)

Also Read: Python Variables and Data Types


2. Comparison Operators

These help you compare values and return a Boolean (True or False).

OperatorMeaningExampleOutput
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than10 > 5True
<Less than5 < 3False
>=Greater or equal10 >= 10True
<=Less or equal3 <= 2False

Example:

age = int(input("Enter your age: "))
if age >= 18:
    print("You are eligible to vote!")
else:
    print("Sorry, you must be 18 or older.")

3. Logical Operators

Used to combine multiple conditions.

OperatorMeaningExampleOutput
andTrue if both are true5 > 3 and 4 > 2True
orTrue if at least one is true5 > 3 or 4 < 2True
notInverts resultnot (5 > 3)False

Example:

username = "admin"
password = "1234"

if username == "admin" and password == "1234":
    print("Login successful!")
else:
    print("Login failed.")

4. Assignment Operators

These are shortcuts to assign values.

OperatorMeaningExampleSame As
=Assignx = 5x = 5
+=Add and assignx += 3x = x + 3
-=Subtract and assignx -= 2x = x - 2
*=Multiply and assignx *= 4x = x * 4
/=Divide and assignx /= 2x = x / 2
//=Floor divide and assignx //= 3x = x // 3
%=Modulus and assignx %= 2x = x % 2
**=Exponent and assignx **= 2x = x ** 2

5. Membership Operators

Used to check whether a value exists in a sequence (like a list, string, etc.)

OperatorMeaningExampleOutput
inPresent in sequence"a" in "smarttejas"True
not inNot present5 not in [1,2,3]True

Example:

email = input("Enter your email: ")

if "@" in email:
    print("Valid email")
else:
    print("Invalid email")

6. Identity Operators

Used to compare object identities (more advanced but good to know).

OperatorMeaningExampleOutput
isSame objectx is yTrue/False
is notNot same objectx is not yTrue/False
a = [1, 2]
b = a
print(a is b)       # True (both point to the same object)

Gotchas to Watch Out For

  1. = vs ==
    • = assigns a value
    • == checks equality
    • 👉 Always use == in comparisons.
  2. Integer division:
    • 10 / 3 ➝ 3.33
    • 10 // 3 ➝ 3 (no decimals)
  3. Operator precedence matters!
    • 2 + 3 * 4 ➝ 14, not 20
    • Use parentheses for clarity: (2 + 3) * 4

Try It Yourself: Mini Practice

Create a Python program that:

  1. Takes the number of travel days as input
  2. Asks for the daily cost of:
    • Food
    • Travel
    • Hotel stay
  3. Calculates the total trip cost using arithmetic operations
  4. Displays the final result using print()

It’s a great way to review arithmetic, comparison, and logical operators all in one mini-project!

Conclusion

Operators are the building blocks of logic in Python. They’re small symbols, but they hold massive power.

Whether you’re doing math, checking passwords, validating inputs, or writing conditions — you’re using operators under the hood.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Translate »
Scroll to Top