LOOPS IN PYTHON
FOR LOOP EXAMPLE 1
 Code:
 for i in range(1, 6):
print(i)
 Output: 1 2 3 4 5
FOR LOOP EXAMPLE 2
 Code:
 fruits = ['apple', 'banana', 'mango']
for item in fruits:
print(item)
 Output: apple, banana, mango
WHILE LOOP EXAMPLE
 Code:
 num = 1
while num <= 5:
print(num)
num += 1
 Output: 1 2 3 4 5
PATTERN PROGRAM 1: SQUARE
 Code:
 for i in range(3):
for j in range(3):
print('*', end=' ')
print()
 Output:
* * *
* * *
* * *
PATTERN PROGRAM 2: TRIANGLE
 Code:
 for i in range(1, 6):
print('*' * i)
 Output:
*
**
***
****
*****
PATTERN PROGRAM 3: NUMBER PYRAMID
 Code:
 for i in range(1, 6):
for j in range(1, i+1):
print(j, end=' ')
print()
 Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
WORKSHEET – LOOP QUESTIONS
 Solve the following:
 1.Write a for loop to print numbers from 10 to 1.
 2.Write a while loop to print even numbers from 2 to 20.
 3.Write a loop to print each character in the string 'PYTHON'.
 4.Write a program to print a right-angle star triangle.
 5.Write a program using break to stop when number == 5.
FOR LOOP TO PRINT NUMBERS FROM 10 TO 1
Code:
for i in range(10, 0, -1):
print(i)
WHILE LOOP TO PRINT EVEN NUMBERS
FROM 2 TO 20
num = 2
while num <= 20:
print(num)
num += 2
LOOP TO PRINT EACH CHARACTER IN THE
STRING 'PYTHON'
for ch in "PYTHON":
print(ch)
PROGRAM TO PRINT A RIGHT-ANGLE STAR
TRIANGLE
for i in range(1, 6):
print("*" * i)
PROGRAM USING BREAK TO STOP WHEN
NUMBER == 5
for i in range(1, 11):
if i == 5:
break
print(i)
SIMPLE COMPARISON TABLE
Feature For Loop While Loop
When to use?
When number of repeats
is known
When repeats depend on
a condition
Example thought “Repeat 5 times”
“Repeat until the work is
done”
Control Controlled by count Controlled by condition
EASY WAY TO REMEMBER
 For loop = Counted loop (You know the count)
 While loop = Conditional loop (It stops only when the condition fails)

Python_Loops_Advanced_Presentation .pptx

  • 1.
  • 2.
    FOR LOOP EXAMPLE1  Code:  for i in range(1, 6): print(i)  Output: 1 2 3 4 5
  • 3.
    FOR LOOP EXAMPLE2  Code:  fruits = ['apple', 'banana', 'mango'] for item in fruits: print(item)  Output: apple, banana, mango
  • 4.
    WHILE LOOP EXAMPLE Code:  num = 1 while num <= 5: print(num) num += 1  Output: 1 2 3 4 5
  • 5.
    PATTERN PROGRAM 1:SQUARE  Code:  for i in range(3): for j in range(3): print('*', end=' ') print()  Output: * * * * * * * * *
  • 6.
    PATTERN PROGRAM 2:TRIANGLE  Code:  for i in range(1, 6): print('*' * i)  Output: * ** *** **** *****
  • 7.
    PATTERN PROGRAM 3:NUMBER PYRAMID  Code:  for i in range(1, 6): for j in range(1, i+1): print(j, end=' ') print()  Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
  • 8.
    WORKSHEET – LOOPQUESTIONS  Solve the following:  1.Write a for loop to print numbers from 10 to 1.  2.Write a while loop to print even numbers from 2 to 20.  3.Write a loop to print each character in the string 'PYTHON'.  4.Write a program to print a right-angle star triangle.  5.Write a program using break to stop when number == 5.
  • 9.
    FOR LOOP TOPRINT NUMBERS FROM 10 TO 1 Code: for i in range(10, 0, -1): print(i)
  • 10.
    WHILE LOOP TOPRINT EVEN NUMBERS FROM 2 TO 20 num = 2 while num <= 20: print(num) num += 2
  • 11.
    LOOP TO PRINTEACH CHARACTER IN THE STRING 'PYTHON' for ch in "PYTHON": print(ch)
  • 12.
    PROGRAM TO PRINTA RIGHT-ANGLE STAR TRIANGLE for i in range(1, 6): print("*" * i)
  • 13.
    PROGRAM USING BREAKTO STOP WHEN NUMBER == 5 for i in range(1, 11): if i == 5: break print(i)
  • 14.
    SIMPLE COMPARISON TABLE FeatureFor Loop While Loop When to use? When number of repeats is known When repeats depend on a condition Example thought “Repeat 5 times” “Repeat until the work is done” Control Controlled by count Controlled by condition
  • 15.
    EASY WAY TOREMEMBER  For loop = Counted loop (You know the count)  While loop = Conditional loop (It stops only when the condition fails)