Python Variables Explained: Full Beginner Guide with Examples
π Variables in Python
What · Why · Rules · Types · Assignment · Dynamic Typing · Best Practices
A variable in Python is a name used to store data. It works like a container that holds information such as numbers, text, or other values. Instead of writing the same value again and again, we store it in a variable and use it whenever needed.
For example, if you want to store your name in a program, you can use a variable instead of writing your name multiple times. This makes the code shorter and easier to understand.
Python is very beginner-friendly because you do not need to declare the type of variable. The type is automatically decided based on the value.
name = "Rahul" age = 18 print(name) print(age)
18
Variables are very important in programming because they help store and manage data easily. Without variables, you would have to write the same values again and again, which makes code long and confusing.
Variables help make programs dynamic. This means you can change the value of a variable and the program will behave differently. For example, changing a user's age or name in a program becomes easy using variables.
They also make code more readable and understandable for others.
price = 100 discount = 20 final_price = price - discount print(final_price) # 80
When creating variables, you must follow some rules. These rules help avoid errors and make your code valid.
A variable name must start with a letter or underscore (_). It cannot start with a number. Spaces are not allowed, and only letters, numbers, and underscores can be used. Also, Python is case-sensitive, so "Name" and "name" are different variables.
my_name = "Alex" # ✅ correct age2 = 20 # ✅ correct # 2name = "John" ❌ wrong # my name = "Sam" ❌ wrong
# if = 10 — using reserved keyword# 2age = 20 — starts with a number
Assigning a value to a variable means storing data inside it. In Python, we use the equals (=) sign for assignment.
You can assign values in different ways. You can assign single values, multiple values, or even the same value to multiple variables.
x = 10 y = 20 print(x + y) # 30
a, b, c = 1, 2, 3 print(a, b, c) # 1 2 3
x = y = z = 5 print(x, y, z) # 5 5 5
Python variables can store different types of data. Let's understand the main types.
These store numbers like integers and decimals.
x = 10 y = 3.5 print(x, y) # 10 3.5
These store text.
name = "Python" print(name) # Python
These store True or False values.
is_passed = True print(is_passed) # True
Store multiple values.
fruits = ["apple", "banana", "mango"] print(fruits)
Store data in key-value form.
student = {"name": "Rahul", "age": 18} print(student["name"]) # Rahul
One of the best features of Python is that you can change variable values anytime.
x = 10 print(x) # 10 x = 50 print(x) # 50
50
This makes Python very flexible.
Python is dynamically typed, which means a variable can change its type during execution.
x = 10 print(type(x)) # <class 'int'> x = "Hello" print(type(x)) # <class 'str'>
You can check the type of a variable using the type() function.
x = 10 name = "Rahul" print(type(x)) # <class 'int'> print(type(name)) # <class 'str'>
Let's understand variables with a real-life example:
name = "Rahul" age = 18 marks = 85.5 passed = True print("Name:", name) print("Age:", age) print("Marks:", marks) print("Passed:", passed)
Age: 18
Marks: 85.5
Passed: True
This shows how different types of data are stored together.
Beginners often make mistakes like:
# if = 10 ❌ keyword # 2age = 20 ❌ starts with number # my age = 25 ❌ space in name
age = 20 # ✅ my_age = 25 # ✅ age2 = 22 # ✅
student_name, total_price, is_active❌ Bad:
x, n1, temp123
3.14159 everywhere, store it once as pi = 3.14159 and use the name throughout your program.
Variables are the foundation of Python programming. They help store and manage data efficiently. By learning variables, beginners can write better programs and understand coding concepts easily. Python makes variables simple and flexible, which is why it is one of the best languages for beginners. Mastering variables is the first step toward becoming a good programmer.
Comments
Post a Comment