Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Why is tuple() listed as a way of creating a tuple. Isn't the = sign necessary? e.g tuple = (1,2,3)

This question was in the quiz and the answer was not what I expected.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Good question! In the twoples quiz, it asks:

Quiz Question _n_ of 5

What creates a tuple?
Choose the correct answer below:

Skip Quiz Review Video  Get Help Next Question 
A   tuple()
B   A comma between two bits of data
C   ( )

The are some semantic points to clarify first.

A tuple is an object type. Specifically, a immutable container, that is, the container or collection of objects can not change once created. Though if one of the objects in the container is a mutable object, that object's contents may change.

Creating a tuple is not the same as assigning a reference to the created tuple. This reference assignment is done using an equal sign.

Thought not always necessary, using the class constructor tuple() will explicitly create a tuple in the same fashion the constructor list() can be used to create a list.

tuples are created all the time by simply using a comma. For example, look at the statement

b, a = 1, 2

An assignment using equal can only assign one object to one reference. The single object on the left "b, a" is a tuple with two references "a" and "b". The left side tuple is expecting to receive two values from the right side of the equation. The right side is also a tuple consisting of two int values.

The constructor tuple() can be used to create a new immutable object from a mutable one:

a_list = [1, 2, 3]
a_tuple = tuple(a_list)

Post back if you need more help. Good luck!!!