1️⃣ x = (5)
Even though it has parentheses, this is NOT a tuple.
Python treats it as just the number 5 because there is no comma.
So Python interprets it as:
x = 5
Therefore:
type(x) → int
2️⃣ y = (5,)
Here we added a comma.
In Python, the comma creates the tuple, not the parentheses.
So this becomes a single-element tuple.
y → (5,)
Therefore:
type(y) → tuple
3️⃣ Final Output
(<class 'int'>, <class 'tuple'>)
Key Rule (Very Important)
A comma makes a tuple, not parentheses.
Examples:
a = 5
b = (5)
c = (5,)
d = 5,
print(type(a)) # int
print(type(b)) # int
print(type(c)) # tuple
print(type(d)) # tuple


0 Comments:
Post a Comment