1. Indent
-공백 4칸 들여쓰기 원칙
-첫번쨰 출에 파라미터 맞추기
foo =long_function_name(var_one, var_two,
var_three, var_four)
- 아니면 이렇게(공백 4칸 인덴트 한번 더)
foo =long_function_name(
var_one, var_two, var_three,
var_four):
2. List Comprehension
-lambda,map보다 우선적으로 사용
-Pythonic의 대표주자, 가독성이 매우 높다
리스트: [i for i in range(1,10)]
딕셔너리: {key:value for key, value in original.items()}
-표현식이 2개를 넘지 않게끔 주의
3. Generator
-생성만 해두고 저장은 안함 >>메모리에 보관 필요X,그때그때 끌어쓰기
-yield를 이용해 Generator 리턴 ㄱㄴ, 함수 끝 도달까지 계속 실행
-next(generator)로 값 추출
대표 ex) Range
>>>list(range(5))
[0,1,2,3,4]
>>>range(5)
range(0,5)
>>>type(range(5))
<class 'range'>
4. Enumerate
-값에 인덱스 자동으로 부여
for i,v in enumerate(list,set,tuple):
print(i,v) (i=index, v=value)
5. Divmod
-몫과 나머지를 동시에 구하는 함수(생각보다 자주 사용)
>>> divmod(5,3)
(1, 2)
6. Print
-가장 기본이자 중요. 코토에서 디버깅을 위해 제공되는 유일한 기능
- 리스트 >> 문자열 출력에 join이용 ex)"구분자".join(리스트)
✅ 문자열 포매팅(f-string) ✅ (가장 강력하다!!)
f ' 문자열 {변수} 문자열 '
>>> idx=1
>>> fruit="apple"
>>> print(f'{fruit} {idx}개 주세요')
apple 1개 주세요
7. Pass
-Null 연산. 아무것도 하지 않음 >> 인덴트 오류 방지용
-함수 그냥 넘어가기 가능
8. Locals()
-로컬에 선언된 모든 변수를 조회할 수 있음(클래스 메소드 내부)
-pprint와 같이 사용(보기좋게 줄바꿈처리해줌)
>>> import pprint
>>> pprint.pprint(locals())
{'__annotations__': {},
'__builtins__': <module 'builtins' (built-in)>,
'__doc__': None,
'__loader__': <class '_frozen_importlib.BuiltinImporter'>,
'__name__': '__main__',
'__package__': None,
'__spec__': None,
'a': ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
'fruit': 'apple',
'idx': 1,
'pprint': <module 'pprint' from 'C:\\Users\\강원도청\\AppData\\Local\\Programs\\Python\\Python39\\lib\\pprint.py'>}
>>>