What a variable is and why you need one
A variable is a container that holds a piece of information so you can use it later in your code. Think of it like a labeled box: you put something inside, give the box a name, and then whenever you need that thing, you just call the box by its name instead of repeating the whole thing over again.
Without variables, you would have to type the same information every time you wanted to use it. If you were writing code to calculate someone's age, you would either type their birth year over and over, or you could store it in a variable once and refer to it by name. Variables make your code shorter, easier to read, and much simpler to change later if you need to.
Python makes creating a variable straightforward: you write the name you want, then an equals sign, then the value you want to store. That is the entire process.
Key Takeaways
- A variable is created by typing a name, an equals sign, and a value: age = 25 stores the number 25 in a variable called age.
- Variable names must start with a letter or underscore, can contain letters and numbers, and cannot have spaces or special characters.
- Python figures out what type of information you are storing (number, text, true/false) automatically when you create the variable.
- Once you create a variable, you can use its name anywhere in your code to refer to the value it holds.
- You can change what a variable holds by assigning a new value to it with the equals sign.
The basic syntax for creating a variable
To create a variable in Python, type the name you want, then =, then the value:
name = "Alice"
This line creates a variable called name and stores the text "Alice" inside it. From that point forward, whenever you type name in your code, Python knows you mean the value "Alice".
You can create as many variables as you need, one per line:
age = 28 city = "Portland" is_student = True
The first line stores a number. The second stores text (called a string in programming). The third stores a true/false value (called a boolean). Python figures out which type you mean based on what you write.
Naming rules that Python enforces
Variable names in Python must follow specific rules, or Python will refuse to run your code. The name must start with a letter (a through z, uppercase or lowercase) or an underscore. After the first character, you can use letters, numbers, or underscores.
These names are valid:
- user_age
- firstName
- _private_value
- score2
These names will cause an error:
- 2score — starts with a number
- first-name — contains a hyphen
- first name — contains a space
- user@email — contains a special character
Python also has a list of words it reserves for its own use, like if, for, while, and print. You cannot use these as variable names. If you try, Python will show an error.
How Python determines what type of data you are storing
When you create a variable, Python looks at what you wrote on the right side of the equals sign and decides what type of information it is. You do not have to tell Python — it figures it out.
If you write a number without quotes, Python treats it as a number:
temperature = 72
If you write text inside quotes (single or double), Python treats it as a string:
greeting = "Hello" greeting = 'Hello'
Both lines do the same thing. The quotes tell Python that the content is text, not a command or a number.
If you write True or False (with capital letters), Python treats it as a boolean:
is_raining = True
This matters because Python treats different types differently. You can add two numbers together, but you cannot add a number to text without telling Python how to handle it. Understanding what type your variable holds helps you avoid mistakes.
Changing what a variable holds
A variable is not permanent. You can change its value at any time by assigning a new value to it using the equals sign again:
score = 10 print(score) score = 20 print(score)
The first print shows 10. The second shows 20. The variable score now holds 20, and the old value of 10 is gone.
You can also change a variable based on what it currently holds:
score = 10 score = score + 5
The second line takes the current value of score (10), adds 5 to it, and stores the result (15) back into score. This is one of the most common operations in programming.
Using variables in your code
Once you have created a variable, you use it by typing its name wherever you need the value it holds. If you have stored a person's name in a variable, you can print it, combine it with other text, or pass it to another part of your program:
name = "Jordan" print(name)
This prints: Jordan
You can combine variables with text:
name = "Jordan" age = 30 print("My name is " + name + " and I am " + str(age) + " years old")
This prints: My name is Jordan and I am 30 years old
Notice that age is a number, but we wrapped it with str() to turn it into text so we could combine it with the other text. This is necessary because you cannot mix numbers and text without converting one of them.
Common mistakes when creating variables
The most common mistake is forgetting the equals sign. If you write name "Alice" instead of name = "Alice", Python will show an error because it does not understand what you want to do.
Another frequent error is using a variable before you create it. If you try to use a variable that does not exist yet, Python will tell you the variable is not defined:
print(score) score = 10
This fails because score does not exist when you try to print it. You must create the variable first, then use it.
A third mistake is confusing the equals sign with comparison. In Python, = means "store this value in this variable". If you want to check whether two things are equal, you use == (two equals signs). This distinction matters when you write more complex code.
Frequently Asked Questions
Can I create a variable with a name that is already used somewhere else in my code?
Yes, but it will overwrite the old one. If you create a variable called count early in your program and then create another variable called count later, the second one replaces the first. The old value is lost. This is usually a mistake, so most programmers try to use unique names for different pieces of information.
What happens if I put quotes around a number?
Python treats it as text, not a number. age = "25" stores the text "25", not the number 25. This matters because you cannot do math with text. If you try to add 5 to "25", Python will show an error. You can convert it back to a number with int() if you need to do math with it.
Do I have to create a variable before I use it?
Yes. Python requires that a variable exist before you try to use it. If you reference a variable that was never created, Python stops and shows an error message saying the variable is not defined. Always create the variable first with an assignment, then use it in your code.
Can a variable hold more than one value at a time?
Not in the way you might think. A single variable holds one value. However, Python has structures called lists and dictionaries that can hold multiple values inside a single variable. For now, focus on single-value variables — lists and dictionaries come later as you learn more.