CodingNic

Python Foundations

Operators

Python Foundations 20 min read

Operators

Operators

Operators are special symbols that tell Python to do something with values. They can help you add numbers, compare values, check conditions, and update variables.

You already know some operators from normal math, such as + and -. Python uses these and many more.

Why Operators Matter

Operators are used in almost every program.

For example, you can use operators to:

  • Add prices in a shopping app
  • Compare exam scores
  • Check if a user is old enough to sign up
  • Increase a game score
  • Test if a number is even or odd

Arithmetic Operators

These operators are used for calculations.

  • + adds values
  • - subtracts values
  • * multiplies values
  • / divides values
  • // divides and removes decimals
  • % gives the remainder
  • ** raises to a power
python
print(10 + 5)
print(10 - 5)
print(10 * 5)
print(10 / 5)
print(10 // 3)
print(10 % 3)
print(2 ** 3)

Output:

text
15
5
50
2.0
3
1
8

Comparison Operators

These operators compare two values.

The result will always be True or False.

  • == equal to
  • != not equal to
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to
python
print(10 == 10)
print(10 != 5)
print(8 > 3)
print(4 < 2)

Output:

text
True
True
True
False

Logical Operators

These operators help combine conditions.

  • and means both conditions must be true
  • or means at least one condition must be true
  • not reverses the result
python
age = 20
has_id = True

print(age >= 18 and has_id)
print(age < 18 or has_id)
print(not has_id)

Assignment Operators

These operators store or update values.

python
score = 10
score = score + 5
print(score)

Shorter way:

python
score = 10
score += 5
print(score)

Order of Operations

Python follows math rules.

Multiplication happens before addition unless you use brackets.

python
print(2 + 3 * 4)
print((2 + 3) * 4)

Output:

text
14
20

Code Along

python
num1 = 12
num2 = 4

print(num1 + num2)
print(num1 > num2)
print(num1 % num2)

Mini Challenge

Create a program that:

  • Stores two numbers
  • Prints their sum
  • Prints their difference
  • Checks if the first number is bigger
  • Prints the remainder after division

Real World Use Case

A shopping website uses operators to calculate totals, compare prices, apply discounts, and check if items are in stock.

Quiz

  1. Which operator adds numbers?
  2. What does % return?
  3. What is the result of 5 > 8?
  4. What does and mean?

Assignment

Build a simple calculator that asks the user for two numbers and prints the results of +, -, *, and /.

Summary

You learned that operators are symbols that help Python calculate, compare values, test conditions, and update variables.