Variables in Python: A Complete Beginner's Guide

← PrevNext →

If you are starting your journey with Python programming, understanding variables is one of the first things you need to learn. Variables help you store, manage, and use data in your programs. In this beginner-friendly guide, you will learn what variables are, how to create them, Python variable naming rules, and how to assign and update values with practical examples.

What Are Variables in Python?

Variables are one of the most important concepts in Python programming. A variable is a name used to store data in a program.

You can think of a variable as a container that holds information. This information can be a number, text, decimal value, or other types of data.

Example:

name = "Souvik"
age = 25
salary = 130000

Here, name stores the text Shouvik. The age stores the integer value 25. And salary stores an integer value 130000.

Python automatically determines the data type based on the assigned value.

How to Create Variables in Python

Creating a variable in Python is simple. You just need to write a variable name, followed by an equal sign (=), and then the value.

Syntax:

variable_name = value

Example:

city = "Bhopal"
temperature = 30
price = 99.50

Note: In Python, you do not need to declare the variable type before assigning a value.

Rules for Naming Variables

Python has certain rules for naming variables:

Valid Variable Names

name = "Amit"
student_age = 20
total_marks = 450
_price = 100

Invalid Variable Names

2name = "Amit"
student age = 20
class = "Python"

These examples are invalid because they violate Python's variable naming rules.

Assigning Multiple Variables

Python allows you to assign values to multiple variables in one statement.

Example:

name, age, city = "Max", 22, "New York"

print(name)
print(age)
print(city)

Output:

Max
22
New York

You can also assign the same value to multiple variables.

a = b = c = 10
print(a, b, c)

Output:

10 10 10

Changing Variable Values

Variables can be updated whenever required. Assigning a new value to an existing variable replaces its previous value.

Example:

marks = 80
print(marks)

marks = 95
print(marks)

Output:

80
95

The second assignment replaces the previous value stored in the variable.

Understanding Dynamic Typing

Python is a dynamically typed language. This means you do not have to specify the data type when creating a variable.

Example:

value = 100
print(value)

value = "Python"
print(value)

Output:

100
Python

The same variable can store different types of values at different times.

Checking Variable Data Types

Python provides the type() function to identify the data type stored in a variable.

Example:

name = "Anita"
age = 24
price = 150.50

print(type(name))
print(type(age))
print(type(price))

Output:

<class 'str'>
<class 'int'>
<class 'float'>

Conclusion

Variables are essential for storing and managing information in Python programs. They allow developers to work with different values, update information, and perform calculations.

Understanding variables and their naming rules is an important step toward learning Python programming. Practice creating variables with different data types to improve your understanding.

← PreviousNext →