Python Forum
I can't understand this problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I can't understand this problem
#2
The first answer is a little tricky. Lists are mutable types. One consequence of this is that if you modify a list you passed to a function, the list outside will be modified also. But you didn't modify the list. You reassigned the variable name of the parameter to a new list. The variable name exists only inside the function, and has no affect outside of it.

Note that my_list + [4, 5] is almost the same as my_list.extend([4, 5]). The difference is that the addition creates a new list you can reassign, while the second modifies the original list in place.

The second question is easier. When you print a function call, you are printing the return value of that function. Since you didn't specify a return value with a return statement, you get the default return value of None. If you had ended your function with return my_list, printing the function call would have printed the modified list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
I can't understand this problem - by muhammedkhairy - Dec-23-2018, 07:37 PM
RE: I can't understand this problem - by ichabod801 - Dec-23-2018, 08:36 PM
RE: I can't understand this problem - by stullis - Dec-23-2018, 09:35 PM
RE: I can't understand this problem - by ichabod801 - Dec-23-2018, 09:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Couldn't really understand the problem Batselot 1 2,386 Dec-13-2018, 07:59 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020