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:])
Run
! 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.
mytuple = ('Modeling', 'Simulation', 2017, 2018)
anothertuple = ('To', 'Bodly', 'Go')
thirdtuple = mytuple + anothertuple;
print(thirdtuple)
# Following action is not valid for tuples
# mytuple[0] = 100;
Run
!
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!")