index(), find() - 원소의 인덱스
index(), find() 메소드는 원소의 인덱스 위치를 반환한다 index()는 해당 원소가 없으면 ValueError를 리턴하고, find()는 -1을 리턴한다 find()는 문자열에서만 지원한다 # tuple >>> (1,2,3,4).index(2) 1 >>> (1,2,3,4).index(5) Traceback (most recent call last): File "", line 1, in ValueError: tuple.index(x): x not in tuple >>> (1,2,3,4).find(2) Traceback (most recent call last): File "", line 1, in AttributeError: 'tuple' object has no attribute 'find'..
2020. 9. 1.
string unpacking - 문자열 언팩킹
언팩킹이란? 요소를 여러 변수에 나누어 담는 것 locals()? 현재 scope 에 있는 local 변수를 딕셔너리 type( {key:value} ) 으로 반환 >>> nums = [1,2,3,4] # unpacking >>> a,*b = nums >>> a 1 >>> b [2,3,4] >>> f'{a} {b} {nums}' 1 [2,3,4] [1,2,3,4] # unpacking >>> '{a} {b} {nums}'.format(**locals()) 1 [2,3,4] [1,2,3,4]
2020. 8. 23.