Python has several inbuilt collection data types, which are used to contain other items. The
A list in Python is a data structure that can contain a sequence of other objects, of
in a specific order. Lists can have objects added to them and
than once, at different positions in the sequence. For example, you could store the number
of days in each month of a year as a list, as illustrated below. Often in Python programs
you will be accessing the elements contained in a list by referring to a specific position (an
index) within that list and by going through all the elements in a list in their given order. In
Python we use square brackets to specify the beginning and end of a list:
days = [31, 28.243, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
matrix = [[-1, 0, 0], [0, 1, 0], [0, 0, 3]]
Defining lists in Python; a simple list of numbers (both integers and floating point) and a second list
which contains three sub-lists, each representing a row in a matrix.
Tuples
A tuple is a data structure that is very much like a list, but which you cannot change once
it is created. In Python a tuple may be defined using round brackets ‘()’. Although you
cannot change its items, a tuple is used to contain a sequence of elements in a specific
order, and the different positions of this sequence can be interrogated. The contents of a
tuple are defined in their entirety when the tuple object is made. Having a kind of list that
you cannot change may seem like a pointless data structure, but tuples are a surprisingly
useful type of object. If you know that a sequence should definitely not have any elements
modified, added or deleted, then you can use a tuple to ensure that it is not possible to
deviate from this plan: for example, if you want to specify a vector with exactly three
spatial coordinates, e.g. (x, y, z), using a tuple ensures that you can’t have an invalid
vector with too few or too many values. Similarly, tuples are used where you have
elements that you know always go together; accordingly you could use tuples to specify a
text font like (‘helvetica’, 10) or (‘roman’, 12), where you must have two elements to
represent the name and the size of the font, and if you were to redefine the font you would
have to specify both. Tuples, unlike lists, can be used as keys to refer to data in dictionary
data structures (see the Dictionaries section below).
Sets
Sets, like lists and tuples, are data containers that encompass a collection of other objects.
However, unlike lists and tuples, the elements are in no particular order and the elements
cannot be repeated in a set. A notable use for sets is when you have some data that you
know, or suspect, contains repeat objects. By placing such data within a set any
duplication will be removed. For example, you might have a list containing the colours of
different items; if you put these colours into a set object you can find out how many
different colours were used. Also sets can be useful because you can easily perform set
operations, for example, to find the items that two collections have in common; this would
be trickier using lists or tuples.
females = set(['marge', 'maude', 'lisa', 'maggie', 'edna'])
simpsons = set(['homer', 'marge', 'bart', 'lisa', 'maggie'])
print(females & simpsons)
Defining Python sets and performing set operations; here finding an intersection (common
elements).
There is actually another variety of set, called a
frozen set. These are the same as
regular sets with the exception, as the name suggests, that they cannot be altered once
created (just like tuples). A useful consequence of this is that they can be used as keys to
extract data from dictionary data structures (see below).
Do'stlaringiz bilan baham: