Lesson 19 of 55
15 mins readPython Union and Intersection Types
In Plain English
Union types allow a value to be one of several types, while intersection types merge multiple types together into a single combined type.
Core Rules to Remember

Union (|): Value can be any of the union member types.

Intersection (&): Value must satisfy all combined type requirements simultaneously.
Live Interactive Example
Hit Run Code to see it liveCombining Types with Unions and Intersections
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: printId accepts string and number, while post contains both createdAt and author.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Combine User Account Capabilities
Define type Admin = { role: "admin"; permissions: string[] }.
Define type Profile = { username: string }.
Create type SuperUser = Admin & Profile and initialize root with username "root", role "admin", and permissions ["all"]. Print username and role.
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.