Python beginners_note 3: introduction to the list

1. What is a list?

A ' list ' is a series of elements arranged in a specific order. We can add anything specified (all letters in the alphabet, numbers 0-9, Chinese characters, etc.) to the list, and there can be no relationship between the elements in the list.

In view of the fact that the list usually contains multiple elements, we usually assign a name to the list (such as letters, digits, names, etc.).

In Python, square brackets ([]) are used to denote lists, and commas are used to separate elements.

Examples:

1 cars = [ ' audi ' , ' toyota ' , ' bmw ' , ' hongqi ' , ' jili ' , ' byd ' ]
 2  print (cars)

Output: If we let Python print the list, Python will print the internal representation of the list, including square brackets, commas, and quotation marks

[ ' audi ' , ' toyota ' , ' bmw ' , ' hongqi ' , ' jili ' , ' byd ' ] #output results

But this is not the output we want the user to see, so we need to learn how to access the list elements.

1.1 Access list elements

Note: The list is an ordered collection , so to access any element of the list, just tell Python the position or index of the element.

To access list elements, you need to point out: the name of the list, the index of the element (the index of the element needs to be placed in square brackets).

We can also call the previously introduced string methods (such as title (), upper (), lower (), etc.) on any list element.

Examples:

1 cars = [ ' audi ' , ' toyota ' , ' bmw ' , ' hongqi ' , ' jili ' , ' byd ' ]
 2  print (cars [0])
 3  print (cars [0] .title ())

This output is exactly what we want the user to see-neat and clean output. The output of the above example is as follows

 1 audi 2 Audi 

1.2 The index of the list element starts from 0 instead of 1.

 In Python, the index of the first list element is 0, not 1. This is true in most programming languages, which is related to the underlying implementation of list operations. In the process of accessing the elements of the list, be careful not to make the 'miss one' mistake.

To access any element in the list, you can decrement its position by 1 and use the result as an index.

Python provides a special syntax for accessing the last list element. By specifying the index as -1, Python can be returned to the last list element. This method is useful when accessing the last element without knowing the length of the list. This convention also applies to other ' negative ' indexes.

Example: Bicycles with index 1 and 3 and index -1 and -4 in the access list

1 cars = ['audi','toyota','bmw','hongqi','jili','byd']
2 
3 print(cars[1].title())
4 print(cars[3].title())
5 print(cars[-1].title())
6 print(cars[-4].title())

The output is:

Toyota 
Hongqi 
Bid 
Bmw

1.3 Use the values ​​in the list

The values ​​in the list can be used like other variables.

Example: You can use stitching to create a message based on the values ​​in the list.

1 cars = ['audi','toyota','bmw','hongqi','jili','byd']
2 message = "My first car was a "+cars[3].title()+"."
3 
4 print(message)

Output result:

My first car was a Hongqi.

 

Guess you like

Origin www.cnblogs.com/jingxiaodong/p/12688139.html