Variables and basic data types
Course contents (15 lessons)
- Getting started
- Installing Python 3.13 and running your first script
- Variables and basic data types
- Text, numbers and user input
- Working with strings
- Numbers and arithmetic
- Input and output: talking to the user
- Making decisions and repeating work
- Making decisions with if, elif and else
- Loops: for, while, break and continue
- Collections of data
- Lists and tuples
- Dictionaries and sets
- Functions and modules
- Writing your own functions
- Modules, pip and virtual environments
- Files and errors
- Reading and writing files
- Errors and exceptions
- Final project: expense tracker
- Project: build the expense tracker, part 1
- Project: summaries, search and improvements
A variable is a name for a value
A variable is a label you stick on a value so you can use it later. Think of a small shop. You might keep a price, an item name and a count of how many are in stock. In Python you write that like this:
item = "notebook"
price = 150
in_stock = 12
is_on_sale = False
discount_rate = 0.1
print(item)
print(price)
print(type(price))
print(type(discount_rate))
print(type(is_on_sale))
The = sign here does not mean "equals" like in maths. It means "store the value on the right under the name on the left". Read it as "item becomes notebook".
The basic types
Every value in Python has a type. The type decides what you can do with the value. These four cover most of what you need at the start.
| Type | Meaning | Example |
|---|---|---|
int | Whole numbers | 42, -7, 0 |
float | Numbers with a decimal point | 3.14, 0.1 |
str | Text | "hello", '42' |
bool | True or False | True, False |
The output of the program above is:
notebook
150
<class 'int'>
<class 'float'>
<class 'bool'>
The type() function tells you the type of any value. I use it a lot when something behaves strangely, because the answer is often "that is a string, not a number".
Naming rules
Variable names can contain letters, digits and underscores. They cannot start with a digit and they cannot contain spaces. Names are case sensitive, so Price and price are two different variables.
The Python community uses snake_case: lowercase words joined with underscores. Write total_price, not totalPrice. Pick names that say what the value is. price is better than p. You will thank yourself when you read the code next month.
You also cannot use Python's reserved words, such as if, for, class or True, as variable names.
Variables can change
You can give a variable a new value at any time, even one of a different type. Python does not stop you.
stock = 12
stock = stock - 1 # one sold
print(stock) # 11
stock = "unknown" # allowed, but confusing
print(stock)
Python allows this, but I would avoid it. A variable that holds a number in one place and text in another is hard to read.
Converting between types
Sometimes you have text that looks like a number and you want to do arithmetic with it. Use the conversion functions int(), float(), str() and bool().
quantity_text = "3"
quantity = int(quantity_text)
total = quantity * 150
print(total) # 450
print("Total: " + str(total)) # Total: 450
print(float("2.5") + 1) # 3.5
If the text cannot be converted, Python raises a ValueError. For example, int("abc") and int("3.5") both fail. We will handle that kind of failure properly in the errors lesson.
The most common beginner mistake
This one catches everyone at least once. Joining text and a number with + does not work.
age = 30
print("I am " + age + " years old") # TypeError
Python refuses because it does not know whether you mean arithmetic or joining. Fix it by converting: "I am " + str(age) + " years old". In the strings lesson you will learn f-strings, which are nicer still.
Another one: "5" + "5" gives "55", not 10. Text and numbers behave differently even when they look the same.
None: the value that means nothing
Python has one more value you will meet soon. None means "no value here". You might set discount = None for an item that has no discount yet. Its type is NoneType. It is not zero and it is not an empty string.
Multiple assignment
You can assign several variables in one line, which is handy for related values.
width, height = 4, 5
print(width * height) # 20
Try it yourself
Write a script that stores a product name, a unit price (a float) and a quantity (an int). Calculate the total price in a new variable and print all four values, using str() where you need it. Then print the type() of the total. Is it an int or a float? Change the price to a whole number and see if the answer changes.
item = "notebook"
price = 150
in_stock = 12
is_on_sale = False
discount_rate = 0.1
print(item)
print(price)
print(type(price))
print(type(discount_rate))
print(type(is_on_sale))
This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.
Sign up free I already have an account