lecture_11.pptx
Download
Report
Transcript lecture_11.pptx
For Loops
CS 160: Computer Science Orientation
Today’s Schedule
•
•
•
•
•
For Loops
Range Function
Assigning and Copying Sequences
List Comprehension
Live Coding
1
July 29, 2016
For Loops
for loop: iterative control statement that iterates once for each
element in a specified sequence of elements
• Convenient way to iterate through sequence
• Method to construct definite loops
2
July 29, 2016
For Loops
• Syntax:
for item in sequence:
statement
statement
...
3
July 29, 2016
For Loops
for keyword
for item in sequence:
statement
statement
...
4
July 29, 2016
For Loops
loop variable
for item in sequence:
statement
statement
...
5
July 29, 2016
For Loops
valid sequence (List, tuple, String)
for item in sequence:
statement
statement
...
6
July 29, 2016
For Loops
colon
for item in sequence:
statement
statement
...
7
July 29, 2016
For Loops
for item in sequence:
statement
statement
Python instructions
...
8
July 29, 2016
For Loops
• Example: List of integers
>>> nums = [10, 20, 30]
>>> for n in nums:
...
print n
...
10
20
30
>>>
9
July 29, 2016
For Loops
• Example: List of integers
>>> nums = [10, 20, 30]
>>> for n in nums:
...
print n
...
10
NOTE: the for loop variable gives
20
us each element in the sequence,
NOT the index of the element
30
>>>
10
July 29, 2016
For Loops
• Example: tuple of Strings
>>> animals = ('dog','cat','goat')
>>> for a in animals:
...
print a
...
dog
cat
goat
>>>
11
July 29, 2016
For Loops
• Example: characters within String
>>> name = 'Marc'
>>> for ch in name:
...
print ch
...
M
a
r
c
>>>
12
July 29, 2016
For Loops
• What if we want to update the sequence??
>>> nums = [10, 20, 30]
>>> for n in nums:
...
n = n * 2
...
>>> nums
[10, 20, 30]
List was NOT updated
>>>
13
July 29, 2016
For Loops
• What if we want to update the sequence??
>>> nums = [10, 20, 30]
>>> nums[0] = nums[0] * 2
>>> nums[1] = nums[1] * 2
>>> nums[2] = nums[2] * 2
>>> nums
[20,40,60]
must use index to update
14
July 29, 2016
For Loops
What if we want to update a LARGE sequence??
• ... use the range() function!
15
July 29, 2016
Today’s Schedule
•
•
•
•
•
For Loops
Range Function
Assigning and Copying Sequences
List Comprehension
Live Coding
16
July 29, 2016
Range Function
• range() function generates a sequence of integers
• Convenient way to create list of numbers that for loop can
iterate through
17
July 29, 2016
Range Function
• range( N ) – generates a sequence from 0 through N-1
>>> r = range(10)
>>> r
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
18
July 29, 2016
Range Function
• range( M, N ) – generates a sequence from M through N-1
>>> r = range(5,8)
>>> r
[5, 6, 7]
>>>
19
July 29, 2016
Range Function
• range( M, N, S ) – generates a sequence from M through N-1
with a given step value S
>>> r = range(0,10,2)
>>> r
[0, 2, 4, 6, 8]
>>>
20
July 29, 2016
Range Function
• range( M, N, S ) – S value can be negative to step “backwards”
>>> r = range(10,0,-2)
>>> r
[10, 8, 6, 4, 2]
>>>
21
July 29, 2016
Range Function
• range() is often paired with for loops
>>> for i in range(5):
...
print i
...
0
1
2
3
4
>>>
22
July 29, 2016
Range Function
• Now we can update a sequence
>>> nums = [10, 20, 30]
>>> for i in range(3):
...
nums[i] = nums[i] * 2
...
>>> nums
[20, 40, 60]
>>>
23
July 29, 2016
Range Function
• Now we can update a sequence
>>> nums = [10, 20, 30]
>>> for i in range(3):
...
nums[i] = nums[i] * 2
...
>>> nums
What if we don’t know
[20, 40, 60]
the sequence length
>>>
?
24
July 29, 2016
Range Function
• Use len( ) function to get sequence length
>>> nums = [10, 20, 30]
>>> for i in range(len(nums)):
...
nums[i] = nums[i] * 2
...
>>> nums
[20, 40, 60]
>>>
25
July 29, 2016
Today’s Schedule
•
•
•
•
•
For Loops
Range Function
Assigning and Copying Sequences
List Comprehension
Live Coding
26
July 29, 2016
Assigning and Copying
• What happens with the following?
>>>
>>>
>>>
>>>
???
>>>
???
>>>
27
July 29, 2016
L1 = [10,20,30]
L2 = L1
L2[0] = -999
L1
L2
Assigning and Copying
• What happens with the following?
>>> L1 = [10,20,30]
>>> L2 = L1
>>> L2[0] = -999
>>> L1
[-999, 20, 30]
>>> L2
[-999, 20, 30]
>>>
28
July 29, 2016
Assigning and Copying
• What happens with the following?
>>> L1 = [10,20,30]
>>> L2 = L1
>>> L2[0] = -999
>>> L1
[-999, 20, 30]
>>> L2
[-999, 20, 30]
>>>
29
July 29, 2016
When I modify L2, L1
gets modified as well!!
Assigning and Copying
• What happens with the following?
>>> L1 = [10,20,30]
>>> L2 = L1
>>> L2[0] = -999
>>> L1
[-999, 20, 30]
>>> L2
[-999, 20, 30]
>>>
30
July 29, 2016
It’s because L2 and L1
both refer to the same
List
And remember, Lists
are mutable, so they
CAN be changed
Assigning and Copying
• To make a copy of a List, use the list() function
>>> L1 = [10, 20, 30]
>>> L2 = list(L1)
>>> L2[0] = -999
>>> L1
[10, 20, 30]
>>> L2
[-999, 20, 30]
>>>
31
July 29, 2016
Assigning and Copying
• To make a copy of a List, use the list() function
>>> L1 = [10, 20, 30]
>>> L2 = list(L1)
>>> L2[0] = -999
>>> L1
[10, 20, 30]
>>> L2
[-999, 20, 30]
>>>
32
July 29, 2016
separate copies
Assigning and Copying
• Issue of assign vs. copy does not occur with immutable
sequences like Tuples and Strings
• Copies are always made
33
July 29, 2016
Today’s Schedule
•
•
•
•
•
For Loops
Range Function
Assigning and Copying Sequences
List Comprehension
Live Coding
34
July 29, 2016
List Comprehension
• Very powerful feature of Python
• Generate more complicated sequences using very little code
>>> [x**2 for x in [1,2,3]]
[1, 4, 9]
>>>
35
July 29, 2016
List Comprehension
[x**2 for x in [1,2,3]]
create a List
containing...
36
July 29, 2016
List Comprehension
[x**2 for x in [1,2,3]]
create a List containing...
the result of some expression...
37
July 29, 2016
List Comprehension
[x**2 for x in [1,2,3]]
create a List containing...
the result of some expression...
applied to elements in a sequence.
38
July 29, 2016
List Comprehension
>>> [x**2 for x in range(5)]
[0, 1, 4, 9, 16]
>>>
>>> [ord(ch) for ch in 'Hello']
[72, 101, 108, 108, 111]
>>>
39
July 29, 2016
List Comprehension
• Can also add a conditional to include certain elements
>>> vowels = ('a’,'e’,'i','o','u')
>>> [ch for ch in 'hello' if ch in vowels]
['e', 'o']
>>>
40
July 29, 2016
Today’s Schedule
•
•
•
•
•
For Loops
Range Function
Assigning and Copying Sequences
List Comprehension
Live Coding
41
July 29, 2016
Live Coding
Practice with:
• for loops
• range() function
• list comprehension
42
July 29, 2016