반응형
1일차 자료형 끝
# use print # https://www.python-course.eu/python3_formatted_output.python3_formatted_output.php # basic output print('Python') print("Python") print('''Python''') print("""Python""") print() # separator options print('P', 'Y', 'T', 'H', 'O', 'N', sep=' ') print('010', '1234', '1234', sep='-') print('python', 'google.com', sep='@') print() # end options print('Welcome to', end=' ') print('IT News', end=' ') print('Web Site') print() # file options import sys print('Learn Python', file=sys.stdout) print() # use format(d, s, f) print('%s %s' % ('one', 'two')) print('{} {}'.format('one', '2')) print('{1} {0}'.format('one', 'two')) print() # %s print('%10s' % ('nice')) print('{:>10}'.format('nice')) print('%-10s' % ('nice')) print('{:10}'.format('nice')) print('{:->10}'.format('nice')) print('{:^10}'.format('nice')) print('%.5s' % ('nice')) print('%.5s' % ('learnpython')) print('%5s' % ('learnpython')) print('{:10.5}'.format('pythonstudy')) print() # %d print('%d %d' % (1,2)) print('{} {}'.format(1,2)) print('%4d' % (42)) print('{:4d}'.format(42)) print() # %f print('%f' % (3.14159212121212)) print('%1.8f' % (3.14159212121212)) print('{:f}'.format(3.14159212121212)) print('%06.2f' % (3.14159212121212)) print('{:06.2f}'.format(3.14159212121212)) # use variable # basic variable n = 700 # output print(n) print(type(n)) print() # simultaneous declaration x = y = z = 700 print(x, y, z) print() # Re-declaration var = 75 var = "Change Value" print(var) print(type(var)) print() # Object References print(300) print(int(300)) # n -> 777 n = 777 print(n, type(n)) print() m = n # m -> 777 <- n print(m, n) print(type(m), type(n)) print() m = 400 print(m, n) print(type(m), type(n)) print() # id(identity) check m = 800 n = 655 print(id(m)) print(id(n)) print(id(m) == id(n)) print() # Reference to same Object m = 800 n = 800 print(id(m)) print(id(n)) print(id(m) == id(n)) # Various variable declaration # Camel Case : numberOfCollegeGraduates -> Method # Pascal Case : NumberOfCollegeGraduates -> Class # Snake Case : number_of_college_graduates -> variable # Allowable variable declaration age = 1 Age = 2 aGe = 3 AGE = 4 a_g_e = 5 _age = 6 age_ = 7 _AGE_ = 8 # Reservation word cant use variable name """ and except lambda with as finally nonlocal while assert false None yield break for not class from or continue global pass def if raise del import return elif in True else is try """ # numerical form # Pythons Support data type """ int : 정수 float : 실수 complex : 복소수 bool : 불린 str : 문자열(시퀀스) list : 리스트(시퀀스) tuple : 튜플(시퀀스) set : 집합 dict : 사전 """ # data type str1 = "Python" bool = True str2 = 'Anaconda' float_v = 10.0 int_v = 1 list = [str1, str2] dict = { "name" : "Machine Learning", "version" : 2.0 } tuple = (7, 8, 9) # 7, 8, 9 set = {1, 2, 3} # data type output print(type(str1)) print(type(bool)) print(type(str2)) print(type(float_v)) print(type(int_v)) print(type(dict)) print(type(tuple)) print(type(set)) # numerical form Operator """ + - * / // : only Value % : remainder abs(x) : Absolute value pow(x, y) == x ** y == 2 ** 3 """ # int Declaration i = 77 i2 = -14 big_int = 777777777777777777777777777777777777777777777777777777 # int output print(i) print(i2) print(big_int) # float Declaration f = 0.9999 f2 = 3.141592 f3 = -3.9 f4 = 3 / 9 # float output print(f) print(f2) print(f3) print(f4) # Calculation i1 = 39 i2 = 939 big_int1 = 888888888888888888888888888888888888888888888888888888888888888 big_int2 = 328328394792374982739472893479823749237492387492837497492342493 f1 = 1.234 f2 = 3.939 # + print(">>>> +") print("i1 + i2 = ", i1 + i2) print("f1 + f2 = ", f1 + f2) print("big_int1 + big_int2 = ", big_int1 + big_int2) # Shape transformation a = 3. b = 6 c = .7 d = 12.7 # type output print(type(a), type(b), type(c), type(d)) print(float(b)) print(int(c)) print(int(d)) print(int(True)) # True : 1, False : 0 print(float(False)) print(complex(3)) print(complex('3')) # str -> int print(complex(False)) # Numerical math function print(abs(-7)) x, y = divmod(100, 8) print(x, y) print(pow(5,3), 5 ** 3) # external module import math print(math.ceil(5.1)) # x 이상의 수 중에서 가장 작은 정수 print(math.pi) # Python String # make python strings str1 = "I am Python" str2 = 'Python' str3 = """How are you?""" str4 = '''Thank you!''' print(type(str1), type(str2), type(str3), type(str4)) print(len(str1), len(str2), len(str3), len(str4)) str1_t1 = '' str2_t2 = str() print(type(str1_t1), len(str1_t1)) print(type(str2_t2), len(str2_t2)) # Escape strings # I'm Boy print("I'm Boy") print('I\'m Boy') print('I\\m Boy') print('a \"\" b') escape_str1 = "Do you have a \"retro games\"?" print(escape_str1) escape_str2 = 'What\'s on TV?' print(escape_str2) # \t \n t_s1 = "Click \t Start!" t_s2 = "New Line \n Check!" print(t_s1) print(t_s2) # Raw String output raw_s1 = r'D:\python\test' print(raw_s1) # multi_line input (use \) multi_str = \ """ String Multi Line Test """ print(multi_str) # Strings Calculation str_o1 = "python" str_o2 = "Aplle" str_o3 = "How are you doing" str_o4 = "Seoul Deajeon Busan Jinju" print(str_o1 * 3) print(str_o1 + str_o2) print('y' in str_o1) print('z' in str_o1) print('P' not in str_o2) # string type conversion print(str(66), type(str(66))) print(str(10.1)) print(str(True), type(str(True))) # string functions (upper, isalnum, startswith, count, endswith, isalpha...) print("Capitallize : ", str_o1.capitalize()) print("endswith : ", str_o2.endswith("e")) print("replace : ", str_o1.replace("thon", 'Good')) print("sorted : ", sorted(str_o1)) print("split : ", str_o4.split(' ')) # Repetition(시퀀스) im_str = "Good Boy!" print(dir(im_str)) # __iter__ # output for i in im_str: print(i) # Python list # Can Change or Remove a = [] b = list() c = [88, 28, 30, 25] # 0 1 2 3 d = [1000, 10000, 'Ace', 'Base', 'Captine'] e = [1000, 10000, ['Ace', 'Base', 'Captine']] f = [21,12, 'foot', 2, 4, False, 3.141592] # indexing print('>>>>>') print('d - ', type(d), d) print('d - ', d[1]) print('d - ', d[0] + d[1] + d[1]) print('d - ', d[-1]) print('e - ', e[-1][1]) print('e - ', list(e[-1][1])) # Slicing print('>>>>>') print('d - ', d[0:3]) print('d - ', d[2:]) print('e - ', e[-1][1:3]) # list Calculation print('>>>>>') print('c + d', c + d) print('c * 3', c * 3) # print("'Test' + c[0]", 'Test' + c[0]) ## type error print("'Test' + c[0]", 'Test' + str(c[0])) # Compare Values print(c == c[:3] + c[3:]) print(c) print(c[:3] + c[3:]) # Identity(id) temp = c print(temp, c) print(id(temp), id(c)) # List remove and delete print('>>>>>') c[0] = 4 print('c - ', c) c[1:2] = ['a', 'b', 'c'] # [['a', 'b', 'c']] print('c - ', c) c[1] = ['a', 'b', 'c'] print('c - ', c) c[1:3] = [] print('c - ', c) del c[2] print('c - ', c) # list functions a = [5, 2, 3, 1, 4] print('a - ', a) a.append(10) print('a - ', a) a.sort() print('a - ', a) a.reverse() print('a - ', a) print('a - ', a.index(3), a[3]) a.insert(2, 7) print('a - ', a) a.reverse() print('a - ', a) # del a[1234] a.remove(10) print('a - ', a) print('a - ', a.pop()) print('a - ', a) print('a - ', a.pop()) print('a - ', a) print('a - ', a.count(1)) print('a - ', a) ex = [8, 9] a.extend(ex) print('a - ', a) # 삭제 : remove, pop, # 반복문 while a: data = a.pop() print(data) # python tuple # cant remove, del a = () b = (1,) c = (1,2) d = (100, 1000, 'Ace', 'Base', 'Captine') e = (100, 1000, ('Ace', 'Base', 'Captine')) # indexing print('>>>>>') print('d - ', d[1]) print('d - ', d[0] + d[1] + d[1]) print('d - ', d[-1]) print('e - ', e[-1]) print('e - ', e[-1][1]) print('e - ', list(e[-1][1])) # slicing print('>>>>>') print('d - ', d[0:3]) print('d - ', d[2:]) print('e - ', e[2][1:3]) # tuple Calculation print('>>>>>') print('c + d', c + d) print('c * 3', c * 3) # tuple function a = (5, 2, 3, 1, 4) print('a - ', a) print('a - ', a.index(3)) print('a - ', a.count(2)) # packing & unpacking # packing t = ('foo', 'bar', 'baz', 'qux') print(t) print(t[0]) print(t[-1]) # unpacking (x1, x2, x3, x4) = t # x1, x2, x3, x4 = t print(type(x1), type(x2), type(x3), type(x4)) print(x1, x2,x3, x4) # packing and unpacking t2 = 1, 2, 3 # t2 = (1, 2, 3) t3 = 4, x1, x2, x3 = t2 x4, x5, x6 = 4, 5, 6 # unpacking print(t2) print(t3) print(x1, x2, x3) print(x4, x5, x6) # python Dictionary # many many use (JSON) # Can do it remove and del a = {'name' : 'kim', 'phone' : '01011112222', 'birth' : '525252'} # key : value # {'name' : 'kim', 'name' : 'al'} XXXXX b = {0 : 'Hello Python'} c = {'arr' : [1, 2, 3, 4]} d = { 'Name' : 'Niceman', 'City' : 'Gumi', 'Age' : 5252, 'Grade' : 'F', 'Status' : True } e = dict([ ('name', 'Ji'), ('City', 'Gumi'), ('Age', '5252') ]) f = dict( Name = 'Hyeon', City = 'Gumi', Age = 5252, Grade = 'F' ) print('a - ', type(a), a) print('b - ', type(b), b) print('c - ', type(c), c) print('d - ', type(d), d) print('e - ', type(e), e) print('f - ', type(f), f) # output print('a - ', a['name']) # key X -> error print('a - ', a.get('name')) # key X -> Output "None" print('b - ', b[0]) print('b - ', b.get(0)) print('f - ', f.get('City')) print('f - ', f.get('Age')) # add Dictionary a['address'] = 'seoul' print('a - ', a) a['rank'] = [1, 2, 3] print('a - ', a) print('a - ', len(a)) # dict_keys, dict_values, dict_items : 반복문(__iter__) 사용 가능 print('a - ', a.keys()) print('a - ', list(a.keys())) print('a - ', a.values()) print('a - ', a.items()) print('a - ', a.pop('name')) print('a - ', a) print('f - ', f.popitem()) print('f - ', f) print('a - ', 'birth' in a) # 수정 a['test'] = 'test_dict' print('a - ', a) a['address'] = 'dj' print('a - ', a) a.update(birth='909090') print('a - ', a) temp = {'address': 'Busan'} a.update(temp) print('a - ', a) # set a = set() b = set([1, 2, 3, 4]) c = set([1, 4, 5, 6]) d = set([1, 2, 'Pen', 'Cap', 'Plate']) e = {'foo', 'bar', 'baz', 'foo', 'qux'} f = {42, 'foo', (1, 2, 3), 3.14} print('a - ', type(a), a) print('b - ', type(b), b) print('c - ', type(c), c) print('d - ', type(d), d) print('e - ', type(e), e) print('f - ', type(f), f) # Change (set -> tuple) t = tuple(b) print('t - ', type(t), t) print('t - ', t[0], t[1:3]) # Change (set -> list) l = list(c) l2 = list(e) print('l - ', l) print('l2 - ', l2) # len print(len(a)) # use set s1 = set([1, 2, 3, 4, 5, 6]) s2 = set([4, 5, 6, 7, 8, 9]) print('s1 & s2 : ', s1 & s2) print('s1 & s2 : ', s1.intersection(s2)) print('s1 | s2 : ', s1 | s2) print('s1 | s2 : ', s1.union(s2)) print('s1 - s2 : ', s1 - s2) print('s1 - s2 : ', s1.difference(s2)) # 중복 원소 확인 print('s1 & s2 : ', s1.isdisjoint(s2)) # 부분 집합 확인 print('subset : ', s1.issubset(s2)) print('superset : ',s1.issuperset(s2)) # 추가 & 제거 s1 = set([1, 2, 3, 4]) s1.add(5) print(s1) s1.remove(2) print(s1) s1.discard(3) print(s1) s1.discard(7) s1.clear() print(s1) a = [1, 2, 3] a.clear() print(a)
반응형