DEV Community

Lokesh
Lokesh

Posted on

Variables in Python

What Are Variables in Python?

Think of a variable as a small container where we can store information.

For example, imagine you want to store your name, age, or favorite programming language. Instead of writing the same information again and again, you can save it in a variable and use it whenever needed.

In Python, creating a variable is very simple.

python
name = "John"
age = 22
language = "Python"

print(name)
print(age)
print(language)

Output:

text
John
22
Python

That's how easy it is!

How Do Variables Work?

In Python, you don't need to specify the data type when creating a variable. Python automatically understands the type of value you're storing.

For example:

python
name = "Alex" # String
age = 21 # Integer
height = 5.9 # Float
is_student = True # Boolean

Python identifies these data types automatically.

A Few Rules for Naming Variables

There are a few simple rules to remember when naming variables in Python:

Variable names can contain letters, numbers, and underscores.
They cannot start with a number.
Variable names are case-sensitive.
Spaces are not allowed in variable names.
Python keywords cannot be used as variable names.

For example:

python
my_name = "Alex" # Valid
age1 = 20 # Valid
1name = "Alex" # Invalid

Top comments (0)