2024³â 2¿ù 3ÀÏ
''' str1 = 'pythen' print(str1[4]) str1[4] = 'o' # immutable, ¿À·ù ''' # List (¹è¿) ¼øÂ÷ÀûÀ¸·Î µ¥ÀÌÅ͸¦ ³ª¿ # [10, 20, 30, 40, 50] list1 = [10,20,30,40,50] print(list1) list2 = ['¾È³çÇϼ¼¿ä','¹Ý°©½À´Ï´Ù'] list3 = ['p','y','t','h','o','n'] list4 = [] # ºó ¸®½ºÆ® print(type(list1)) print(type(list2)) print(type(list3)) print(type(list4)) print(list3[1]) print(list3[1:4]) print(type(list3[1:4])) str1 = 'python' print(str1[1:4]) print(type(str1[1:4])) list5 = list() print(list5) print(type(list5))
# List Ư¡ # 1. ¼ø¼°¡ ÀÖ´Ù (À妽Ì, ½½¶óÀ̽Ì) # 2. ¼öÁ¤ÀÌ °¡´É str1 = 'pithon' list1 = ['p','i','t','h','o','n'] print(list1[1]) list1[1] = 'y' print(list1) list1[1] = ['y','o','u'] print(list1) print(list1[1][1]) print(list1[1][1:]) list1[1:3] = ['y','o','u'] print(list1) # 3. »èÁ¦ del(delete) print(list1) del list1[2] print(list1) del list1[2:4] print(list1) ''' del list1 print(list1) ''' # 4. Áߺ¹ °¡´É list1 = [100,100,100,100,100,100,100,100,100,100,100,100] # ¸®½ºÆ® ¿¬»çÀÚ # +(¿¬°á), *(¹Ýº¹) print(list1 + ['py','thon']) print([1,3,5]*5) # ¸®½ºÆ® ³»ÀåÇÔ¼ö # append() : ()¾ÈÀÇ µ¥ÀÌÅÍ ¸Ç µÚ Ãß°¡ list2 = [5,2,1,3,4] print(list2) list2.append(6) print(list2) # insert(index,data) : index À§Ä¡¿¡ data Ãß°¡ list2.insert(3,7) print(list2) # reverse() : ¸®½ºÆ®¸¦ ¿ª¼øÀ¸·Î list2.reverse() print(list2) print(list2[::-1]) # sort() : ¿À¸§Â÷¼ø Á¤·Ä list2.sort() print(list2) # ³»¸²Â÷¼ø # 1. ¿À¸§Â÷¼ø Á¤·Ä ÈÄ ¿ª¼ø list2.sort() list2.reverse() print(list2) # 2. list2.sort(reverse=True) print(list2) # remove(data) : data¸¦ list¿¡¼ ¼øÂ÷ÀûÀ¸·Î ã¾Æ¼ »èÁ¦ list3 = [10,20,30,10,10,10] del (list3[3]) print(list3) list3 = [10,20,30,10,10,10] list3.remove(10) print(list3) # pop() : µ¥ÀÌÅ͸¦ Ãâ·Â ÈÄ »èÁ¦ # 1. ¸®½ºÆ®.pop() : ¸Ç µÚ¿¡ ÀÖ´Â ¿ä¼Ò ²¨³½ ÈÄ »èÁ¦ list3 = [1,2,3,4,5] e = list3.pop() print(e) print(list3) # 2. ¸®½ºÆ®.pop(index) : index¿¡ ÇØ´çµÇ´Â ¿ä¼Ò ²¨³½ ÈÄ »èÁ¦ list3.pop(2) print(list3) list4 = [10,20,30,10,10,10] list4.pop(3) # del list4[3] print(list4) str2 = 'pithon' li = list(str2) print(li) li[1] = 'y' print(li) str3 = str(li) print(str3) print(type(str3)) # join() # ¹®ÀÚ¿, ¸®½ºÆ®, Æ©Çà print("_".join(li)) print(type("_".join(li))) print("".join(li)) print(".".join("¾ö°¡¿ø"))
# ¸®½ºÆ® : ¼ø¼o, ¼öÁ¤o, »èÁ¦o, Áߺ¹o # Æ©Çà : ¼ø¼o, ¼öÁ¤x, »èÁ¦x, Áߺ¹o list1 = [1,2,3,4,5] tup1 = (1,2,3,4,5) tup2 = ('p','y','t','h','o','n') print(tup1) print(tup2) print(tup2[2]) tup3 = 1,2,3,4,5 print(tup3) print(type(tup3)) tup4 = (10) print(tup4) print(type(tup4)) tup5 = 10 print(tup5) print(type(tup5)) tup6 = (10,) print(tup6) print(type(tup6)) tup8 = () print(tup8) print(type(tup8)) tup9 = tuple() print(tup9) print(type(tup9)) tup10 = ('p','i','t','h','o','n') #tup10[1] = 'y' #del tup10[1] tup11 = (10,10,10,10,10) list10 = list(tup11) print(list10) print(type(list10)) list10[0] = 20 print(list10) tuple11=tuple(list10) print(type(tuple11)) print(tup11[2:]) tup12 = (1,2,3,4,('a','b','c'),5,6) print(tup12[4][1]) print(tup12[4][1:]) # Æ©Çà ¿¬»êÀÚ # +(¿¬°á), *(¹Ýº¹) print((1,2,3)+('a','b','c')) print((1,2,3)*5) # Æ©Çà ³»ÀåÇÔ¼ö # index(data) : data°¡ ÀÖ´Â À§Ä¡(index)°ªÀ» ¹Ýȯ a = (1,2,3,1,2,3) print(a.index(2)) b = [1,2,3,1,2,3] print(b.index(2,3)) # count(data) : dataÀÇ °³¼ö print(a.count(1)) print(b.count(1))
# µñ¼Å³Ê¸® # ¼ø¼x, Áߺ¹x, ¼öÁ¤o, »èÁ¦o # key, value dic1 = {'apple':'»ç°ú','strawberry':'µþ±â'} print(dic1) print(type(dic1)) print(dic1['apple']) print(dic1['strawberry']) dic2 = {1:'a',2:'b',3:'c'} print(dic2[2]) user = {'name':'È«±æµ¿','age':25,'phone':'010-0000-2222'} print(user['phone']) user = {'name':'È«±æµ¿','age':25,'phone':['010-0000-2222','010-0000-3333']} print(user['phone']) print(user['phone'][0:2]) # µ¥ÀÌÅÍ Ãß°¡ user['birth'] = '2024-02-03' print(user) # keys() : µñ¼Å³Ê¸®¿¡ ÀúÀåµÈ key°ªµé ¹Ýȯ print(user.keys()) print(type(user.keys())) print(list(user.keys())) print(type(list(user.keys()))) # values() : µñ¼Å³Ê¸®¿¡ ÀúÀåµÈ value°ªµé ¹Ýȯ print(user.values()) print(list(user.values())) # items() : µñ¼Å³Ê¸®¿¡ ÀúÀåµÈ (key,value) Æ©Çà ÇüÅ·Π¹¾î¼ ¹Ýȯ print(user.items()) # get() : key¿¡ ÇØ´çµÇ´Â value °ª ¹Ýȯ print(user['age']) print(user.get('age')) # Â÷ÀÌÁ¡ print(user.get('address')) # print(user['address']) ¿À·ù ¹ß»ý # in ¿¬»êÀÚ print('name' in user) print('address' in user) print('address' not in user) print(not('address' in user))
# ÁýÇÕÀÚ·áÇü(Sets) : ¼ø¼x, Áߺ¹x a = set() b = set([1,2,3,4,5,6]) c = set((1,2,3,4,5,6)) d = {1,2,3,4,5,6} print(type(a)) print(type(b)) print(type(c)) print(type(d)) print(b) e = set('hello') print(e) print(type(e)) aSet = {1,2,3,4,5,6} bSet = {4,5,6,7,8,9} # ÇÕÁýÇÕ (Áߺ¹x) print(aSet|bSet) print(aSet.union(bSet)) print(bSet|aSet) # ±³ÁýÇÕ (°øÅëµÈ µ¥ÀÌÅÍ) # & (Ampersand) print(aSet&bSet) print(aSet.intersection(bSet)) print(bSet&aSet) # Â÷ÁýÇÕ (°øÅëµÈ µ¥ÀÌÅ͸¦ »©ÁÖ´Â °Å) print(aSet-bSet) print(aSet.difference(bSet)) print(bSet-aSet) print(bSet.difference(aSet)) list1 = [10,10,10,20,20,20,30,30,30,40] set1 = set(list1) print(set1) list2 = list(set1) print(list2) list2.sort() print(list2) # Ãß°¡ ¹× Á¦°Å set1.add(50) print(set1) set1.update([60,70,20,60,80,90]) print(set1) set1.remove(90) print(set1) # ¸®½ºÆ®, Æ©ÇÃ, µñ¼Å³Ê¸®, ÁýÇÕ => pop() dict1 = {1:'a',2:'b',3:'c'} res1 = dict1.pop(1) print(res1, dict1) print(set1.pop())
¹®Á¦Ç®±â # 11. ´ÙÀ½ ¸®½ºÆ®¿¡¼ "Apple" Ç׸ñ¸¸ »èÁ¦Çϼ¼¿ä. : ["Banana", "Apple", "Orange"] li1 = ["Banana", "Apple", "Orange"] li1.pop(1) print(li1) # 12. ´ÙÀ½ Æ©ÇÃÀ» ¸®½ºÆ®·Î º¯È¯Çϼ¼¿ä. : (1,2,3,4) tu1 = (1,2,3,4) li2 = list(tu1) print(li2) # 13. ´ÙÀ½ Ç׸ñÀ» µñ¼Å³Ê¸®(dict)À¸·Î ¼±¾ðÇغ¸¼¼¿ä. : <¼ºÀÎ - 100000 , û¼Ò³â - 70000 , ¾Æµ¿ - 30000> dic1 = {'¼ºÀÎ' : 100000 , 'û¼Ò³â' : 70000 , '¾Æµ¿' : 30000} # 14. 13¹ø ¿¡¼ ¼±¾ðÇÑ dict Ç׸ñ¿¡ <¼Ò¾Æ - 0> Ç׸ñÀ» Ãß°¡Çغ¸¼¼¿ä. dic1['¼Ò¾Æ'] = 0 print(dic1) # 15. 13¹ø¿¡¼ ¼±¾ðÇÑ µñ¼Å³Ê¸®(dict)¿¡¼ Key Ç׸ñ¸¸ Ãâ·ÂÇغ¸¼¼¿ä. print(dic1.keys()) # 16. 13¹ø¿¡¼ ¼±¾ðÇÑ µñ¼Å³Ê¸®(dict)¿¡¼ value Ç׸ñ¸¸ Ãâ·ÂÇغ¸¼¼¿ä. print(dic1.values()) # 17. a¸®½ºÆ®¿¡¼ Áߺ¹¼ýÀÚ¸¦ Á¦°ÅÇغ¸¼¼¿ä. a = [1,1,1,2,2,2,3,3,3] a = [1,1,1,2,2,2,3,3,3] aSet = set(a) print(aSet) # 18. bÆ©Çÿ¡ °ª 4¸¦ Ãß°¡ÇÏ¿© (1,2,3,4)¸¦ Ãâ·ÂÇغ¸¼¼¿ä. b = (1,2,3,4) b = (1,2,3,4) li2 = list(b) li2.append(4) print(li2) # 19. ['Life', 'is', 'too','short'] ¸¦ "Life is too short"¹®ÀÚ¿·Î ¸¸µé¾î¼ Ãâ·ÂÇغ¸¼¼¿ä. li3 = ['Life', 'is', 'too','short'] print(" ".join(li3)) # 20. [1,3,4,2,5] ¸®½ºÆ®¸¦ ³»¸²Â÷¼øÇÏ¿© Ãâ·ÂÇغ¸¼¼¿ä. li4 = [1,3,4,2,5] li4.sort(reverse=True) print(li4)