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
print(10 + 5)
print(10 - 5)
print(10 * 5)
print(10 / 5)
print(10 // 3)
print(10 % 3)
print(2 ** 3)
Output:
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
print(10 == 10)
print(10 != 5)
print(8 > 3)
print(4 < 2)
Output:
True
True
True
False
Logical Operators
These operators help combine conditions.
andmeans both conditions must be trueormeans at least one condition must be truenotreverses the result
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.
score = 10
score = score + 5
print(score)
Shorter way:
score = 10
score += 5
print(score)
Order of Operations
Python follows math rules.
Multiplication happens before addition unless you use brackets.
print(2 + 3 * 4)
print((2 + 3) * 4)
Output:
14
20
Code Along
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
- Which operator adds numbers?
- What does
%return? - What is the result of
5 > 8? - What does
andmean?
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.