Python Variables - A Beginner's Guide
What Is a Variable?
A variable in Python is like a container that holds data. You can think of it as a label you stick on a box, so that you know what’s inside.
Why Use Variables?
Variables help you to:
- Store information
- Reuse it later
- Change it easily
📝 Creating a Variable
To create a variable in Python, just write a name, an equal sign =, and a value.
name = "Alice"
age = 25
height = 5.6
Here:
-
nameis a variable storing a string -
ageis storing an integer -
heightis storing a floating-point number
📤 Viewing Variable Output
You can display the value stored in a variable using the print() function.
name = "Alice"
age = 25
print(name)
print(age)
Output:
Alice
25
This helps you verify that your variables are storing the correct values.
🗃 Variable Naming Rules
✅ You can:
-
Use letters, numbers, and underscores (
_) - Start with a letter or underscore
❌ You can’t:
- Start with a number
-
Use spaces or symbols like
@or!
✅ Valid:
first_name = "John"
user2 = "Jane"
_temp = 50
❌ Invalid:
2user = "Error"
user-name = "Oops"
🔄 Changing a Variable's Value
You can update a variable at any time:
score = 10
score = 20 # Now score is 20
Python Is Dynamically Typed
You don’t have to tell Python the type of variable. It figures it out on its own:
x = 10 # int
x = "ten" # now it's a string
This is called dynamic typing.
📦 Assigning Multiple Variables at Once
Python allows you to assign values to multiple variables in a single line.
x, y, z = 10, 20, "Python"
print(x)
print(y)
print(z)
Output:
10
20
Python
This makes your code shorter and easier to read when initializing several variables together.
Example with Numbers
a = 5
b = 3
sum = a + b
print("Sum is:", sum) # Output: Sum is: 8
🗣 Example with Strings
first = "Python"
second = "Rocks"
message = first + " " + second
print(message) # Output: Python Rocks
⚠️ Common Mistakes Beginners Make
-
Using spaces in variable names
user name = "John" # ❌ Error -
Starting variable names with numbers
1value = 10 # ❌ Error -
Overwriting important values accidentally
count = 5 count = count + 1 # Value changes intentionally
Being careful with naming and reassignment helps avoid unexpected bugs.
🧠 Quick Practice Quiz
Test your understanding of Python variables. Try answering before checking the answers.
-
Which of the following is a valid Python variable name?
- a)
2name - b)
user-name - c)
user_name
- a)
-
What will be the output of the following code?
x = 10 x = "Hello" print(x) -
Which statement about Python variables is true?
- a) You must declare the variable type
- b) Variables cannot change type
- c) Python determines the variable type automatically
Answers:
- 1️⃣ c)
user_name - 2️⃣
Hello - 3️⃣ c) Python determines the variable type automatically
If you answered most of these correctly, you're ready to move on to Python Data Types.
Best Practices
- Use descriptive names:
x = 10 # Bad user_age = 10 # Good - Stick to lowercase_with_underscores for readability
📌 Quick Recap
| Feature | Example |
|---|---|
| Create variable | name = "Alice" |
| Update value | name = "Bob" |
| Use in math | total = a + b |
| Combine text | msg = first + last |
❓ Frequently Asked Questions
Do I need to declare variable types in Python?
No. Python automatically determines the data type based on the value you assign.
Can I change a variable’s type later?
Yes. Python allows you to assign a new value of a different type to the same variable.
Are Python variables case-sensitive?
Yes. age and Age are treated as two different variables.
📚 Continue Learning in This Python Series
If you’re following along with our beginner-friendly Python tutorials, here are the next topics you should explore:
- 👉 Python Data Types - Learn about integers, floats, strings, lists, and more.
- 👉 How to Use Lists in Python - Understand how to store and manage multiple values in one variable.
- 👉 Python Functions for Beginners with Examples - Learn how to write reusable blocks of code to simplify your programs.
Following these tutorials in order will help you build a strong foundation in Python programming.
✅ Try It Yourself
Copy and paste this code into a Python editor:
name = "Charlie"
age = 30
print(name, "is", age, "years old.")
You’ll see: Charlie is 30 years old.
watch following video if you like to understand in video format.
In this video you can see :
What is a variable ?
Why to use variables?
What are python variables and how they are used?
How python deals with variables and how these are declared?