Python: Compound Structures - Intro

Python: Compound Structures - Intro ? A compound structure or data type is one in which a single variable represents

multiple values ? This section examines three:

1. Lists 2. Tuples 3. Sets ? Lists and tuples represent sequences of values ? Collections of values where order is of importance ? Python sets are just like sets in math ? Collections of values where membership is of importance

1

Python: Compound Structures - Lists ? A Python list is an ordered sequence of values ? Lists are created using the following syntax:

? For example: >>> x = [1, 2, 3] >>> x [1, 2, 3] >>> y = [11.2, 'hello', 6, True] >>> y [11.2, 'hello', 6, True] >>> z = [77, [3, 2, 1]] >>> z [77, [3, 2, 1]]

? Note that a list can have values of different data types, and lists may be embedded

? Internally, list variables represent pointers to a set of pointers:

2

Python: Compound Structures - Lists (2)

? Individual list elements are accessed via an index ? Syntax:

, where index is an integer expression ? Lists use zero-based indexing: The first element is in position zero, etc.

For example: >>> x = [11.2, 'hello', 6] >>> x[0] 11.2 >>> x[1] 'hello' >>> x[2] 6 >>> x[3] Traceback (most recent call last): File "", line 1, in IndexError: list index out of range

? Python also allows negative indices These reference elements in a leftward direction from position zero For example: >>> x = [11.2, 'hello', 6] >>> x[-1] 6 >>> x[-2] 'hello' >>> x[-3] 11.2

? If there are n elements in a list, index i must be in the range -n i n-1

3

Python: Compound Structures - Lists (3) ? While indices can be thought of as referring to specific elements, it is sometimes

relevant to think of them as referring to the boundary between two elements or in front of an element

? For example, index 0 lies in front of the first element (or between the last and first elements); index 2 lies in front of the third element, or between elements two and three; index -1 lies in front of the last element, or between the last and next-to-last elements

? Slicing ? Slicing means accessing a sequence of elements at one time ? The sequence is called a slice ? Syntax:

? Semantics: Return the elements starting from index1 up to but not including index2

? If index2 precedes index1 in the list, the empty list ([]) is returned ? If index1 is omitted, everything from position 0 to index2 is returned ? If index2 is omitted, everything from index1 to the end of the list is returned ? If both index1 and index2 are omitted, a copy of the entire list is returned

4

Python: Compound Structures - Lists (4) ? Reassigning elements

? Individual elements can be reassigned by simply assigning a value to an indexed reference:

>>> x [2, 4, 5, 8, 10] >>> x[2] = 6 >>> x [2, 4, 6, 8, 10]

? Slices can be reassigned by assigning a list to the referenced slice: >>> x [2, 4, 6, 8, 10] >>> x[2:4] = [9, 7, 5] >>> x [2, 4, 9, 7, 5, 10]

The number of elements assigned do not need to match the number being replaced

? Python provides a large number of operators, methods, functions, etc. for manipulating lists (and other iterable types) that are described in the next sections. ? Some of these are destructive operations - they change the value of the operand ? Others are nondestructive - they produce a result without altering the value(s) of their operands

5

Python: Compound Structures - List Operators 1. Addition

? Syntax:

? Returns the result of attaching list2 to the end of list1 without changing either

? Examples: >>> x [1, 2, 3] >>> y ['a', 'b', 'c'] >>> x + y [1, 2, 3, 'a', 'b', 'c'] >>> x [1, 2, 3] >>> y ['a', 'b', 'c']

2. Multiplication ? Syntax:

? Returns a list made from int copies of list ? Examples:

>>> x [1, 2, 3] >>> 2 * x [1, 2, 3, 1, 2, 3] >>> x [1, 2, 3]

6

Python: Compound Structures - List Operators (2) 3. in

? Syntax: ? Returns True/False if < expression > is a member of the list ? Examples:

>>> x [2, 3, 4, 5, 6, 8] >>> 3 in x True >>> 7 in x False

7

Python: Compound Structures - List Methods 1. append

? Syntax:

? Adds element to end of list ? Examples:

>>> x [1, 2, 3] >>> x.append(4) >>> x [1, 2, 3, 4]

? Note that the argument to append becomes the last element of the list: >>> x [1, 2, 3] >>> x.append([4, 5]) >>> x [1, 2, 3, [4, 5]]

2. count ? Syntax:

? Counts how many times expression appears in the list ? Examples:

>>> x [1, 2, 3, 1, 1] >>> x.count(1) 3 >>> x.count(3) 1

8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download