Code Explanation:
1. The for Loop
for i in range(3):
The range(3) function generates a sequence of numbers starting from 0 up to, but not including, 3. The sequence is: [0, 1, 2].
The for loop iterates over each number in this sequence:
On the first iteration, i is 0.
On the second iteration, i is 1.
On the third iteration, i is 2.
2. The print() Function with end Parameter
print(i, end=", ")
The print() function is used to display output.
Normally, print() adds a newline (\n) after each call. However, the end parameter allows you to specify a custom string to append instead of the default newline.
Here, end=", " means a comma followed by a space (", ") is appended to the output instead of moving to the next line.
3. Output Construction
The loop executes print(i, end=", ") for each value of i:
On the first iteration: i = 0, so 0, is printed.
On the second iteration: i = 1, so 1, is appended.
On the third iteration: i = 2, so 2, is appended.
Final Output:
0, 1, 2,
0 Comments:
Post a Comment