FOR loop


Good Morning students

Today we will look at the comparison between WHILE and FOR loops and see how the FOR loop works.
-----------------------------------------------------------------------------------------
The Google meet link for today's class is :

meet.google.com/awj-jydm-ask

------------------------------------------------------------------------------------------------------------
By the end of today's session, you will be able to:

  • Differentiate between CONDITIONAL and COUNTING Loops.
  • Understand the working of the FOR loop
  • Apply the knowledge to create appropriate FOR loops to give the desired results.
---------------------------------------------------------------------------------------------------------------
CONDITIONAL LOOP:
  • The loops that repeat until a certain thing happens
  • They keep repeating as long as some condition is true.
  • Therefore, we can say that WHILE loop is conditional loop
  • We cant say in advance, how many times this loop will execute.

 COUNTING LOOPS:
  • The loops that repeat a certain number of times
  • it is designed to process the items of any sequence, such as a list or string , one by one
  • FOR loop is a counting loop

The general form of FOR LOOP is as given below:

for variable in sequence :
     statements_to_repeat

Syntax

for iterating_var in sequence:
   statements(s)
If a sequence contains an expression list, it is evaluated first.
 Then, the first item in the sequence is assigned to the iterating variable iterating_var.
Next, the statements block is executed. 
Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.

Flow Diagram

for loop in Python
For example, consider the following loop:

for a in [1,4,7]:
      print(a)
      print(a*a)

In the above example, variable a will be assigned each value of list one by one,
  •   for the first time a will be 1
  •   then  a will become 4
  •   and finally a will become 7 
---------------------------------------------------
  • The body of the loop is showing in the pink colour. 
  • All statements in the body of the loop will be executed for each value of loop variable a 
  • i.e. for a=1 , then for a=4 then for a=7  

------------------------------------------------------------------------------------------
A loop in Python is processed as
  • The loop-variable is assigned the first value in the sequence
  • All the statements in the body of the FOR loop are executed with assigned value of loop variable (STEP 2)
  • Once STEP 2 is over, the loop variable is assigned the next value in the sequence and the loop-body is executed(i.e. STEP 2 repeated) with the new value of loop-variable
  • This continues until all values in the sequences are processed.
  • It has the ability to iterate over the items of any sequence, such as a list or a string.
Example
#!/usr/bin/python

for letter in 'Python':     # First Example
   print 'Current Letter :', letter

fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # Second Example
   print 'Current fruit :', fruit

print "Good bye!"
When the above code is executed, it produces the following result −
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!

Comments

Post a Comment

Popular posts from this blog

HOLIDAY HW