A variable is a named container that holds a value your code can use and change
In Python, you create a variable by typing a name, then an equals sign, then the value you want to store. That's it. Python figures out what type of data you're storing — a number, text, true/false — without you having to say so. Once the variable exists, you can use its name anywhere in your code to refer to that value, and you can change what it holds whenever you need to.
The simplest example: name = "Alice" creates a variable called name and stores the text "Alice" in it. Later, you can print it, compare it, or change it to name = "Bob". Python keeps track of what's inside.
Key Takeaways
- Create a variable by writing its name, an equals sign, and a value: age = 25 or city = "Portland".
- Variable names must start with a letter or underscore, can contain letters and numbers, and are case-sensitive — Name and name are different variables.
- Python automatically detects whether you're storing a number, text, true/false value, or a list, so you don't declare the type yourself.
- You can change a variable's value at any time by assigning a new value to it with the equals sign.
- Use descriptive names like user_age or total_price so your code is easier to read and debug later.
The basic syntax: name, equals sign, value
The pattern is always the same. Write the name you want to give the variable, then =, then the value:
score = 95 stores the number 95 in a variable called score. message = "Hello" stores the text "Hello" in a variable called message. is_raining = True stores a true/false value (called a boolean) in a variable called is_raining.
Once you've created a variable, you can use its name in your code. If you write print(score), Python prints 95. If you write print(message), Python prints Hello. The variable name is a shortcut to the value inside it.
Naming rules and why they matter
Variable names have a few hard rules. They must start with a letter (a–z, A–Z) or an underscore (_). After the first character, you can use letters, numbers, and underscores. You cannot use spaces, hyphens, or special characters like @, #, or $. So user_name is valid, but user-name and user name are not.
Python is case-sensitive, which means Name, name, and NAME are three completely different variables. If you create name = "Alice" and then try to print Name, Python will tell you that Name doesn't exist yet.
Beyond the rules, use names that describe what the variable holds. user_age is clearer than x. total_price is clearer than tp. When you come back to your code in a week, or when someone else reads it, a good name saves time and prevents mistakes.
Python figures out the data type automatically
When you assign a value to a variable, Python looks at what you gave it and decides what type of data it is. You don't have to tell Python "this is a number" or "this is text." It knows.
age = 30 creates a variable holding an integer (a whole number). price = 19.99 creates a variable holding a float (a decimal number). name = "Sarah" creates a variable holding a string (text). is_active = True creates a variable holding a boolean (true or false). Python detected the type from the value itself.
This automatic detection is one reason Python is easier to start with than languages like Java or C, where you have to declare the type before you create the variable. But it also means you need to be careful: if you assign a string to a variable and later try to do math with it, Python will complain.
Changing a variable's value
A variable is called a variable because its value can change. You reassign it the same way you created it: name, equals sign, new value.
count = 0 creates a variable. count = 1 changes it. count = count + 1 takes the current value (1), adds 1 to it, and stores the result (2) back in count. This last pattern is so common that Python has a shorthand: count += 1 does the same thing.
You can also change a variable's type. value = 10 stores a number. value = "ten" stores text in the same variable. Python allows this, though it's usually a sign that your code could be clearer — if a variable sometimes holds a number and sometimes holds text, readers (including you, later) will get confused.
Common mistakes and how to avoid them
The most common mistake is using a variable before you've created it. If you write print(score) but never assigned anything to score first, Python stops and says "NameError: name 'score' is not defined." The variable doesn't exist yet. Always assign a value before you use the name.
Another frequent error is forgetting that Python is case-sensitive. You create user_name = "Alex" but then write print(User_name) (capital U). Python treats it as a different variable and tells you it doesn't exist. Check your capitalization.
A third trap is using spaces or hyphens in variable names. user name = "Alex" and user-name = "Alex" will both cause errors. Use underscores instead: user_name = "Alex". This style is called snake_case and is the Python convention.
Frequently Asked Questions
Can I create multiple variables at once?
Yes. You can write x = 5; y = 10; z = 15 on one line, separating assignments with semicolons. More commonly, you write each on its own line for readability. You can also unpack: x, y, z = 5, 10, 15 assigns all three in one statement.
What's the difference between = and ==?
The single equals sign = assigns a value to a variable. The double equals sign == compares two values to see if they're the same. age = 25 stores 25 in age. age == 25 checks whether age is 25 and returns true or false.
Do I have to name variables in snake_case?
No, Python allows camelCase (like userName) and other styles. But the Python community standard, documented in a style guide called PEP 8, recommends snake_case for variable names. Following the standard makes your code easier for others to read.
Can a variable name be a Python keyword?
No. Words like if, for, while, def, and return are reserved by Python for its own use. If you try for = 10, Python will give you a syntax error. Use a different name, like for_loop or iteration.
What happens if I assign the same variable twice?
The second assignment overwrites the first. x = 5 then x = 10 means x now holds 10, and the value 5 is gone. Python doesn't keep a history of old values unless you store them in another variable first.