Loops and Repetition in Programming

C
S
E
 
1
3
2
1
M
o
d
u
l
e
 
3
 
 
P
a
r
t
 
2
Loops and Repetition
T
o
p
i
c
s
Motivation
while
 Loop Statement
for
 
Loop Statement
Infinite Loops
Nested Loops
Using
 
break
 
and
 
continue
M
o
t
i
v
a
t
i
o
n
You want to print a string (e.g., "Welcome to CSE 1321!") a
thousand times
. You could copy/paste the following:
print(
"Welcome to CSE 1321!"
)
There has to be a better way!  How do we solve this problem?
USING LOOPS
L
o
o
p
 
S
t
a
t
e
m
e
n
t
s
Loops are 
repetition statements
 that allow us to execute a
statement (or block of statements) multiple times
 They are controlled by B
oolean expressions
 We will study two types of loop statements:
the 
while
 loop
the 
for
 loop
We must 
choose the right type
 for the situation at hand
L
o
o
p
 
S
t
a
t
e
m
e
n
t
s
The 
while
 loop runs 
undetermined
 (unknown) number of
iterations
The 
for
 loop, on the other hand, runs a 
pre-determined
(known) number of iterations (sometimes called a 
counting
loop)
G
e
n
e
r
a
l
 
c
r
i
t
e
r
i
a
 
f
o
r
 
l
o
o
p
s
1.
 Ha
ve
 
some
 
in
i
ti
a
l 
c
o
n
di
t
ion
Star
t
i
n
g
 
a
 
c
o
unt
er
Beg
i
n
n
i
n
g
 
in 
a
 
ce
rta
in 
s
ta
t
e
2.
  M
u
st
 
h
a
ve
 
a 
t
est
 
to
 
cont
i
n
u
e
3.
M
u
st
 
m
a
ke
 
pr
o
gr
e
ss
 
t
o
war
d
s
 
fi
n
ishi
n
g
w
h
i
l
e
 
L
o
o
p
 
S
t
a
t
e
m
e
n
t
A 
while
 loop (statement) has the following syntax:
while
 (
condition
):
    statement block
 
//loop body
If the 
condition is 
true
, the statement block is executed
Then the condition is evaluated again, and executes the statement
until the condition becomes 
false
 If the condition is false initially, the statement (loop body) is 
never
executed
 Therefore, the body of a 
while
 loop will execute 
zero or more times
while
 Loop Logic
N
o
t
e
:
 
I
f
 
t
h
e
 
i
n
i
t
i
a
l
 
e
v
a
l
u
a
t
i
o
n
o
f
 
t
h
e
 
c
o
n
d
i
t
i
o
n
 
i
s
 
f
a
l
s
e
,
 
 
t
h
e
l
o
o
p
 
b
o
d
y
 
e
x
e
c
u
t
e
s
 
z
e
r
o
t
i
m
e
s
.
 
T
h
e
r
e
f
o
r
e
,
 
t
h
e
 
w
h
i
l
e
l
o
o
p
 
e
x
e
c
u
t
e
s
 
z
e
r
o
 
o
r
 
m
o
r
e
t
i
m
e
s
Example: Writing 1,000 Sentences
(using a while loop)
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
counter
0
Example: Writing 1,000 Sentences
(using a while loop)
counter = 0
while
 
counter < 1000
:
 
print (str(counter) + “ I will not…”)
counter
0
Example: Writing 1,000 Sentences
(using a while loop)
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
Output: 
0
 I will not eat at Taco Bell again
counter
0
Example: Writing 1,000 Sentences
(using a while loop)
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
counter
0
Example: Writing 1,000 Sentences
(using a while loop)
counter = 0
while
 
counter < 1000
:
 
print (str(counter) + “ I will not…”)
counter
0
Example: Writing 1,000 Sentences
(using a while loop)
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
Output: 
0
 I will not eat at Taco Bell again
counter
0
Example: Writing 1,000 Sentences
(using a while loop)
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
counter
0
Example: Writing 1,000 Sentences
(using a while loop)
counter = 0
while
 
counter < 1000
:
 
print (str(counter) + “ I will not…”)
counter
0
Example: Writing 1,000 Sentences
(using a while loop)
counter = 0
while
 counter < 1000):
 
print (str(counter) + “ I will not…”)
Output: 
0
 I will not eat at Taco Bell again
