If you’re new to Python, one of the first and most important concepts you need to understand is variables. This guide will walk you through what variables are, how they work, and how to use them in real Python programs—even if you’ve never written a line of code before.
Let’s begin your Python journey by understanding the basic building blocks of any program: variables.
What is a Variable in Python?
A variable is like a container or a label that holds a piece of information or data. You can store a number, a word, or even complex objects in a variable and use it throughout your program.
Just like you save a contact name in your phone to store a phone number, in Python, a variable name stores a value.
Example:
name = "Alice"
age = 25
In this example:
name
is a variable that stores the string"Alice"
age
is a variable that stores the number25
Why are Variables Important?
Variables allow you to:
- Store and reuse data
- Make your code flexible and readable
- Perform calculations and decisions based on data
They are essential for writing dynamic and interactive Python programs.
Python Variable Syntax
Here’s the simple format for creating a variable in Python:
variable_name = value
Examples:
city = "Hyderabad"
temperature = 32.5
is_raining = False
Python uses =
(equal to) as the assignment operator to assign values.
Rules for Naming Variables in Python
When choosing a name for your variable, follow these rules:
- Must start with a letter (A-Z or a-z) or an underscore
_
- Cannot start with a number
- Can only contain letters, numbers, and underscores
- Cannot use Python keywords like
if
,while
,for
, etc.
Valid Variable Names:
first_name = "Rahul"
marks_10th = 92
_is_active = True
Invalid Variable Names:
2name = "Invalid" # starts with a number
first-name = "Invalid" # hyphen not allowed
if = "Invalid" # 'if' is a Python keyword
Types of Variables in Python
Python is dynamically typed, which means you don’t need to declare the type of variable explicitly. It automatically understands based on the value you assign.
Common Data Types:
Types | Examples |
String (str) | “hello”, ‘Python” |
Integer (int) | 1, 100, -50 |
Float (float) | 3.14, 9.8, 0.0 |
Boolean (bool) | True, False |
Example:
language = "Python" # String
year = 2025 # Integer
price = 499.99 # Float
is_available = True # Boolean
Reassigning Variables
You can change the value of a variable anytime:
score = 50
score = 75 # updated value
Now score holds the value 75.
Multiple Assignments in Python
Python allows assigning values to multiple variables in a single line:
a, b, c = 1, 2, 3
You can also assign the same value to multiple variables:
x = y = z = 100
Best Practices for Writing Variables
- Use descriptive names: Instead of
x
,y
, usestudent_name
,total_marks
- Stick to snake_case for variable naming:
user_age
,is_logged_in
- Keep your variable names short but meaningful
Variable Type Checking with type()
You can use the built-in type()
function to check the data type of a variable.
value = 100
print(type(value)) # Output: <class 'int'>
Variables analogy example
Imagine your kitchen. You label a container as “Sugar.”
Now, anyone who reads the label knows what’s inside.
If you change it and add “Salt” instead, it still says “Sugar” unless you change the label.
In Python:
- Label = Variable Name
- Content = Value stored in it
Common Mistakes Beginners Make
- Using the wrong name (e.g., case-sensitive confusion like
Name
vsname
) - Forgetting quotes around strings:
fruit = Apple # error
fruit = "Apple" # correct
- Using invalid characters in variable names like
#
,%
, or spaces
Summary – Python Variables for Beginners
Concept | Details |
What is a variable ? | A named container to store a value |
Assignment syntax | variable_name = value |
Data types | String, Integer, Float, Boolean |
Variable rules | Must follow naming rules, can’t the keywords |
Type checking | Use type() to know the variable type |
Reassignment | You can update variable values anytime |
Ready to Practice ?
Now that you’ve understood variables in Python, it’s time to write your own code. Try this simple exercise:
Exercise:
# Create three variables for name, age, and city
# Print a sentence like: "My name is Ravi, I am 25 years old and I live in Hyderabad"
# Your code here
Variables are the core foundation of Python. Whether you’re building a calculator, a website, or even an AI bot everything begins with variables.
Keep experimenting and practicing. In the next chapter, we’ll explore Python Data Types in more detail, which will help you understand how Python treats different kinds of values.