Variables

What are Variables?

Variables are containers for storing data values. In Python, you don't need to declare variable types explicitly.

Creating Variables

Try running this code to see how variables work:

name = "Alice"
age = 25
height = 5.6
is_student = True

print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is student:", is_student)

Variables are created the moment you assign a value to them. Python automatically determines the type based on the value you assign.

Checking Variable Types

You can check what type a variable is:

name = "Alice"
age = 25
price = 19.99

print(type(name))
print(type(age))
print(type(price))

Variable Naming Rules

  • Must start with a letter or underscore
  • Can contain letters, numbers, and underscores
  • Case-sensitive (age and Age are different)
  • Cannot use Python keywords (like if, for, while, etc.)

Good Variable Names

# Good examples - descriptive and following conventions
user_name = "Alice"
total_count = 100
is_valid = True

print("User:", user_name)
print("Count:", total_count)
print("Valid:", is_valid)

Updating Variables

Variables can be changed after they're created:

score = 0
print("Initial score:", score)

score = 10
print("After scoring:", score)

score = score + 5
print("Final score:", score)

Try It Yourself!

Experiment with the code below. Try these challenges: - Change the name to your own name - Add a new variable for your favorite color - Print all the variables together in one sentence

name = "Alice"
age = 25

print("My name is", name)
print("I am", age, "years old")

Practice Tips

  • Use descriptive variable names that explain what the data represents
  • Follow Python naming conventions (lowercase with underscores)
  • Choose names that make your code self-documenting

Variables

points

You earned points! 🎉 Keep practicing! 💪

Sign up to save your points and access 10,000+ exercises Sign up to track your progress and access 10,000+ exercises

Daily Limit Reached

🔥 exercises left — Upgrade for more!

This module requires

Try the first module of this course for free, then upgrade to unlock all modules and exercises.

No exercise selected

Click "New Exercise" to begin

Generating exercise...

Syntax Syntax OK Syntax off

Write a short answer to the question above (2-5 sentences).

Score
+ /
Base /10
Bonus
×
Total
+

Feedback

Tip

Your Solution

Correct Solution