Introduction to Python zero Basics - 14 - tuples in Python

Posted by dslax27 on Thu, 24 Feb 2022 01:38:49 +0100

What is a tuple

Tuple is a data type similar to list and. They have basically the same characteristics, but they also have many differences.

  • Tuples, like lists, are queues that can store multiple data structures
  • The same tuple is also an ordered set with repeatable elements

Definition and creation of tuples

  • In Python, tuple represents the type of tuple, which can also be used to define a primitive ancestor
  • In Python, a tuple is a data structure of unlimited length
  • In Python, elements in tuples exist within a () parenthesis, such as name = tuple ('lily ',' Jack ')

The difference between tuples and lists

  • Tuples take up less resources than lists
  • After the list is created, it can be modified
  • After the tuple is created, it cannot be modified

Modification list example

An example of modifying a list is as follows:

>>> x = [1, 2, 3]
>>> x[0] = 11
>>> x
[11, 2, 3]
  • In line 1, the list x is created
  • On line 2, modify the element of item 0 of list x
  • In line 3, the modified list is displayed
  • In line 4, the result indicates that the modification was successful

Modify tuple example

Examples of modifying tuples are as follows:

>>> y = (1, 2, 3)
>>> y[0] = 11
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> y
(1, 2, 3)
  • In line 1, the tuple y is created
  • In line 2, modify the element of item 0 of tuple y
  • On line 3, a prompt appears for TypeError. Because 'tuple' object does not support item assignment, it cannot be modified
  • On line 6, the tuple y is displayed
  • In line 7, the result indicates that the tuple y has not been modified

Special tuple

If a tuple does not contain any elements, use () to represent an empty tuple. An example is as follows:

>>> t = ()
>>> t
()

If a tuple contains only one element item, add a comma after the element and use (item,) to represent the tuple. Examples are as follows:

>>> t = (123, )
>>> t
(123,)

Note that (123,) and (123) have different meanings:

  • (123,) represents a tuple, which contains an integer 123
  • (123) represents an expression in parentheses, and the integer 123 is embedded in parentheses

The two are different. Examples are as follows:

>>> t = (123, )
>>> i = (123)
>>> t == 123
False
>>> i == 123
True
  • In line 3, the tuple (123,) is compared with the integer 123 and the result is false
  • On line 5, compare the expression (123) with the integer 123 and the result is true

Abbreviation for tuple

When the tuple is to the right of the assignment operator =, the parentheses can be omitted. An example is as follows:

>>> t = 123, 456
>>> t
(123, 456)
>>> x = 123,
>>> x
(123, )
  • In line 1, tuples (123, 456) are created
  • In line 4, the tuple (123,) is created

Type of tuple

str_tuple = ('name', 'age', ' ', '')
int_tuple = (1, 10, 22, 34, 66, 100)
float_tuple = (1.1, 3.14, 6.58, 8.88)
bool_tuple = (False, True, False)
none_tuple = (None, None, None)
tuple_tuple = ((1, 2, 3), (1.4, 3.1415926, 6.88))
list_tuple = ([123, 456], [789, 520])    # It should be noted here that the list is variable, but when the list becomes a member of tuple, it is immutable

Use of in, max and min in the list

Keyword in

Check whether the tuple contains the specified element through the keyword in. An example is as follows:

>>> 'lily' in ["lily", "jack", "hanmeimei"]
True
>>> 'neo' in ["lily", "jack", "hanmeimei"]
False
  • In line 1, detect that the string 'lily' is in the tuple
  • In line 3, the detection string 'neo' is not in the tuple

Max (tuple) function

Use the function max to obtain the largest element in the tuple. An example is as follows:

>>> max((1, 2))
2
>>> max((1, 3, 2))
3

Min (tuple) function

Use the function min to obtain the smallest element in the tuple. An example is as follows:

>>> min((1, 2))
1
>>> min((1, 3, 2))
1

index(item) method

Because tuples cannot be modified, common methods for modifying lists cannot be used on tuples, but the index(item) method can be used;

The index(item) method finds the specified element item in the tuple. If the element item is found, the index of the element item is returned; If not found, an exception is thrown. Examples are as follows:

>>> x = ('lily', 'neo', 'hanmeimei')
>>> x.index('lily')
1
>>> x.index('jack')

# >>>The results are as follows:
# >>> Traceback (most recent call last):
# >>>   File "<stdin>", line 1, in <module>
# >>> ValueError: 'hehe' is not in tuple
  • In line 2, use the index method to find the element 'lily' in the tuple
  • In line 3, the index of the display element 'lily' in the tuple is 1
  • In line 4, use the index method to find the element 'jack' in the tuple
  • In line 5, because the tuple does not contain the element 'jack', the error "ValueError: 'mooc' is not in tuple" is displayed

Be sure to pay attention to the differences between tuples and lists. Tuples are generally suitable for storing unchanged data.

Topics: Python Back-end