counter
0
Infinite Loops
This loop isn’t making a lot of progress!
Loops that repeat forever are called 
infinite loops
Apparently “lock up”
Output:
0
 I will not eat at Taco Bell again
0
 I will not eat at Taco Bell again
0
 I will not eat at Taco Bell again
0
 I will not eat at Taco Bell again
 
.
 
.
 
.
Problem Solved
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
 
counter += 1
counter
0
Problem Solved
counter = 0
while
 
counter < 1000
:
 
print (str(counter) + “ I will not…”)
 
counter += 1
counter
0
Problem Solved
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
 
counter += 1
Output: 
0
 I will not eat at Taco Bell again
counter
0
Problem Solved
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
 
counter += 1
#
 
R
e
m
e
m
b
e
r
,
 
c
o
u
n
t
e
r
 
+
=
 
1
;
 
i
s
 
t
h
e
 
s
a
m
e
 
a
s
#
 
c
o
u
n
t
e
r
 
=
 
c
o
u
n
t
e
r
 
+
 
1
counter
1
Problem Solved
counter = 0
while
 
counter < 1000
:
 
print (str(counter) + “ I will not…”)
 
counter += 1
counter
1
Problem Solved
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
 
counter += 1
Output: 
1
 I will not eat at Taco Bell again
counter
1
Problem Solved
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
 
counter += 1
counter
2
Problem Solved
counter = 0
while
 
counter < 1000
:
 
print (str(counter) + “ I will not…”)
 
counter += 1
counter
2
Problem Solved
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
 
counter += 1
Output: 
2
 I will not eat at Taco Bell again
counter
2
Problem Solved
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
 
counter += 1
counter
3
How does it end?
counter = 0
while
 
counter < 1000
:
 
print (str(counter) + “ I will not…”)
 
counter += 1
counter
999
Problem Solved
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
 
counter += 1
Output: 
999
 I will not eat at Taco Bell again
counter
999
Problem Solved
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
 
counter += 1
counter
1000
Problem Solved
counter = 0
while
 counter < 1000:
 
print (str(counter) + “ I will not…”)
 
counter += 1
# So we never print out
# 1000 I will not eat at Taco Bell again
counter
1000
How does it end?
counter = 0
while
 
counter <= 1000
:
 
print (str(counter) + “ I will not…”)
 
counter += 1
counter
1000
now 
True
S
e
n
t
i
n
e
l
 
V
a
l
u
e
s
 
Question:
 How can 
the user
 control a while loop?
A 
sentinel value
 is a 
special input value
 that represents the
end of inputs from the user
The 
sentinel value
 should be included in the prompt so that
the user knows how to stop the loop. For example,
 
 
PRINTLINE
(
“Enter a grade (type 9999 to quit):”
)
 
A 
sentinel value
 
gives the user control
 over the loop
I
n
p
u
t
 
V
a
l
i
d
a
t
i
o
n
A 
while
 loop can be used for 
input validation
,
making a program more robust
Ensure correct input values
 before processing
Can 
issue error messages 
for invalid data
In-class Problem:
 Input Validation
Problem Statement: Write a program in which you allow
your user to guess a 
secret number between 1 and 10
.
For this example, set your secret number to a literal for
easy testing.  When you are done, think about how you
can modify the program to allow the user to continue to
guess until they get it right.  Also, think about how you
could use conditionals to give them valuable feedback to
reach the answer!
I
n
p
u
t
 
V
a
l
i
d
a
t
i
o
n
 
E
x
a
m
p
l
e
if __name__ == “__main__”:
    secretNum = 5
    userGuess = int(input(
"Enter a number between 1 and 10: "
))
    while
 1 < userGuess < 10:
        userGuess = input(
"Not between 1 and 10. Try again!"
)
    if
 userGuess == secretNum:
        print(str(userGuess) + 
" is the secret number!"
)
    else
:
        print(str(userGuess) + 
" isn't the secret number!"
)
for
 Loop
In Python, 
for
 loops iterate through items in
a sequence
Common sequences include lists (more on
those later), strings, or a range of numbers
for
 Loop syntax
for
 element in iterable:
    pass 
# code
e.g.:
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
, end=
“”
)
#A-l-i-c-e-
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
Output:
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
letter = 
“A”
Output:
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
letter = 
“A”
Output:
A-
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
letter = 
“l”
Output:
A-
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
letter = 
“l”
Output:
A-l-
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
letter = 
“i”
Output:
A-l-
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
letter = 
“i”
Output:
A-l-i-
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
letter = 
“c”
Output:
A-l-i-
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
letter = 
“c”
Output:
A-l-i-c-
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
letter = 
“e”
Output:
A-l-i-c-
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
letter = 
“e”
Output:
A-l-i-c-e-
for
 Loop in action
