1- Reverse Indexing: Similar to the lists elements in the tuple can also be accessed starting from the end which is reverse indexing. Reverse indexing starts with -1 which indicates the last element.
So, to access the last element of p_tup: p_tup[-1] can be used and it would give you the last element of the tuple. p_tup[-2] would give you the second from the last, p_tup[-3] third from the last and so on.
2- Main difference of tuples from lists is that they are immutable which means they can’t be changed after they are created. Other than re-creating them you can’t append or remove elements the way you could for the lists.
So, it makes sense to use tuples where you know you won’t change your elements and otherwise it makes more sense to use lists.
.count() method is useful to find out how many occurrences there are in a tuple for a specific element.
In the example below, you can see that integer 5 is counted inside the list lucky_numbers. When we print, we get the output 1, meaning 5 occurs once in the list.
There are some other methods and Python functions we will see in the upcoming lessons. Some of them deserve their own lesson for the sake of keeping this lesson at a reasonable length.
Having Python setup on your computer can expedite the learning experience and help you gain the real life skills in a non-simulation environment. Installing Python can have different nuances that can be confusing for everyone. We compiled a complete tutorial about the best and most convenient Python installing practices Check it out by clicking the button below.
Thank you for visiting our website! We use essential cookies to help us understand and enhance user experience. By clicking “Accept”, you consent to the use of ALL the cookies.