Page 95 - Computer - 8
P. 95
Lab Activity 2
To use For loop to display each character in a string. Experiential Learning
1. Type the code as shown in the image in the Python Editor.
2. Press the F5 key to execute the code. Sample output of the program is given.
USE OF RANGE( ) FUNCTION IN FOR LOOP
The Range( ) function is used to specify a range of values which are assigned in sequence to the counter
variable in the For loop. The syntax of the Range( ) function is:
range( [Start_val], End_val, [Step_val] )
The Start_val represents the initial value, and the End_val represents the final value. The Step_val
represents the value by which the counter variable is incremented or decremented. The Start_val and
the Step_val are optional values but the End_val is compulsory. The default value for the Start_val is 0
and Step_val is 1. The counter variable begins with the initial value, increases the value by the step value
until it reaches a value 1 less than the value specified in the range function.
Consider the following examples of range function:
• The statement range(5) will create a list of numbers from 0 to 4, i.e., 0, 1, 2, 3, 4
• The statement range (1,10) will create a list of numbers from 1 to 9, i.e., 1, 2, 3, 4, 5, 6, 7, 8, 9
• The statement range (1, 10, 2) will create a list of numbers from 1 to 9 with a gap of 2 between
two consecutive numbers, i.e., 1, 3, 5, 7, 9
Lab Activity 3 Experiential Learning
To display the use of Range( ) function in For loop.
1. Type the code as shown in the image in the Python Editor.
93