[Python]내장함수(built-in)
파이썬의 내장함수(built-in)에 대해 알아보자.
# 파이썬 내장(Built-in) 함수 # 절대값 # abs() print(abs(-3)) print() # all : iterable 모든 요소가 True일 때 True print(all([1, 2, 3])) print(all([0, 2, 3])) print(all(['', 2, 3])) print() # any : iterable 최소 1개이상 요소가 True일 때 True print(any([0, '', 1])) print(any(['', False, 3])) print(any(['', 0, False])) print() # chr : 아스키 -> 문자, ord : 문자 -> 아스키 print(chr(67)) print(ord('C')) print() # enumerate : 인덱스 + iterable 객체 생성 for idx, value in enumerate(['hello', 'good', 'jigi']): print(idx, value) print() # filter : 반복가능한 객체 요소를 지정한 함수 조건에 맞는 값만 추출( def conv_pos(x): return abs(x) > 2 x = [-1, 2, -3, 4, -5, 6, -7] print(type(filter(conv_pos, x))) print(list(filter(conv_pos, x))) print(list(filter(lambda x: abs(x) > 2, x))) # 람다함수를 직접 선언가능 print() # id : 객체의 주소값(레퍼런스) 반환 print(id(int(5))) print(id(4)) print() # len : 요소의 길이 반환 print(len('abcdef')) print(len([2, 3, 4, 5, 6])) print() # max, min : 최대값, 최소값 print(max([1, 2, 3])) print(min([1, 2, 3])) print(max('abcdef')) print(min('abcdef')) print() # map : 반복가능한 객체 요소를 지정한 함수 실행 후 추출 def conv_abs(x): return abs(x) print(list(map(conv_abs, [-1, 2, -3, 4, -5, 6]))) print(list(map(lambda x: abs(x), [-1, 2, -3, 4, -5, 6]))) print() # pow : 제곱값 반환 print(pow(2, 10)) # range : 반복가능한 객체(Iterable) 반환 print(range(1, 10)) print(list(range(1, 10))) print(list(range(1, 10, 2))) print(tuple(range(1, 10))) print(set(range(1, 10))) print() # round : 반올림 print(round(6.2174, 2)) print(round(6.2, 2)) print(round(6.7, 2)) # sorted : 반복가능한 객체(Iterable)를 오름차순 정렬 후 반환 print(sorted([5, 2, 5, 1, 0, 9, 8])) l = sorted({5, 2, 5, 1, 0, 9, 8}) print(l) print(sorted(['c', 'a', 'b', 'e'])) print() # sum : 반복가능한 객체의 합 반환 print(sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) print(sum(range(1, 11))) print() # type : 자료형 확인 print(type(3)) print(type({3})) print(type((3,))) print(type([3])) print() # zip : 반복가능한 객체(iterable)의 요소를 묶어서 반환 print(zip([10, 20, 30], [40, 50, 60])) print(list(zip([10, 20, 30], [40, 50, 60]))) # 짝을 맞춰준다. print(list(zip([10, 20], [40, 50, 60]))) # 짝이 안맞는 것은 반환하지 않는다.
결과:
3 True False False True True False C 67 0 hello 1 good 2 jigi <class 'filter'> [-3, 4, -5, 6, -7] [-3, 4, -5, 6, -7] 2149898322288 2149898322256 6 5 3 1 f a [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6] 1024 range(1, 10) [1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 3, 5, 7, 9] (1, 2, 3, 4, 5, 6, 7, 8, 9) {1, 2, 3, 4, 5, 6, 7, 8, 9} 6.22 6.2 6.7 [0, 1, 2, 5, 5, 8, 9] [0, 1, 2, 5, 8, 9] ['a', 'b', 'c', 'e'] 55 55 <class 'int'> <class 'set'> <class 'tuple'> <class 'list'> <zip object at 0x000001F4903A7100> [(10, 40), (20, 50), (30, 60)] [(10, 40), (20, 50)]
최신 댓글