05 build in function

Built_in function

  • Python 내장함수 docs에서 알파벳 순으로 함수 목록을 확인할 수 있다.
  • 속성별로 분류를 해보면
    • 산술연산함수:abs, bin, oct, hex, pow, round, divmod, hash
    • 기본 타입/클래스 생성자:bool, bytearray, bytes, complex, dict, float, frozenset, int, list, memoryview, object, slice, str, tuple, zip
    • 실행관련:complie, eval, exec
    • 연속열/반복관련:all, any, enumrate, filter, iter, len, map, max, min, next, range reversed, sorted, sum,
    • 문자열과 IO관련:ascii, chr, foramt, ord, repr, input, open,print
    • 객체와 모듈관련:help, callable, delattr, dir, gettattr, globals, hasattr, id, isinstance, issubclass, locals, setattar, type, vars
    • 그외:classmethod, staticmethod, property, supter, import

all

In [1]:
print(all([1, 2, 3]))
print(all([1, 2, 3, 0]))
True
False

enumerate

In [2]:
for i, name in enumerate(['a', 'bc', 'dc']):
    print(i, name)

    
0 a
1 bc
2 dc

ord, char

In [3]:
print(ord('a'))
print(chr(99))
97
c

Sorted

In [4]:
print(sorted(['a','c','b']))
print(sorted("zero"))
#리스트 자료형의 sort함수는 리스트 객체 그 자체를 소트할 뿐이지 소트된 결과를 리턴하지는 않는다.
['a', 'b', 'c']
['e', 'o', 'r', 'z']

reversed

In [5]:
print(reversed("zero"))
#In Python, reversed  returns a reverse iterator.
rev_zero= reversed("zero")
print(list(rev_zero))
print( ''.join(reversed('abcde')))
<reversed object at 0x7f9418090860>
['o', 'r', 'e', 'z']
edcba

Filter

In [6]:
def positive(x):
    return x > 0

print(list(filter(positive, [1, -3, 2, 0, -5, 6])))
[1, 2, 6]

Range

In [7]:
a = range(10, 0, -2)
print(type(a))
# range() 함수의 결과는 반복가능(iterable) 함 
print(list(range(10, 0, -2)))
for i in a:
    print(i)
<class 'range'>
[10, 8, 6, 4, 2]
10
8
6
4
2

zip

In [9]:
list1 = ['one', 'two']
list2 = ['X','Y']
print(zip(list1, list2))
print(list(zip(list1, list2)))
aa = (zip(list1, list2))
#생성된 zip을 다시 분해 
zip1, zip2 = zip(*aa)
print(zip1, zip2)
<zip object at 0x7f941809b708>
[('one', 'X'), ('two', 'Y')]
('one', 'two') ('X', 'Y')


© 2017. All rights reserved.

Powered by ZooFighter v0.12