There are three basic sequence types in Python: lists, tuples, and range. In this post, I will be writing about “tuples”. You will learn, what tuple is, how to create a tuple, how to slice a tuple, indexing in a tuple, and how to delete a tuple.
Prerequisites:
Tuples are like lists but tuples, unlike lists, can’t be changed. They are immutable. Tuples are ordered as lists. Tuples use brackets, while lists use square brackets. As lists, values in tuples are separated by a comma.
Let´s create our first tuple, shall we?
Code:
>>>tup = ("Techsody", "rulez", 1, 2, 3,) # we create tuple with this command.
>>>print(type(tup)) # we are testing if it is really a tuple.
Output:
<class 'tuple'>
Let us create an empty tuple:
Code:
>>> tup = ()
Let´s create a tuple with only 1 item in it.
Code:
>>>tup = (50,)
Did you notice that I put a comma? If I wouldn´t put it and write “50” Python would think it is an integer. Please try it with a string, a complex, and a floating number but without the comma. Then check the type.
Indexing
As we did it before with lists, we can use the indexing function with tuples.
Code:
>>>tup = ("Techsody", "rulez", 1, 2, 3)
>>>print(tup[1])
Output:
(3)
Slicing
We can of course use negative indexing too.
Code:
>>>tup = ("Techsody", "rulez", 1, 2, 3)
>>>print(tup[-1])
Output:
('rulez')
Tupples are immutable so you can´t change or add or remove. However, what you can do is to slice it.
Code:
>>>tup = ("Techsody", "rulez", 1, 2, 3)
>>>print(tup[1:3])
Output:
('rulez', 1)
Deleting
Another thing you can do is to delete tuple completely.
Code:
>>>tup = ("Techsody", "rulez", 1, 2, 3) # here we create a tuple
>>>print(tup) # we print the tuple to confirm that we created the tuple
>>>del tup # we delete the tuple here
>>>print(tup) # and we confirm that we deleted and there is no more a tuple
Output:
('Techsody', 'rulez', 1, 2, 3) # this is the output for the first “print command”
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-226e9902c831> in <module>
2 print(tup)
3 del tup
----> 4 print(tup)
NameError: name 'tup' is not defined
Changing Tuples
I know I said tuples are immutable therefore they are not changeable and it is correct but, if we have a mutable list, in our tuple, then what happens?
Code:
>>> tup = ("Techsody", "Rulez", [1, 2, 3]) # we created a tuple that contains a list.
>>>print(tup) # we are printing to the screen to confirm our tuple
Now, let's add something to our list:
Code:
>>>tup[2][0] = 7 # here I am using indexing to select my list and again using indexing to choose where to add what I want to replace. In this scenario, I am replacing “1” in my list with “7”.
Output:
('Techsody', 'rulez', [1, 2, 3]) # printing original tuple to the screen
('Techsody', 'rulez', [7, 2, 3]) # printing after changing first element of the list in the tuple.
As you see now we have successfully replaced “1” with “7”. Please try it on your computer and again do not copy-paste so, you learn.
There are more than you can do with tuples, for now, I will stop here. Too much information in the short term means no information at all. So take it slow in general and practice. I can hear you are saying I want to start coding and I can handle more but the faster you learn the faster you forget.