Self-taught python notes tuple update with little turtle...

After reading these notes, you can get started with Python.
Watch the video of Little Turtle on Station B, and organize your notes by the way.

Lesson 10 list
Lesson 12 strings
Lesson 13 format format

Lesson 11 Tuples

1. Summarize tuples in one sentence

      A tuple is a list of shackles. The closed list, once defined, cannot be changed (cannot be added, deleted or modified).
      In the last lesson, I learned about lists. Ta is like a large warehouse. It can hold various types of data and can be added or deleted at will. However, some physical things do not need to be changed frequently. Some are even created once they are created. unable to be changed. Then, selecting the list at this time is not so ideal. At this time, Python provides another tool for storing data-tuples.

2. The creation and access of tuples

      1, tuple1 = (1,2,3,4,5)
      2, tuple2 = 1,2,3,4,5
the above two methods to create a tuple can be created, which can be seen the main element to create a tuple is a comma .
      The main element of the tuple is a comma. Next, write a small example to enhance memory:

8 * (8) #64
8 * (8,) # (8,8,8,8,8,8,8,8)

      The principle of accessing tuple elements is the same as that of list slicing:
      tuple1[1] = 2
      tuple1[3:] = (4,5)

4. To create a tuple, under what circumstances must the comma and the parentheses exist at the same time, neither of which is indispensable

Insert picture description here
      The above example is the splicing of tuples. When splicing tuples with only one element, the parentheses and commas must exist at the same time. If you want to know more about contacting and typing the code, just compare them.

5. Exercises for distinguishing lists and tuples

      1: So when do you need to use tuples instead of lists?
      Answer: When we hope that the content will not be easily rewritten, we use tuples (to put power in a cage).
      2: When tuples and lists fall into the water, who will you save?
      Answer: If it were me, I would save the list, because the list provides more built-in methods than tuples, which greatly improves the flexibility of programming. Looking back at tuples, tuples are safe, but they cannot be modified if they are created (unless they are modified indirectly by creating a new tuple, but this brings consumption), and we people often waver, so Tuples are only used in special situations, and lists are usually used a lot.
      3: x, y, z = 1, 2, 3 Are x, y, z tuples?
      Answer: The default type of all multi-object, comma-separated, and not explicitly defined collections are tuples.
Insert picture description here
      4: Please write down whether to use lists or tuples to save data in the following scenarios?

  1. Attributes of the characters in the game: list
  2. Your ID information: tuple
  3. Forum members: list
  4. Team work together to develop the program, passing to a parameter of a function that you don’t know the specific implementation: tuple
  5. Specific configuration parameters of each component of the space rocket: tuple
  6. Record data of discovered planets in NASA system: list

Guess you like

Origin blog.csdn.net/A_Tu_daddy/article/details/105092320