Python 2.7 – Fundamentals – Collections – Tuples

Tuples are very similar to lists, except that they are immutable. In other words, once a tuple is created you cannot modify it in any way. Tuples are enclosed in normal brackets and the items separated by commas. Items can be of mixed data types. Some examples are shown below:

 

 

 

 

 

Operators

The following operators are available for tuples:

  1. [] – index operator.
  2.  + – concatenation
  3. * – repetition
  4. [:]  – slicing
  5.  in – membership
  6. len – length (this is a function not really an operator)

We will look at the above in more detail.

[] – index operator

Tuples are collections of data types. So to access individual items in a tuple we use the index operator. The lowest index value is zero which represents the first item and the highest index value is the length of the tuple minus one.

 

 

 

 

Trying to access an index beyond the length of the tuple gives an error.

+ – concatenation

This operator lets you join two tuples together.

 

 

 

 

*- repetition

This repeats the tuple x number of times.

 

 

 

[:] slicing

The index operator lets you extract one item from the tuple. The slicing operator lets us extract multiple items from a tuple. The operator takes two numbers [x:y] where x is the starting index and y is the ending index. Note that the ending index is not inclusive eg. if you try print name[1:3] it will extract character at index 1 and then stop at index 2.

The slicing operator has various permutations and combinations as shown below:

  • [x:y] – extract from x and stop before y
  • [x:] – extract from x till the rest of the string
  • [:y] – extract from the first item till y
  • [-x:] – extract x item from the end
  • [:-y] – extract from the first item and stop before y
  • [:] – no x and y . Extract the whole tuple

 

 

 

 

 

in – membership

The in operator returns True if an item is in the tuple else False

 

 

 

 

len – length

The len function returns the length of a tuple

 

 

 

lists vs tuples

Seeing the similarities between tuples and lists, one would want to ask when a tuple should be used and when a list should be used. The general consensus is that tuples should be used when you just want a container to store items, specially items which will remain constant and do not need to change. lists can also do the same thing but they will allow the altering of the items within the list. Since lists have more functionality than tuples, tuples are lightweight in comparison and process faster than lists. lists are best used when you to work with a collection of items which need changing in terms of addition, updating and removal of items.

The next collection we will examine are sets .

Save

Save

Save

1 Trackback / Pingback

  1. Python 2.7 – Fundamentals – Collections – Lists – Truelogic Blog

Leave a Reply

Your email address will not be published.


*


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