Python Variables Explained with Examples for Beginners

Shubham DawareShubham Daware
2 min read

Introduction

In programming, we often need to store data and reuse it later. For example, your name, your age, or even your exam score. In Python, we use variables to store this information.

Think of a variable like a box with a label on it. You can put something inside the box, give it a name, and then use it later whenever you need it.

๐Ÿ”น What is a Variable?

A variable is a name that points to a value stored in memory. Instead of remembering the number 25 or the word "Alice", you can assign it to a variable and use the name instead.

๐Ÿ–ฅ๏ธ Example 1: Creating Simple Variables:

name = "Alice"
age = 20
print(name)
print(age)

->Output:
Alice
20

Here:

  • name is a variable storing the text "Alice".

  • age is a variable storing the number 20.


๐Ÿ”น Rules for Naming Variables in Python

When creating variables, follow these rules:

  1. Names must start with a letter or underscore (_name).

  2. They cannot start with a number (2name โŒ).

  3. They can only contain letters, numbers, and underscores.

  4. Variable names are case-sensitive (Age and age are different).

โœ… Examples: user_name, score1, _hiddenValue
โŒ Invalid: 1score, user-name, my variable

๐Ÿ–ฅ๏ธ Example 2: Different Data Types in Variables:

x = 10            # integer
y = 3.14          # float (decimal number)
name = "Bob"      # string (text)
is_student = True # boolean (True/False)

->Output:
10
3.14
Bob
True

๐Ÿ”น Why Are Variables Important?

  • They make programs flexible and reusable.

  • Instead of writing values directly everywhere, you assign them once and use the variable.

  • This helps when data changes (you only update one variable).

๐Ÿ–ฅ๏ธ Example 3: Updating a Variable:

score = 50
print("Initial score:", score)

score = 75
print("Updated score:", score)

-?Output:
Initial score: 50
Updated score: 75

Here, we first assigned score = 50 but later updated it to 75.

โœ… Summary (These point are easy to remember while answering theoretical questions) :

  • Variables are like boxes that store data.

  • You can assign values like numbers, text, or True/False.

  • Python variables are flexible and can be updated.

  • Follow the naming rules to avoid errors.

๐Ÿ™‹โ€โ™‚๏ธ Try it yourself :

Try creating your own variables in Python! For example:

  • Your name

  • Your favorite number

  • A True/False variable (like likes_pizza = True)

What variables did you create? Share them in the comments below ๐Ÿ‘‡

0
Subscribe to my newsletter

Read articles from Shubham Daware directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shubham Daware
Shubham Daware