tuple of python list

Tuple

  1. The list is very suitable for storing data sets that may constantly change during the running of the program, so the list can be modified. The advantage of being able to be modified is very important in dealing with the user list of the website or the list of characters in the game, but some It is also necessary to create a series of unmodifiable elements, so Python has derived the concept of tuples.
    In Python, the value that cannot be modified is called immutable, and the immutable list is called tuple.

  2. Defining tuples
    Tuples look like lists, but are marked with parentheses () instead of square brackets []. After the tuple is defined, you can use the index to access its elements, just as you can access list elements. Let's take an example to intuitively feel for
    example. If you have a rectangle whose size should not change, you can store its length and width in a tuple to ensure that they cannot be modified:

    dimensions.py	
    
		1 dimensions = (200, 50)   #定义元组,使用的是圆括号
  		2 print(dimensions[0])        #接下来打印元组中的元素
  		3 print(dimensions[1])

Look at the execution results:

	[email protected]:~/python$ python dimensions.py 
	200
	50

The syntax used was found to be the same as the syntax used to access the list elements.
Let's do an experiment, try to modify an element in the tuple dimensions, and see how the result is:

 	 1 dimensions = (200, 50)
  	  2 dimensions[0] = 300
  	  3 print(dimensions[0])
  	  4 print(dimensions[1])

The result of the implementation:

	[email protected]:~/python$ python dimensions.py 
	Traceback (most recent call last):
	File "dimensions.py", line 2, in <module>
		dimensions[0] = 300
	**TypeError: 'tuple' object does not support item assignment**

Because the operation of trying to modify the tuple in Python is prohibited, Python pointed out that it is not possible to assign values ​​to the elements of the tuple, so the above execution result prompts an error message, which is in line with our expectations.

  1. Iterate over all the values ​​in the
    tuple . Because the tuple is an unmodifiable list, you can also use a for loop to iterate through all the values ​​in the tuple like a list. Try it yourself:

    dimensions.py
    
     1 dimensions = (200, 50)
  	 2 for value in dimensions:
  	 3     print(value)

Look at the execution results:

	[email protected]:~/python$ python dimensions.py 
	200
	50

Well, the result is the same as the access list.

  1. Modifying tuple variables
    Although elements of tuples cannot be modified, you can assign values ​​to variables that store tuples. Therefore, if you want to modify the size of the aforementioned rectangle, you can redefine the entire tuple and write it. After all, practice is the only standard for testing truth ---- Lu Xun
 1 dimensions = (200, 50)
  2 print("original value: ")
  3 for value in dimensions:
  4     print(value)
  5 
  6 
  7 dimensions = (500, 100)
  8 print("new value: ")
  9 for value in dimensions:
 10    print(value)

Look at the test results:

[email protected]:~/python$ python dimensions.py 
original value: 
200
50
new value: 
500
100

First, define a tuple, and print out the size of its storage, then store a new tuple to the variable storage to the variable dimensions, and then print the new size, the execution result does not report any errors, because the meta Assignment of group variables is legal.
Compared to lists, tuples are a simpler data management structure. If you need to store a set of values ​​that will remain unchanged throughout the life of the program, you can use tuples.
Speaking of data structures, do n’t you have any deep insights into the understanding of these four words? Sometimes, after a certain level of accumulated insights, people suddenly see a sentence, or feel something, they will have a newer and deeper understanding of some words that they originally understood. And it is the kind of heartfelt empowerment that comes from the heart. Data ---- structure, the foundation is data, and promotion is structure. After understanding the essence, it will be more profound to learn and use ...

Published 53 original articles · praised 16 · visits 2213

Guess you like

Origin blog.csdn.net/m0_37757533/article/details/105157912