In Python, a tuple
is a built-in data type that allows you to create immutable sequences of values. The values or items in a tuple can be of any type. This makes tuples pretty useful in those situations where you need to store heterogeneous data, like that in a database record, for example.
Through this tutorial, you’ll dive deep into Python tuples and get a solid understanding of their key features and use cases. This knowledge will allow you to write more efficient and reliable code by taking advantage of tuples.
In this tutorial, you’ll learn how to:
- Create tuples in Python
- Access the items in an existing tuple
- Unpack, return, copy, and concatenate tuples
- Reverse, sort, and traverse existing tuples
- Explore other features and common gotchas of tuples
In addition, you’ll explore some alternative tools that you can use to replace tuples and make your code more readable and explicit.
To get the most out of this tutorial, you should have a good understanding of a few Python concepts, including variables, functions, and for
loops. Familiarity with other built-in data structures, especially lists, is also a plus.
Get Your Code: Click here to download the free sample code that shows you how to write more readable code with tuples in Python.
Getting Started With Python’s tuple
Data Type
The built-in tuple
data type is probably the most elementary sequence available in Python. Tuples are immutable and can store a fixed number of items. For example, you can use tuples to represent Cartesian coordinates (x, y)
, RGB colors (red, green, blue)
, records in a database table (name, age, job)
, and many other sequences of values.
In all these use cases, the number of elements in the underlying tuple is fixed, and the items are unchangeable. You may find several situations where these two characteristics are desirable. For example, consider the RGB color example:
>>> red = (255, 0, 0)
Once you’ve defined red
, then you won’t need to add or change any components. Why? If you change the value of one component, then you won’t have a pure red color anymore, and your variable name will be misleading. If you add a new component, then your color won’t be an RGB color. So, tuples are perfect for representing this type of object.
Note: Throughout this tutorial, you’ll find the terms items, elements, and values used interchangeably to refer to the objects stored in a tuple.
Some of the most relevant characteristics of tuple
objects include the following:
- Ordered: They contain elements that are sequentially arranged according to their specific insertion order.
- Lightweight: They consume relatively small amounts of memory compared to other sequences like lists.
- Indexable through a zero-based index: They allow you to access their elements by integer indices that start from zero.
- Immutable: They don’t support in-place mutations or changes to their contained elements. They don’t support growing or shrinking operations.
- Heterogeneous: They can store objects of different data types and domains, including mutable objects.
- Nestable: They can contain other tuples, so you can have tuples of tuples.
- Iterable: They support iteration, so you can traverse them using a loop or comprehension while you perform operations with each of their elements.
- Sliceable: They support slicing operations, meaning that you can extract a series of elements from a tuple.
- Combinable: They support concatenation operations, so you can combine two or more tuples using the concatenation operators, which creates a new tuple.
- Hashable: They can work as keys in dictionaries when all the tuple items are immutable.
Tuples are sequences of objects. They’re commonly called containers or collections because a single tuple can contain or collect an arbitrary number of other objects.
Note: In Python, tuples support several operations that are common to other sequence types, such as lists, strings, and ranges. These operations are known as common sequence operations. Throughout this tutorial, you’ll learn about several operations that fall into this category.
In Python, tuples are ordered, which means that they keep their elements in the original insertion order:
>>> record = ("John", 35, "Python Developer")
>>> record
('John', 35, 'Python Developer')
The items in this tuple are objects of different data types representing a record of data from a database table. If you access the tuple object, then you’ll see that the data items keep the same original insertion order. This order remains unchanged during the tuple’s lifetime.
You can access individual objects in a tuple by position, or index. These indices start from zero:
>>> record[0]
'John'
>>> record[1]
35
>>> record[2]
'Python Developer'
Positions are numbered from zero to the length of the tuple minus one. The element at index 0
is the first element in the tuple, the element at index 1
is the second, and so on.
Cool! You’ve had a first glance at tuples. It’s time to dive deeper into all of the above characteristics of tuples and more. To kick things off, you’ll start by learning the different ways to create tuples in Python.
Constructing Tuples in Python
Read the full article at https://realpython.com/python-tuple/ »
[ Improve Your Python With ? Python Tricks ? – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
Find A Teacher Form:
https://docs.google.com/forms/d/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ/viewform?edit_requested=true#responses
Email:
public1989two@gmail.com
www.itsec.hk
www.itsec.vip
www.itseceu.uk
Leave a Reply