› Forums › Python › CS50’s Introduction to Artificial Intelligence with Python › What Is a Set in Python? The Forgotten Data Type Beginners Discover Later
- This topic is empty.
-
AuthorPosts
-
April 27, 2026 at 5:02 am #6460
Many Python beginners first learn these data types:
- Integer (
int) - Float (
float) - String (
str) - List (
list) - Tuple (
tuple) - Dictionary (
dict)
Then one day they see this word:
setAnd ask:
“Wait… is set also a real Python data type?”
Yes — absolutely.
Yes, Set Is a Built-in Python Data Type
type({1, 2, 3})Output:
setSo Python officially recognizes
setjust likelistortuple.
What Is a Set?
A set is an unordered collection of unique values.
{1, 2, 3}Duplicates are automatically removed:
{1, 1, 2, 3}Becomes:
{1, 2, 3}
List vs Tuple vs Set
Type Example Ordered? Mutable? Duplicates? List [1,2,2] Yes Yes Yes Tuple (1,2,2) Yes No Yes Set {1,2,2} No Yes No
Why Sets Are Powerful
Sets are commonly used for:
- Removing duplicates
- Fast membership checking
- Comparing groups
- Mathematical set operations
Examples
1. Remove duplicates
nums = [1,1,2,3] unique = set(nums)2. Membership test
if 5 in {1,2,3,5}: print("Found")3. Intersection
A = {1,2,3} B = {2,3,4} A & BOutput:
{2,3}
Important Beginner Warning
This is an empty dictionary:
{}This is an empty set:
set()Many beginners mix these up.
Why CS50 Uses Sets
In the Degrees project:
names["tom hanks"] = {"158", "999"}A set is ideal because:
- Multiple people may share the same name
- Duplicate IDs should be prevented
- Lookup is fast
Final Thought
Set is one of Python’s most practical data types. It may be introduced later than lists and tuples, but professionals use it constantly.
When you learn sets, your Python thinking becomes sharper and faster.
- Integer (
-
AuthorPosts
- You must be logged in to reply to this topic.
