45 Mins

2. Python Variables

Core Concept

A variable is a named container that holds a value in memory. Think of it like a labeled box — you give the box a name, put something inside it, and you can always refer to that box by name later to get or change what's inside.

In Python, you do not need to declare a variable's type before using it. You simply write the name, an equals sign, and the value. Python figures out the type automatically.

Creating a Variable

Creating a variable in Python requires just one line. The = sign is the assignment operator — it assigns the value on the right to the variable name on the left.

Python 3
name = "Alice"
age = 25
height = 5.7

print(name)
print(age)
print(height)
OutputAlice 25 5.7

Notice that name holds text (a string), age holds a whole number (an integer), and height holds a decimal number (a float). Python handled all three types automatically.

Variable Naming Rules

Variable names in Python must follow a specific set of rules. Breaking these rules will cause a SyntaxError and your program will not run.

  • A variable name must start with a letter or an underscore ( _ )
  • A variable name cannot start with a number
  • Variable names can only contain letters, numbers, and underscores — no spaces or hyphens
  • Variable names are case-sensitive — age, Age, and AGE are three different variables
  • Variable names cannot be Python keywords like print, if, for, or True
Python 3
# Valid variable names
my_name = "Alice"
_score = 100
playerAge = 22

# Invalid — would cause a SyntaxError
# 2fast = "nope"
# my-name = "nope"
# class = "nope"  (class is a reserved keyword)

Casting

Casting means explicitly converting a value from one data type to another. By default Python decides the type, but sometimes you need to force a specific one.

  • str() — Converts a value to a string (text)
  • int() — Converts a value to an integer (whole number)
  • float() — Converts a value to a float (decimal number)
Python 3
x = str(3)      # x will be '3'  (a string)
y = int(3.9)    # y will be 3    (truncated, not rounded)
z = float(3)    # z will be 3.0  (a float)

print(x)
print(y)
print(z)
Output3 3 3.0

Notice that int(3.9) becomes 3, not 4. Casting to int always truncates (cuts off) the decimal portion — it does not round.

Get the Type

You can check exactly what data type Python has assigned to any variable using the built-in type() function. This is incredibly useful when debugging your programs.

Python 3
x = 10
y = 3.14
z = "Hello"
w = True

print(type(x))
print(type(y))
print(type(z))
print(type(w))
Output<class int> <class float> <class str> <class bool>

Assigning Multiple Variables

Python gives you two efficient ways to assign multiple variables at once, saving lines and making your code cleaner.

Python 3
# Assign different values to multiple variables in one line
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
OutputOrange Banana Cherry
Python 3
# Assign the same value to multiple variables at once
a = b = c = 100
print(a)
print(b)
print(c)
Output100 100 100

Global vs Local Variables

Python Memory Model: Names as References to PyObjects Variables do not store values directly; they store memory pointers to objects on the CPython heap NAMESPACE (Stack / Scope) x = 100 y = x Variable Names (Symbols) HEAP MEMORY: PyObject (int) ob_refcnt: 2 (Referenced by x and y) ob_type: <class 'int'> ob_ival: 100 id(x) == id(y) → True (Identical Memory Address) When ob_refcnt drops to 0 → Immediate deallocation via Garbage Collector

A variable created inside a function is called a local variable — it only exists within that function. A variable created outside all functions is a global variable — it can be accessed from anywhere in the file.

Python 3
x = "global"    # This is a global variable

def my_function():
    x = "local"   # This is a local variable — different from the global x
    print(x)

my_function()
print(x)
Outputlocal global

The function printed its own local x. After the function, the global x is still unchanged. They are completely separate variables that happen to share the same name.

Best Practice: Use the global keyword sparingly. Overusing global variables makes code harder to debug and maintain. Prefer passing values as function arguments instead.

Knowledge Check

Ready to test your understanding of 2. Python Variables?

Back