Python Variables Explained with Examples for Beginners


Introduction
In programming, we often need to store data and reuse it later. For example, your name, your age, or even your exam score. In Python, we use variables to store this information.
Think of a variable like a box with a label on it. You can put something inside the box, give it a name, and then use it later whenever you need it.
๐น What is a Variable?
A variable is a name that points to a value stored in memory. Instead of remembering the number 25
or the word "Alice"
, you can assign it to a variable and use the name instead.
๐ฅ๏ธ Example 1: Creating Simple Variables:
name = "Alice"
age = 20
print(name)
print(age)
->Output:
Alice
20
Here:
name
is a variable storing the text"Alice"
.age
is a variable storing the number20
.
๐น Rules for Naming Variables in Python
When creating variables, follow these rules:
Names must start with a letter or underscore (
_name
).They cannot start with a number (
2name
โ).They can only contain letters, numbers, and underscores.
Variable names are case-sensitive (
Age
andage
are different).
โ
Examples: user_name
, score1
, _hiddenValue
โ Invalid: 1score
, user-name
, my variable
๐ฅ๏ธ Example 2: Different Data Types in Variables:
x = 10 # integer
y = 3.14 # float (decimal number)
name = "Bob" # string (text)
is_student = True # boolean (True/False)
->Output:
10
3.14
Bob
True
๐น Why Are Variables Important?
They make programs flexible and reusable.
Instead of writing values directly everywhere, you assign them once and use the variable.
This helps when data changes (you only update one variable).
๐ฅ๏ธ Example 3: Updating a Variable:
score = 50
print("Initial score:", score)
score = 75
print("Updated score:", score)
-?Output:
Initial score: 50
Updated score: 75
Here, we first assigned score = 50
but later updated it to 75
.
โ Summary (These point are easy to remember while answering theoretical questions) :
Variables are like boxes that store data.
You can assign values like numbers, text, or True/False.
Python variables are flexible and can be updated.
Follow the naming rules to avoid errors.
๐โโ๏ธ Try it yourself :
Try creating your own variables in Python! For example:
Your name
Your favorite number
A
True/False
variable (likelikes_pizza = True
)
What variables did you create? Share them in the comments below ๐
Subscribe to my newsletter
Read articles from Shubham Daware directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
