Python Programming - tuples

Keywords: Python Java

I. cognition

Definition: lists are ideal for storing datasets that may change during program operation. The list is modifiable, which is crucial for dealing with the user list of the website or the role list in the game. However, sometimes you need to create a series of immutable elements, and tuples can meet this requirement.

Python calls immutable values immutable, and immutable lists tuples.

Features: immutable data type, no addition, deletion, modification and query; can store any data type

1. Define tuples
>>> t = (1,1.2,True,'westos')
>>> print(t,type(t))
((1, 1.2, True, 'westos'), <type 'tuple'>)

If the tuple contains variable data types, the contents of the tuple can be modified indirectly

>>> t1 = ([1,2,4],5)
>>> t1[0].append(3)
>>> print(t1)
([1, 2, 4, 3], 5)

Definition of empty tuple

>>> t2 = ()
>>> print(t2)
()
>>> t3 = tuple([])        #Convert list to tuple
>>> print(t3)
()
>>> t4 = list(())         #Convert tuples to lists
>>> print(t4)
[]

If there is only one element in a tuple, comma must be added after it, otherwise the data type is uncertain

>>> t5 = (1.2)
>>> print(t5,type(t5))
(1.2, <type 'float'>)
>>> t5 = (1.2,)
>>> print(t5,type(t5))
((1.2,), <type 'tuple'>)

Characteristics of tuples

1. index
>>> t = (1,1.2,True,'westos')
>>> print(t[0])
1
>>> print(t[1])
1.2
2. slice
>>> t = (1,1.2,True,'westos')
>>> print(t[:-1])
(1, 1.2, True)
>>> print(t[::-1])
('westos', True, 1.2, 1)
3. connection
>>> t = (1,1.2,True,'westos')
>>> print(t + (1,3,4))
(1, 1.2, True, 'westos', 1, 3, 4)
>>> print(t + [5,6,7])                  #Cannot connect between different data types
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "list") to tuple
>>> print(t + 'redhat')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "str") to tuple
4. repetition
>>> t = (1,1.2,True,'westos')
>>> print(t * 3)
(1, 1.2, True, 'westos', 1, 1.2, True, 'westos', 1, 1.2, True, 'westos')
5.for loop traversal
t = (1,1.2,True,'westos')
for i in t:
    print(i)

Test:

1
1.2
True
westos
6. Member operator
>>> t = (1,1.2,True,'westos')
>>> print(1 in t)
True
>>> print(1 not in t)
False

Common methods of tuples

>>> t = (1,1.2,True,'westos')
>>> print(t.count('westos'))
1
>>> print(t.index(1))
0

IV. application scenarios of tuples

1. Variable exchange value

Principle: first, encapsulate (a,b) into a tuple (1,2), B, a = a, B = = > b, a = (1,2), b=(1,2)[0], a=(1,2)[1]

>>> a = 1
>>> b = 2
>>> b,a = a,b
>>> print(a)
2
>>> print(b)
1
2. Print variable value
>>> name = 'westos'
>>> age = 11
>>> t = (name,age)
>>> print('name:%s,age:%d' %(name,age))
name:westos,age:11
>>> print('name:%s,age:%d' %t)
name:westos,age:11
3. Tuple assignment: receive as many elements as you want
>>> t = ('westos',10,100)
>>> name,age,score=t
>>> print(name,age,score)
('westos', 10, 100)
4. Tuple sorting (unable to perform permanent sorting, it must be converted to list for sorting)
>>> scores = (100,49,89,78)
>>> scoreli = list(scores)
>>> scoreli.sort()
>>> print(scoreli)
[49, 78, 89, 100]

Can be temporarily sorted

>>> scores = (100,49,89,78)
>>> score_sort = sorted(scores)
>>> print(score_sort)
[49, 78, 89, 100]

Practice:
Title: copy data from one list to another

>>> origin = ['python','java','c']
>>> redhat = origin[:]
>>> print(redhat)
['python', 'java', 'c']

Posted by rskandarpa on Sun, 01 Dec 2019 08:17:29 -0800