Python Variables in 5 Minutes
Variables are the foundation of any programming language, and Python makes them incredibly simple to work with.
What is a Variable?
A variable is like a labeled box that stores data. In Python, you don't need to declare the type of variable - Python figures it out automatically!
# Creating variables
name = "Code Mage"
age = 25
is_learning = True
Variable Types
Python has several built-in data types:
- Strings: Text data (
"Hello World") - Integers: Whole numbers (
42) - Floats: Decimal numbers (
3.14) - Booleans: True/False values (
True,False)
Variable Reassignment
You can change a variable's value at any time:
score = 100
print(score) # Output: 100
score = 150
print(score) # Output: 150
# You can even change the type!
score = "Perfect!"
print(score) # Output: Perfect!
Best Practices
- Use descriptive names:
user_ageinstead ofa - Use snake_case for variable names
- Don't start with numbers:
2nameis invalid - Avoid Python keywords:
class,def,if, etc.
Quick Exercise
Try creating variables for:
- Your favorite programming language
- The number of hours you code per day
- Whether you enjoy debugging (True/False)
Remember: Practice makes perfect! 🐍✨