Similar to Lists, a Tuple is a sequence of objects. However, the difference is that tuples are immutable. This means that you cannot change the value of elements in a Tuple. Another difference between a tuple and a list is that you create a tuple using rounded braces ( ). But you create a list using square braces [ ].
We would then cover the following
- Creating a Tuple
- Accessing Elements of a Tuple
- Trying to Modify a Tuple
- Basic Tuple Operations
- Indexing and Slicing in a Tuple
- Tuple Functions
1. Creating a New Tuple
The interesting thing is that if you can use lists, then you can use tuples as well. To create a new tuple, simply use square braces and specify the list of items inside. Then assign it to a variable.
For example, the code below creates 3 tuples.
tupNames = ("Osoo", "Adii", "Jackie" "Ottie", "Haty", "Kany") tupScores = (34, 56, 98, 78, 84, 99.9) mytup = ("A", 38, "Budapest", "2018", "Hungary")
Notice that a tuple can contain a mixture of different data type. As you can see in the last tuple.
Also, you can create an empty tuple. You do this using just a pair of braces with nothing inside. This is shown below
tup3 = ()
Then you can create a tuple with a single element. To do that, you must include comma after the element even is there is just one element.
This is shown below
tup2 = (34,)
2. Accessing Elements of a Tuples
You can access individual elements in a list. You do this using the indexes. The index helps you access the value of the element in that index.
Using the lists we created above:
- tupnames would give Osoo
- tupnames would give Jackie
- tupscores would give 84
I recommend you try these yourself.
3. Trying to Modify a Tuple
This is where tuples differ from list. So you cannot really modify the elements of a tuple. Why? Because a tuple is immutable. Therefore the code below would result in an error. Notice that this is true even if you are trying to assign the same value to the same element.
tupNames = "Osondu Mills" tupNames = "Adii"
Also, you cannot delete an element from a tuple. However, you can delete the entire tuple using the del command. Watch the video to see how it it used.
4. Basic Tuple Operations
Just like lists, tuples support the + and the * operators. You use the + operator (concatenation) to combine two tuples. Then the * operator (repetition) is used to repeat a tuple a given number of times. The table below shows the basic operations that can be performed in a tuple
|len([1, 2, 3])||3||Length|
|[2, 3, 4] + [5, 6, 7]||[2, 3, 4, 5, 6, 7]||Concatenation|
|[‘Kedu’] * 2||[‘Kedu’, ‘Kedu’]||Repetition|
|10 in [10, 20, 30]||True||Membership|
|for x in [10, 20, 30]: print x,||10 20 30||Iteration|
5. Indexing, Slicing and Matrices in Tuples
Indexing and slicing work the same way like in lists. Note that slicing means getting a section of a list. Matrices applies to 2-dimensional lists covered in another lesson.
The table below shows how slicing and indexing works in tuples.
For example, if we have the list below:
places = ["Enugu", "Owerri", "London", "Lagos"]
Then the table below shows how you can perform indexing and slicing operations
|places||London||Offsets start from zero|
|places[-2]||London||Negative: count from right|
|places[1:]||[‘Owerri’, ‘London’, ‘Lagos’]||Slicing gets a section|
6. Tuple Function
Tuples include a number of functions that can help you perform various operations. You can find these functions in the table below.
|SN||Function and brief description|
|1||cmp(tuple1, tuple2): This is no longer available in Python 3|
|2||len(tuple): Returns the total length of the tuple. That is number of elements in a tuple|
|3||max(tuple): Returns item from the tuple with max value( i.e largest element)|
|4||min(tuple): Returns item from the tuple with min value (smallest element)|
|5||tuple(seq): Converts from a list into tuple.|