Python. Tuples. Basic concepts. Properties tuples

Tuples. Basic concepts. Properties tuples


Search other websites:

1. The concept of a tuple. Examples. The need to use tuples

In Python, a tuple is a group of objects of different types, taken in parentheses, which does not allow changes (is immutable). Tuples have many properties. With tuples, various operations can be applied.

According to the Python documentation, the general form for describing a tuple class is as follows:

class tuple([iterable])

where iterable – some sequence of objects.

Tuples are needed when you need to use fixed sets of objects. One example would be the use of tuples as keys to dictionary entries (the key must not be changed). Also, the use of tuples is important when the task needs to provide integrity support, that is, the tuple cannot be changed from another link. If you need to use collections of objects that can be changed in the task, then for these cases you need to use lists.

Examples of tuples.

() # empty tuple
(2.3,'ABCDE', False) # tuple of 3 elements of different types
('world', (2.88, "bestprog"), 3.141592) # nested tuple
(3.88, 2,) # A tuple of two elements


2. Properties tuples

The following properties are distinguished for tuples:

  1. Tuples are ordered collections of objects of arbitrary types. Collections of objects are ordered from left to right.
  2. In tuples, access to elements is provided by offset. Tuples support operations that are used to offset elements, such as pulling a slice, indexing, etc.
  3. Tuples are classified as immutable sequences. Direct change operations cannot be applied to tuples. However, if the element of the tuple is a mutable element (for example, a list), then this element can be changed in the tuple.
  4. Tuples are heterogeneous. The term “heterogeneous” means that tuples can contain other composite objects, such as lists, rows, or other tuples.
  5. Tuples support an arbitrary number of attachments.
  6. Tuples have a fixed length. If you need to change the length of the tuple, then a copy of the tuple is necessarily created in a new memory location, the length of which is different.
  7. Tuples can be represented as arrays of references to objects.


3. Differences between tuples and lists

The following differences can be distinguished between tuples and lists:

  • the tuple is formed in parentheses (), the list is formed in square brackets [];
  • tuples refer to immutable sequences; lists refer to mutable sequences. It is impossible to apply operations to tuples that directly modify them. Such operations can be applied to lists;
  • tuples have a fixed length (number of elements), lists have a variable length. If you need to increase the size of the tuple, you need to create a copy.


4. What do tuples and lists have in common?

The following common features can be distinguished between tuples and lists:

  1. Lists and tuples are collections of objects or sequences.
  2. Lists and tuples provide the ordering of their objects from left to right.
  3. Tuples and lists can contain elements (objects) of any type.
  4. In tuples and lists, elements are accessed by offset.
  5. Tuples and lists support the same operations based on the use of offset. For example, indexing, slicing, etc.
  6. Tuples and lists are heterogeneous. This means that tuples and lists can contain other complex elements (strings, lists, tuples).
  7. Tuples and lists support an arbitrary number of attachments.
  8. Tuples and lists allow you to store arrays of references to other complex objects, which, in turn, can also be tuples, lists, or strings.


5. Methods (operations) of creating a tuple

You can create a tuple in one of the 4 ways below.

1. Using a pair of empty brackets ().

In this case, an empty tuple is created. For example

() # an empty tuple is created


2. In the case of a single tuple with a comma at the end of an element taken in parentheses.

For example.

(True, )


3. By using multiple elements separated by commas and taken in parentheses.

For example.

(2.5, -11, 3)
(a, True, "Tuples")

4. Using the built-in tuple() command.

For example.

# Creating nested tuples using the = operation
a = (2.5, -11, 3)
b = (a, True, "Tuples") # b = ((2.5, -11, 3), True, 'Tuples')
c = tuple(b) # c = ((2.5, -11, 3), True, 'Tuples')


6. An example of creating a tuple containing integer random numbers


# Create a tuple from a sequence of 5 integers,
# which lie in the range from 0 to 10.

# To use random numbers, you need to include the random module
import random

# 1. Create a list of numbers
lst = [] # empty list first
i = 0
while i < 5:
    num = random.randint(0,10)
    lst = lst + [num]
    i = i+1

# 2. Create a tuple from list items
a = tuple(lst)

print("a = ", a) # a = (3, 0, 2, 6, 2)

The result of the program

a = (4, 1, 4, 5, 7)


7. Example of searching for a given item in a tuple


# Search for a specified item in a tuple
# 1. The specified tuple of strings
a = ('ab', 'abcd', 'cde', 'abc', 'def')

# 2. Input the search string
s = str(input("s = "))

# 3. Tuple bypass cycle
result = False # result
i = 0
while i < len(a): # len(a) - the number of elements in the tuple
    if (a[i]==s):
        result = True
    i = i+1

if (result):


8. Function len(). Number of elements in a tuple

To determine the length of the tuple (the number of elements in the tuple), the standard len() function is used.


# Function len() - the number of elements in the tuple

a = () # empty tuple
l = len(a) # l = 0

b = (1, 3, 7, 13, 'abc', True)
l = len(b) # l = 6

# Nested tuple
c = (a, b, "Hello", 2.33)
l = len(c) # l = 4


9. How to indicate that an object is a tuple consisting of a single element?

To indicate that the object is a tuple, you need to specify the symbol ‘ , ‘ (comma) after the element. Otherwise, the object will be perceived as a number.


# The difference between the object in brackets () and the tuple.
# The number having a value num1 = 255
num1 = (255)
num2 = num1 + 5 # num2 = 255 + 5 = 260 - summation of ordinary integers

# A tuple containing the number 255 - a comma is indicated at the end
A = (255,)
# B = A + 5 - prohibited, exception


Related topics