Python 자료형


숫자형

a = 123
b = -123
a = 0.123
type(a) #int
type(b) #int
type(c) #float

octal = 0o177 #int
hexa = 0x8ff #int

문자열

"Life is too short, You need Python"
'Python is easy and fun'
"""Actually you can add
massive sentences with new lines
if you just use this method"""
'''You can also use this one'''

참 거짓

true
false
a=1
if a:
  print(True)

리스트

numlist = [1,2,3,4,5,6,7,8,9,10]
stringlist = ['a','b','c','d','e','f','g','h','i']
listlist = [[1,2,3],[4,5,6]]
numlist[0] # 1
stringlist[0] # a
listlist[0] # [1,2,3]

튜플

# 연산자 없이 값을 추가할 수 없다.
>>> t1 = ()
>>> t2 = (1,)
>>> t3 = (1, 2, 3)
>>> t4 = 1, 2, 3
>>> t5 = ('a', 'b', ('ab', 'cd'))
t3[0] # 1
t3 * 2 # (1,2,3,1,2,3)
t3 + t3 # (1,2,3,1,2,3)

딕셔너리

철수dic = { "id": 0, "이름": "철수" }

# 리스트에 포함시킬 수도 있다.
users = [
         { "id": 0, "이름": "철수" },
         { "id": 1, "이름": "영희" },
         { "id": 2, "이름": "영수" },
         { "id": 3, "이름": "인석" },
         { "id": 4, "이름": "수현" },
         { "id": 5, "이름": "유진" },
         { "id": 6, "이름": "상현" },
         { "id": 7, "이름": "다현" },
         { "id": 8, "이름": "나연" },
         { "id": 9, "이름": "정현" }
]

영희dic = { "id": 0, "이름": "영희" }
영희dic[1] = 2 # { 1: 2, "id": 0, "이름": "영희" }

집합

"""
중복을 허용하지 않는다.
순서가 없다(Unordered).
"""
s1 = set([1,2,3])
s1 # {1, 2, 3}
s2 = set("Hello")
s2 # {'e', 'l', 'o', 'H'}

# 교집합
s1 = set([1,2,3,4,5,6])
s2 = set([4,5,6,7,8,9])
s1 & s2 # {4, 5, 6}
s1.intersection(s2) # {4, 5, 6}

# 합집합
s1 | s2 # {1,2,3,4,5,6,7,8,9}

# 차집합
s1 - s2 # {1,2,3}

# 값 추가
s1=set([1,2,3])
s1.add(4)
s1 # {1,2,3,4}

s1=set([1,2,3])
s1.update([4,5,6])
s1 # {1,2,3,4,5,6}

# 값 제거
s1=set([1,2,3])
s1.remove(1)
s1 # {2,3}