as the sequence. Initially, the value of x is set to the first element of the string, (i.e. ‘H’), so the print statement inside the loop is executed. Then, the control variable x is updated with the next element of the string and the print statement is executed again.
In this way the loop runs until the last element of the string is accessed. In the same way, example . (b) prints the string “Hello World”, five times, until the control variable x reaches last element of the given sequence. Instead of creating sequence of values manually, we can use range().
The range() is a built-in function, to generate series of values between two numeric intervals. The syntax of range() is as follows: range (start,stop,[step]) Where, start – refers to the initial value stop – refers to the final value step – refers to increment value, this is optional part. range ( , , ) will start the range of values from and end at range ( , , ) will start the range of values from and end at range ( , ,- ) - will start the range of values from and end at range ( ) will consider this value as the end value(or upper limit) and starts the range count from to (remember always range() will work till stop - value only) Example . (c): Examples for range() 12th Computer Chapter - - Last item reached?
Body of for Exit loop for each item in sequence Yes No Fig . for loop execution for i in range ( , , ): print (i, end=' ') Output: Example . : #program to illustrate the use of for loop - to print single digit even number Following is an illustration using else part in for loop for i in range( , , ): print (i,end=' ') else: print ("\nEnd of the loop") Output: End of the loop Example . : #program to illustrate the use of for loop - to print single digit even number with else part In Python, indentation is important in loop and