35.8k views
0 votes
Num_list = [1,2,3,4]

for num in num_list:
num_list.append(num)
print(num)
What is the output of this code?

1 Answer

3 votes

Answer:

Is the num.append supposed to be indented along with the print(num)?

If the code is supposed to be indented

then we will get an infinite output of:

1

2

3

4

1

2

3

4

...

The reason why we will get a infinite output in this case is because we a re repeatedly appending the elements that are in the array at the end mean while we are iterating which will cause the size of the array to grow which means it will constantly repeat 1 2 3 4 line by line.

If the code is not indented then we will get an error: IndentationError

User Fjuan
by
7.5k points