Variables

Introduction

In this chapter, you will learn what variables are and how to use them in Python. Variables are one of the most important building blocks in programming because they let you store and reuse data. If this feels abstract at first, do not worry; we will use simple metaphors to make it intuitive.

Prerequisites

  • Python 3.10+ installed
  • Ability to run a basic .py file
  • Familiarity with print() from previous chapters
  • A code editor (PyCharm or VS Code)

What Is a Variable

A variable is a name that points to a value.

Think of it like:

  • A labeled box: the label is the variable name, and the content is the value.
  • A sticky note on a container: you read the note (user_name) to find what is inside ("Alice").

In code:

python
# Store a text value in a variable
user_name = "Alice"
 
# Use the variable later
print(user_name)

Why Variables Matter

Without variables, you would repeat values everywhere, which makes code hard to change and easy to break.

With variables, you can:

  • Reuse values
  • Update values in one place
  • Make code easier to read
  • Build logic step by step

Tip

Memory Trick

Variable = label + value.
You usually care about the label for readability and the value for computation.

Variable Assignment Basics

In Python, assignment uses =.

python
# Assign a number
age = 18
 
# Assign text
city = "New York"
 
# Assign a decimal number
price = 9.99
 
# Assign a boolean
is_student = True

Use Variables in Real Expressions

Variables are most useful when combined.

python
# Unit price of one notebook
notebook_price = 12.5
 
# Quantity purchased
quantity = 3
 
# Compute total cost
total_cost = notebook_price * quantity
 
# Print result
print(total_cost)

Variables Can Change

The value under a variable name can be updated.

python
# Initial score
score = 60
 
# Add bonus points
score = score + 10
 
# Show updated score
print(score)

Metaphor:

  • Same box label, new content inside.

Naming Rules and Best Practices

Follow these rules from Python standards:

  • Use letters, numbers, and underscore (_)
  • Do not start with a number
  • Names are case-sensitive (age and Age are different)
  • Use meaningful snake_case names

Good names:

  • user_name
  • total_price
  • retry_count

Avoid:

  • a
  • x1
  • tmp_value_abc (unless temporary and scoped clearly)

Warning

Do not use Python keywords as variable names, such as class, if, or for.

Common Beginner Mistakes

Mistake 1: Using a Variable Before Defining It

python
# This raises NameError because user_age is not defined yet
print(user_age)

Fix:

python
# Define first, then use
user_age = 20
print(user_age)

Mistake 2: Confusing Text and Number Types

python
# This is text, not a number
age = "18"

If you need arithmetic, convert it:

python
# Convert text to integer
age = int("18")
print(age + 2)

Mini Practice

Try this small script:

python
# Store your name
name = "Tom"
 
# Store your learning day count
learning_days = 9
 
# Build a message using variables
message = f"{name} has studied Python for {learning_days} days."
 
# Print final message
print(message)

Expected output example:

  • Tom has studied Python for 9 days.

FAQ

Do variables store data directly?

At a beginner level, you can think "yes, they store values." More precisely, a variable name references an object in memory.

Why does = not mean mathematical equality in Python?

In Python, = means assignment: "put this value into this variable name."

Can I change a variable from number to text?

Yes. Python is dynamically typed, so a variable can reference different value types over time.

How do I choose better variable names?

Use names that explain purpose, not implementation details. If someone can guess the value meaning from the name, it is usually a good name.