PYTHON · Learning guideTheory · Labs · Knowledge checks
Chapter 03

Basic data types and collections

Numbers, booleans and strings

Python includes int, float, bool and str. Variables are names bound to objects; Python is dynamically typed, not weakly typed in the usual sense.

age = 25
price = 19.95
active = True
name = "Ada"
print(f"{name}: {age}, active={active}")
Hands-on lab: Inspect types with type() and convert input with int().

Lists, tuples, sets and dictionaries

Lists are mutable ordered sequences, tuples are immutable sequences, sets hold unique elements, and dictionaries map keys to values.

names = ["Ada", "Grace"]
point = (3, 5)
tags = {"python", "docker", "python"}
user = {"name": "Ada", "level": 2}
print(names[0], point, tags, user["level"])
Hands-on lab: Add a list element and update a dictionary.

Conversion and validation

input() returns a string. Convert values explicitly and handle invalid input.

value = input("Enter an integer: ")
try:
    number = int(value)
    print(number * 2)
except ValueError:
    print("Not an integer")
Hands-on lab: Try valid and invalid values.