Tutorials / Python from Scratch / Variables and basic data types

Variables and basic data types

13 min read python
Course contents (15 lessons)
  1. Getting started
  2. Installing Python 3.13 and running your first script
  3. Variables and basic data types
  4. Text, numbers and user input
  5. Working with strings
  6. Numbers and arithmetic
  7. Input and output: talking to the user
  8. Making decisions and repeating work
  9. Making decisions with if, elif and else
  10. Loops: for, while, break and continue
  11. Collections of data
  12. Lists and tuples
  13. Dictionaries and sets
  14. Functions and modules
  15. Writing your own functions
  16. Modules, pip and virtual environments
  17. Files and errors
  18. Reading and writing files
  19. Errors and exceptions
  20. Final project: expense tracker
  21. Project: build the expense tracker, part 1
  22. 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.

TypeMeaningExample
intWhole numbers42, -7, 0
floatNumbers with a decimal point3.14, 0.1
strText"hello", '42'
boolTrue or FalseTrue, 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.

python
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