Learning

Understanding Variables in Python: The Ultimate Beginner’s Guide (With Examples & Tips)

If you are going to study Python programming, then variables are one of the first and most important concepts you will come across. Here you will know all about the variables from general life example to programming example.


What is a Variable?

A variable indicate anything that can be changed and do not remains constant throughout, obviously it is called variable but here in programming variables are also considered as containers for storing data.

As in Kitchen, different containers are present that are used to store various items, in each container we store specific item at a time. Like for water storage we have tanks, bottles and for storing rice we use jars. Thus for storing various items we have various containers.

Same as in python programming, variables are anything (any valid name or letter) which are used to store data (can be number, name or any form of data). Thus any container where you store your data is a variable. In python we also have to give some name to our containers (variables) where we are going to store some value same as we just used bottles for storing water and jar for storing rice.

Variables examples

As the figure shows the different containers used to store various items. Now it is up to you how you can use them and for what purpose you use.

In python, variables store values like numbers, words, and characters. You would understand it even better as we proceed further with examples.

Example:

variables in python
name = "Ali"
age = 25

Here:

  • name is a variable (because it is the name of container) storing the string (data) "Ali"
  • age is a variable storing the number (data) 25

No Declarations Needed!

Unlike many other programming languages (like Java or C++), Python does not ask you to declare the type of a variable before using it. Just assign a value, and Python figures out the type automatically. Type of a data means which type of data it is i.e. either it is a integer, string, boolean or float.

✅ This is valid:

price = 9.99      # float
is_active = True  # boolean
students = 35     # integer

❌ This is not needed in Python:

int students = 35; // Not how we do it in Python

Dynamic Typing

Python is dynamically typed. That means a variable can change its type during program execution.

data = "Hello"   # data is a string
data = 123       # now it's an integer

This is flexible, but be careful—sometimes it can lead to confusion if you’re not keeping track therefore to keep things understandable and simple avoid such practices.


Naming Variables: Rules & Tips

Rules (things Python enforces):

  1. Names must start with a letter (a,b,c,…) or an underscore (_)
  2. The rest of the name can contain letters, numbers, and underscores
  3. Names are case-sensitive (Age and age are different variables)

🚫 These are invalid:

1name = "John"   # starts with a number
my-name = "Sam"  # hyphen is not allowed

Best Tips:

  • Use descriptive names: user_name instead of u
  • Stick to snake_case (words separated by underscores)
  • Avoid Python keywords as names (like class, def, for)

Common Data Types Stored in Variables

TypeExampleDescription
int100Whole numbers
float3.14Decimal numbers
str"hello"Text (strings)
boolTrue or FalseBoolean values
list[1, 2, 3]Collection of items
dict{"name": "Ali"}Key-value pairs
score = 100         # int
height = 5.9        # float
username = "alex"   # string
passed = True       # boolean

Reassigning Values

Variables can be reassigned at any time, but whenever we reassign a new value the previous value will be gone and the new value will be store just like below

status = "active"
status = "inactive"

Remember this thing, whenever replacing occur the old value is replaced. You don’t need to declare a new variable every time you want to change the value.


Types of Variables:

Global and Local Variables

Python variables also have something called scope — it means where in the program the variable is accessible.

Local Variables

Defined inside a function and only accessible within that function. Just like local people living in a specific place.

pythonCopyEditdef greet():
    message = "Hello"
    print(message)

greet()
print(message)  # ❌ Error: message is not defined globally

Global Variables

Defined outside of all functions and accessible everywhere. The global variable are accessible from any place, there is no restriction of boundry.

pythonCopyEditname = "Alex"

def show_name():
    print(name)  # ✅ Works fine

show_name()

Modifying Global Variables

If you want to change a global variable inside a function, use the global keyword.

pythonCopyEditcount = 0

def increment():
    global count
    count += 1

increment()
print(count)  # Output: 1

🚨 Be cautious when using global. It’s better to pass variables into functions if possible — it keeps your code clean and bug-free.

Just remeber one thing if you are confused of the difference between Local and Global as, Local are those which are known to specific people like your local star, either in the form of singer or sports player. But Global are those who is known by all over the world just like Ronaldo, Messi, Justin Bieber etc

Multiple Assignments

You can assign multiple variables in one line:

x, y, z = 10, 20, 30

Or assign the same value to multiple variables:

a = b = c = "Python"

Storing and Using Variables

Once you’ve stored a value in a variable, you can use it anywhere in your code:

name = "Fatima"
print("Hello, " + name)

Or perform operations:

num1 = 10
num2 = 5
total = num1 + num2
print("Sum is", total)

Final Thoughts

Variables are the backbone of any program. They help your code remember things, keep track of states, and make your program dynamic and interactive.

As you get more advanced, you’ll learn about:

But for now, just remember: Variables are your program’s memory. Treat them well!


Also Read: The Story Behind Python, Why the World Fell in Love with Python

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button