"파이썬 del"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 12개는 보이지 않습니다)
2번째 줄: 2번째 줄:
;Python del Keyword, Python del
;Python del Keyword, Python del
;파이썬 del 키워드, 파이썬 del
;파이썬 del 키워드, 파이썬 del
* 객체를 삭제하는 파이썬 키워드
* 리스트에서 인덱스, 딕셔너리에서 키의 대상이 되는 객체를 삭제할 때 사용할 수 있다.
<syntaxhighlight lang='python' run>
x = 1
del x
print( x )
</syntaxhighlight>
:→ 1행에서 x를 선언했지만, 2행에서 삭제(del)했으므로, 3행 print 문에서 변수를 찾지 못해 오류 발생


{{소스헤더|리스트}}
{{소스헤더|리스트}}
<source lang='python'>
<syntaxhighlight lang='python' run>
names = ['john', 'jane', 'chris', 'alex']
names = ['john', 'jane', 'chris', 'alex']
del names[0]
del names[0]
print(names)
print( names )
# ['jane', 'chris', 'alex']
# ['jane', 'chris', 'alex']
</source>
</syntaxhighlight>
<source lang='python'>
<syntaxhighlight lang='python' run>
names = ['john', 'jane', 'chris', 'alex']
names = ['john', 'jane', 'chris', 'alex']
del names[:2]
del names[:2]
print(names)
print( names )
# ['chris', 'alex']
# ['chris', 'alex']
</source>
</syntaxhighlight>


{{소스헤더|딕셔너리}}
{{소스헤더|딕셔너리}}
<source lang='Python'>
<syntaxhighlight lang='Python' run>
fruits = { 'a' : 'apple', 'b' : 'banana', 'c': 'cranberry' }
fruits = { 'a' : 'apple', 'b' : 'banana', 'c': 'cherry' }
del fruits['b']
del fruits['b']
print fruits
print( fruits )
# {'a': 'apple', 'c': 'cranberry'}
</syntaxhighlight>
</source>


==같이 보기==
==같이 보기==
* [[파이썬 리스트 스터디]]
* [[파이썬 리스트 스터디]]
* [[Pandas 데이터프레임 컬럼명으로 컬럼 제거]]
* [[함수 unset()]]
* [[함수 unset()]]


35번째 줄: 44번째 줄:
[[category: Python 리스트]]
[[category: Python 리스트]]
[[category: Python 딕셔너리]]
[[category: Python 딕셔너리]]
[[분류: Python 키워드]]

2021년 3월 14일 (일) 15:36 기준 최신판

1 개요[ | ]

Python del Keyword, Python del
파이썬 del 키워드, 파이썬 del
  • 객체를 삭제하는 파이썬 키워드
  • 리스트에서 인덱스, 딕셔너리에서 키의 대상이 되는 객체를 삭제할 때 사용할 수 있다.
x = 1
del x
print( x )
→ 1행에서 x를 선언했지만, 2행에서 삭제(del)했으므로, 3행 print 문에서 변수를 찾지 못해 오류 발생
리스트
names = ['john', 'jane', 'chris', 'alex']
del names[0]
print( names )
# ['jane', 'chris', 'alex']
names = ['john', 'jane', 'chris', 'alex']
del names[:2]
print( names )
# ['chris', 'alex']
딕셔너리
fruits = { 'a' : 'apple', 'b' : 'banana', 'c': 'cherry' }
del fruits['b']
print( fruits )

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}