16 June 2023

x=[0,1,2,3,4]
x.append(x)
print(x)

Output:

[0, 1, 2, 3, 4, [...]]

I think the output should be:

[0, 1, 2, 3, 4, [0, 1, 2, 3, 4]]

But if I change the code into:

x=[0,1,2,3,4]
y=[0,1,2,3,4]
x.append(y)
print(x)

The output then makes sense.Can you explain that to me?

Course Progress
  • event_note Easy to Learn
  • view_kanban 2 Modules
  • punch_clock 10 to 15 Hours
  • terminal Coding Lessons

What will you learn?

  • lock

    Unit 1

    keyboard_arrow_down
    By default Python‘s print() function ends with a newline. A programmer with C/C++ background may wonder how to print without a newline. Python’s print() function comes with a parameter called ‘end‘. By default, the value of this parameter is ‘\n’, i.e. the new line character.
    • lock_outline Article Java
  • check

    Unit 2

    keyboard_arrow_down
    By default Python‘s print() function ends with a newline. A programmer with C/C++ background may wonder how to print without a newline. Python’s print() function comes with a parameter called ‘end‘. By default, the value of this parameter is ‘\n’, i.e. the new line character. By default Python‘s print() function ends with a newline. A programmer with C/C++ background may wonder how to print without a newline. Python’s print() function comes with a parameter called ‘end‘. By default, the value of this parameter is ‘\n’, i.e. the new line character.