Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 04

Variables and Their Limitations

Store values in named variables, follow naming rules, and understand what variables can and cannot do in beginner Batch Mode programs.

Beginner15 min

What you will learn

By the end of this lesson, you'll understand:

  • What is a variable in Python?
  • How do you create and update a variable?
  • What are the naming rules for variables?
  • What are the limitations of variables in beginner programs?

What is a variable?

A variable is a named storage location for a value in a program.

Instead of repeating the same number or text everywhere, you give the value a name and reuse that name.

Assignment stores a value in a variable

Think of a variable as a label on a box. The label is the name (a). The contents are the value (10).

Creating variables (assignment)

We create or update a variable with the assignment operator =:

python
variable_name = value
Part Meaning
Left side Variable name (identifier)
= Assignment — store the value
Right side Value or expression

Example from addex1.py

This Batch Mode program uses three variables:

python
# addex1.py
a = 10
b = 20
c = a + b
print(a)
print(b)
print(c)
text
10
20
30
Three variables a, b, and c in addex1.py
Variable Value after assignment
a 10
b 20
c 30 (result of a + b)

You saw the same pattern in the Interactive Approach (a = 10, b = 20, c = a + b). Batch Mode saves those variables inside a .py file so the program can run again.

Updating a variable

A variable can hold only one value at a time. A new assignment replaces the old value.

Reassignment overwrites the previous value
python
x = 5
print(x)   # 5
x = 20
print(x)   # 20

The name x stays the same; the stored value changes.

Naming rules (identifiers)

Variable names must follow Python identifier rules:

Rule Valid example Invalid example
Letters, digits, underscore score1, total_amt 1score (cannot start with digit)
No spaces student_name student name
Case-sensitive Age and age are different —
Not a keyword amount print, if, for

Good habits for beginners:

  • Use short but meaningful names: a, b, c in small examples; principle, rate in formulas
  • Use lowercase with underscores for longer names: student_name
  • Avoid confusing names like l (letter L) and O (letter O)

Temporary variables

Sometimes you need an extra variable for a short step. In SwapLogic1.py (covered later in this module), k is called a temporary variable — it holds a value only while two other variables are being swapped.

python
k = a   # temporary storage
a = b
b = k

Temporary variables are normal variables with a limited role in one algorithm.

Limitations of variables

Variables are powerful, but beginners should know their limitations early.

1. You must assign before you use

If Python has not seen an assignment yet, the name does not exist:

python
print(x)
x = 10
NameError when using a variable before assignment
text
NameError: name 'x' is not defined

Fix: assign first, then use: x = 10 then print(x).

2. Interactive Mode does not keep variables forever

In the Interactive Approach, variables exist only while the shell session is open. When you close the shell, variables and statements are lost — no .py file is created automatically.

Batch Mode fixes this by saving variables inside a file such as addex1.py.

3. Hard-coded variables are not dynamic

In addex1.py, values are fixed in code:

python
a = 10
b = 20

Limitation: to add different numbers, you must edit the source file and run again.

Later lessons fix this with input() so variables can receive values from the keyboard at run time.

4. input() always gives a string

When you read keyboard input, the variable stores text (str) until you convert it:

python
a = input("Enter a number: ")   # a is str, e.g. "25"

Limitation: you cannot do reliable arithmetic on that variable until you use int() or float(). That is covered in the input and type-casting lessons.

5. One variable name, one value at a time

A single variable cannot hold two different values simultaneously. Reassignment replaces the old value — it does not keep a history of past values.

6. Variables do not declare a fixed type in Python

Unlike some languages, you do not write int a = 10. Python decides the type from the value you assign. The same name can later refer to a different type:

python
x = 10      # integer value
x = "hello" # now a string value

This flexibility is useful but can confuse beginners if names are reused carelessly. The Data Types module explores this in depth.

Variables vs constants (beginner view)

In small programs, values like a = 10 act like constants during one run — they do not change unless you assign again. Real constants (values that should never change) come later with naming conventions such as PI = 3.14. For now, remember: any variable can be reassigned unless you design the program not to.

Key terms

Term Meaning
Variable A named storage location for a value
Assignment Storing a value with =
Identifier The name of a variable
Temporary variable A helper variable used for a short step (e.g. k when swapping)
Reassignment Giving a new value to an existing variable name
NameError Error when a variable is used before assignment

Summary

  • A variable stores a value under a name: a = 10
  • Use meaningful identifiers and follow naming rules
  • Assign before you use a variable
  • Reassignment replaces the previous value
  • Hard-coded variables require editing the file to change data
  • Interactive sessions lose variables when closed; Batch Mode saves them in .py files
  • Keyboard input stores strings until you convert with int() or float()

Quick knowledge check

  1. What operator creates or updates a variable?
    = (assignment)

  2. What error appears if you print(x) before x = 10?
    NameError: name 'x' is not defined

  3. In addex1.py, what value does c hold after c = a + b when a = 10 and b = 20?
    30

  4. Why are variables in Interactive Mode lost when the shell closes?
    They are not saved to a .py file unless you write Batch Mode code.

  5. Can a variable hold two different values at the same time?
    No — reassignment overwrites the previous value.

Next lesson: print() — displaying output in Python.

Loading playground…