Learn a little python knowledge in three minutes 3-----------My understanding of python lists and tuples

Insert image description here

1. What is python?

In Python, both lists and tuples are data structures used to store multiple data items, but they have some subtle differences:

  1. 列表It is mutable, which means that elements in the list can be added, deleted, and modified at any time; while tuples are immutable, meaning that once they are created, they cannot be modified. 元组Usually used to store immutable data items, such as coordinates, dates, etc.
  2. 列表Use square brackets [] to express, 元组use round brackets () to express.

2. List application

  • Lists are often used to store variable-length data collections, such as a group of students' names, ages, genders and other information.
  • Lists can be dynamically added, deleted, and modified, so they are widely used in data construction, algorithm operations, and other occasions.
  • Lists can also contain other types of data structures, such as nested lists, which can be used to construct data structures such as two-dimensional arrays and trees.

For example, here is an example of using a list to store student information:

students = [
    {
    
    'name': 'Tom', 'age': 18, 'gender': 'male'},
    {
    
    'name': 'Lucy', 'age': 19, 'gender': 'female'},
    {
    
    'name': 'Bob', 'age': 20, 'gender': 'male'}
]

for student in students:
    print('{} is {} years old, {}.'.format(student['name'], student['age'], student['gender']))

3. Application of tuples:

  • Tuples are immutable, so they are usually used to store some immutable data collections, such as a coordinate, a set of dates, etc.
  • Tuples can be assigned by unpacking and can be used for multiple assignments of function return values.
  • Tuples are more efficient than lists in some performance-sensitive situations because tuples can be treated as immutable and can be cached and optimized.

For example, here is an example of using tuples to store coordinates:

def distance(p1, p2):
    """ 计算两个点之间的距离 """
    x1, y1 = p1
    x2, y2 = p2
    dx = x2 - x1
    dy = y2 - y1
    return (dx**2 + dy**2) ** 0.5

p1 = (0, 0)
p2 = (3, 4)
print('Distance between', p1, 'and', p2, 'is', distance(p1, p2))

4. Further understand lists and tuples in depth

4.1. List example:

fruits = ['apple', 'banana', 'orange'] # 创建一个列表
print(fruits) # 输出 ['apple', 'banana', 'orange']

fruits.append('grape') # 在列表末尾添加 'grape'
print(fruits) # 输出 ['apple', 'banana', 'orange', 'grape']

fruits[1] = 'pear' # 修改第二个元素为 'pear'
print(fruits) # 输出 ['apple', 'pear', 'orange', 'grape']

fruits.remove('orange') # 删除 'orange'
print(fruits) # 输出 ['apple', 'pear', 'grape']

4.2. Tuple example:

coordinates = (3, 4) # 创建一个元组
print(coordinates) # 输出 (3, 4)

# 下面的语句会引发 TypeError
# coordinates[0] = 5 # 尝试修改元组中的第一个元素

# 元组也可以使用拆包特性进行赋值,例如:
x, y = coordinates
print('x:', x, 'y:', y) # 输出 x: 3 y: 4

Summarize

It should be noted that although the tuple itself is immutable, if the tuple contains mutable objects, such as a list, the elements in the list can be modified.

Guess you like

Origin blog.csdn.net/qlkaicx/article/details/131348320