Skip to main content

What are variables?

A variable is like a labeled box where you can store information. You give it a name and put a value inside. Later, you can use that name to get the value back. In real life:
  • Your name is a “variable” that stores what people call you
  • Your age is a “variable” that stores a number
  • Your favorite color is a “variable” that stores text
In Python, we create variables to store data our programs need to remember.

Creating your first variable

Here’s how to create a variable:
Let’s break this down:
  • name is the variable name
  • = means “store the value on the right in the variable on the left”
  • "Alice" is the value we’re storing

Naming rules

Python has rules for variable names: Allowed:
Not allowed:

Python naming convention

In Python, we use lowercase letters with underscores between words. This is called “snake_case” and it’s the standard way to name variables in Python.
Use descriptive names for your variables! Instead of x or temp, use names like user_score or file_path. Your future self (and teammates) will thank you when reading the code later.

Changing variables

Variables can change (that’s why they’re called variables!):

Common mistakes

What’s next?

Before we explore data types, let’s learn about comments - a crucial tool for making your code readable.

Comments

Document your code clearly