Python Variables

Create variables and name them well.

✍️ 3 exercises + code challenge in this lesson

A variable is created the moment you assign a value to it. You do not declare a type.

  • Names can contain letters, numbers and underscores, and cannot start with a number.
  • Names are case-sensitive: age and Age are different.
  • The convention is snake_case: first_name, total_price.
  • Assign several at once: x, y, z = 1, 2, 3
  • Variables declared outside functions are global.

Key Takeaways: Python Variables

Mastering Python Variables helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Python Variables Code Example

Python
name = "Usman"
age = 27
first_name = "Ali"

x, y, z = "Lahore", "Karachi", "Quetta"
a = b = c = 0

print(name, age)
print(x, y, z)
print(a, b, c)

age = "twenty-seven"   # a variable can change type
print(age)
Free sign-in required · Google login available

Python Variables Practice Exercises

Check what you learned in “Python Variables”.

/ 3 solved
Fill in the blank

Assign three variables in one line.

x, y, z = 
✓ Correct! Not quite. Try again.
Multiple choice

Which is a valid variable name?

✓ Correct! Not quite. Try again.
Code challenge

Create a variable city = "Quetta" and print it.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services