ITERATION in PYTHON
Slide: 1
ITERATION
• Repeated execution of a set of statements is called
iteration.
• Most commonly syntax are for and constructs
• In for, the number of repetitions is specified explicitly
in advance
• In while, the code block executes until some condition
is met.
The While construct
• The while statement allows you to repeatedly execute a block of statements as long as a condition is true.
• Syntax
while (condition):
Body of while
• Test expression is checked first.
• The body of the loop is entered only if the condition is True.
• After one iteration, the test expression is checked again.
• This process continues until the condition becomes False.
• Body starts with indentation
The While loop
While loop - Example
Program to print numbers from 1 to 5
Output : 1 2 3 4 5
The for loop
Syntax
for val in sequence:
Body of for
• val is the variable that takes the value of the item
inside the sequence on each iteration.
• Loop continues until we reach the last item in the
sequence.
• The body of for loop is separated from the rest of the
code using indentation.
For Loop
Program to print first 5 numbers
number = 5;
for i in range(1,number+1):
print (i, end=' ‘);
Slide: 8
For loop - Example
• Program to find the sum of first 10 numbers using
range function
number = int(input("Enter any Number: "))
sum = 0
for value in range(1, number + 1):
sum = sum + value
print("The Sum of Numbers = ", sum))
The range ( ) function
• To loop through a set of code a specified number of times, we can use the range()function,
• The range() function returns a sequence of numbers, starting from 0 by default, and increments
by 1 (by default), and ends at a specified number.
• We can also define the start, end and step size as range(start,end,step size) – the last value will
be always end -1
• •range(5)will generate numbers from 0 to 4
• •range(2, 6) will generate numbers : 2, 3, 4, 5
• •range(1, 10, 2) will generate numbers : 1, 3, 5, 7, 9
• •range(5, 0,-1) will generate numbers : 5, 4, 3, 2, 1
Example : for loop
Slide: 11
Example : for loop
Slide: 12
Nested for loop
Slide: 13
• Nested for loop – find the output of the below codes:

PYTHON.pptx

  • 1.
  • 2.
    ITERATION • Repeated executionof a set of statements is called iteration. • Most commonly syntax are for and constructs • In for, the number of repetitions is specified explicitly in advance • In while, the code block executes until some condition is met.
  • 3.
    The While construct •The while statement allows you to repeatedly execute a block of statements as long as a condition is true. • Syntax while (condition): Body of while • Test expression is checked first. • The body of the loop is entered only if the condition is True. • After one iteration, the test expression is checked again. • This process continues until the condition becomes False. • Body starts with indentation
  • 4.
  • 5.
    While loop -Example Program to print numbers from 1 to 5 Output : 1 2 3 4 5
  • 6.
    The for loop Syntax forval in sequence: Body of for • val is the variable that takes the value of the item inside the sequence on each iteration. • Loop continues until we reach the last item in the sequence. • The body of for loop is separated from the rest of the code using indentation.
  • 7.
  • 8.
    Program to printfirst 5 numbers number = 5; for i in range(1,number+1): print (i, end=' ‘); Slide: 8
  • 9.
    For loop -Example • Program to find the sum of first 10 numbers using range function number = int(input("Enter any Number: ")) sum = 0 for value in range(1, number + 1): sum = sum + value print("The Sum of Numbers = ", sum))
  • 10.
    The range () function • To loop through a set of code a specified number of times, we can use the range()function, • The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. • We can also define the start, end and step size as range(start,end,step size) – the last value will be always end -1 • •range(5)will generate numbers from 0 to 4 • •range(2, 6) will generate numbers : 2, 3, 4, 5 • •range(1, 10, 2) will generate numbers : 1, 3, 5, 7, 9 • •range(5, 0,-1) will generate numbers : 5, 4, 3, 2, 1
  • 11.
    Example : forloop Slide: 11
  • 12.
    Example : forloop Slide: 12
  • 13.
    Nested for loop Slide:13 • Nested for loop – find the output of the below codes: