0% found this document useful (0 votes)
7 views24 pages

week6_lecture2

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)
7 views24 pages

week6_lecture2

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/ 24

more for loops.

Week 6 | Lecture 2 (6.2)


This Week’s Content
▪ Lecture 6.1
▪ for loops
▪ Reading: 9.3, 9.4
▪ Lecture 6.2
▪ for loops on indices, nested loops
▪ Reading: 9.5 – 9.9
▪ Lecture 6.3
▪ Design Problem: Wordle
for loops name = ‘Sebastian’

▪ A for loop starts with for character in name:


the keyword for. print(character)

Output:
S
e
b
a
s
t
i
a
n
for loops name = ‘Sebastian’

▪ Next, we provide the for character in name:


name of one of more print(character)
variables.
▪ We have called the
variable character, but
you can call it whatever
you like as long as it
for item1, item2 in iterable:
follows rules for naming do something.
a variable.
for loops name = ‘Sebastian’

▪ Our variable character for character in name:


will be bound to each of print(character)
the items in the
sequence in turn.
Output:
S
e
b
a
s
t
i
a
n
for loops name = ‘Sebastian’

▪ Specify what the values for character in name:


are in. print(character)
▪ What is the iterable?
▪ An iterable is an object
that can be iterated over. Output:
▪ Strings are iterable (we S

know these from last


e
b
week). a
s
▪ Lists (next week) are t
iterable. i
a
n
for loops name = ‘Sebastian’

▪ As with the while loop, for character in name:


the for loop statement print(character)
ends with a colon.
▪ This is how Python
knows you are going to Output:
create a new block of S
e
code. b
a
s
t
i
a
n
for loops name = ‘Sebastian’

▪ Indenting four spaces for character in name:


tells Python what lines of print(character)
code are in that block
you want to repeated.
Output:
S
e
b
a
s
t
i
aIndent
n
Breakout Session 1
▪ We want to do some analysis of
Dean Yip’s Tweets.
▪ Before we can do this, we’ll need Open your
to make the tweet all lower case
and replace all the punctuations notebook
with white space.
▪ ‘impact... Exciting’ → ‘impact exciting‘
Click Link:
1. Breakout Session 1
Today’s Content

▪Looping through indices with a for loop.


▪Nested for loops.
Looping Through Indices
▪ Last lecture we saw that we can use while loops to loop
over the indices of a string.
▪ Then we saw that a for-loop requires less code but it
iterates over the values, not the indices.

while for
i = 0 for character in chrome_4:
while i < len(chrome_4): print(character)
print(i, chrome_4[i])
i += 1
Looping Through Indices

▪Can we use a for loop to loop over indices?

while for
i = 0 for character in chrome_4:
while i < len(chrome_4): print(character)
print(i, chrome_4[i])
i += 1
Looping on a range()
▪Python has a built-in function called range() that
can be used to generate a sequence of numbers. The
general syntax of range is as follows:
range(start, stop, step)
▪ Similar to the string slicing syntax:
▪ The stop value is not included in the sequence of numbers
generated.
▪ Can omit start and step which will result in default values being
used. range(n) → range(0, n, 1)
Looping on a range()
▪ range() is typically used in a for
loop to iterate over a sequence of
numbers. Open your
▪ range() is an iterable.
notebook
This thing has to be an iterable.
Click Link:
for i in range(5): 2. Using range()
print(i)
Example 1

▪Add up all the even numbers


between 1 and 100 using a
for loop and range(). Open your
notebook
▪2+4+…..+96+98+100
Click Link:
3. Example 1
Breakout Session 2
▪ Write a function that returns the
number of times that a character
and the next character are the Open your
same.
▪ If you have a bug in a loop, with notebook
probability ~1 its an off-by-one
index error. Click Link:
count_adjacent_repeats('abccdeffggh’) 4. Breakout Session 2

>>> 3
Nested for Loops for item in iterable:
do something.
▪The bodies of loops
can contain any
statement, for i in range(10, 13):
including other for j in range(1, 5):
loops! print(i, j)
▪When this occurs, it Output
is known as a 10, 1
nested loop. 10, 2
10, 3

Nested for Loops

▪The bodies of loops


can contain any
statement, Open your
including other notebook
loops!
▪When this occurs, it Click Link:
is known as a 5. Nested for Loops
nested loop.
Turtles

▪Turtle is a pre-installed Python


library that enables users to
create pictures and shapes by Open your
providing them with a virtual notebook
canvas.
Click Link:
6. Turtles
Draw A Grid

▪Use Turtle and nested for


loops to draw a grid.
Open your
notebook

Click Link:
7. Grid
Breakout Session 3
▪ Use Turtle and nested for loops to
draw the pattern below.
We just finished this.
Open your
notebook

Click Link:
4. Breakout Session 3
Lecture Recap
▪The general form of a for loops.
for item in iterable:
do something.
▪Iterable types have indices and items.
▪For loops always iterate over the items in the
iterable variable.
▪Using range(start, end, step) we can keep
track of where we are in a sequence (i.e. index).
Advanced Turtles

▪Create a function that draws a


polygon at (x, y).
Open your
notebook

Click Link:
draw_polygon(x, y, 8. Advanced Turtles
num_sides,
side_length)
more for loops.
Week 6 | Lecture 2 (6.2)

You might also like