Professional Documents
Culture Documents
Week 7 Bfor Loop
Week 7 Bfor Loop
Week 7: Loops
Computer Programming
Repetitive Structures
Structures
Computer Programming
Python Loops
• A loop is a programming structure that repeats a sequence of
instructions until a specific condition is met. Loops allow
you to repeat a process over and over without having to
write the same (potentially long) instructions each time
you want your program to perform a task.
Computer Programming
for loop
for loop
Computer Programming
for loop
Python Loops
Computer Programming
Python Loops
Python Loops
Python Loops
Example
Solution
Example
Solution
Example
Solution
for loop
Range
range() function returns a sequence of numbers,
• Starting from 0 by default,
• Increments by 1 (by default)
• Stops before a specified number
Computer Programming
for loop
Range
Computer Programming
for loop
Example
Computer Programming
for loop
Solution
Computer Programming
Range
n = 4 n = 4
for k in range(n): for k in range(1,n):
print k print k
0 1
Output: 1 2
Output:
2 3
3
“Counting from Here to
(Almost) There”
Here = 20
There = 24
for k in range(Here,There):
print k
20
21
Output: 22
23
“Counting Down”
Here = 20
There = 24
for k in range(There,Here,-1):
print k
24
23
Output: 22
21
for loop
Computer Programming
for loop
Computer Programming
for loop
Computer Programming
for loop
Week 5: Introduction
Computer Programming
to Python 27 / 28
for loop
Computer Programming
for loop
Example
Computer Programming
for loop
Solution
Computer Programming
for loop
Example
Computer Programming
for loop
Solution
Computer Programming
for loop
Example
Computer Programming
Iterating Through a String
Output:
s = ‘abcd’
a
for c in s: b
print c c
d
Output:
n = 4
0
for k in range(n):
1
print k 2
3
n = 4 s = ‘abcd’
for k in range(n): for c in s:
print k print c
0 a
Output: 1 Output: b
2 c
3 d
Example
n = 4
s = 0
for k in range(n):
x = 2**k
s = s + x Output:
print s
Loop Body
k is increased by 1
1+2+4+8
s = 0
for k in range(4): s -> 1
x = 2**k
k -> 1
s = s + x
print s x -> 1
1+2+4+8
s = 0
for k in range(4): s -> 1
x = 2**k
k -> 1
s = s + x
print s x -> 1
k is increased by 1
1+2+4+8
s = 0
for k in range(4): s -> 3
x = 2**k
k -> 2
s = s + x
print s x -> 2
1+2+4+8
s = 0
for k in range(4): s -> 3
x = 2**k
k -> 2
s = s + x
print s x -> 2
k is increased by 1
1+2+4+8
s = 0
for k in range(4): s -> 7
x = 2**k
k -> 3
s = s + x
print s x -> 4
1+2+4+8
s = 0
for k in range(4): s -> 7
x = 2**k
k -> 3
s = s + x
print s x -> 4
k is increased by 1
1+2+4+8
s = 0
for k in range(4): s -> 15
x = 2**k
k -> 4
s = s + x
print s x -> 8
1+2+4+8
s = 0
for k in range(4): s -> 15
x = 2**k
k -> 4
s = s + x
print s x -> 8
Output
15
More General:
1 + 2 + 4 + … + 2**(n-1)
Loop Body
Computer Programming
Continue
• a continue keyword is an expression used in the
loops that allows us to skip this iteration and return
to the beginning of the loop
Week 5: Introduction
Computer Programming
to Python 67 / 28
Example
• Enter 10 integer numbers and find the
largest of them and print the screen
Computer Programming
Solution
• Enter 10 integer numbers and find the
largest of them and print the screen
Computer Programming
Example
Computer Programming
Solution
Computer Programming
Example
Computer Programming
Solution
Computer Programming
Example
Computer Programming
Solution
Computer Programming
Example
Computer Programming
Solution
Computer Programming
Example
Write a code for giving following output
Computer Programming
Solution
Computer Programming