Python · Module 1 · Lesson 21 min read

Variables & Data Types

Variables

A variable is a name that points to a value. You create one with =, and you never declare a type: Python works it out from the value (dynamic typing).

Built-in types you'll use every day

TypeExampleMeaning
int42whole number (no size limit)
float3.14decimal number
str"Asha"text
boolTrue / Falseyes / no
NoneTypeNone"no value yet"

Checking types

  • type(x) tells you the type.
  • isinstance(x, int) checks a type in conditions. Prefer it over type(x) == int.

Naming rules

  • Letters, digits and _; it can't start with a digit.
  • Use snake_case: total_marks, is_logged_in.
  • Constants are written in CAPITALS by convention: MAX_USERS = 100.

Variables can change type

x = 5 then x = "five" is allowed, which is flexible but can hide bugs.

💡 Multiple assignment: a, b = 1, 2 and swapping a, b = b, a.

▶️ Runs in the app.

Example

Python
name = "Alice"
age = 30
height = 5.6
is_student = False
middle_name = None

print(type(age))               # <class 'int'>
print(type(height))            # <class 'float'>
print(isinstance(age, int))    # True

# Multiple assignment and swapping
a, b = 1, 2
a, b = b, a
print(a, b)                    # 2 1

# Big integers just work
print(2 ** 100)                # 1267650600228229401496703205376