외부 모듈과 다르게 import 를 하지 않아도 사용 가능한 함수
> all([1,2,3])
True
> all([1,2,3,0]) # 0은 거짓이므로
False
> all([]) # 입력 인수가 비었을 경우는 참
True
> all(())
True
> any([1, 2, 3, 0])
True
> any([0, ""]) # 모두 거짓이므로 거짓
False
> any([]) # 입력 인수가 비었을 경우는 거짓
False
> chr(97)
'a'
> chr(44232)
'곈'
> ord("b")
98
> ord("나")
45208
> dir([1,2,3]) #리스트 관련 함수
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
> dir({'1':'a'}) #딕트 관련 함수
['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
> divmod(10,2)
(5, 0)
for i, name in enumerate(['body', 'foo', 'bar']):
print(i, name)
#결과
0 body
1 foo
2 bar
> eval("1+2")
3
> eval("'hello'+'girl'")
'hellogirl'
> eval("divmod(3,4)")
(0, 3)
[적용전]
def positive(l):
result = [] # 빈리스트
for i in l:
if i > 0:
result.append(i) #리스트에 추가
return result
print(positive([1,-3,2,0,-5,6]))
#결과
[1, 2, 6]
[적용후]
def positive(x):
return x > 0
print(list(filter(positive, [1, -3, 2, 0, -5, 6])))
※ positive 함수에 두 번째 인수인 리스트이 요소들이 입력되었을때, 참인 값들만 돌려준다.
> a = input()
i wanna sleep rn
> a
'i wanna sleep rn'
> int(3.4) #소수점 버리고 정수로
3
> int("4")
4
int(x,radix)는 radix 진수로 표현된 문자열 x를 10진수로 변화해서 돌려준다.
> int("11",2) # 2진수로 표현된 11의 10진수 값을 돌려줌
3
> int("1A",16) # 16진수로 표현된 1A의 10진수 값을 돌려줌.
26
class Person:
pass
a = Person()
res= isinstance(a,Person) # 인스턴스, 클래스이름
print(res) # 클래스의 인스터스인지 아닌지 판단여부
#결과
True
> len("python")
6
> len([1,2,3,4,5,6,7])
7
> len((1,"aaaa"))
2
- list
len(s)는 반복 가능한 자료형 s를 입력받아 리스트로 만들어 돌려준다.
> list((1,2,3,4,5,6,7))
[1, 2, 3, 4, 5, 6, 7]
> list("python")
['p', 'y', 't', 'h', 'o', 'n']
def two_times(x):
return x*2 #두배
a =list(map(two_times,[1,2,3,4,5]))
print(a)
1이 먼저 two_time에 들어가 [2]가 된다.
다음으로 2가 two_times에 들어가 4가된다. 그리고 리스트는 [2,4]가 된다.
이렇게 반복되어 [2,4,6,8,10]이 된다.
※ 람다 함수 사용하기
a = list(map(lambda a: a*2, [1, 2, 3, 4]))
print(a)
> max([1,2,3,4,5])
5
> max("hello im hazel")
'z'
> min([1,2,3,4,4,4,4])
1
> min("hello im hazel")
' '
> min("hello")
'e'
> oct(100)
'0o144'
> oct(10000)
'0o23420'
> pow(2, 4)
16
> pow(3, 3)
27
> list(range(5))
[0, 1, 2, 3, 4]
> list(range(1,10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
> list(range(1,10,2))
[1, 3, 5, 7, 9]
> list(range(0,-10,-2))
[0, -2, -4, -6, -8]
> round(4.6)
5
> round(4,2)
4
> round(4.678,2) #소수점 2에서 반올림
4.68
> sorted([3, 1, 2])
[1, 2, 3]
> sorted(['a', 'c', 'b'])
['a', 'b', 'c']
> sorted("zero")
['e', 'o', 'r', 'z']
> sorted((3, 2, 1))
[1, 2, 3]
> str(4)
'4'
> str("hazel")
'hazel'
> str("hello".upper())
'HELLO'
> sum([1,2,3])
6
> sum((4,5,6))
15
> tuple("aaaaa")
('a', 'a', 'a', 'a', 'a')
> tuple(["adfadfa"])
('adfadfa',)
> tuple(["adfadfa","A","As"])
('adfadfa', 'A', 'As')
> type("abc")
<class 'str'>
> type([ ])
<class 'list'>
> type(open("test", 'w'))
<class '_io.TextIOWrapper'>
> list(zip([1, 2, 3], [4, 5, 6]))
[(1, 4), (2, 5), (3, 6)]
> list(zip([1, 2, 3], [4, 5, 6], [7, 8, 9]))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
> list(zip("abc", "def"))
[('a', 'd'), ('b', 'e'), ('c', 'f')]