0% found this document useful (0 votes)
14 views4 pages

11-Sequences 4pp

The document discusses lists and sequences in Python. It covers built-in list operations like length, indexing, concatenation and contains. It also discusses iterating over lists using for loops and unpacking elements. Range is introduced as a sequence type and list comprehensions are shown as a way to build lists programmatically.

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

11-Sequences 4pp

The document discusses lists and sequences in Python. It covers built-in list operations like length, indexing, concatenation and contains. It also discusses iterating over lists using for loops and unpacking elements. Range is introduced as a sequence type and list comprehensions are shown as a way to build lists programmatically.

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sequences Announcements

Working with Lists


>>> digits = [1, 8, 2, 8] >>> digits = [2//2, 2+2+2+2, 2, 2*2*2]
The number of elements
>>> len(digits)
4

An element selected by its index


>>> digits[3] >>> getitem(digits, 3)
Lists 8 8

Concatenation and repetition

>>> [2, 7] + digits * 2 >>> add([2, 7], mul(digits, 2))


[2, 7, 1, 8, 2, 8, 1, 8, 2, 8] [2, 7, 1, 8, 2, 8, 1, 8, 2, 8]

Nested lists
>>> pairs = [[10, 20], [30, 40]]
['Demo'] >>> pairs[1]
[30, 40]
>>> pairs[1][0]
30
4
Containers

Built-in operators for testing whether an element appears in a compound value

>>> digits = [1, 8, 2, 8]


>>> 1 in digits
True
>>> 8 in digits
True
Containers >>> 5 not in digits
True
>>> not(5 in digits)
True

(Demo)

Sequence Iteration

def count(s, value):


total = 0
for element in s:

For Statements Name bound in the first frame


of the current environment
(not a new frame)

if element == value:
total = total + 1
return total

(Demo)

8
For Statement Execution Procedure Sequence Unpacking in For Statements

A sequence of
for <name> in <expression>: fixed-length sequences
<suite>

>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]

>>> same_count = 0
1. Evaluate the header <expression>, which must yield an iterable value (a sequence)

A name for each element in a Each name is bound to a value, as in


2. For each element in that sequence, in order:
fixed-length sequence multiple assignment

A. Bind <name> to that element in the current frame


>>> for x, y in pairs:
... if x == y:
B. Execute the <suite> ... same_count = same_count + 1

>>> same_count
2

9 10

The Range Type

A range is a sequence of consecutive integers.*

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...

range(-2, 2)
Ranges Length: ending value - starting value
(Demo)
Element selection: starting value + index

>>> list(range(-2, 2)) List constructor


[-2, -1, 0, 1]

>>> list(range(4)) Range with a 0 starting value


[0, 1, 2, 3]

* Ranges can actually represent more general integer sequences.


12
List Comprehensions

[<map exp> for <name> in <iter exp> if <filter exp>]

Short version: [<map exp> for <name> in <iter exp>]

A combined expression that evaluates to a list using this evaluation procedure:


List Comprehensions
1. Add a new frame with the current frame as its parent

2. Create an empty result list that is the value of the expression

3. For each element in the iterable value of <iter exp>:


>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'm', 'n', 'o', 'p']
>>> [letters[i] for i in [3, 4, 6, 8]] A. Bind <name> to that element in the new frame from step 1

['d', 'e', 'm', 'o'] B. If <filter exp> evaluates to a true value, then add the value of <map exp>
to the result list

14

First in Line

Implement promoted, which takes a sequence s and a one-argument function f. It returns a


list with the same elements as s, but with all elements e for which f(e) is a true value
ordered first. Among those placed first and those placed after, the order stays the same.

def promoted(s, f):


"""Return a list with the same elements as s, but with all
elements e for which f(e) is a true value placed first.
Example: Promoted
>>> promoted(range(10), odd) # odds in front
[1, 3, 5, 7, 9, 0, 2, 4, 6, 8]
"""
[e for e in s if f(e)] + [e for e in s if not f(e)]
return _____________________________________________________

16

You might also like