name = 
“Alice”
for
 letter in name:
    print(letter + 
“-”
 , end=
“”
)
M
e
m
o
r
y
name = 
“Alice”
Output:
A-l-i-c-e-
for
 Loop syntax
We can also iterate through a range() of numbers
With a single input, range() iterates from 0 up to (input – 1)
With 2 inputs, range() iterates from the left number to
(right number – 1)
With 3 inputs, range() iterates from the left number to
(middle number – 1) skipping (right number). The right
number is usually called the “step”
for
 Loop syntax
for
 num in range(10):
    print(num)
# prints the following
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
for
 Loop syntax
for
 num in range(5, 10):
    print(num)
# prints the following
# 5
# 6
# 7
# 8
# 9
for
 Loop syntax
for
 num in range(0, 10, 2):
    print(num)
# with a step of 2, prints the following
# 0
# 2
# 4
# 6
# 8
In-class Problem:
Problem Statement: Write a program in which you
enable your user to enter a number for which they
want to see the multiplication table.
for 
Loop
 
Example
if __name__ == “__main__”:
    choice = int(input(
"Enter a number to see the table: "
))
    for
 multiplier in range(11):
        print(str(multiplier) + 
" * " 
+ str(choice) + 
" = " 
+ str(multiplier * choice))
5
.
 
 
I
n
f
i
n
i
t
e
 
L
o
o
p
s
The body of a loop must eventually make the condition false
If not, it is called 
an 
infinite loop
, which will execute until the
user interrupts the program
This is a common 
logical error 
 so double check!
E
x
a
m
p
l
e
An example of an infinite loop:
count = 1
while
 (count <= 25):
   
print(
count)
   count -= 1 
#Error
This loop will continue executing until interrupted or until an
underflow error occurs
B
e
 
C
a
r
e
f
u
l
!
Always check that the
 
condition
 
can eventually become 
False
so the loop can terminate
Not so much of a problem in iterative loops
Definitively a problem in 
while
 loops!
6
.
 
 
N
e
s
t
e
d
 
L
o
o
p
s
Similar to nested 
Selection
 statements, loops
can 
contain other loop statements
For each iteration of the outer loop, the inner loop
iterates completely
Nested for 
Loop Example
Problem Statement: Write a program that
uses 
nested FOR loops 
to print 10 rows of
stars, as shown in the picture to the right.
Python
 – Nested for 
Loop Example
# key insight: the outer loop is in charge of printing rows
# the inner loop is in charge of printing columns
If __name__ == “__main__”:
    for 
row in range(10):
        
for
 column in range(row):
            print(
"*"
, end=
""
)
        print() 
# once a row is done, we must skip to the next one
Nested Loops Iterations
How many times will the string 
"I am here"
 be printed?
if __name__ == “__main__”:
    count1 = 1
    
while
 count1 <= 10:
        count2 = 1
        
while
 count2 <= 5:
            print(
"I am here!“
, end=
“”
)
            count2 += 1
        print()
        count1 += 1
 
 
7
.
 
U
s
i
n
g
 
b
r
e
a
k
 
a
n
d
 
c
o
n
t
i
n
u
e
We can additionally control the flow of how loops
work with keywords 
break
 and 
continue
break
 stops the loop
continue
 “skips” the current iteration
E
x
a
m
p
l
e
 
o
f
 
b
r
e
a
k
sum = 0
number = 0
while
 (number < 20):
    number = number + 1
    sum = sum + number
    
if
 (sum >= 100):  
// stop if sum is over 100
        
break
print(
"The number is “ 
+ str(number))
print(
"The sum is “
 + str(sum))
E
x
a
m
p
l
e
 
o
f
 
c
o
n
t
i
n
u
e
sum = 0
number = 0
while
 (number < 10) :
    number = number + 1
    
if
 (number == 5 or number == 6) :
        
continue
;  
// do not add 5 and 6 to sum
    sum = sum + number
print(
"The number is “
 + str(number))
print(
"The sum is “
 + str(sum))
 
 
 
 
I
n
-
c
l
a
s
s
 
