All coding lessonsLesson 2 of 10

Variables and Data Types

Storing values, naming things and Python's core types

A variable is a name that refers to a value. You create one with the assignment operator =. Python is dynamically typed: you never declare the type, the value carries it, and a name may later point to a value of a different type.

age = 15
height = 1.72
name = "Laurin"
is_student = True

print(type(age))       # <class 'int'>
print(type(height))    # <class 'float'>
print(type(name))      # <class 'str'>
print(type(is_student))# <class 'bool'>

The four core built-in types

  • int — whole numbers of unlimited size: 0, -7, 12345678901234567890
  • float — decimal numbers stored in binary: 3.14, -0.5, 2.0
  • str — text, in single or double quotes: 'hi', "hi"
  • bool — the truth values True and False (capitalised!)

Converting between types

user_input = "42"
number = int(user_input)   # 42 as an integer
print(number + 8)          # 50

print(str(3.5) + " metres")
print(float("2.5") * 2)    # 5.0
print(bool(0), bool(""), bool(3))  # False False True

Trying to add a string and a number directly raises a TypeError. Python refuses to guess what you meant — you must convert explicitly.

F-strings: the modern way to build text

name = "Ada"
score = 96.5
print(f"{name} scored {score:.1f} points")
# Ada scored 96.5 points

Naming rules and style

  • Names may contain letters, digits and underscores, but cannot start with a digit.
  • Names are case-sensitive: score and Score are different variables.
  • Use snake_case for variables and functions: total_price, not TotalPrice.
  • Constants that never change are written in UPPER_CASE by convention.
Ask Teacher Smith questions about this Python lessonOpens your AI tutor with a question about “Variables and Data Types” →