Code Mage LogoCode Mage
Back to Lessons
beginner5:12

Python Variables in 5 Minutes

Quick intro to variables, types, and reassignment.

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

  1. Use descriptive names: user_age instead of a
  2. Use snake_case for variable names
  3. Don't start with numbers: 2name is invalid
  4. 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! 🐍✨