➕ Lesson 3: Python Operators
⏱️ Estimated time: 15 minutes | Difficulty: Beginner
Logic and Math
Operators are special symbols that carry out arithmetic or logical computation. Python makes math super easy!
What is a Variable?
A variable is a container that stores information. Think of it like a labeled box where you put things for later use.
Real-World Analogy:
Imagine your refrigerator:
- 📦 Each shelf is labeled (variable name)
- 🥛 What's on the shelf is the data (value)
- 🔄 You can change what's in the shelf (variable can change)
- 📝 You remember which shelf has what (the label)
Creating Variables
name = "Alice"
age = 25
height = 5.6
is_student = True
The pattern is: variable_name = value
Data Types - Different Kinds of Information
Python automatically detects what type of information you're storing:
String (Text)
Any text, always in quotes
name = "Bob"
city = 'New York'
greeting = "Hello!"
Integer (Whole Numbers)
Numbers without decimals
age = 25
year = 2024
score = -5
Float (Decimal Numbers)
Numbers with decimals
height = 5.9
price = 19.99
temperature = -3.5
Boolean (True/False)
Only True or False
is_student = True
is_raining = False
is_adult = True
Using Variables
name = "Alice"
age = 25
print("My name is", name)
print("I am", age, "years old")
print("Next year I will be", age + 1)
Output:
My name is Alice
I am 25 years old
Next year I will be 26
Changing Variables
score = 10
print("Score:", score)
score = 15
print("New score:", score)
score = score + 5
print("Even better:", score)
Output:
Score: 10
New score: 15
Even better: 20
Naming Variables (Best Practices)
✅ Good Variable Names:
name = "Bob"
user_age = 25
is_student = True
total_price = 99.99
❌ Bad Variable Names:
n = "Bob"
x = 25
a = True
t_p = 99.99
Rules for Variable Names:
- ✅ Use lowercase with underscores:
first_name - ✅ Use descriptive names:
student_countnotsc - ❌ Don't start with numbers:
123name - ❌ Don't use spaces:
first name - ❌ Don't use special characters:
user@name