python/기본
-
Forpython/기본 2022. 4. 29. 17:55
some_list = [1, 2, 3] for i in some_list: print(i) for s in 'abc': print(s) # a b c for x in ['A', 'B', 'C']: print(x) else: print('printed_all') # A B C printed_all #################################################################### d = {'x': 100, 'y': 200} for v in d: print(v) # x y print(d.items()) # dict_items([('x', 100), ('y', 200)]) for k, v in d.items(): print(k, ':', v)
-
Whilepython/기본 2022. 4. 29. 17:45
count = 0 while count = 5: break if count == 2: count += 1 continue print(count) # count += 1 # 0, 1, 3, 4 #################################################################### count = 0 while count < 5: print(count) count += 1 else: print('done') # while 안에서 break 에 걸..
-
Setpython/기본 2022. 4. 29. 15:17
a = {1, 2, 3, 2, 3, 4, 4, 5} print(a) # {1, 2, 3, 4, 5} print(type(a)) # b = {2, 3, 7} c = a - b print(c) # {1, 4, 5} print(a) # {1, 2, 3, 4, 5} print(b) # {2, 3, 7} c = a & b print(c) # {2, 3} 교집합 c = a | b print(c) # {1, 2, 3, 4, 5, 7} 합집합 c = a ^ b print(c) # {1, 4, 5, 7} 대칭 차집합 #################################################################### s = {1, 2, 3, 4, 5} s.add(6) print(s) # {1, 2,..
-
Dictionarypython/기본 2022. 4. 29. 14:59
d = {'x': 10, 'y': 20} print(type(d)) # print(d['x']) # 10 d['x'] = 100 print(d) # {'x': 100, 'y': 20} d['z'] = 'ZZZ' print(d) # {'x': 100, 'y': 20, 'z': 'ZZZ'} d[1] = 10000 print(d) # {'x': 100, 'y': 20, 'z': 'ZZZ', 1: 10000} print(dict(a=10, b=20)) # {'a': 10, 'b': 20}, 사전 생성의 또다른 방법 #################################################################### d = {'x': 10, 'y': 20} print(help(dict)) p..
-
Tuplepython/기본 2022. 4. 29. 14:36
# Tuple 은 immutable # immutable 이 필요한 곳에서는 list 대신에 tuple 을 사용하자 t = (1, 2, 3, 4, 1, 2) print(type(t)) # # t[0] = 100 # TypeError: 'tuple' object does not support item assignment print(t[0]) # 1 print(t[1:3]) # (2, 3) print(t.count(1)) # 2 t = ([1, 2, 3], [4, 5, 6]) print(t[0][0]) # 1 t[0][0] = 100 print(t) # ([100, 2, 3], [4, 5, 6]) ##############################################################..
-
Listpython/기본 2022. 4. 29. 12:08
# List 는 mutable l = [0, 1, 2, 3, 4, 5] print(l[0]) # 0 print(l[-1]) # 5 print(l[-2]) # 4 print(l[0:2]) # [0, 1] print(l[2:]) # [2, 3, 4, 5] print(list('abc')) # ['a', 'b', 'c'] print(type(l)) # 'list' print(l[::2]) # [0, 2, 4] print(l[::-1]) # [5, 4, 3, 2, 1, 0] a = ['a', 'b', 'c'] n = [1, 2, 3] x = [a, n] # [['a', 'b', 'c'], [1, 2, 3]] print(x[0]) # ['a', 'b', 'c'] print(x[0][1]) # 'b' #######..