Concepts / Variables and Data Types

Variables and Data Types

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.

  • CORE CONCEPT
  • Programming
Interactive lab

Try it: Assignment, Statement by Statement

How an assignment statement evaluates its right side first (using the names' current values) and only then binds the name on the left: updating x = x + 1, augmented operators like += and //=, chained a = b = 0, simultaneous a, b = b, a + b versus one-at-a-time assignment, rebinding a name to a value of another type, the NameError for a name that was never assigned, and the SyntaxError for a name that breaks the naming rules.

How it works

  1. Before anything runs, Python compiles the whole program; a name that breaks the rules (starts with a digit, contains a space or a dash, or is a keyword) is a SyntaxError and no line runs at all.
  2. For each assignment, evaluate the complete right side first, looking every name up in the variable table as it is now.
  3. Then bind the name(s) on the left to that value: a new name is added to the table, an existing one is replaced (even by a value of a different type).
  4. x += 5 reads x, applies the operator (+, -, *, //, %, **) and binds the result back to x; a = b = 0 evaluates 0 once and binds a, then b.
  5. a, b = b, a + b builds the whole right-hand tuple from the OLD values before binding a and b; two separate statements would use the new a in the second line.
  6. Reading a name that is not in the table raises NameError and the program stops at that line.

Default run (10 steps): A 4-line program. Python first compiles all of it, then runs it top to bottom. The variable table on the right starts empty. … Program finished. Final table: x = 16.

Simplified: The programs are fixed templates: you choose the template, its numbers, the operator and the variable name, never the code. The lab's own small Python tokenizer and parser (written for the statements these programs use: assignments, print calls, if / elif / else, for, while, def and return) checks the text; nothing is executed as real Python. Every program the lab can show was compiled by CPython 3.12 and the lab reproduces its exception type, message, line, caret columns and the printed report. A small interpreter written for the same statements then runs them (integers of any size, the str/float results these templates produce, and print); each program was also run in CPython 3.12 under sys.settrace, and the lab reproduces the variables after every line, the output and the error.

Educational simulation

Loading the simulation…

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.

Evaluate right side10Create or locate namexBind name to valuex now refers to 10thenthen
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.

Key point

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.

count = 7type: intcount = 7.5type: floatcount = 'seven'type: strreassignreassign
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.

x5ybound tobound to
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

Worked example

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?

  1. 1
    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.

  2. 2
    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.

  3. 3
    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.

  4. 4
    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.

Result

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

  • Common mistake

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

    What it looks like

    Believing that when you write y = x, you copy the value from the container x into the container y, so they are now independent copies.

    Why it fails

    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.

    Do this instead

    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.

  • Common mistake

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

    What it looks like

    Writing x = 5 and then later writing x = 'hello' and being surprised that Python allows it.

    Why it fails

    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.

    Do this instead

    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.

  • Common mistake

    Confusing the assignment operator = with the equality operator ==

    What it looks like

    Writing if x = 5 instead of if x == 5 in a conditional statement.

    Why it fails

    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.

    Do this instead
    Use = only when you want to bind a name to a value. Use == when you want to check if two values are equal.
  • Common mistake

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

    What it looks like

    Writing a = 3; b = 5; c = a + b; a = 10 and expecting c to now be 15 instead of 8.

    Why it fails

    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.

    Do this instead

    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.

Related concepts

More concepts starting with V