DataLens

Variables and basic data types

Give values names with variables, and meet Python's four everyday types: str, int, float and bool.

Level: Foundations · 9 min read

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

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".

Analogy

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:

And a few conventions that every professional follows:

Clean code

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:

TypeNameExamplesUsed for
strstring"Ayu", 'Jakarta', ""text
intinteger95000, -3, 0whole numbers
floatfloating-point number4.7, 0.11, -2.5numbers with decimals
boolbooleanTrue, Falseyes/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:

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

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

Next: Working with strings and f-strings →