abs(3) # 3
abs(-3) # 3
*반복 가능한 자료형: for문으로 값 출력 가능(ex:리스트, 튜플,문자열,딕셔너리,집합)
리스트 : [1,2,3]
all([1, 2, 3]) #True
all([1, 2, 3, 0]) #False
all([]) # True
any([1, 2, 3, 0]) # True
any([0, ""]) # False
any([]) # False
chr(97) # 'a'
chr(44032) # '가'
4-1. ord(c) : 문자의 유니코드 값을 돌려준다
ord('a')
97
ord('가')
44032
dir([1, 2, 3])
['append', 'count', 'extend', 'index', 'insert', 'pop',...]
dir({'1':'a'})
['clear', 'copy', 'get', 'has_key', 'items', 'keys',...]
divmod(7, 3) # (2, 1)
for i, name in enumerate(['body', 'foo', 'bar']):
print(i, name)
0 body
1 foo
2 bar
eval('1+2') # 3
eval("'hi' + 'a'") # 'hia'
eval('divmod(4, 3)') # (1, 1)
def positive(x):
return x > 0
print(list(filter(positive, [1, -3, 2, 0, -5, 6]))) # [1,2,6]
*lambda 코드 사용
list(filter(lambda x: x > 0, [1, -3, 2, 0, -5, 6])) # [1, 2, 6]
hex(234) # '0xea'
hex(3) #'0x3'
a = 3
id(3)
135072304
id(a)
135072304
b = a
id(b)
135072304
a = input()
hi
a
'hi'
b = input("Enter: ")
Enter: hi
int(3.4)
3
int('11', 2)
3
int('1A', 16)
26
class Person: pass
a = Person()
isinstance(a, Person) # True
b = 3
isinstance(b, Person) # False
len("python")
6
len([1,2,3])
3
len((1, 'a'))
2
list("python")
['p', 'y', 't', 'h', 'o', 'n']
list((1,2,3))
[1, 2, 3]
def two_times(x):
return x*2
list(map(two_times, [1, 2, 3, 4])) # [2, 4, 6, 8]
list(map(lambda a: a*2, [1, 2, 3, 4])) # [2, 4, 6, 8]
max([1, 2, 3])
3
max("python")
'y'
min([1, 2, 3])
1
min("python")
'h'
oct(34)
'0o42'
oct(12345)
'0o30071'
w : 쓰기 모드
r : 읽기 모드
a : 추가 모드
b : 바이너리 모드
pow(2, 4)
16
pow(3, 3)
27
i) 인수가 하나일 경우
list(range(5))
[0, 1, 2, 3, 4]
ii) 인수가 2개일 경우
list(range(5, 10))
[5, 6, 7, 8, 9]
iii) 인수가 3개일 경우
list(range(1, 10, 2))
[1, 3, 5, 7, 9]
list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
round(4.6)
5
round(4.2)
4
round(5.678, 2)
5.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]
sum([1,2,3])
6
sum((4,5,6))
15
tuple("abc")```
코드를 입력하세요
('a', 'b', 'c')
tuple([1, 2, 3])
(1, 2, 3)
tuple((1, 2, 3))
(1, 2, 3)
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')]