Concepts / Variables and Data Types

Variables and Data Types

A variable is a name bound to a value using the assignment operator; assigning a value to a name is itself a real instruction, referred to as a statement.

  • CORE CONCEPT
  • Programming

What a Variable Really Is

When you first learn to program, you might hear that a variable is like a box: you put a value inside it, and the box holds that value. This mental model is intuitive but incomplete. In Python, a variable is not a container at all. Instead, a variable is a name that is bound to a value. The value itself lives somewhere in the computer's memory, and the variable name is simply a label that points to that value. This distinction matters because it changes how you understand what happens when you reassign a variable, when you copy one variable to another, and how Python tracks types.

Think of it this way: if you write the statement i = 5, you are not putting the number 5 into a box called i. Instead, you are creating a binding: the name i now refers to the value 5. If later you write i = 'hello', the name i is now bound to a completely different value—a string. The old binding is gone. The name i is just a label; it can point to any value at any time.

Assignment as an Instruction

An assignment statement is not a mathematical equation; it is an instruction that Python executes. When you write x = 10, you are telling Python to perform an action: evaluate the expression on the right side of the equals sign, then bind the resulting value to the name on the left side. This is a real instruction that changes the state of your program.

The assignment operator = is directional. The right side is evaluated first. The result is then bound to the name on the left. This is why you cannot write 10 = x; the left side must be a name, not an expression or a value.

thenthenEvaluate right side10Create or locate namexBind name to valuex now refers to 10
What happens when Python executes x = 10? Walk through the order of operations.

Type Follows the Value

In Python, you do not declare a variable's type before using it. There is no statement like 'declare x as an integer' or 'declare s as a string'. Instead, a variable's type is determined by the value currently bound to it. If you bind the name score to the value 95, then score is an integer. If you later bind score to the value 95.5, then score is now a floating-point number. The type changed because the value changed.

A variable's type is not fixed. It is a property of the value the variable currently refers to, not a property of the name itself. The same name can refer to an integer, then a string, then a list, all in the same program.

reassignreassigncount = 7type: intcount = 7.5type: floatcount = 'seven'type: str
When you reassign a variable to a different value, how does its type change? Trace the state of the variable name through three assignments.

Names and Values: One Value, Many Names

Multiple variable names can be bound to the same value. When you write x = 5 and then y = x, you are not creating a copy of the value 5. Instead, both x and y are now names bound to the same value. If you later reassign x to a new value, y is unaffected because y is still bound to the original value 5. The names are independent; they just happen to refer to the same value at that moment.

bound tobound tox5y
When you assign x = 5 and then y = x, what is the relationship between x, y, and the value 5? Are they separate copies or shared references?

Worked Example: Tracing Variable Bindings

Tracking Type and Value Through Reassignments

Consider these four assignment statements in order. After each one, what is the value of each variable, and what is its type? x = 10; y = x; x = 'hello'; z = y. At the end, what are the values and types of x, y, and z?

Execute x = 10: The name x is bound to the integer value 10. x has type int and value 10. y and z do not exist yet.

Execute y = x: The name y is bound to the same value that x refers to, which is 10. Now both x and y refer to the integer 10. Both have type int and value 10.

Execute x = 'hello': The name x is now bound to a new value: the string 'hello'. The binding of x to 10 is broken. x now has type str and value 'hello'. y is unaffected; y still refers to 10 and has type int. z still does not exist.

Execute z = y: The name z is bound to the value that y refers to, which is still 10. z has type int and value 10.

At the end: x has type str and value 'hello'; y has type int and value 10; z has type int and value 10. Notice that x and y have different types even though they both referred to 10 at one point. This is because x was reassigned to a string, while y was not.

Expressions Involving Variables

Variables can appear in expressions. When you write a = 3; b = 5; c = a + b, Python evaluates the expression a + b by looking up the current values of a and b, adding them, and then binding the result to c. The variable c is now bound to the value 8. If you later change a or b, c does not automatically update; c is bound to the value 8, not to the expression a + b.

A variable can also be reassigned to an expression that includes itself. For example, if x = 10, then x = x + 5 is a valid statement. Python evaluates the right side first: it looks up the current value of x (which is 10), adds 5 to get 15, and then binds the name x to the new value 15. This is not a mathematical equation; it is an instruction that changes the binding of x.

Common Mistakes with Variables

  • Thinking a variable is a container that holds a value, not a name bound to a value

    In Python, y = x makes both y and x refer to the same value. They are not independent copies; they are two names for one value. If you later reassign x to a new value, y is unaffected because y still refers to the original value.

    Fix: Think of a variable as a label or a pointer, not a box. When you assign y = x, you are making y point to the same value that x points to. Reassigning x changes what x points to, but it does not change what y points to.

  • Assuming a variable has a fixed type that you declare at the start

    Python does not require type declarations. A variable's type is determined by the value it currently refers to. You can reassign a variable to a value of a completely different type.

    Fix: Remember that the type is a property of the value, not the name. The name x can refer to an integer, then a string, then a list. The type changes because the value changes.

  • Confusing the assignment operator = with the equality operator ==

    The single = is assignment; it binds a name to a value and is an instruction. The double == is equality; it compares two values and returns True or False. Assignment is not a comparison.

    Fix: Use = only when you want to bind a name to a value. Use == when you want to check if two values are equal.

  • Thinking that c = a + b creates a live connection between c and the expression a + b

    When you execute c = a + b, Python evaluates the expression using the current values of a and b, computes the result, and binds c to that result. c is now bound to the value 8, not to the expression. Changing a later does not change c.

    Fix: Understand that assignment evaluates the right side once and binds the name to the resulting value. The binding is to the value, not to the expression.

Why This Matters

Understanding that a variable is a name bound to a value, not a container, is crucial for reasoning about your code correctly. It explains why reassigning a variable does not affect other variables that previously referred to the same value. It explains why you can reassign a variable to a completely different type. It explains why variables in expressions are evaluated at the moment the statement runs, not dynamically updated later. This mental model is the foundation for understanding more complex topics like mutable versus immutable values, and how variables interact in functions and loops.

Practice

MEDIUM

Trace the following sequence of statements and determine the type and value of each variable at the end. a = 'start'; b = 42; a = b; b = 3.14; c = a. What are the final values and types of a, b, and c?

Hints
  • Remember that a = b makes a refer to the same value that b currently refers to.
  • When b is reassigned to 3.14, a is not affected because a is bound to the value that b referred to before the reassignment.
  • c = a makes c refer to the value that a currently refers to after a has been reassigned.
EASY

Write a sequence of assignment statements where a variable x is assigned three different values of three different types. For each assignment, identify the type of x after that statement executes.

Hints
  • Start with an integer, then reassign to a string, then to a floating-point number (or any other combination of types).
  • After each assignment, the type of x is the type of the value you just assigned to it.
  • Python will allow all three assignments without any error because types are not fixed.

Key Takeaways

  • A variable is a name bound to a value, not a container that holds a value. The name is a label that points to the value.
  • Assignment is an instruction that Python executes: it evaluates the right side and binds the resulting value to the name on the left.
  • A variable's type is determined by the value it currently refers to, not by a separate type declaration. The same name can refer to values of different types at different times.
  • Multiple names can be bound to the same value. Reassigning one name does not affect the others.
  • When a variable appears in an expression, its current value is used to evaluate the expression. The result is then bound to a name; the binding is to the value, not to the expression itself.