Tuples

 Tuples are like lists, but we cannot modify their values. The initial values are the values that will stay for the rest of the program. An example where tuples are useful is when a program needs to store the names of the months of the year.


To declare a tuple, we write tupleName = (initial values). We use round brackets when declaring a tuple. Multiple values are separated by a comma.


As an example: monthsOfYear = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec")


We access individual values of a tuple using their indexes, just like with a list.

Hence, monthsOfYear[0] = "Jan"monthsOfYear[-1] = "Dec".

No comments:

Post a Comment