You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
properties: ordered, iterable, immutable, can contain multiple data types
create a tuple
digits= (0, 1, 'two') # create a tuple directlydigits=tuple([0, 1, 'two']) # create a tuple from a listzero= (0,) # trailing comma is required to indicate it's a tuple
examine a tuple
digits[2] # returns 'two'len(digits) # returns 3digits.count(0) # counts the number of instances of that value (1)digits.index(1) # returns the index of the first instance of that value (1)
elements of a tuple cannot be modified
digits[2] =2# throws an error
concatenate tuples
digits=digits+ (3, 4)
create a single tuple with elements repeated (also works with lists)
(3, 4) *2# returns (3, 4, 3, 4)
sort a list of tuples
tens= [(20, 60), (10, 40), (20, 30)]
sorted(tens) # sorts by first element in tuple, then second element# returns [(10, 40), (20, 30), (20, 60)]
tuple unpacking
bart= ('male', 10, 'simpson') # create a tuple
(sex, age, surname) =bart# assign three values at once```python