home page » programing language » How to understand and use lists in Python

How to understand and use lists in Python

 
 How to understand and use lists in Python How to understand and use lists in Python

 How to understand and use lists in Python

 How to understand and use lists in Python

 

Before you start talking about technology, say more. You should also know that more than 90% of my articles are original, and only one or two of them are for friends. In order to ensure the quality of the public account content, I am not willing to write more about other people's things or reprint a large number of articles. Even if it is an article to be reprinted, I will read it thoroughly first, draw nutrition from it, and then add my own thinking, and finally I will share it.

As for why a technical company will share some personal and non-technical content, no reader has asked me about this question at present. But I want to answer this question on my own initiative. First, this name is personal. Second, technology is always boring. If I keep talking about technology, I believe that few people will stick to reading it. Therefore, I decided to share some personal experiences, work, and Internet thinking from time to time.

All right, here we go.

Anyway, as a Python beginner, if you read This is an introductory article on Python And Coding problems that should be noticed in Python For these two articles, I believe you can write some simple code. But these are far from enough. These two articles have more courage to stand at the door of Python.

Then, after reading this article, we can really open the door.

This article will go deep into Python syntax. The content is about the concept of lists in Python and how to use lists.

What is a list?

In Mandarin, a list is composed of a series of elements arranged in a specific order. Put them into examples in life, such as all the names, ages and heights of your family, and string them together to form a list. Name list, age list, height list.

Back to Python, use square brackets [] to represent the list in Python, and use commas to separate the elements, such as:

 >> > names = [ 'xiyouMc' , 'Shuai Chao' , 'Super Brother' ] >> > print(names) [ 'xiyouMc' , 'xe5xb8x85xe8xb6x85' , 'xe8xb6x85xe5x93xa5' ]

This is a list. At the same time, I printed it out through the print function. As for the reason why the transcoding is xe5..., it is because when writing data, the default transcoding is UTF-8 provincial storage. I don't know why my friends will go back to the previous article to make up for it.

Now that we have seen what the list looks like, how should we access it?

Access list elements

It is also mentioned that the list is composed in a specific order. Can we access an element by its position (index)? Of course.

For example:

 >> > names=[ 'xiyouMc' , 'Shuai Chao' , 'Super Brother' ] >> > names[ zero ] 'xiyouMc'
 >> >

If we specify to access the first element, we can get the element at 0 position (index) in the names list.

What is an index?

In the eyes of programmers, the first number of an array (list) is not really the first. It is because there is a number in front of it, which is the zero number. Therefore, you will think that the index represents the location, and there is an index of 0 in the computer.

Back to the list, in Python, the index of the first list element is 0, not 1. So don't tell a programmer that you are the first or top 1 when chatting with him in the future. You should say that you are the zero or top 0. (No sense of discord)

In the list, the index of the second list element is 1, and the index of the Nth element is N-1.

For example:

 >> > names=[ 'xiyouMc' , 'Shuai Chao' , 'Super Brother' ] >> > names[ zero ] 'xiyouMc'
 >> > names[ two ] 'xe8xb6x85xe5x93xa5'
 >> >

If you specify an incorrect index value when accessing a list element, an error will be reported indicating that the array is out of bounds.

In Python, the index value can be negative. Negative numbers do not mean that there are elements of parallel space-time before the first position, but that the calculation starts from the position behind the array. For example, - 1 represents the last element, and - 2 represents the penultimate element:

 >> > names=[ 'xiyouMc' , 'Shuai Chao' , 'Super Brother' ] >> > names[- one ] 'xe8xb6x85xe5x93xa5'
 >> > names[- two ] 'xe5xb8x85xe8xb6x85'
 >> > names[- three ] 'xiyouMc'
 >> >

Negative indexes are very useful in actual development. Sometimes you need to access the last element without knowing the length of the list. At this time, using negative indexes can solve the problem at once.

Use in actual code

For the list of names above, we now need to apply this list to a text:

"XiyouMc is called handsome or super brother"

 > >> names=[ 'xiyouMc' , 'Shuai Chao' , 'Super Brother' ] > >> content = names[0] + ' is called ' + names[1] + 'or' + names[2] > >> print (content) XiyouMc is called > >>

Of course, you can also use negative indexes to combine.


This article first makes a simple understanding and use of the list in Python. If you are a beginner, you must practice it, or try to take some notes to record it.

Next, I will share higher level operations in the list, such as adding, deleting, modifying, sorting, combining, and summarizing some common errors.


Source: DeveloperPython

 

Original link: How to understand and use lists in Python , Please indicate the source for reprinting!

fabulous zero