Explanation:
๐น Line 1: Create a Set
s = {1}
Python creates a set containing one element.
Current set:
s = {1}
Memory:
s
│
▼
{1}
๐ A set is:
Unordered
Mutable
Stores only unique values
๐น Line 2: Call print()
print(s.pop(), s)
Before printing, Python first evaluates:
s.pop()
๐น Step 1: Execute pop()
s.pop()
pop() removes one element from the set and returns it.
Since the set contains only one element:
{1}
Python removes:
1
Returned value:
1
Now the set becomes:
set()
which is an empty set.
Memory after pop():
Before
{1}
↓
pop()
↓
{}
๐น Why Does Python Show set() Instead of {}?
Many beginners expect:
{}
❌ That's incorrect.
In Python:
{}
creates an empty dictionary, not an empty set.
Example:
print(type({}))
Output:
<class 'dict'>
To create an empty set, Python uses:
set()
Example:
print(type(set()))
Output:
<class 'set'>
๐น Step 2: Evaluate s
After pop(), the variable s now refers to:
set()
Current value:
set()
๐น Step 3: Execute print()
Now Python has two values:
First:
1
Second:
set()
So it executes:
print(1, set())
Output:
1 set()

0 Comments:
Post a Comment