-
[Python] 내장함수 정리Python 2021. 10. 28. 15:57
abs()
절대값을 돌려준다
print(abs(3)) => 3 print(abs(-3)) => 3 print(abs(-3.0)) => 3.0
all([])
List type으로 요소를 넣어주고 그중 하나라도 falsy값이 있으면 False를 반환한다.
print(all([1,2,3,4])) print(all([0,1,2,3,4])) print(all([-1,0,1,2,3,4]))
any([])
all과 비슷하게 List type으로 요소를 받으며 하나라도 trusy값이 있으면 True를 반환, 모두 falsy값 일때만 false를 반환
print(any([])) =>false print(any([0])) =>false print(any([1])) =>true
chr()
Unicode를 입력 받아 문자로 출력한다
print(chr(61)) => = print(chr(62)) => > print(chr(63)) => ?
dir()
사용할수 있는 변수나 함수를 알려준다
my_dict = ({"name" : "kim"}) my_list = [1,2,3,4] my_set = {1,2,3,4} my_tuple = ("one", "two", "three") print(dir(my_dict)) => ['clear', 'copy', 'fromkeys', 'get', 'items', 'keys'...] print(dir(my_list)) => ['append', 'clear', 'copy', 'count', 'extend', 'index'...] print(dir(my_set)) => ['add', 'clear', 'copy', 'difference', 'difference_update'...] print(dir(my_tuple)) => ['count', 'index'...]
divmod()
2개의 int를 값으로 받아 a와 b를 나눈 몫과 나머지를 tuple type으로 돌려 준다
print(divmod(7,2)) => (3, 1) print(divmod(128,7)) => (18, 2)
enumerate()
순서가 있는 자료형 (List, Tuple, String)을 입력으로 받아 값을 돌려준다. 주로 반복문이랑 같이 사용하며 순서를 확인한다.
my_list = ["first", "second", "third"] for i, num in enumerate(my_list): print(i, num) => 0 first 1 second 2 third
eval()
입력 받은 str을 코드로 바꿔 파이썬으로 실행하고 결과를 돌려준다.
# 예시1 print(eval('abs(-1)')) =>1
'Python' 카테고리의 다른 글
[Python] module, package (0) 2021.10.29 [Python] input(), eval() 사용해서 "피타고라스의 정리 계산기" 만들기 (0) 2021.10.28 [Python] isinstance() 자료형 확인하기 (0) 2021.10.27 [Python] Dictionary에 Key가 없을 경우 (0) 2021.10.25 [Python] Dictionary Indexing []와 .get() 의 차이 (0) 2021.10.25