P
r
o
b
l
e
m
Write code for a calculator program that enables the
user to select an operation (Add, Subtract, Multiply,
Divide, or Quit) and then enter a series of numbers
where that operation is applied.  The program should
continue asking for numbers until the user enters a 0,
at which point it asks for another operation.
I
n
-
c
l
a
s
s
 
P
r
o
b
l
e
m
 
#
2
Problem Statement: Write a program that asks the user to input a
number.  The program should contain a while loop that runs if the user
inputs any value other than 0.  The loop body should accumulate the
sum of all numbers that the user inputs.  During each iteration, output
the sum of the numbers input so far.  When the user inputs a 0, the
loop stops.  After the loop has stopped, output the total of all numbers
input and the average; if the loop never ran, indicate this to the user.
Slide Note
Embed
Share

Loops in programming allow us to execute a set of statements multiple times based on certain conditions. This content covers the motivation behind using loops, different types of loops like while and for loops, criteria for choosing the right loop, and the syntax and logic of while loops. By understanding loops, programmers can efficiently solve repetitive tasks and improve code readability and maintainability.

  • Programming
  • Loops
  • Repetition
  • Syntax
  • Logic

Uploaded on Sep 10, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. CSE 1321 CSE 1321 Module 3 Module 3 Part 2 Part 2 Loops and Repetition

  2. Topics Topics Motivation while Loop Statement forLoop Statement Infinite Loops Nested Loops Using break and continue

  3. Motivation Motivation You want to print a string (e.g., "Welcome to CSE 1321!") a thousand times. You could copy/paste the following: print("Welcome to CSE 1321!") There has to be a better way! How do we solve this problem? USING LOOPS

  4. Loop Statements Loop Statements Loops are repetition statements that allow us to execute a statement (or block of statements) multiple times They are controlled by Boolean expressions We will study two types of loop statements: the while loop the for loop We must choose the right type for the situation at hand

  5. Loop Statements Loop Statements The while loop runs undetermined (unknown) number of iterations The for loop, on the other hand, runs a pre-determined (known) number of iterations (sometimes called a counting loop)

  6. General c General criteria riteriafor for l loops oops 1. Have some initial condition Starting a counter Beginning in a certain state 2. Must have a test to continue 3. Must make progress towards finishing

  7. while Loop Statement Loop Statement A while loop (statement) has the following syntax: while (condition): statement block //loop body If the condition is true, the statement block is executed Then the condition is evaluated again, and executes the statement until the condition becomes false If the condition is false initially, the statement (loop body) is never executed Therefore, the body of a while loop will execute zero or more times

  8. while Loop Logic Note: If the initial evaluation of the condition is false, the loop body executes zero times. Therefore, the while loop executes zero or more times

  9. Example: Writing 1,000 Sentences (using a while loop) counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not )

  10. Example: Writing 1,000 Sentences (using a while loop) counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not )

  11. Example: Writing 1,000 Sentences (using a while loop) counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not ) Output: 0 I will not eat at Taco Bell again

  12. Example: Writing 1,000 Sentences (using a while loop) counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not )

  13. Example: Writing 1,000 Sentences (using a while loop) counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not )

  14. Example: Writing 1,000 Sentences (using a while loop) counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not ) Output: 0 I will not eat at Taco Bell again

  15. Example: Writing 1,000 Sentences (using a while loop) counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not )

  16. Example: Writing 1,000 Sentences (using a while loop) counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not )

  17. Example: Writing 1,000 Sentences (using a while loop) counter = 0 counter 0 while counter < 1000): print (str(counter) + I will not ) Output: 0 I will not eat at Taco Bell again

  18. Infinite Loops This loop isn t making a lot of progress! Loops that repeat forever are called infinite loops Apparently lock up Output: 0 I will not eat at Taco Bell again 0 I will not eat at Taco Bell again 0 I will not eat at Taco Bell again 0 I will not eat at Taco Bell again . . .

  19. Problem Solved counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not ) counter += 1

  20. Problem Solved counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not ) counter += 1

  21. Problem Solved counter = 0 counter 0 while counter < 1000: print (str(counter) + I will not ) counter += 1 Output: 0 I will not eat at Taco Bell again

  22. Problem Solved counter = 0 counter 1 while counter < 1000: print (str(counter) + I will not ) counter += 1 # Remember, counter += 1; is the same as # Remember, counter += 1; is the same as # counter = counter + 1 # counter = counter + 1

  23. Problem Solved counter = 0 counter 1 while counter < 1000: print (str(counter) + I will not ) counter += 1

  24. Problem Solved counter = 0 counter 1 while counter < 1000: print (str(counter) + I will not ) counter += 1 Output: 1 I will not eat at Taco Bell again

  25. Problem Solved counter = 0 counter 2 while counter < 1000: print (str(counter) + I will not ) counter += 1

  26. Problem Solved counter = 0 counter 2 while counter < 1000: print (str(counter) + I will not ) counter += 1

  27. Problem Solved counter = 0 counter 2 while counter < 1000: print (str(counter) + I will not ) counter += 1 Output: 2 I will not eat at Taco Bell again

  28. Problem Solved counter = 0 counter 3 while counter < 1000: print (str(counter) + I will not ) counter += 1

  29. How does it end? counter = 0 counter 999 while counter < 1000: print (str(counter) + I will not ) counter += 1

  30. Problem Solved counter = 0 counter 999 while counter < 1000: print (str(counter) + I will not ) counter += 1 Output: 999 I will not eat at Taco Bell again

  31. Problem Solved counter = 0 counter 1000 while counter < 1000: print (str(counter) + I will not ) counter += 1

  32. Problem Solved counter = 0 counter 1000 while counter < 1000: print (str(counter) + I will not ) counter += 1 # So we never print out # 1000 I will not eat at Taco Bell again

  33. How does it end? counter = 0 now True counter 1000 while counter <= 1000: print (str(counter) + I will not ) counter += 1

  34. Sentinel Values Sentinel Values Question: How can the user control a while loop? A sentinel value is a special input value that represents the end of inputs from the user The sentinel value should be included in the prompt so that the user knows how to stop the loop. For example, PRINTLINE( Enter a grade (type 9999 to quit): ) A sentinel value gives the user control over the loop

  35. Input Validation Input Validation A while loop can be used for input validation, making a program more robust Ensure correct input values before processing Can issue error messages for invalid data

  36. In-class Problem: Input Validation Problem Statement: Write a program in which you allow your user to guess a secret number between 1 and 10. For this example, set your secret number to a literal for easy testing. When you are done, think about how you can modify the program to allow the user to continue to guess until they get it right. Also, think about how you could use conditionals to give them valuable feedback to reach the answer!

  37. Input Validation Input ValidationExample Example if __name__ == __main__ : secretNum = 5 userGuess = int(input("Enter a number between 1 and 10: ")) while 1 < userGuess < 10: userGuess = input("Not between 1 and 10. Try again!") if userGuess == secretNum: print(str(userGuess) + " is the secret number!") else: print(str(userGuess) + " isn't the secret number!")

  38. for Loop In Python, for loops iterate through items in a sequence Common sequences include lists (more on those later), strings, or a range of numbers

  39. for Loop syntax for element in iterable: pass # code e.g.: name = Alice for letter in name: print(letter + - , end= ) #A-l-i-c-e-

  40. for Loop in action Memory name = Alice name = Alice for letter in name: print(letter + - , end= ) Output:

  41. for Loop in action Memory name = Alice name = Alice letter = A for letter in name: print(letter + - , end= ) Output:

  42. for Loop in action Memory name = Alice name = Alice letter = A for letter in name: print(letter + - , end= ) Output: A-

  43. for Loop in action Memory name = Alice name = Alice letter = l for letter in name: print(letter + - , end= ) Output: A-

  44. for Loop in action Memory name = Alice name = Alice letter = l for letter in name: print(letter + - , end= ) Output: A-l-

  45. for Loop in action Memory name = Alice name = Alice letter = i for letter in name: print(letter + - , end= ) Output: A-l-

  46. for Loop in action Memory name = Alice name = Alice letter = i for letter in name: print(letter + - , end= ) Output: A-l-i-

  47. for Loop in action Memory name = Alice name = Alice letter = c for letter in name: print(letter + - , end= ) Output: A-l-i-

  48. for Loop in action Memory name = Alice name = Alice letter = c for letter in name: print(letter + - , end= ) Output: A-l-i-c-

  49. for Loop in action Memory name = Alice name = Alice letter = e for letter in name: print(letter + - , end= ) Output: A-l-i-c-

  50. for Loop in action Memory name = Alice name = Alice letter = e for letter in name: print(letter + - , end= ) Output: A-l-i-c-e-

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#