Explanation:
-
List Initialization:
colors = ["Red", "Yellow", "Orange"]
A list named colors is created with three string elements: "Red", "Yellow", and "Orange". -
For Loop:
for i in range(0, 2):- The range(0, 2) function generates numbers from 0 to 1 (excluding 2).
- The variable i takes each of these values (0 and 1) during the loop.
-
Accessing List Elements:
print(colors[i])- The list elements are accessed using their index (i).
- When i = 0, it prints colors[0] which is "Red".
- When i = 1, it prints colors[1] which is "Yellow".
- The loop stops before reaching 2, so "Orange" is not printed.
Output:
If you want to print all the colors, you should use range(0, 3) or simply range(3).
0 Comments:
Post a Comment