PythonVariables and Basic Databeginner18 min read

Variables in Python

Learn how names refer to values, how assignment works, and how to choose clear variable names.

In this lesson, you will learn:
  • Explain a variable as a name bound to an object.
  • Create, read, and reassign variables with =.
  • Recognize valid identifiers and common naming conventions.
  • Understand case sensitivity and multiple assignment.
  • Distinguish rebinding a name from mutating a mutable object.

1. What is a variable?

A variable is a name that refers to an object. The object may represent a number, text, a Boolean value, or another kind of data.

The assignment operator = binds the name on the left to the object produced by the expression on the right. It is not the same as the equality comparison operator ==.

Create and use a variable

Create and use a variable
python
age = 21
print(age)
Output
21

Python evaluates 21 to an integer object. The name age is bound to that object. print(age) looks up the object currently referred to by age and displays its value.

2. Assigning and reassigning names

A name can be assigned again. After reassignment, reading the name gives the new object. Python does not require every assignment to a name to have the same type.

Use names that explain the meaning of the data. Clear names make programs easier to read and maintain.

Reassignment can change the type

Reassignment can change the type
python
score = 10
print(score)
score = 12
print(score)
score = "twelve"
print(score)
Output
10 12 twelve

The first assignment binds score to the integer 10. The second assignment rebinds score to the integer 12. The third assignment rebinds score to a string. Python names are not permanently fixed to one type.

3. Rules for valid identifiers

An identifier is a name used for a variable, function, class, or another program element. A Python identifier can contain Unicode letters, underscores, and digits after the first character. It cannot begin with a digit, and it cannot be a reserved keyword such as if or for.

Python is case-sensitive: total, Total, and TOTAL are three different names. The rules determine what names are allowed; conventions are recommendations that make code easier to read.

Case-sensitive names

Case-sensitive names
python
count = 3
Count = 8
print(count)
print(Count)
Output
3 8

count and Count differ by capitalization. Python treats them as separate names.

NameValid?Reason
student_nameYesLetters and underscore are allowed.
score2YesDigits may appear after the first character.
2scoreNoAn identifier cannot begin with a digit.
first-nameNoA hyphen is an operator, not part of an identifier.
classNoclass is a reserved keyword.
_countYesAn identifier may begin with an underscore.

4. Naming conventions

For ordinary variables, Python code commonly uses snake_case: lowercase words separated by underscores, such as total_price or student_name. This is a convention, not a syntax rule; names such as totalPrice are legal too.

Avoid names that are too vague, such as x, when a meaningful name would help. Also avoid shadowing built-in names such as list or str, because doing so can make those built-ins harder to use.

Prefer meaningful names

Prefer meaningful names
python
item_price = 120
quantity = 3
total_price = item_price * quantity
print(total_price)
Output
360

Each name describes the information it represents. The expression multiplies price by quantity and binds the result to total_price.

5. Multiple assignment and unpacking

You can bind several names in one statement. In chained assignment, multiple names are bound to the same object. In unpacking assignment, Python assigns items from an iterable to corresponding names; the number of targets must match the number of items.

Assign several names

Assign several names
python
x, y = 4, 9
print(x)
print(y)
Output
4 9

Python evaluates the right-hand values and assigns them to the matching targets.

Swap two names

Swap two names
python
left = "A"
right = "B"
left, right = right, left
print(left, right)
Output
B A

The right-hand side is evaluated before the assignments take effect. The values are then unpacked into left and right, so a temporary variable is not needed.

6. Names, objects, and mutation

Assignment binds a name to an object. If two names refer to the same mutable object, changing that object through one name can be observed through the other. Rebinding one name does not rebind the other name.

Lists are mutable, so their contents can change. Integers and strings are immutable, so an operation that appears to change one actually produces a different object/value and the name can then be rebound.

Two names can refer to one list

Two names can refer to one list
python
first = [1, 2]
second = first
second.append(3)
print(first)
print(second)
Output
[1, 2, 3] [1, 2, 3]

second = first does not copy the list; both names refer to the same list object. append mutates that shared list, so both names show the updated contents.

Key Point

This distinction becomes important when passing lists and dictionaries to functions.

7. Errors involving names and assignment

Reading a name before it has been bound in the current scope usually raises NameError. A malformed assignment, such as placing a literal on the left side of =, raises SyntaxError. These errors are different: one concerns name lookup and the other concerns invalid program syntax.

Valid assignment target

Valid assignment target
python
price = 50
price = price + 5
print(price)
Output
55

The right-hand expression reads the current value of price and adds 5. The result is then assigned back to price.

Common Pitfalls & Mistakes to Avoid

#1 Using = to ask whether two values are equal.

Explanation: Use == for equality comparison. Use = to assign a value to a name.

#2 Assuming a variable has a permanent type.

Explanation: A name can be rebound to an object of another type.

#3 Assuming snake_case is mandatory.

Explanation: snake_case is a convention for ordinary variable names, not a syntax requirement.

#4 Assuming second = first copies a list.

Explanation: It binds second to the same list object. Use first.copy() for a shallow copy when appropriate.

#5 Using a name before assigning it.

Explanation: Make sure the name has been bound in the relevant scope before reading it.

Practice Questions

1

Create item_price with value 40 and quantity with value 3. Calculate and print total_price.

2

Given a = 5 and b = 8, swap their values using Python multiple assignment and print both names on one line separated by a space.

Variables in Python Knowledge Check

1. Which statement assigns the integer 7 to the name count?

2. What does this code print? value = 2 Value = 9 print(value)

3. After a = [1] and b = a, what happens when b.append(2) runs?

Technical & Placement Interview Questions

Key Takeaways & Summary

  • Assignment with = binds a name to an object.
  • Python names are case-sensitive and can be rebound to objects of different types.
  • Follow naming conventions such as snake_case for readable code.
  • Multiple assignment can unpack values and swap names.
  • Rebinding a name and mutating a shared mutable object are different operations.

Finished reading this lesson?

Track your course progress and update your learning dashboard.

Related Topics & Next Steps

Strengthen your programming fundamentals with these related lessons: