Python list

Python creates a list

One type of data built into Python is the list:list A list is an ordered collection that you can add and remove at any time.

For example, to list the names of all the classmates in the class, you can use a list to indicate:

>>> ['Michael', 'Bob', 'Tracy']
['Michael', 'Bob', 'Tracy']

A list is an ordered collection in the mathematical sense, that is, the elements in the list are arranged in order.

Constructing a list is very simple. According to the above code, directly [ ] enclosing all the elements of the list is a list object. Usually, we assign a list to a variable so that we can reference the list via a variable:

>>> classmates = ['Michael', 'Bob', 'Tracy']
>>> classmates # Print the contents of the classmates variable
['Michael', 'Bob', 'Tracy']

Since Python is a dynamic language, the elements contained in the list are not required to be the same data type. We can include all kinds of data in the list:

>>> L = ['Michael', 100, True]

A list that does not have an element is an empty list:

>>> empty_list = []

Leave a Reply

Your email address will not be published. Required fields are marked *