Python tuple

 

TUPLES

Tuples are small collections that can be used to track related but distinct items. Because they are immutable, tuples cannot be changed after they have been created.

You might wonder why Python already uses lists rather than tuples.


There are a few differences between tulips. Tuples, on the other hand, can be used to store a snapshot of data, whereas lists are typically used to store collections of similar items together. Like a list, you can't always change, add to, or take away from them.


What’s in a Tuple?


Tuples appear to be useless, redundant data structures. However, this is merely a misunderstanding. You will be able to distinguish tuples from lists and determine when to use them once you have a solid understanding of them.


In Python, a data structure called a tuple can be used to store object collections. Anything else you want can be in them: strings, numbers, lists, and even other types of tuples! Using regular parentheses and commas to separate the tuples is all that is required to create a tuple.

Uses of tuples

A tuple could be used to store information for a row in a spreadsheet. That data is merely information. Keeping that data up to date or manipulating it doesn't necessarily concern us. A read-only snapshot is all we need.


One of Python's most unique features is tuples, which are intriguing and powerful data types. Lists and dictionaries can be represented in the majority of other programming languages, but tuples are only found in a small number of them. Make the most of them.


Difference between tuples and lists:


Lists can be changed, whereas tuples cannot.


Although mutability is a complicated subject that requires its own article, the fundamental concept is as follows: If an object's value can change after it is defined, it is mutable.


To put it another way, you can change the values of an immutable object but not those of a mutable object.

Tuple Indexing

We should be aware that indexing is used to access any element in a tuple prior to learning about tuple slicing. In addition, the indexing is straightforward and begins at index zero, just like in lists.


Tuple Slicing

Slicing can be used with tuples in the same way that it is used with strings and lists. Most of the time, multiple slicing is used to get a variety of things. Additionally, we use the slicing operator for tuple slicing.


The syntax [start:stop: step]. Furthermore, the "step" part need not be mentioned. If we do not mention the step part, the compiler will default consider it to be 1.

Nested Tuples


A tuple that is written inside another tuple is called a nested tuple in Python. Consider a tuple with seven components as shown below.


tup = ( 10, 20, 30, 40, 50, 60, (100, 200, 300)) This last element is referred to as a nested tuple because it is contained within another tuple.


Using the tuple name and index value, you can get the nested tuple with the elements 100, 200, and 300.


Conclusion


This tutorial went over how to make tuple objects and many other Python tuple-related topics like indexing, slicing, zipping, unpacking, and so on. We also talked about the difference between lists and tuples.


Our code will definitely become more robust and efficient if we use Python tuples correctly.


I hope that my article was beneficial to you. To learn more, click the link here.


Comments

Popular posts from this blog

Web activities in azure data factory

DevOps engineer skill

DevOps deployment tools