Python Course
Variables in Python
Every serious program stores information. Usernames, prices, counters, scores, settings — everything is stored using variables.
A variable is a named reference to a value stored in memory. It allows us to save data and reuse it throughout a program.
Creating Variables
In Python, you create a variable by assigning a value using the equal sign.
# Assigning values to variables
x = 10 # integer
name = "Dataplexa" # string
price = 499.99 # float
Explanation
xstores a whole number.namestores text inside quotes.pricestores a decimal number.- The
=operator assigns the value to the variable. - Python automatically decides the data type.
Displaying Variable Values
Variables remain in memory unless we display them. To see the stored value, we use the print() function.
x = 10
name = "Dataplexa"
print(x) # print number
print(name) # print string
print()sends output to the screen.- Without print(), values stay inside memory only.
Reassigning Variables
Variables can change over time. Python always keeps the most recent value.
x = 10 # initial value
x = 20 # updated value
print(x)
- The original value (10) is replaced by 20.
- Memory now stores only the latest value.
Dynamic Typing
Python is dynamically typed. You do not declare variable types explicitly.
value = 100 # integer
value = "Hello" # now string
value = 3.14 # now float
print(type(value))
- The variable type changes when the value changes.
- Python automatically detects the type.
Multiple Assignment
Python allows assigning multiple variables in one line.
# Assign three values at once
a, b, c = 10, 20, 30
print(a, b, c)
- Values are assigned from left to right.
- This keeps code clean and efficient.
Swapping Variables
Python provides a clean way to swap values without using a third variable.
x = 5
y = 10
# Swap values
x, y = y, x
print(x, y)
- No temporary variable required.
- This is a common Python feature.
Real-World Example: Simple Billing System
Let us simulate a small billing calculation to see variables working together.
# Product information
item_price = 799 # price of one item
quantity = 2 # number of items purchased
tax_rate = 0.18 # 18% tax
# Calculations
subtotal = item_price * quantity
tax = subtotal * tax_rate
total = subtotal + tax
# Output results
print("Subtotal:", subtotal)
print("Tax:", tax)
print("Total:", total)
How This Works
item_pricestores the price.quantitystores the count.subtotalmultiplies price and quantity.taxcalculates tax amount.totalcombines subtotal and tax.- Each variable builds on previous values.
Types of Variables in Python
| Variable Type | Example | Description |
|---|---|---|
| Integer (int) | age = 25 | Stores whole numbers. |
| Float | price = 99.99 | Stores decimal numbers. |
| String (str) | name = "Python" | Stores text values. |
| Boolean (bool) | is_active = True | Stores True or False. |
| List | numbers = [1,2,3] | Stores multiple values in one variable. |
| Dictionary | user = {"name":"Alex"} | Stores key-value pairs. |
In the next lesson, we will explore these data types in detail.
Practice
Variables store values in what?
Quick Quiz
Python automatically detecting type is called?
Recap: You learned how variables store data, how assignment works, how values update, and how variables support real-world calculations.
Next up: Understanding Python Data Types in depth.