0

About create tuples [Solved]

Why can I create tuple in this way, tup = (777) but not in that way? tup = tuple(777)

20th Sep 2025, 8:39 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
3 Réponses
+ 3
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))
20th Sep 2025, 10:46 PM
Lisa
Lisa - avatar
+ 3
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! 👍
20th Sep 2025, 10:57 PM
M G
+ 3
tuples always need to have the comma. even 1 element needs the comma. no comma, no tuple. tup = 1, print(type(tup)) # tuple tup = 1,2,3 print(type(tup)) # tuple
21st Sep 2025, 12:07 AM
Bob_Li
Bob_Li - avatar