0
About create tuples
Why can I create tuple in this way, tup = (777) but not in that way? tup = tuple(777)
3 Antworten
+ 1
your first example does not create a tuple.
your second example gives an error because we must pass an iterable to tuple().
some examples:
x = (7) # int
print(x, type(x))
x = (7,)
print(x, type(x)) # tuple
x = tuple((7,))
print(x, type(x)) # tuple
x = tuple(7) # error as constructor expects an interable
print(x, type(x))
+ 1
Hey! 😃
I see you’re trying to create a tuple in Python.
Here’s the thing:
tup = (777) is NOT a tuple, it's just the number 777.
Check it: print(type(tup)) # <class 'int'>
To make a tuple with one item, you have two ways:
1️⃣ Using a comma inside parentheses:
tup = (777,)
2️⃣ Using tuple() on an iterable (like a list):
tup = tuple([777])
Tip: In Python, parentheses alone don’t make a tuple—**the comma does!**
Good luck with learning Python, it’s an awesome language! 👍
0
tuples always have to have the comma. even 1 element needs a comma. no comma, no tuple.