Course topics

By WebNest Studio Engineering Team

Python Tutorial

Python Variables

A variable in Python is a name that refers to a value stored in memory. Unlike statically typed languages, Python does not require you to declare a variable's type in advance — you simply assign a value to a name, and Python figures out the type automatically at runtime. This is called dynamic typing.

Because variables in Python are really just labels pointing to objects, the same name can be reassigned to a value of a completely different type later in the program. This flexibility is powerful, but it also means naming variables clearly and consistently matters more in Python than in languages where the type is fixed by a declaration.

Why it matters

A variable in Python is a name attached to an object, not a box that holds a value. That one idea explains why no types are declared, why two names can refer to the same list, and why swapping two values takes a single line. It is the basis for understanding everything that follows about mutable and immutable data.

Dynamic Typing

When you write "x = 5", Python creates an integer object with value 5 and makes the name x refer to it. If you later write "x = \"hello\"", the name x now refers to a string object instead — no error occurs, and no type declaration was ever needed. The type is a property of the object, not of the variable name itself, which is why Python is described as "dynamically typed."

Variable Naming Rules

Variable names in Python must start with a letter (a-z, A-Z) or an underscore (_), followed by any combination of letters, digits, and underscores. Names are case-sensitive (age and Age are different variables), and they cannot be a reserved keyword such as if, for, class, or return. By convention (PEP 8), variable names use lowercase words separated by underscores (snake_case), such as total_price or user_name.

  • Valid: age, _count, user_name, total2
  • Invalid: 2total (starts with a digit), user-name (hyphen not allowed), class (reserved keyword)
  • Convention: use snake_case for variables and functions; UPPER_CASE for constants.

Multiple Assignment

Python allows several convenient assignment patterns in a single line. You can assign the same value to multiple names at once (a = b = c = 0), or assign different values to multiple names in one statement by unpacking a sequence (x, y, z = 1, 2, 3). This unpacking form also enables the classic one-line variable swap, "a, b = b, a", without needing a temporary variable.

Example

Dynamic typing and multiple assignment

Python
x = 5
print(type(x), x)

x = "hello"
print(type(x), x)

a, b, c = 1, 2, 3
print(a, b, c)

a, b = b, a
print(a, b)
Output
<class 'int'> 5
<class 'str'> hello
1 2 3
2 1

Common Mistakes

  • Trying to use a variable before it has been assigned a value, which raises a NameError.
  • Using a reserved keyword like "class" or "list" as a variable name, or shadowing a built-in name like "str" or "list" by accident.
  • Assuming a variable "remembers" its original type after reassignment — Python variables have no fixed type; only the object they point to has a type.
  • Starting a variable name with a digit or using invalid characters like hyphens or spaces.

Exercise

Swap the values of the two variables without using a third variable, then print them.

Expected output: 10 5

Python · starter code
a = 5
b = 10

# TODO: swap a and b in one line

print(a, b)
Show hints
  • Python can assign to several names at once: x, y = 1, 2.
  • The right-hand side is evaluated in full before anything is assigned, so a, b = b, a swaps them.
Show solution
Python · solution
a = 5
b = 10

a, b = b, a

print(a, b)

Quick quiz

1. Is this valid Python? x = 5 followed by x = "five"

  • A. No, the type cannot change
  • B. Yes; the name simply refers to a new object of a different type
  • C. Only with a cast
  • D. Only inside a function
Show answer

B. The type belongs to the object, not to the name.

2. After a = b = [] and a.append(1), what is b?

  • A. []
  • B. An error occurs
  • C. None
  • D. [1]
Show answer

D. Both names refer to the same list, so a change through one is visible through the other.

3. Which name follows the PEP 8 convention for a variable?

  • A. UserName
  • B. userName
  • C. USERNAME
  • D. user_name
Show answer

D. Variables and functions use lower-case words joined by underscores.

Interview questions

What is the difference between static and dynamic typing?

Show answer

In a statically typed language such as Java, each variable is declared with a type, and the compiler rejects code that assigns a value of another type. In a dynamically typed language such as Python, a variable has no declared type; the value carries the type, and type errors appear when the offending line runs. Type hints can be added to Python code and checked by tools, but the interpreter does not enforce them.

What happens in memory when you write b = a in Python?

Show answer

No data is copied. The name b is bound to the same object that a refers to. For immutable objects such as numbers and strings this makes no practical difference, but for a mutable object such as a list, changing it through one name changes what the other name sees. A separate list requires an explicit copy, for example b = a.copy().

Key Points to Remember

  • Python variables are dynamically typed: no type declaration is required, and a name can be reassigned to a different type.
  • Names must start with a letter or underscore, contain only letters/digits/underscores, and cannot be reserved keywords.
  • PEP 8 convention is snake_case for variable names.
  • Multiple assignment (a, b = 1, 2) and swapping (a, b = b, a) are idiomatic Python patterns.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering