Operators are special symbols or keywords that perform operations on variables and values.

Types of Operators in Python:

1. Arithmetic Operators

Used for basic math:

+ (add), – (subtract), * (multiply), / (divide), // (floor divide), % (modulus), ** (power)

a = 10

b = 3

print(a + b)   # 13

print(a ** b)  # 1000

2. Comparison Operators

Used to compare two values:

==, !=, >, <, >=, <=

x = 5

print(x == 5)  # True

print(x != 3)  # True

3. Logical Operators

Used to combine conditional statements:

and, or, not

age = 20

print(age > 18 and age < 25)  # True

4. Assignment Operators

Used to assign values to variables:

=, +=, -=, *=, /=, etc.

score = 10

score += 5   # score is now 15

Mini Project: Build a Simple Calculator

Let’s apply what we’ve learned!

Task: Build a calculator that asks the user to enter two numbers and an operator, then prints the result.

Approach

1. Take two numbers from the user.

2. Ask for an operator (+, -, *, /).

3. Perform the operation based on what the user entered.

4. Print the result, or shows “Invalid operator!” if the input is wrong.

Python Code:

num1 = float(input(“Enter first number: “))

op = input(“Enter operator (+, -, *, /): “)

num2 = float(input(“Enter second number: “))

if op == “+”:

    print(num1 + num2)

elif op == “-“:

    print(num1 – num2)

elif op == “*”:

    print(num1 * num2)

elif op == “/”:

    print(num1 / num2)

else:

    print(“Invalid operator!”)