-1

I am just learning Python and programming in general and have come across the following question. I cannot seem to find an answer/explanation to why the statement below passes None in the assignment. Just trying to understand. I am oversimplifying below and understand that once I call .sort() on before that before has changed as well.

before = [67,45,2,13,1,998]

after = before.sort()

print(after)

Why, when I print after is it None?

1
  • Just use before.sort() to print it does not return the object Commented May 8, 2015 at 22:39

1 Answer 1

2

sort is an inplace operation, it does not return a value so as with all python functions that don't return a value returns None by default so you are setting after to None.

Just call.sort and the print your list:

before = [67,45,2,13,1,998]

before.sort() # inplace sorts original list

print(before)

Or use sorted if you want a new list:

before = [67,45,2,13,1,998]

after = sorted(before) # creates a completely  new list 

print(after)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.