Skip to content

Instantly share code, notes, and snippets.

@choco9966
Last active July 17, 2018 06:45
Show Gist options
  • Save choco9966/c31e0407b6cb18fc008377b796e751cb to your computer and use it in GitHub Desktop.
Save choco9966/c31e0407b6cb18fc008377b796e751cb to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@COCOLMAN
Copy link

None이 나오는 이유

list는 변경가능한객체, 즉 mutable이기 때문입니다.

만약 문자열처럼 변경불가능(immutable)한 객체라면 출력가능할겁니다.

immutable객체 수정

a = 'abc'
b = a.title() # 첫 문자를 대문자로 변경하는 메소드
print(a) # 변경 불가능하기 때문에 여전히 'abc'
print(b) # 변경 불가능하기때문에 바뀐값을 리턴했습니다. 그래서 변경된 값이 b에 들거아 있습니다. 'Abc'출력

mutable객체 수정

a = [1, 2, 3]
b = a.reverse()
print(b) # None 출력 reverse 함수가 변경가능한 객체를 수정했기때문에, 수정된 값을 리턴시키지 않습니다.
print(a) # [3, 2, 1] 출력, 리스트가 수정되어 있습니다.

그렇기 때문에 다음 코드는 다음과 같이 수정가능합니다.

print([x for x in range(1, 6+1)].reverse())

에서

a = [x for x in range(1, 6+1)]
a.reverse()
print(a)

으로

혹은

print(list(reversed([x for x in range(1, 6+1)])))

으로

reverse함수를 쓰지 말고 해보세요!

range 함수의 step을 활용해서 해결해보세요

>>> help(range)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment