DEV Community

Anil Khedkar
Anil Khedkar

Posted on

What are variables in Python?

Variables are one of the fundamental concepts in Python programming. They allow developers to store and work with data such as names, numbers, prices, lists, and other values.

If you are new to Python, understanding variables is one of the first steps toward writing useful programs.

What Is a Variable in Python?

A variable is a name that refers to a value stored in memory.

In Python, you can create a variable simply by assigning a value to a name:

name = "Rahul"
age = 25
salary = 45000

Here:

name refers to the string "Rahul"
age refers to the integer 25
salary refers to the integer 45000

Python automatically determines the type of value assigned to the variable.

How to Create a Variable

Python does not require a separate declaration for variables.

city = "Pune"

You can then use the variable elsewhere in your program:

city = "Pune"

print(city)

Output:

Pune
Different Types of Values

Python variables can store different types of data.

String
name = "Anil"
Integer
age = 30
Float
price = 499.99
Boolean
is_active = True
List
fruits = ["Apple", "Banana", "Mango"]
Dictionary
student = {
"name": "Rahul",
"age": 22
}
Dynamic Typing in Python

Python is a dynamically typed language. This means you don't need to specify the data type when creating a variable.

value = 100

Later, the same variable can refer to a different type of value:

value = "Hello"

Python determines the type at runtime.

You can check the type using type():

value = 100

print(type(value))

Output:


Assigning Multiple Variables

Python allows multiple variables to be assigned in one statement:

name, age, city = "Rahul", 25, "Pune"

You can also assign the same value to multiple variables:

x = y = z = 10
Variable Naming Rules

Python variable names should follow certain rules.

Valid Examples
first_name = "Rahul"
age = 25
total_price = 500
Invalid Examples
2name = "Rahul" # Invalid
first-name = "Rahul" # Invalid

A variable name:

Can contain letters, numbers, and underscores
Cannot start with a number
Cannot contain spaces
Is case-sensitive
Should not use Python reserved keywords

For example:

name = "Rahul"
Name = "Amit"

name and Name are considered different variables.

Constants in Python

Python does not have a strict constant keyword. However, developers commonly use uppercase names to indicate that a value should not be changed.

PI = 3.14159
MAX_USERS = 100

This is a naming convention rather than a language-enforced restriction.

Deleting a Variable

You can remove a variable using the del statement:

age = 25

del age

After deletion, trying to use age will result in an error.

Why Are Variables Important?

Variables make programs easier to write and maintain because they allow developers to store information and reuse it.

For example:

price = 1000
quantity = 3

total = price * quantity

print(total)

Output:

3000

Instead of repeatedly writing values directly into calculations, variables make the code more readable and flexible.

Conclusion

Variables are a fundamental part of Python programming. They provide names that refer to values and allow programs to store, manipulate, and reuse information.

Once you understand variables, you can move on to important Python concepts such as data types, operators, conditions, loops, functions, lists, dictionaries, and classes.

Frequently Asked Questions

Do I need to declare a variable in Python?
No. Python creates a variable when you assign a value to a name.

Can a Python variable change its data type?
Yes. Python is dynamically typed, so a variable can refer to values of different types at different times.

Are Python variable names case-sensitive?
Yes. name and Name are different variables.

Can a variable contain spaces?
No. Use underscores instead, such as first_name.

Top comments (0)