List is a sequence

Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in list are called elements or sometimes items. There are several ways to create a new list: Enclose the elements in square brackets
>>> [10, 20, 30, 40]
[10, 20, 30, 40]
>>> type([10, 20, 30, 40])

>>> ['pen 1', 'pen 2', 'pen 3']
['pen 1', 'pen 2', 'pen 3']
>>> type(['pen 1', 'pen 2', 'pen 3'])

The first example in the previous example is list of four integers. The second is a list of three strings. The elements of a list don’t have to be the same type.
>>> ['spam', 2.0, 5]
['spam', 2.0, 5]
A list can be defined inside another list.
>>> [2, 4.5, ['spam', 3]]
[2, 4.5, ['spam', 3]]
So list within another list is nested list.
Empty list is a list that contains no elements and you can create it by typing [].
>>> []
[]
>>> type([])

You can assign list values of variables:
>>> guitars = ['Gibson', 'Fender', 'Jackson']
>>> numbers = [1 , 2 , 3]
>>> emptylist=[]
>>> print guitars, numbers, emptylist
['Gibson', 'Fender', 'Jackson'] [1, 2, 3] []
List can be defined using class list().
>>> s = list()
>>> type(s)

Nema komentara:

Objavi komentar