Python Basics: Immutable Sequence Type & Tuples 1

Sequence type: a data type in Python which is able to store more than one value or less than one value (as a sequence my be empty), and these values can be sequentially browsed element by element (like a list).

Mutability: a property of any of Python’s data that describes its readiness to be freely changed during program execution.

Immutable sequence type: one of the two kinds of mutability (the other kind is mutable sequence type like a list), which cannot be freely updated or modified. For example, a sequence type that can only be read and cannot be appended new element to it, or remove elements from it. The sequence would require a new list or whatever to contain all the elements in the old list plus the new element you want to append.

So tuple is an immutable sequence type, it can behave like a list, but it mustn’t be modified in situ (in position)

Tuple declaration:

tuple1 = (1,2,3,4,5)
 tuple2 = 1., .5, .25, .125
 emptyTuple = ()
 Note: each tuple element can be of a different type.

 //To declare a one element tuple, you must end the value with a comma
 oneElementTuple1 = (1,)
 oneElementTuple2 = 1.,
 Note: removing the commas won't affect much the program in any 
 syntactical sense, but the result will be two single variables, not tuples.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.