A variable is one of the first things you learn in Python, and almost every program uses it. In this guide, you will learn what a variable is, what data types are, and how to use them with simple examples. If you have not installed Python yet, read How to Install Python and VS Code on Windows first. It takes only 10 to 15 minutes.
- What a variable is and how to create one
- The rules for naming variables
- The four basic data types: int, float, str and bool
- How to change a value and how to change one type into another
- How to fix the most common errors, and how to practise right on this page
What Is a Variable?
A variable is a name that stores a value. Think of it like a box with a label on it. The label is the name, and the thing inside the box is the value. When you need the value later, you just call the name.
In Python, you create a variable with the = sign. The = sign does not mean "equal" like in maths. It means "store this value in this name".
Step 1: Create Your First Variables
- Open VS Code and open your python-practice folder.
- Click the New File icon and name the file
variables.py. - Type the code below.
- Press
Ctrl + Sto save the file. - Click the triangle (Play) button at the top right to run it.
name = "Ali"
age = 20
print(name)
print(age)
You should see this output:
Ali
20
Here, name and age are variables. "Ali" and 20 are their values. The print() line shows the value on the screen.
Rules for Naming Variables
You can choose any name for your variable, but Python has a few rules:
- Use letters, numbers and underscore (_). For example:
student_name,age2. - Do not start with a number.
2ageis wrong, butage2is correct. - Do not use spaces. Write
student_name, notstudent name. - Do not use a dash.
my-nameis wrong. Usemy_name. - Capital and small letters are different.
ageandAgeare two different variables. - Do not use Python words as names, like
printorif.
total_marks is much easier to understand than tm.Step 2: Learn the Four Basic Data Types
A data type tells Python what kind of value you are storing. These are the four basic types you will use every day:
- int: a whole number, like
20or-5 - float: a number with a decimal point, like
5.7or99.5 - str: text (also called a string). Text always goes inside quotes, like
"Ali" - bool: only two values:
TrueorFalse
Here is one example of each type:
age = 20 # int
height = 5.7 # float
name = "Ali" # str
is_student = True # bool
Everything after the # sign is a comment. Python ignores it. It is only a note for you.
Remember: True and False must start with a capital letter. true will give an error.
Step 3: Check the Type with type()
If you are not sure what type a variable has, use type(). It tells you the type of the value.
age = 20
name = "Ali"
print(type(age))
print(type(name))
You should see this output:
<class 'int'>
<class 'str'>
int means whole number and str means text. It works the same way for float and bool.
Step 4: Change the Value of a Variable
A variable can change. You can give it a new value any time. Python always remembers the latest value.
age = 20
print(age)
age = 21
print(age)
You should see this output:
20
21
You can also use the old value to make a new one. For example, age = age + 1 adds 1 to the current age.
Step 5: Change One Type into Another
This is a very important lesson. The input() function always gives you text, even if the user types a number. So if the user types 20, Python sees it as "20" (text), and you cannot do maths with it.
To change the type, use these functions:
int("20")changes text into a whole number:20float("5.7")changes text into a decimal number:5.7str(20)changes a number into text:"20"
Here is an example:
age = input("Enter your age: ")
age = int(age)
print("Next year you will be", age + 1)
If you type 20, you will see this output:
Enter your age: 20
Next year you will be 21
Step 6: Practice Program
Now let us use everything together. Create a new file named practice.py and type this:
name = "Ali"
age = 20
print("Name:", name)
print("Age:", age)
print("Next year:", age + 1)
You should see this output:
Name: Ali
Age: 20
Next year: 21
Now change the name and age to your own and run it again. This is the best way to learn.
Common Errors and How to Fix Them
Error 1: NameError: name 'Name' is not defined
Python cannot find the variable. Check three things:
- Check the spelling.
nameandNameare different. - Check that you created the variable before using it.
- Check that you did not forget the quotes around text.
Error 2: SyntaxError: invalid syntax
This usually means the variable name or the line is written wrongly. For example:
student name = "Ali"
There is a space in the name. Write student_name = "Ali" instead.
Error 3: TypeError: can only concatenate str (not "int") to str
This happens when you join text and a number with +:
age = 20
print("Age: " + age)
To fix it, use a comma in print():
print("Age:", age)
Error 4: ValueError: invalid literal for int()
You tried to change text into a number, but the text is not a number. For example, int("abc") will fail. Make sure the user types only digits like 20.
Common Mistakes Beginners Make
- Forgetting the quotes around text. Write
"Ali", notAli. - Writing true or false with small letters. Use
TrueandFalse. - Forgetting that input() gives text. Use
int()before doing maths. - Using spaces in variable names. Use an underscore instead.
- Forgetting to save the file. Press
Ctrl + Sbefore you run the code.
Practice Time
Try these small tasks on your own. Click a question to open the code box, write your Python code and press Run. If your code is correct, you will see a green message. If there is an error, you will see the error, the line number, the mistake and how to fix it.
- Create variables for your name, age and city. Print all three.
- Use
type()to check the type of each variable. - Change your age to a new value and print it again.
- Ask the user for two numbers with
input(), change them withint(), and print their sum.
What to Learn Next
Now you know how to store values in variables. The next step is input and output. You will learn how to take information from the user and show clean messages on the screen. In the next post, we will learn it with simple examples. If you missed the full learning plan, read my Python roadmap for beginners.
Frequently Asked Questions (FAQs)
What is a variable in Python?
A variable is a name that stores a value, like a labelled box. You create it with the = sign, for example age = 20.
What are the basic data types in Python?
The four basic types are int (whole numbers), float (decimal numbers), str (text) and bool (True or False).
Why does input() give me text and not a number?
The input() function always returns text. If you want to do maths, change the text into a number first with int() or float().
Can I change the value of a variable later?
Yes. Just give the same name a new value, and Python will remember the latest one.
Final Thoughts
Variables and data types are the base of Python. Once you understand them, every other topic becomes easier. Remember the four types: int, float, str and bool, and remember that input() always gives text. Practice a little every day, and try to change the values in the examples to see what happens.







Comments
Post a Comment