Lesson 2 of 34 · 0 of 2 exercises solved

Variables & Data Types

store values so you can use them later. Python is dynamically typed, so you don't declare a type: it works it out from the value you assign.

Variables in Python are created by assignment. There's no need for keywords like var, let, or int. The built-in types cover all common use cases: integers, floats, , , and (Python's null).

# Integer
age = 25
year = 2024

# Float
pi = 3.14159
temperature = -10.5

# String — single or double quotes are equivalent
name = "Alice"
greeting = 'Hello, world'

# Multi-line string
bio = """I'm Alice.
I like Python.
I am a multi-line string."""

# Boolean
is_student = True
has_car = False

# None (the absence of a value)
result = None

# Check a variable's type
print(type(age))         # <class 'int'>
print(type(pi))          # <class 'float'>
print(type(name))        # <class 'str'>
print(type(is_student))  # <class 'bool'>
Python (loads on first run)Ctrl+Enter runs

Python supports multiple assignment in one line, and you can swap variables without a temporary variable.

# Multiple assignment
x, y, z = 1, 2, 3

# Swap without a temp variable
x, y = y, x
print(x, y)   # 2 1

# String formatting — f-strings are the modern way
name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.")

# Expressions work inside f-strings
print(f"Next year I'll be {age + 1}.")

# Type conversion
num_str = "42"
num = int(num_str)       # str -> int
flt = float(num_str)     # str -> float
back = str(num)          # int -> str
Python (loads on first run)Ctrl+Enter runs

Common question

Does a Python variable have a type?

A variable in Python is a name bound to an object. You can rebind it at any time, and the same name can point at objects of different types over the life of a program. The type lives with the object, and Python checks it when an operation runs. That is why like adding a string to a number show up at run time, not when you create the variable.

Two things follow. First, conversion is explicit: int("42") makes a new int object and leaves the string alone. Second, assignment shares instead of copying, so with objects you need to decide when you actually want a copy. (name: str) document intent and let tools check it ahead of time; they do not change any of this.

Source: The Python language : "Every object has an identity, a type and a value." Names are not mentioned. Types belong to objects.

Common mix-ups

Each of these sounds reasonable. Think about why it is wrong, then open the explanation.

  • Since age = 25 and later age = "twenty-five" both work, a variable has no type at all.

  • Because "3" + 4 fails, Python must be secretly declaring types the way C does.

  • Writing y = x copies the value, so changing x afterwards won't affect y.

Practice

The variable age_text holds a number typed by a user, so it is a string. Convert it to an integer, add one, store the result in next_year, and print Next year you will be 18.

Exercise

From text to number

3 checks
age_text = "17"

# Convert, add one, and print the sentence
 
Python (loads on first run)Ctrl+Enter runs
Output
Press Run to try your code, then Check answer to grade it.

Swap the values of a and b using Python's -unpacking trick — no third variable allowed. Then build the string summary with an so it reads a=2, b=1.

Exercise

The swap

3 checks
a = 1
b = 2

# swap them here

summary = ""  # build this with an f-string
print(summary)
 
Python (loads on first run)Ctrl+Enter runs
Output
Press Run to try your code, then Check answer to grade it.

Check yourself

Quick check

What is type(7 / 2) in Python 3?

Quick check

Which of these is the conventional Python name for a variable holding a user's email address?

Finished reading?2 exercises still open. You can come back to them later.

Tip: use the ← and → keys to move between lessons.