The idea
A program that only prints fixed text isn't very useful. Real programs remember things: a customer's name, a price, how many items are in stock. A variable is a name that refers to a value, so you can use that value later without typing it again.
shop_name = "Toko Kita"
price = 95000
print(shop_name)
print(price)
Toko Kita
95000
The = sign here does not mean "equals" like in maths. It means assign: "make the name on the left refer to the value on the right". Read price = 95000 as "price is now 95000".
Think of a variable as a name tag stuck on a value. The value lives in the computer's memory; the tag lets you find it. You can move the tag to a different value at any time.
Assigning and reassigning
You can point a variable at a new value whenever you like. The old value is simply forgotten:
stock = 80
print(stock)
stock = 75
print(stock)
80
75
Python always works out the right-hand side first, then attaches the name. That's why this line, which looks impossible in maths, is completely normal in code:
stock = 75
stock = stock - 5
print(stock)
70
Step by step: Python reads stock - 5, looks up stock (75), calculates 70, and then makes stock refer to 70.
If you use a name before you've assigned it, Python stops with an error:
print(discount)
NameError: name 'discount' is not defined
Naming variables well
Python has a few hard rules for names:
- Use letters, digits and underscores only:
total_priceworks,total-pricedoesn't (Python reads the dash as a minus sign). - Don't start with a digit:
item2is fine,2nd_itemis not. - Names are case-sensitive:
price,PriceandPRICEare three different variables. - Don't use Python's keywords (
if,for,class…) as names.
And a few conventions that every professional follows:
- Use snake_case: all lowercase, words joined by underscores, like
unit_priceandcustomer_city. - Make names say what they mean.
monthly_revenuebeatsmr,xordata2. - Don't reuse the names of built-in functions such as
print,list,sumortype.sum = 10works, but then the realsum()function stops working for the rest of your program. - Values that should never change, like a tax rate, are written in
UPPER_CASE:VAT_RATE = 0.11. Python won't stop you changing them, but the capitals tell other programmers "don't".
You'll read a variable name many more times than you type it. A slightly longer name that explains itself saves time every single time someone reads the code.
The four everyday types
Every value in Python has a type that decides what you can do with it. You'll use these four constantly:
| Type | Name | Examples | Used for |
|---|---|---|---|
str | string | "Ayu", 'Jakarta', "" | text |
int | integer | 95000, -3, 0 | whole numbers |
float | floating-point number | 4.7, 0.11, -2.5 | numbers with decimals |
bool | boolean | True, False | yes/no facts |
The type() function tells you the type of any value:
product = "Arabica Gayo 250g"
price = 95000
rating = 4.7
in_stock = True
print(type(product))
print(type(price))
print(type(rating))
print(type(in_stock))
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
A few details trip people up:
TrueandFalsemust start with a capital letter.trueis just an undefined name."95000"(with quotes) is a string, not a number. It looks like a number to you, but Python treats it as text, and you can't do maths with it until you convert it. More on that in the numbers module.95000and95000.0are different types: the first is anint, the second afloat.
There's one more value you'll meet everywhere: None, which means "no value". It has its own type, NoneType, and you'll use it to say "nothing here yet", for example discount = None.
Types belong to values, not names
In some languages you declare a variable's type up front. In Python, the value has a type and the name just points at it, so the same name can point at different types over time:
code = 101
print(type(code))
code = "A-101"
print(type(code))
<class 'int'>
<class 'str'>
This flexibility is handy, but changing what kind of thing a variable holds makes code confusing. Keep each variable to one type and one meaning.
Assigning several variables at once
Python lets you assign multiple names in one line, matching values left to right:
city, orders = "Bandung", 12
print(city, orders)
Bandung 12
This also gives you a neat trick for swapping two values without a helper variable:
first, second = "Ayu", "Budi"
first, second = second, first
print(first, second)
Budi Ayu
Python builds the pair on the right ("Budi", "Ayu") before assigning anything, so nothing gets overwritten too early.
Why this matters for clean code
Variables are how code tells a story. total = price * quantity explains itself; t = p * q makes the reader decode it. Choose the right type for the job (numbers for maths, strings for text, booleans for yes/no) and name every variable after what it means. Most "clean code" advice starts right here.
Recap
name = valueassigns: the name now refers to the value. The right side is worked out first.- Use snake_case, descriptive names, and
UPPER_CASEfor constants. Don't shadow built-ins likesumorlist. - The four everyday types are
str,int,floatandbool.type()tells you which one you have. Nonemeans "no value".a, b = b, aswaps two variables in one line.