10/27/22

Loops in Python

Python Notes Unit 2 , Part 2

UNIT-2 (Part-2)




For loops and iterations.

While loops, Loop manipulation using continue break and else (and pass in Python).

Programming using conditional and loops block.


 

For loop

    1.   For loop is used when we have a block of code which we want to repeat a confirm number of times.

    2.   For loop has the ability to iterate over the objects of any sequence, for example a list or a string, tuple.

Syntax-

             For variable _name in sequence:

                        Statement_1

                        Statement_2

                         …

    Here variable _name indicates target variable which will assign a new value for each iteration of the upcoming loop body.

    Statement_1 and statement_2 is indicating the block of program statements.

Example 1 –

                 #print each book in a book list:

                      Books = [“English”, “Math”, “History”,]

                        for x in books:

                               print(x)

     Output -

                  English

                  Math

                  History

Example 1 –

n = 4

for i in range(0, n):

    print(i)

Output: -

                                  0

                                  1

                                  2

                                  3

       

While Loop –

    1.   While loop is used when the block of code as long as the test conditions is true.

    2.   We are generally use the loop when we don’t know the number of times to iterate beforehand.

    3.    This loop is used to execute a block of statements repeatedly whenever the condition is satisfied.

    4.   When the condition becomes false then the line of code immediately after the loop in the block of code is executed.

 

Syntax –

               While test _condition:

                   Body of while

Example –

          #python program to implementing while loop

            Count = 0

              while (count < 2):

                 Count = count+1

                 print (“Hello world”)

Output –

             Hello world

             Hello world

 

Loop manipulation using continue, break and pass

 

Continue -
The continue statement is used to skip the rest of code inside a loop body for the current iterations only.

 

Syntax –   

             Continue

 

 Example – 

                    #program to show the use of continue statement in python

                        for val in “string”:

                            if val == “i”;

                                  continue

                            print (val)

               print (“the end”)

Output –

                 S

                 T

                 R

                 N

                 G

                 the end

 

Break –

This statement is used for break the line of codes and conditions inside the loop. This statement is also used to terminates the loop. Break statements terminate the current loop and resume execution process at the next statement. The break statement can be used in both loops for loop and while loop.

Syntax –

                Break

 

Example –

              #use of break statement

                for val in “diploma”:

                   if val == “l”:

                        break

                  print(val)

              print(“the end”)

Output –

               D

               I

               P

              The end

 

Pass –

The pass statement is like a null operation it is used for writing empty loops. The name suggests pass statement simply does nothing. Pass statement is also used for empty control functions or statements.

 

Syntax –

                  pass

 

Example –

                       for letter in ‘Diploma’:

                           if letter == ‘l’:

                               pass

                               print ‘this is pass block’

                          print ‘current letter:’, letter

                  print “good morning!”

 

Output –

                 Current letter: d

                 Current letter: i

                 Current letter: p

                 T is pass block

                 Current letter: l

                 Current letter: o

                 Current letter: m

                 Current letter: a

                 good morning!

 =============== THE END===============

Keep learning and keep exploring

Please do comment for more updates

Also subscribe my you tube channel


Engineers Success Tutorial


UNIT -01 Notes

Introduction, Variables and Data types Part-1 

Introduction, Variables and Data Types Part 2

Introduction, Variable and Data Type Part 3


UNIT -02 Notes

Conditional statements in python part 1          

 

 

 


















No comments:

Post a Comment

Please do not enter any spam link in the comment box and use English and Hindi language for comment.

Latest Update

Key Components of XML

Popular Posts