0% found this document useful (0 votes)
9 views

week6_lecture1

python lesson
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)
9 views

week6_lecture1

python lesson
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/ 34

for loops.

Week 6 | Lecture 1 (6.1)


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: Madlibs
Looping (Iterating)
List of
Customers
▪ Looping means repeating Looping
something over and over
until a particular condition
is satisfied.

Send
Email Promotional
Email
Looping (Iterating)
List of
Tweets
▪ Looping means repeating Looping
something over and over
until a particular condition
is satisfied.

Does the
Tweet
Yes/No
contain
#cleancode
Looping (Iterating)
▪ Looping means repeating
something over and over
until a particular condition
is satisfied.

Check
for Looping
Yes/No
Waldo
Looping (Iterating)
▪ Looping means repeating
something over and over
until a particular condition
is satisfied.

Check
for Looping
Yes/No
Waldo
Looping (Iterating)
▪ Looping means repeating
something over and over
until a particular condition
is satisfied.

Check
for Looping
Yes/No
Waldo
Looping (Iterating)
▪ Looping means repeating
something over and over
until a particular condition
is satisfied.

Check
for Looping
Yes/No
Waldo
Looping (Iterating)
▪ Looping means repeating
something over and over
until a particular condition
is satisfied.

Check
for Looping
Yes/No
Waldo
for loops
▪ There are several ways to while expression:
repeat a block of code. do something.
▪ We’ve already seen
while loops and this
week, we’ll discuss for
loops. for item in iterable:
▪ Do Something = block of do something.
code we want to execute.
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
for loops name = ‘Sebastian’

▪ What output should we for character in name:


get? print(character)

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

▪ What output should we for character in name:


get? print(character)

Output:
S
e
b
a
s
t
i
a
n
for loops
▪ Let’s try it ourselves.

Open your
notebook

Click Link:
1. Your first for loop
for vs while
▪ You’ve learned about for loops and
while loops, but when should you use
them?
▪ Firstly, all for loops can be written as
while loops, and vice-versa.
▪ You should use a for loop when you
know how many times the loop should
run.
▪ If you want the loop to break based on a
condition (do this until….) you should
use a while loop.
for vs while
▪ Problem: You have had your DNA
sequenced and each of your
chromosomes is represented by a string
of nucleotides: adenine (A), thymine (T), Open your
guanine (G), and cytosine (C).
notebook
▪chrome_4 = ATGGGCAA
▪ Create a function to count the number
of occurrences of a nucleotides. Click Link:
2. while vs for
my_func(chrome_4, ‘A’)
>>> 3
while for
i = 0 counter = 0
counter = 0 for character in chrome_4:
while i < len(chrome_4): if character == 'A':
if chrome_4[i] == 'A': counter += 1
counter += 1
i += 1
▪ Differences
while for
i = 0 counter = 0
counter = 0 for character in chrome_4:
while i < len(chrome_4): if character == 'A':
if chrome_4[i] == 'A': counter += 1
counter += 1
i += 1
▪ Differences
▪ In the while loop, the loop variable (i) was the
index of each character, while in the for loop the
loop variable (character) is the value of each
character.
▪ No indexing [i] required in the for loop.
while for
i = 0 counter = 0
counter = 0 for character in chrome_4:
while i < len(chrome_4): if character == 'A':
if chrome_4[i] == 'A': counter += 1
counter += 1
i += 1
▪ Differences
▪ We do not have to worry about how long the
string is (e.g., use len()) because the for loop will
go through every character of the string exactly
once.
while for
i = 0 counter = 0
counter = 0 for character in chrome_4:
while i < len(chrome_4): if character == 'A':
if chrome_4[i] == 'A': counter += 1
counter += 1
i += 1
▪ Differences
▪ We do not have to worry about incrementing the
loop variable (i += 1) as the for loop takes care
of this.
while for
i = 0 counter = 0
counter = 0 for character in chrome_4:
while i < len(chrome_4): if character == 'A':
if chrome_4[i] == 'A': counter += 1
counter += 1
i += 1 4 lines
▪ Differences
6 lines ▪ The for loop is MUCH easier to read and therefore,
desirable when writing code for large collaborative
projects.
▪ #cleancode
for vs while
▪ You should use a for loop when you
know how many times the loop should
run.
▪ If you want the loop to break based on a
condition (do this until….) you should
use a while loop.
while & for Loops
List of
Customers
▪ for loop or while loop? Looping

Send
Email Promotional
Email
while & for Loops
List of
Tweets
▪ for loop or while loop? Looping

Does the
Tweet
Yes/No
contain
#cleancode
while & for Loops
▪ for loop or while loop?

Check
for Looping
Yes/No
Waldo
Breakout Session 1 Vowels: a, e, i, o, u.

▪ Write a function that takes in a


string and returns the number of
vowels in the string. (use for loop) Open your
▪ Test 1
▪ count_vowels('Happy notebook
Anniversary!')
▪5 Click Link:
▪ Test 2 3. Breakout Session 1
▪ count_vowels('xyz')
▪0
Breakout Session 2
▪ Write a function to return the
unique separators in a string of
integer codes. (use for loop)
▪ The string only contains integers Open your
and separators. notebook
▪ Test 1
▪ find_seperators('23,613-23;2:45')
▪ ',-;:' Click Link:
▪ Test 2 4. Breakout Session 2
▪ find_seperators('613-555-3224')
▪ '-'
Lecture Recap
Practice!
▪for loops.
▪Looping over strings.
▪When to use a for loop over a while loop.
for loops.
Week 6 | Lecture 1 (6.1)

You might also like