0
0
Pythonprogramming~10 mins

Set creation in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Set creation
Start
Use curly braces {} or set()
Add elements separated by commas
Duplicates removed automatically
Set created with unique elements
End
This flow shows how a set is created in Python by using curly braces or the set() function, adding elements, and automatically removing duplicates.
Execution Sample
Python
my_set = {1, 2, 2, 3}
print(my_set)
Creates a set with some duplicate numbers and prints the set showing only unique elements.
Execution Table
StepActionElements AddedSet StateOutput
1Start set creationNoneset()None
2Add element 11{1}None
3Add element 22{1, 2}None
4Add element 2 (duplicate)2{1, 2}None
5Add element 33{1, 2, 3}None
6Print setNone{1, 2, 3}{1, 2, 3}
7EndNone{1, 2, 3}None
💡 All elements processed; duplicates ignored; set printed with unique elements
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
my_setset(){1}{1, 2}{1, 2}{1, 2, 3}{1, 2, 3}
Key Moments - 2 Insights
Why does the set not include duplicate '2'?
Sets automatically remove duplicates, so when the second '2' is added (see step 4 in execution_table), it does not change the set.
Can we create an empty set with {}?
No, {} creates an empty dictionary. To create an empty set, use set() function.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what happens when adding the duplicate element '2'?
AThe set ignores the duplicate '2' and stays the same
BThe set adds another '2', so it has two '2's
CThe set removes all '2's
DThe set throws an error
💡 Hint
Check the 'Set State' column at step 3 and 4 in execution_table; the set remains {1, 2}
At which step does the set first contain three unique elements?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Set State' column in execution_table; at step 5 the set becomes {1, 2, 3}
If we replace {1, 2, 2, 3} with set([1, 2, 2, 3]), what will be the output?
A{1, 2, 2, 3}
B[1, 2, 3]
C{1, 2, 3}
DError
💡 Hint
Both {} and set() create sets that remove duplicates, so output is unique elements in curly braces
Concept Snapshot
Set creation in Python:
- Use curly braces {} with elements separated by commas
- Or use set() function with iterable
- Duplicates are automatically removed
- Empty set must be created with set(), not {}
- Sets store unique, unordered elements
Full Transcript
This visual execution shows how Python creates a set using curly braces with elements including duplicates. Step by step, elements are added to the set. When a duplicate element is added, it is ignored, so the set only keeps unique elements. The variable 'my_set' changes from empty to containing unique numbers. The final print shows the set with unique elements. Important points include that {} creates a set with elements, but {} alone creates an empty dictionary, so use set() for an empty set. Sets do not allow duplicates and automatically remove them during creation.