Zero-based introductory learning Python (12)-tuple tuple

Tuple

Tuples are only readable, not writable

Use () to create tuples, use [] to create lists

The iconic symbol of tuple is ",", parentheses are not the key

Insert picture description here
Insert picture description here
Insert picture description here
Create an empty tuple with ()
Insert picture description here

Update and delete a tuple

Split by segmentation and overwrite with a new label. The original tuple is still there, but there is no label pointing to it. Python's collector will throw away the old tuple, keep the new
Insert picture description here
del and delete the entire tuple.
Insert picture description here

Tuple-related operators

  • Splicing operator+

  • Repeat operator *

  • Relational operators >,<,<=,==,>=

  • Logical operator and/or

  • Member operator in/ not in

Task

0. Please describe in one sentence what is a list? What is a tuple in another sentence?
List: A large warehouse, you can add and delete anything in it at any time. Tuple
: a closed list, once defined. Cannot be changed (cannot add, delete and modify)

1. When do you need to use tuples instead of lists?

When we want the content not to be easily rewritten, we use tuples (to put power in a cage).

When we need to frequently modify data, we use lists.
2. When tuples and lists fall into the water, who will you save?

Lists, because lists provide richer built-in methods than tuples, which greatly improves the flexibility of programming; tuples are safe, but they cannot be modified once they are created (unless they are modified indirectly by creating a new tuple, but this It brings consumption)
3. Please connect the built-in methods in the list on the left with the comments on the right, and circle the methods that can be used for tuples.
Insert picture description here

4. To create a tuple, under what circumstances must the comma and parentheses exist at the same time, and neither of them are indispensable?
When splicing tuples with only one element
5. x, y, z = 1, 2, 3 Are x, y, z tuples?
No, it's an integer
All multi-object, comma-separated collections that are not explicitly defined with symbols are tuples by default.
Insert picture description here

6. Please write down whether you should use lists or tuples to save data in the following scenarios:

1) 游戏中角色的属性:列表

2) 你的身份证信息:元组

3) 论坛的会员:列表

4) 团队合作开发程序,传递给一个你并不了解具体实现的函数的参数:元组

5) 航天火箭各个组件的具体配置参数:元组
  
6) NASA系统中记录已经发现的行星数据:列表

7. In the last class, we learned the "list comprehension" through homework, so if I change the square brackets to parentheses, will I get the "tuple comprehension"?
No, a list generator is enough

Guess you like

Origin blog.csdn.net/qq_44520665/article/details/113435287