325,351 views
6 votes
6 votes
What is happening here after if?

values = [10,20,30,40.4233,50]


for i in range(len(values)):

if i > 0:

print(" | ", end = "")

print(values[i], end="")

print()

User Manojkumar Khotele
by
3.4k points

1 Answer

18 votes
18 votes

Answer:

The code in the for loop will iterate through the values in the list "values" and execute the code inside the loop for each value.

The if statement checks if the value of "i" is greater than 0. If it is, it will print " | " followed by the value at the current index of "values". The "end" parameter in the print statement specifies that no newline character should be added at the end of the printed string, so that the next print statement will be printed on the same line.

Finally, the print statement outside the loop will print a newline character, effectively ending the line of output.

Here is an example of the output of this code:

| 10 | 20 | 30 | 40.4233 | 50

Note that the first value in the list is not printed, because the if statement skips it since the value of "i" (which is 0) is not greater than 0.

User Darko Petkovski
by
3.1k points