This was a code I stumbled upon to:
tuple1 = 10, 20
tuple2 = (10, 20)
print(type(tuple1))
print(type(tuple2))
This was a code I stumbled upon to:
tuple1 = 10, 20
tuple2 = (10, 20)
print(type(tuple1))
print(type(tuple2))
That's how the syntax for tuples works. The parentheses are optional when creating a tuple.
... on output tuples are always enclosed in parentheses, so that nested tuples are interpreted correctly; they may be input with or without surrounding parentheses, although often parentheses are necessary anyway (if the tuple is part of a larger expression).
When you use comma-separated expressions on the right-hand side of an assignment, this is called “tuple packing”. It creates a tuple; parentheses are not required for this:
tuple1 = 10, 20
Similarly, if you give comma-separated names on the left-hand side of an assignment, “tuple unpacking” is performed:
int1, int2 = tuple1
This is how multiple assignment works – It’s actually tuple packing combined with tuple unpacking:
int1, int2 = 10, 20
You can use this to swap the values of two variables without having to use a temporary variable:
int1, int2 = int2, int1