Codeskill

Learn to code, step by step

Variables and Data Types in Python: The Fundamentals

Variables and data types in Python. How to store values, what types exist, and how to work with them.

What are variables?

A variable is a name that holds a value. You store something, retrieve it later, or change it. Think of it as a labelled box – the label is the variable name, the contents are the data.

Creating variables in Python

Python does not ask you to declare a type upfront. It is dynamically typed – the interpreter works out the type from the value you assign:

greeting = "Hello, Python world!"
counter = 10
pi = 3.14
is_active = True

greeting is a string, counter an integer, pi a float, and is_active a boolean.

Understanding data types

Data types classify the kind of value you are working with. The common built-in types:

  • String (str): text. Single quotes, double quotes, or triple quotes all work.
name = "John"
description = 'Python blogger'
multiline_string = """This is a multi-line
string in Python."""
  • Integer (int): whole numbers, no decimal point.
age = 30
year = 2021
  • Float (float): numbers with a decimal point.
price = 19.99
weight = 65.5
  • Boolean (bool): either True or False.
is_member = True
has_license = False

Manipulating variables

Variables are not just for storage. You can do arithmetic with numbers:

a = 5
b = 2
sum = a + b
difference = a - b
product = a * b
division = a / b

Strings can be joined together:

first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
greeting = "Hello, " + full_name

Dynamic typing in Python

You can reassign a variable to a different type. Flexible, but easy to confuse yourself if you are not paying attention:

x = 100      # x is an integer
x = "Python" # x is now a string

Lists and tuples

Beyond basic types, Python has collections for storing multiple items.

  • List: ordered and mutable – you can change it after creation.
colors = ["red", "green", "blue"]
colors.append("yellow")
  • Tuple: ordered but immutable – fixed once created.
dimensions = (200, 50)

Dictionaries

A dictionary stores key-value pairs – useful when you need to look things up by name:

person = {"name": "Alice", "age": 30}
print(person["name"])  # Outputs: Alice

Variables and data types turn up in almost every Python program you will write. Have a play with the examples above – change values, mix types, see what happens.

PreviousHello, Python! Writing Your First Python Program