Project Star -- A Tour of Python's Built-in Objects (List) (1)

Creation of the list:

films = ['钢铁侠','环太平洋','星球大战','流浪地球','复仇者联盟']
print(films)

Creating a list of movies As shown above, it is a good idea to give the list a plural name (such as films, or names, etc.). Like other types of python variable assignment, when creating a list, you can also use the assignment operator "=" to directly assign a list to a variable.

Create a list of values

Numerical lists are very common in python. For example, the system records student scores, records player game scores, positions, and so on. A list of values ​​can be used to store the corresponding data.

num = [1,2,3,4,5]
print(num)

Numbers in the list do not need to use single or double quotation marks, but English words and Chinese need to use quotation marks otherwise an error will be reported.

list function syntax:

list(data)

Where data represents the data that can be converted into a list, and its type can be a range object, a string, a tuple or other iterable types of data.

For example we create a list of all even numbers between 90-100. (excluding 100)

number = list(range(90,100,2))
print(number)
 
#输出结果为:[90,92,94,96,98]

The function range: can make python start from the first value you specify and stop when it reaches the second value you specify, but the value does not include the second value (note: 100 is not included here) . In our example above it can be expressed that the function range() starts at 90 because we are finding the even number equal to +2 continuously until the final value is reached or exceeded. (100)

access list element

The list is an ordered set , so if we want to access an element in the list, we include the square brackets on the left and right when outputting the list. If we don't want to output all the elements, we only need to tell python the position and index of the element. . First, we point out the name of the list, and then point out the index of the element we want to access. --------- Let's demonstrate below, we set our visit target to be the element 'Iron Man'. What we do output returns the element 'Iron Man' without square brackets and quotes.

films = ['钢铁侠','环太平洋','星球大战','流浪地球','复仇者联盟']
print(films[0])

#输出结果: 钢铁侠

the index of the list element

In python, the index of the first list element is 0 , not 1. The second index element of the list is 1.

films = ['钢铁侠','环太平洋','星球大战','流浪地球','复仇者联盟']
print(films[0])    #输出结果:钢铁侠
print(films[1])    #输出结果:环太平洋
print(films[2])    #输出结果:星球大战
print(films[3])    #输出结果:流浪地球
print(films[4])    #输出结果:复仇者联盟

 Python provides a special method for accessing the last list element. You can tell python to return the last list element by specifying the index as -1 .

films = ['钢铁侠','环太平洋','星球大战','流浪地球','复仇者联盟']
print(films[-1])     #输出结果:复仇者联盟
print(films[-2])     #输出结果:流浪地球
print(films[-3])     #输出结果:星球大战
print(films[-4])     #输出结果:环太平洋
print(films[-5])     #输出结果:钢铁侠

quote

Let's create a message

films = ['钢铁侠','环太平洋','星球大战','流浪地球','复仇者联盟']
message = 'My favourite film was a' + films[2]+'.'
print(message)

We generate a sentence using films [2] and store it in the variable message. The output is a sentence.

My faourite film was a 星球大战

 

If you feel that the author's writing is okay, please give the author a one-click three-link. thanks 

 

Guess you like

Origin blog.csdn.net/m0_62069409/article/details/121711587