week6_lecture1
week6_lecture1
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’
Output:
S
e
b
a
s
t
i
a
n
for loops name = ‘Sebastian’
Output:
S
e
b
a
s
t
i
a
n
for loops name = ‘Sebastian’
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.