Home / Topic Library / Python / Variables
🐍 Python Topic Library

Python Variables — Everything You Need to Know

A variable is a name that points to a value. It's the first concept every Python programmer learns — and getting the details right early saves hours of confusion later.

⚡ Quick Answer

In Python you create a variable simply by assigning a value with = : age = 12. No type declaration needed — Python figures out the type from the value, and you can reassign any variable to any type at any time.

§Creating variables

Assignment creates the variable — there is no separate declaration step like in C or Java.

creating_variables.py
name = "Ada"        # a string
age = 12            # an integer
height = 1.52       # a float
is_coder = True     # a boolean

print(name, age)    # Ada 12

§Naming rules & conventions

Names can contain letters, digits and underscores, but can't start with a digit and can't be a reserved word (if, for, class…). Python convention is snake_case for variables.

naming.py
user_name = "ok"      # good: snake_case
userName  = "works"   # allowed but not Pythonic
_hidden   = "ok"      # leading underscore = internal by convention
# 2cool = "no"        # SyntaxError: can't start with a digit
# for = 5             # SyntaxError: reserved word

§Dynamic typing & reassignment

A Python variable is just a label — it can point to a value of any type, and be re-pointed freely. Check the current type with type().

dynamic.py
x = 10
print(type(x))   # <class 'int'>
x = "ten"        # perfectly legal reassignment
print(type(x))   # <class 'str'>

§Multiple assignment & swapping

Python lets you assign several variables in one line — and swap values without a temporary variable, a trick that surprises programmers coming from other languages.

multiple.py
a, b, c = 1, 2, 3      # unpack in one line
x = y = z = 0          # same value to all three
a, b = b, a            # swap! no temp variable needed
print(a, b)            # 2 1

§Constants (by convention)

Python has no true constants — the convention is ALL_CAPS names to signal "do not change me".

constants.py
PI = 3.14159
MAX_PLAYERS = 4
SITE_NAME = "BitWithBite"
# Nothing stops reassignment — the caps are a promise between programmers.

§Common Mistakes to Avoid

🚨 Watch out for these
  • Using a variable before assigning it — causes NameError: name 'x' is not defined — assignment must come first
  • Confusing = and === assigns, == compares. Writing if x = 5: is a SyntaxError
  • Shadowing built-ins — naming a variable list, str or print silently breaks those functions for the rest of your program

§Frequently Asked Questions

Do I need to declare a variable's type in Python?

No. Python is dynamically typed — the type comes from the value you assign, and you can check it anytime with type(x).

What's the difference between a variable and a constant in Python?

Technically nothing — Python has no enforced constants. By convention, ALL_CAPS names like MAX_SIZE tell other programmers not to reassign them.

Are Python variables case-sensitive?

Yes. age, Age and AGE are three different variables.

Can a variable name start with an underscore?

Yes — a leading underscore conventionally marks it as internal/private, and double leading underscores trigger name mangling inside classes.

🎓 Want to master Python properly?This reference covers one topic — the full free course takes you from zero to projects, with quizzes and a certificate.
Start the Free Python Course →

§Related Python Topics

Data Types →