Python Variables

What is a Variable?

A variable is like a label you stick on a box to remember what’s inside.

Imagine you have a box where you can store anything you want — a number, a word, or even a whole sentence. In Python, we use something called a variable to store information like that!

For example, you can create a variable called name and store your name in it:

name = "Arjun"

Here, name is the variable, and "Arjun" is the information we are saving inside it.

{ "array": ["Arjun"], "showIndices": false, "highlightIndicesBlue": [0], "labels": { "0": "name" } }

Another Example

Let's save a number in a variable:

age = 25

Now, the variable age holds the number 25.

{ "array": [25], "showIndices": false, "highlightIndices": [0], "labels": { "0": "age" } }

How to Use Variables

After you create a variable, you can use it whenever you want. For example:

name = "Arjun"
age = 25

print(name)
print(age)

When you run this code, Python will show:

Arjun
25

Important Rules for Naming Variables

  • Names can only have letters, numbers, and underscores (_).
  • Names cannot start with a number.
  • Names should not have spaces. Use _ instead. (Example: my_age)
  • Pick names that make sense! (Example: price is better than p.)