A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Here is an example of how to build a tuple.
mytuple = ('Modeling', 'Simulation', 2017, 2018) # notice this is () and not [] that is the difference between a tuple and list print(mytuple[0]) # prints 1 print(mytuple[1]) # prints 2 print(mytuple[2]) # prints 3 print(mytuple[3]) # prints 4 print("\n" + str(type(mytuple)) + "\n") print(mytuple) print("") print(mytuple[1:3]) print(mytuple[:3]) print(mytuple[2:])
Just press Run!
I snuck in mytuple[1:3] which only looks at the 2nd,3rd value (indexing starts at 0). Here :3 printed values up to 3 so indexes (0,1,2). Also, 2: printed indexes after 2 (2,3). These are all example of how to index into tuples and lists. And again, I sneakly snuck str(type(mytuple)) into this example - as an example to cast one type of object to a string to get it to print cleanly.


Can you update a tuple? No. - The only way is to create a new one. Tuples are immutable which means you cannot update or change the values of tuple elements. You are able to take portions of existing tuples to create new tuples.
mytuple = ('Modeling', 'Simulation', 2017, 2018) anothertuple = ('To', 'Bodly', 'Go') thirdtuple = mytuple + anothertuple; print(thirdtuple) # Following action is not valid for tuples # mytuple[0] = 100;
Just press Run!



Exercise:

In this exercise, you will need to create a simple tuple.
mytuple = ()#add code print(mytuple) mytuple = (1.0,-150.3,20.45,300.45) print(mytuple) test_output_contains("(1.0, -150.3, 20.45, 300.45)", no_output_msg= "Make sure that you have added the values above to a tuple () correctly.") success_msg("Great Job!")
Look at the top examples to access and add to tuples!