Variables in Python
In this lesson, you will learn what variables are, how they work in Python, different ways to create them, and how they are used in real projects. Variables are the basic building blocks of every Python program.
What Is a Variable?
A variable is simply a name that stores a value in memory. You can think of it like a label on a container that holds some data.
x = 10
name = "Dataplexa"
price = 499.99
Here:
xstores a numbernamestores textpricestores a decimal value
Rules for Naming Variables
Python has some simple rules for variable names:
- Must start with a letter (a–z or A–Z) or underscore (
_). - Cannot start with a number.
- Can only contain letters, numbers, and underscore.
- Cannot contain spaces or special symbols like
@, $, %. - Python is case-sensitive (
ageandAgeare different).
Valid and Invalid Variable Names
| Valid | Invalid |
|---|---|
| age, total_price, student_name | 1age, total-price, student name |
| _count, value2, data_set | price$, my-variable, @value |
Reassigning Variables
You can change the value of a variable at any time:
x = 10
x = 20 # value updated
print(x)
The latest value assigned to the variable is the value Python will use.
Different Types of Values
Python automatically detects the type of data you store in a variable.
age = 25 # int
rating = 4.8 # float
name = "Sreekanth" # str
is_student = True # bool
Multiple Assignment
You can assign values to multiple variables in one line:
a, b, c = 10, 20, 30
print(a, b, c)
This is useful when you want to set up several values quickly and clearly.
Swapping Variables (Without Temporary Variable)
A very popular Python trick is swapping values in one line:
a = 5
b = 10
a, b = b, a
print(a, b) # 10 5
Python makes it easy to swap without using a third variable.
Dynamic Typing in Python
Python is dynamically typed. You do not need to specify the data type when creating a variable. Python decides the type based on the value.
x = 10 # int
x = "Hello" # now str
x = 3.14 # now float
The same variable name can hold different types at different times.
Constants in Python (Naming Convention)
Python does not have real constants, but by convention we write constant-like values in UPPERCASE to show they should not be changed.
PI = 3.14159
MAX_USERS = 100
APP_NAME = "Dataplexa"
This is a style rule, not a strict language rule.
Real-World Example: Simple Shopping Cart
Let’s see variables working together in a mini example:
item_name = "Wireless Mouse"
item_price = 799
quantity = 2
gst_rate = 0.18
subtotal = item_price * quantity
tax = subtotal * gst_rate
total_bill = subtotal + tax
print("Item:", item_name)
print("Subtotal:", subtotal)
print("Tax:", tax)
print("Total Bill:", total_bill)
This is how real applications store and calculate values using variables.
📝Practice Exercises
Try these exercises to check your understanding.
Exercise 1
Create three variables: first_name, last_name, and country.
Print them in a single sentence.
Exercise 2
Store the price of a product in price and a discount percentage
in discount.
Calculate and print the final price after discount.
Exercise 3
Use multiple assignment to store 10, 20, 30 in a, b, c
and print their average.
Exercise 4
Swap the values of two variables x and y using the Python swap trick.
✅ ExerciseAnswers
Answer 1
first_name = "Sreekanth"
last_name = "Reddy"
country = "USA"
print("My name is", first_name, last_name, "and I live in", country)
Answer 2
price = 1500
discount = 10 # percent
final_price = price - (price * discount / 100)
print("Final Price:", final_price)
Answer 3
a, b, c = 10, 20, 30
average = (a + b + c) / 3
print("Average:", average)
Answer 4
x = 5
y = 9
x, y = y, x
print("x:", x)
print("y:", y)