# IndexError ์์ธ: ๋ฆฌ์คํธ์ ๊ธธ์ด๋ฅผ ๋๋ ์ธ๋ฑ์ค๋ก ์์์ ์ ๊ทผํ๊ณ ์ ํ ๋ ๋ฐ์
# ---------------------------------------- #
listA = [1,2,3]
listB = [4,5,6]
print(listA + listB) # [1,2,3,4,5,6]
print(listA * 3) # [1,2,3,1,2,3,1,2,3]
print(len(listA)) # 3
listA.extend(listB) # (์ฐ๊ฒฐ ์ฐ์ฐ์ '+' ์ ๋ฌ๋ฆฌ) 'ํ๊ดด์ ์ฒ๋ฆฌ'
print(listA) # [1,2,3,4,5,6]
# ---------------------------------------- #
listA = [1,2,3]
listB = [4,5,6]
listC = [4,5,6]
listA.append(4) # ๋ฆฌ์คํธ ๋ค์ ์์ ์ถ๊ฐ
print(listA) # [1,2,3,4]
# ๋น๊ตํ๊ธฐ) ๋ ๋ค 'ํ๊ดด์ ์ฒ๋ฆฌ'๋ค.
# listB.append(listA) -> [4,5,6,[1,2,3]]
# listC += listA -> [4,5,6,1,2,3]
listA.insert(0,10) # ๋ฆฌ์คํธ ์ค๊ฐ์ ์์ ์ถ๊ฐ
print(listA) # [10,1,2,3,4]
# ---------------------------------------- #
#์ธ๋ฑ์ค๋ก ์์ ์ ๊ฑฐ
listA = [0,1,2,3,4,5]
del listA[1]
print(listA) # [0,2,3,4,5]
listA.pop()
print(listA) # [0,2,3,4]
listA.pop(2)
print(listA) # [0,2,4]
listB = [0,1,2,3,4,5,6]
listC = [0,1,2,3,4,5,6]
listD = [0,1,2,3,4,5,6]
del listB[3:6]
print(listB) # [0,1,2,6]
del listC[:3]
print(listC) # [3,4,5,6]
del listD[3:]
print(listD) # [0,1,2]
# ---------------------------------------- #
# <์ฌ๋ผ์ด์ฑ>
# ํ์>> ๋ฆฌ์คํธ_์ด๋ฆ [ ์์_์ธ๋ฑ์ค : ๋_์ธ๋ฑ์ค : ๋จ๊ณ ]
numbers1 = [1,2,3,4,5,6,7,8]
numbers2 = [1,2,3,4,5,6,7,8]
print(numbers1[0:5:2]) # [1,3,5]
print(numbers2[::-1]) # [8,7,6,5,4,3,2,1]
# ---------------------------------------- #
# ๊ฐ์ผ๋ก ์ ๊ฑฐ
listA = [1,2,1,2]
listB = [0,1,2,3,4,5]
listA.remove(2)
print(listA) # [1,1,2]
# ๋ฆฌ์คํธ์ ์ค๋ณต๋ ์ฌ๋ฌ๊ฐ์ ๊ฐ์ ๋ชจ๋ ์ ๊ฑฐํ๋ ค๋ฉด ๋ฐ๋ณต๋ฌธ๊ณผ ์กฐํฉํด์ ์ฌ์ฉํด์ผํจ
# while 2 in listC:
# listC.remove(2)
# print(listC)
listB.clear()
print(listB) # []
# ---------------------------------------- #
# ์์ ์ ๋ ฌํ๊ธฐ
listA = [52, 273, 103, 32, 275, 1, 7]
listA.sort() # ์ค๋ฆ์ฐจ์
print(listA) # [1,7,32,52,103,273,275]
listA.sort(reverse = True) # ๋ด๋ฆผ์ฐจ์
print(listA) # [275,273,103,52,32,7,1]
# ---------------------------------------- #
# in / not in ์ฐ์ฐ์
# ํน์ ๊ฐ์ด ๋ด๋ถ์ ์๋์ง ํ์ธ
listA = [273,32,103,57,52]
print(273 in listA) # True
print(10 in listA) # False
print(273 not in listA) # False
print(10 not in listA) # True
# ---------------------------------------- #
# for์ ๋ฆฌ์คํธ
array = [273,32,103,57,52]
for element in array:
print(element)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ:
# 273
# 32
# 103
# 57
# 52
for character in "์๋
ํ์ธ์":
print("-", character)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ:
# - ์
# - ๋
# - ํ
# - ์ธ
# - ์
listOfList = [
[1,2,3],
[4,5,6,7],
[8,9]
]
for items in listOfList:
for item in items:
print(item)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ:
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# ---------------------------------------- #
# ์ ๊ฐ ์ฐ์ฐ์ '*'
listA = [1, 2, 3, 4]
listB = [*listA, *listA]
listC = [*listA, 5]
listD = [listA, listA]
print(listA) # [1, 2, 3, 4]
print(listB) # [1, 2, 3, 4, 1, 2, 3, 4]
print(listC) # [1, 2, 3, 4, 5]
print(listD) # [[1, 2, 3, 4], [1, 2, 3, 4]]
# ---------------------------------------- #
# min(๋ฆฌ์คํธ_์ด๋ฆ): ๋ฆฌ์คํธ ๋ด๋ถ์ ์ต์'๊ฐ' ๋ฐํ
# max(๋ฆฌ์คํธ_์ด๋ฆ): ๋ฆฌ์คํธ ๋ด๋ถ์ ์ต๋'๊ฐ' ๋ฐํ
>>> max(iterable, *iterables, key=None, default=None)
>>> min(iterable, *iterables, key=None, default=None)
- iterable: ์ฐพ์ ๋์์ธ ํ๋์ ๋ฐ๋ณต ๊ฐ๋ฅํ ๊ฐ์ฒด (ํ์)
- *iterables: ์ถ๊ฐ์ ์ธ iterable (์ ํ)
- key: ํจ์๋ฅผ ์ธ์๋ก ๋ฐ์ ๊ฐ ์์์ ์ ๋ ฌ ์์๋ฅผ ๊ฒฐ์ ํ๋๋ฐ ์ฌ์ฉ
- default: itrable์ด ๋น์ด์์ ๋ ๋ฐํ๋ ๊ฐ (๋น ์ง์ ์ ValueError ๋ฐ์)
# sum(๋ฆฌ์คํธ_์ด๋ฆ): ๋ฆฌ์คํธ ๋ด๋ถ์ ๊ฐ์ ๋ชจ๋ ๋ํ '๊ฐ'์ ๋ฐํ
# ---------------------------------------- #
# reversed(๋ฆฌ์คํธ_์ด๋ฆ): ๋ค์ง์ด์ง '์ดํฐ๋ ์ดํฐ'๋ฅผ ๋ฐํ
# ๋ฐํ ๊ฒฐ๊ณผ๊ฐ '๋ฆฌ์คํธ'๊ฐ ์๋์ ๊ธฐ์ตํ์!
numbers = [1,2,3,4,5]
for i in reversed(numbers):
print('๋ฐ๋ณต๋ฌธ: {}'.format(i))
# ์คํ ๊ฒฐ๊ณผ
# ๋ฐ๋ณต๋ฌธ: 5
# ๋ฐ๋ณต๋ฌธ: 4
# ๋ฐ๋ณต๋ฌธ: 3
# ๋ฐ๋ณต๋ฌธ: 2
# ๋ฐ๋ณต๋ฌธ: 1
# ---------------------------------------- #
# enumerate(๋ฆฌ์คํธ_์ด๋ฆ)
# : ๋ฆฌ์คํธ์ ์์๋ฅผ ๋ฐ๋ณตํ ๋ ์ธ๋ฑ์ค๊ฐ ๋ช ๋ฒ์งธ์ธ์ง ํ์ธํ๊ธฐ ์ํด ์ฌ์ฉ
exampleList = ['A','B','C']
print('# ๋จ์ ์ถ๋ ฅ')
print(exampleList)
print('-')
print('# enumerate()ํจ์ ์ ์ฉ')
print(enumerate(exampleList))
print('-')
print('# list() ํจ์๋ก ๊ฐ์ ๋ณํ ์ถ๋ ฅ')
print(list(enumerate(exampleList)))
print('-')
print('# ๋ฐ๋ณต๋ฌธ๊ณผ ์กฐํฉํ๊ธฐ')
for i, value in enumerate(exampleList): # for ์ in ์ฌ์ด์ ๋ณ์๊ฐ ๋ ๊ฐ๊ฐ ๋ค์ด๊ฐ
print('{}๋ฒ์งธ ์์๋ {}์
๋๋ค.'.format(i,value))
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# # ๋จ์ ์ถ๋ ฅ
# ['A', 'B', 'C']
# -
# # enumerate()ํจ์ ์ ์ฉ
# <enumerate object at 0x000001E037044DC0>
# -
# # list() ํจ์๋ก ๊ฐ์ ๋ณํ ์ถ๋ ฅ
# [(0, 'A'), (1, 'B'), (2, 'C')]
# -
# # ๋ฐ๋ณต๋ฌธ๊ณผ ์กฐํฉํ๊ธฐ
# 0๋ฒ์งธ ์์๋ A์
๋๋ค.
# 1๋ฒ์งธ ์์๋ B์
๋๋ค.
# 2๋ฒ์งธ ์์๋ C์
๋๋ค.
# ๋ฆฌ์คํธ_์ด๋ฆ = [ ํํ์ for ๋ฐ๋ณต์ in ๋ฐ๋ณตํ _์_์๋_๊ฒ ]
# ๋ฆฌ์คํธ_์ด๋ฆ = [ ํํ์ for ๋ฐ๋ณต์ in ๋ฐ๋ณตํ _์_์๋_๊ฒ if ์กฐ๊ฑด๋ฌธ ]
array1=[]
for i in range(0,20,2):
array1.append(i * i)
print(array1)
# ๋์
array2 = [i * i for i in range(0,20,2)]
# ํด์: range(0,20,2)์ ์์๋ฅผ i๋ผ๊ณ ํ ๋ i * i๋ก ๋ฆฌ์คํธ๋ฅผ ์ฌ์กฐํฉํด ์ค
print(array2)
# ---------------------------------------- #
array = ['apple','plum','chocolate','banana','cherry']
# output = [fruit for fruit in array if fruit != 'chocolate'] ๋๋
output = [fruit
for fruit in array
if fruit != 'chocolate']
print(output) # ['apple', 'plum', 'banana', 'cherry']
myDic = {
"name" : "7D mango",
"type" : "fruit",
"ingredient" : ["mango", "sugar", "salt", "color"],
"origin" : "Philippines"
}
print("name:",myDic["name"])
print("type:",myDic["type"])
print("ingredient:",myDic["ingredient"])
print(myDic["ingredient"][1])
print("origin:",myDic["origin"])
print()
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# name: 7D mango
# type: fruit
# ingredient: ['mango', 'sugar', 'salt', 'color']
# sugar
# origin: Philippines
myDic["name"] = "8D mango" # ๊ฐ์ ์์
print("name:",myDic["name"]) # name: 8D mango
myDic["price"] = 5000 # ํค์ ๊ฐ์ ์ถ๊ฐ
print(myDic)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# {'name': '8D mango', 'type': 'fruit',
# 'ingredient': ['mango', 'sugar', 'salt', 'color'],
# 'origin': 'Philippines', 'price': 5000}
del myDic['type'] # ํค์ ๊ฐ์ ์ ๊ฑฐ
print(myDic)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# {'name': '8D mango', 'type': 'fruit',
# 'ingredient': ['mango', 'sugar', 'salt', 'color'],
# 'origin': 'Philippines'}
# ---------------------------------------- #
myDic = {}
print('์์ ์ถ๊ฐ ์ด์ :',myDic)
myDic['name'] = 'new name'
myDic['head'] = 'new spirit'
myDic['body'] = 'new body'
print('์์ ์ถ๊ฐ ์ดํ:',myDic)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# ์์ ์ถ๊ฐ ์ด์ : {}
# ์์ ์ถ๊ฐ ์ดํ: {'name': 'new name', 'head': 'new spirit', 'body': 'new body'}
# ---------------------------------------- #
# KeyError : ์กด์ฌํ์ง ์๋ ํค์ ์ ๊ทผ์ ๋ฐ์
# ---------------------------------------- #
myDic = {
"name" : "7D mango",
"type" : "fruit",
"ingredient" : ["mango", "sugar", "salt", "color"],
"origin" : "Philippines"
}
key = input('> ์ ๊ทผํ๊ณ ์ ํ๋ ํค: ')
if key in myDic:
print(myDic[key])
else:
print("์กด์ฌํ์ง ์๋ ํค์ ์ ๊ทผํ๊ณ ์์ต๋๋ค.")
# ---------------------------------------- #
# get() : '๋์
๋๋ฆฌ_์ด๋ฆ [ ํค ]' ์ ๊ฐ์ ๊ธฐ๋ฅ์ ํ๋๋ฐ
# ์กด์ฌํ์ง ์๋ ํค์ ์ ๊ทผํ ๊ฒฝ์ฐ KeyError ๋ฐ์ ๋์ None ์ถ๋ ฅ
value = myDic.get('์กด์ฌํ์ง ์๋ ํค')
print('๊ฐ:',value)
if value == None:
print("์กด์ฌํ์ง ์๋ ํค์ ์ ๊ทผํ์์ต๋๋ค.")
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# ๊ฐ: None
# ์กด์ฌํ์ง ์๋ ํค์ ์ ๊ทผํ์์ต๋๋ค.
# ---------------------------------------- #
# for๋ฌธ๊ณผ ๋์
๋๋ฆฌ
for key in myDic:
print(key,':',myDic[key])
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# {'name': '8D mango', 'type': 'fruit',
# 'ingredient': ['mango', 'sugar', 'salt', 'color'],
# 'origin': 'Philippines'}
# ---------------------------------------- #
# ๋์
๋๋ฆฌ_์ด๋ฆ.item() ํ์ฉ
exDic = {
'keyA':'valueA',
'keyB':'valueB',
'keyC':'valueC'
}
print(exDic.items())
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# dict_items([('keyA', 'valueA'), ('keyB', 'valueB'), ('keyC', 'valueC')])
for key, element in exDic.items():
print(key,':',element)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# keyA : valueA
# keyB : valueB
# keyC : valueC
๐ก ํ์ด์ฌ์์ ๋ฒ์๋ฅผ ์ง์ ํ ๋ ๋ฒ์์ ๋์ผ๋ก ์
๋ ฅํ ์ซ์๋ ํฌํจ๋์ง ์๋๋ค!
ex) range(0,10) โ โ0 ๋ถํฐ 9 ๊น์งโ ๋ฅผ ์๋ฏธํจ!!
for i in range(4,-1,-1): # range(4,0-1,-1) ํํ์ผ๋ก ๊ฐ์กฐํ ์๋ ์์
print("ํ์ฌ ๋ฐ๋ณต ๋ณ์: {}".format(i))
for i in reversed(range(5)):
print("ํ์ฌ ๋ฐ๋ณต ๋ณ์: {}".format(i))
# ์คํ ๊ฒฐ๊ณผ
# ํ์ฌ ๋ฐ๋ณต ๋ณ์: 4
# ํ์ฌ ๋ฐ๋ณต ๋ณ์: 3
# ํ์ฌ ๋ฐ๋ณต ๋ณ์: 2
# ํ์ฌ ๋ฐ๋ณต ๋ณ์: 1
# ํ์ฌ ๋ฐ๋ณต ๋ณ์: 0
output = ''
for i in range(1,11):
for j in range(i):
output+='*'
output += '\n'
print(output)
# ์ถ๋ ฅ๊ฒฐ๊ณผ
# *
# **
# ***
# ****
# *****
# ******
# *******
# ********
# *********
# **********
# ---------------------------------------- #
output = ''
for i in range(1,11):
for j in range(11-i):
output += ' '
for k in range(2 * i - 1):
output += '*'
output += '\n'
print(output)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# *
# ***
# *****
# *******
# *********
# ***********
# *************
# ***************
# *****************
# *******************
import time
number = 0
targetTick = time.time() + 5
while time.time() < targetTick:
number += 1
print('5์ด ๋์ {}๋ฒ ๋ฐ๋ณตํ์ต๋๋ค.'.format(number))
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# 5์ด ๋์ 29396701๋ฒ ๋ฐ๋ณตํ์ต๋๋ค.
# ---------------------------------------- #
#10์ด ํ์ด๋จธ ๋ง๋ค๊ธฐ
import time
count = 1
maxTime = time.time() + 10
while True:
time.sleep(1)
print(count)
if time.time() > maxTime:
break
count += 1
i = 1
while True:
print('{}๋ฒ์งธ ๋ฐ๋ณต๋ฌธ์
๋๋ค.'.format(i))
i += 1
inputText = input('> ์ข
๋ฃํ์๊ฒ ์ต๋๊น?(y/n): ')
if inputText in ['y','Y']:
print('๋ฐ๋ณต์ ์ข
๋ฃํฉ๋๋ค')
break
# 'y' ๋๋ 'Y'๊ฐ ์
๋ ฅ๋ ๋๊น์ง ๋ฐ๋ณต
# 'y' ๋๋ 'Y'๊ฐ ์
๋ ฅ๋๋ฉด ํ๋ก๊ทธ๋จ ์ข
๋ฃ
# ---------------------------------------- #
# ์ด์ค ๋ฃจํ ํ์ถ
i = 1
while True:
print('{}๋ฒ์งธ ๋ฐ๋ณต๋ฌธ์
๋๋ค.'.format(i))
i += 1
while True:
inputText = input('์ข
๋ฃํ์๊ฒ ์ต๋๊น? (y/n) : ')
if inputText in ['y','Y']:
print('๋ฐ๋ณต์ ์ข
๋ฃํฉ๋๋ค.')
break
elif inputText in ['n', 'N']:
break
else:
print('์ฌ๋ฐ๋ฅธ ์
๋ ฅ์ด ์๋๋๋ค!')
continue
if inputText in ['y', 'Y']:
break
# ---------------------------------------- #
numbers = [5,15,6,20,7,25]
for number in numbers:
if number < 10:
continue
print(number)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# 15
# 20
# 25
test = (
'์ด๋ ๊ฒ ์
๋ ฅํด๋ '
'ํ๋์ ๋ฌธ์์ด๋ก ์ฐ๊ฒฐ๋์ด '
'์์ฑ๋ฉ๋๋ค.'
)
print(test) # ์ด๋ ๊ฒ ์
๋ ฅํด๋ ํ๋์ ๋ฌธ์์ด๋ก ์ฐ๊ฒฐ๋์ด ์์ฑ๋ฉ๋๋ค.
print(type(test)) # <class 'str'>
# ---------------------------------------- #
# ๋ฌธ์์ด.join(๋ฌธ์์ด๋ก_๊ตฌ์ฑ๋_๋ฆฌ์คํธ)
print('::'.join(['1','2','3','4','5'])) # 1::2::3::4::5
# ์์ฉ:
number = int(input('์ ์ ์
๋ ฅ> '))
if number % 2 == 0:
print('\n'.join([
'์
๋ ฅํ ๋ฌธ์์ด์ {}์
๋๋ค.',
'{}๋(์) ์ง์์
๋๋ค.'
]).format(number, number))
else:
print('\n'.join([
'์
๋ ฅํ ๋ฌธ์์ด์ {}์
๋๋ค.',
'{}๋(์) ํ์์
๋๋ค.'
]).format(number, number))
# ๋ ๊ฐ๋จํ๊ฒ๋
number = int(input('์ ์ ์
๋ ฅ>> '))
answer = [
'์
๋ ฅํ ๋ฌธ์์ด์{}์
๋๋ค',
'{}๋(์) {}์
๋๋ค.'
]
if number % 2 == 0:
print('\n'.join(answer).format(number, number, 'even'))
else:
print('\n'.join(answer).format(number, number, 'odd'))
# reversed() ํจ์์ ์ดํฐ๋ ์ดํฐ
numbers = [1,2,3,4,5,6]
rNum = reversed(numbers)
print(rNum)
print(next(rNum))
print(next(rNum))
print(next(rNum))
print(next(rNum))
print(next(rNum))
print(next(rNum))
# ์คํ ๊ฒฐ๊ณผ
# <list_reverseiterator object at 0x000001E037012D90>
# 6
# 5
# 4
# 3
# 2
# 1
๐ก ์ reversed() ํจ์๋ ๋ฆฌ์คํธ๋ฅผ ๋ฐ๋ก ๋ฆฌํดํด ์ฃผ์ง ์๊ณ ์ดํฐ๋ ์ดํฐ๋ฅผ ๋ฆฌํดํด ์ฃผ๋ ๊ฑธ๊น?
: ๋ฉ๋ชจ๋ฆฌ์ ํจ์จ์ฑ์ ์ํด์! ์๋ง์ ์์๊ฐ ๋ค์ด์๋ ๋ฆฌ์คํธ๋ฅผ ๋ณต์ ํ ๋ค ๋ค์ง์ด์ ๋ง
ํดํ๋ ๊ฒ๋ณด๋ค ๊ธฐ์กด์ ์๋ ๋ฆฌ์คํธ๋ฅผ ํ์ฉํด์ ์์
ํ๋ ๊ฒ์ด ํจ์ฌ ํจ์จ์ ์ด๊ธฐ ๋๋ฌธ.
def printNtimes(n, *values):
for i in range(n):
for value in values:
print(value)
print('-')
printNtimes(3, 'hello', 'world', 'good', 'morning')
print()
def printNtimes(n, *values):
for i in range(n):
for value in values:
print(value)
print('-')
printNtimes(3, ['hello', 'world', 'good', 'morning'])
print()
def printNtimes(n, values):
for i in range(n):
for value in values:
print(value)
print('-')
printNtimes(3, ['hello', 'world', 'good', 'morning'])
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# hello
# world
# good
# morning
# -
# hello
# world
# good
# morning
# -
# hello
# world
# good
# morning
# -
#
# ['hello', 'world', 'good', 'morning']
# -
# ['hello', 'world', 'good', 'morning']
# -
# ['hello', 'world', 'good', 'morning']
# -
#
# hello
# world
# good
# morning
# -
# hello
# world
# good
# morning
# -
# hello
# world
# good
# morning
# -
def printNtimes(*values, n = 2):
for i in range(n):
for value in values:
print(value)
print('-')
printNtimes('hello,','world!','good','morning!',n = 3)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# hello,
# world!
# good
# morning!
# -
# hello,
# world!
# good
# morning!
# -
# hello,
# world!
# good
# morning!
# -
# ---------------------------------------- #
def test(a,b = 10,c = 100):
print(a + b + c)
# a๋ ์ผ๋ฐ ๋งค๊ฐ๋ณ์์ด๋ฏ๋ก ํด๋น ์์น์ ๋ฐ๋์ ์
๋ ฅํด์ผํจ
# ๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์๋ ํ์ํ ๊ฒ๋ง ํค์๋๋ฅผ ์ง์ ํด์ ์
๋ ฅํด์ฃผ๋ ํธ
test(10,20,30)
test(a = 10,b = 100,c = 200)
test(c = 10,a = 100,b = 200)
test(10,c = 200)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# 60
# 310
# 310
# 220
# ---------------------------------------- #
def sumAll(start = 0,end = 100,step = 1):
output = 0
for i in range(start,end + 1,step):
output += i
return output
print('a.',sumAll(10,70,10))
print('b.',sumAll(end = 99))
print('c.',sumAll(end = 99,step = 2))
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# a. 280
# b. 4950
# c. 2450
def flatten(data):
output = []
for item in data:
if type(item) == list:
output += flatten(item)
else:
output.append(item)
return output
example = [[1, 2, 3], [4, [5, 6]], 7, [8, 9]]
print('์๋ณธ: {}'.format(example))
print('๋ณํ: {}'.format(flatten(example)))
# ์ถ๋ ฅ ๊ฒฐ๊ณผ:
# ์๋ณธ: [[1, 2, 3], [4, [5, 6]], 7, [8, 9]]
# ๋ณํ: [1, 2, 3, 4, 5, 6, 7, 8, 9]
# ๊ดํธ๋ฅผ ์๋ตํด๋ ํํ๋ก ์ธ์ํ ์ ์๋ ๊ฒฝ์ฐ๋ ๊ดํธ๋ฅผ ์๋ตํด๋ ๋๋ค.
tupleTest = 10, 20, 30, 40
print('tupleTest: {}'.format(tupleTest))
print('type(tupleTest): {}'.format(type(tupleTest)))
a, b, c = 10, 20, 30
print("a: {}, b: {}, c: {}".format(a, b, c))
# ---------------------------------------- #
****# ํจ์์ ๋ฆฌํด์ ํํ์ ์ฌ์ฉํ๋ฉด ์ฌ๋ฌ ๊ฐ์ ๊ฐ์ ๋ฆฌํดํ๊ณ ํ ๋นํ ์ ์๋ค.
def test():
return (10, 20)
a, b = test()
print('a: {}, b: {}'.format(a, b))****
# ---------------------------------------- #
# <ํํ์ ํ์ฉ>
for i, value in enumerate(['a', 'b', 'c', 'd', 'e', 'f']):
print('{}๋ฒ์งธ ์์๋ {}์
๋๋ค.'.format(i, value))
# ์ถ๋ ฅ ๊ฒฐ๊ณผ:
# 0๋ฒ์งธ ์์๋ a์
๋๋ค.
# 1๋ฒ์งธ ์์๋ b์
๋๋ค.
# 2๋ฒ์งธ ์์๋ c์
๋๋ค.
# 3๋ฒ์งธ ์์๋ d์
๋๋ค.
# 4๋ฒ์งธ ์์๋ e์
๋๋ค.
# 5๋ฒ์งธ ์์๋ f์
๋๋ค.
# ---------------------------------------- #
# ๋ชซ๊ณผ ๋๋จธ์ง๋ฅผ ๊ตฌํ๋ divmod() ํจ์: ํํ์ ๋ฆฌํด
a, b = 97, 40
print(divmod(a, b))
x, y = divmod(a, b)
print('x: {}, y: {}'.format(x, y))
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# (2, 17)
# x: 2, y: 17
: ํจ์์ ๋งค๊ฐ๋ณ์์ ์ฌ์ฉํ๋ ํจ์
# ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ํจ์๋ฅผ 10๋ฒ ํธ์ถ
def call_3_times(func):
for i in range(3):
func()
def printHello():
print('Hello')
# ์กฐํฉํ๊ธฐ
call_3_times(printHello)
# ์ถ๋ ฅ ๊ฒฐ๊ณผ:
# Hello
# Hello
# Hello
# map(): ๋ฆฌ์คํธ์ ์์๋ฅผ ํจ์์ ๋ฃ๊ณ ๋ฆฌํด๋ ๊ฐ์ผ๋ก ์๋ก์ด ๋ฆฌ์คํธ๋ฅผ ๊ตฌ์ฑ
# filter(): ๋ฆฌ์คํธ์ ์์๋ฅผ ํจ์์ ๋ฃ๊ณ ๋ฆฌํด๋ ๊ฐ์ด True์ธ ๊ฒ์ผ๋ก, ์๋ก์ด ๋ฆฌ์คํธ๋ฅผ ๊ตฌ์ฑ
# ํ์: map(ํจ์_๋ช
, ๋ฆฌ์คํธ_๋ช
) | filter(ํจ์_๋ช
, ๋ฆฌ์คํธ_๋ช
)
def power(item):
return item * item
def under3(item):
return item < 3
# ๋์
power = lambda x: x * x
under3 = lambda x: x < 3
# ๋๋ค ์ฌ์ฉ๋ฒ >> lambda ๋งค๊ฐ๋ณ์_๋ช
: ๋ฆฌํด๊ฐ
listInputA = [1,2,3,4,5]
outputA = map(power, listInputA)
print('map(power, listInputA):', outputA)
print('map(power, listInputA):', list(outputA))
outputB = filter(under3, listInputA)
print('filter(under3, listInputA):', outputB)
print('filter(under3, listInputA):', list(outputB))
# ์ถ๋ ฅ ๊ฒฐ๊ณผ:
# map(power, listInputA): <map object at 0x000001D4560B7C70>
# map(power, listInputA): [1, 4, 9, 16, 25]
# filter(under3, listInputA): <filter object at 0x000001D456C4B7C0>
# filter(under3, listInputA): [1, 2]
# ---------------------------------------- #
# ์ธ๋ผ์ธ ๋๋ค
# ํจ์๋ฅผ ์ ์ธํ์ง๋ ์๊ณ ๋งค๊ฐ๋ณ์๋ก ๋ฐ๋ก ์
๋ ฅ
outputA = map(lambda x: x * x, listInputA)
outputB = filter(lambda x: x < 3, listInputA)
# <์ฐ๊ธฐ>
file = open('basic.txt', 'w')
file.write('Hi there!')
file.close()
# ๋๋
with open('basic.txt', 'w') as file:
file.write('Hi there!')
# ---------------------------------------- #
# <์ฝ๊ธฐ>
file = open('basic.txt', 'r')
contents = file.read()
print(contents)
# ๋๋
with open('basic.txt', 'r') as file:
contents = file.read()
print(contents)
# ---------------------------------------- #
import random as rand
hanguls = list('๊ฐ๋๋ค๋ผ๋ง๋ฐ์ฌ์์์ฐจ์นดํํํ')
with open('info.txt', 'w') as file:
for i in range(1000):
name = rand.choice(hanguls) + rand.choice(hanguls)
weight = rand.randrange(40, 100)
height = rand.randrange(140, 200)
file.write('{}, {}, {}\n'.format(name, weight, height))
with open('info.txt', 'r') as file:
for line in file:
(name, weight, height) = line.strip().split(',')
# ๋ฐ์ดํฐ๊ฐ ๋ฌธ์ ๊ฐ ์๋์ง ํ์ธํฉ๋๋ค: ๋ฌธ์ ๊ฐ ์์ผ๋ฉด ์ง๋๊ฐ
if (not name) or (not weight) or (not height):
continue
bmi= int(weight) / (int(weight) / 100) ** 2
result = ''
if 25 <= bmi:
result = '๊ณผ์ฒด์ค'
elif 18.5 <= bmi:
result = '์ ์ ์ฒด์ค'
else:
result = '์ ์ฒด์ค'
print('\n'.join([
'์ด๋ฆ: {}',
'๋ชธ๋ฌด๊ฒ: {}',
'ํค: {}',
'BMI: {:.2f}',
'๊ฒฐ๊ณผ: {}'
]).format(name, weight, height, bmi, result))
# N์ค๋ง ์ถ๋ ฅํ๋ ค๋ฉด: count๋ฅผ ํ๋์ฉ ๋๋ ค๊ฐ์ N๋งํผ ์ถ๋ ฅ๋๋ฉด break ๋๋๋ก ์์ ํ๋ฉด ๋จ!
: ํจ์์ ์ฝ๋๋ฅผ ์กฐ๊ธ์ฉ ์คํํ ๋ ์ฌ์ฉ. ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ์ ์ํด์. ์ ๋๋ ์ดํฐ์ ์ดํฐ๋ ์ดํฐ๋ ์์ ํ ๊ฐ์ง๋ ์์ง๋ง ๊ธฐ๋ณธ์ ์ธ ๋จ๊ณ์์๋ ๊ฑฐ์ ๋น์ทํ๋ค๊ณ ๋ด๋ ๋ฌด๋ฐฉํจ.
def test():
print('A ์ง์ ํต๊ณผ')
yield 1
print('B ์ง์ ํต๊ณผ')
yield 2
print('C ์ง์ ํต๊ณผ')
output = test()
print('D ์ง์ ํต๊ณผ')
a = next(output)
print(a)
print('E ์ง์ ํต๊ณผ')
b = next(output)
print(b)
print('F ์ง์ ํต๊ณผ')
c = next(output)
print(c)
next(output)
# ์คํ ๊ฒฐ๊ณผ:
# D ์ง์ ํต๊ณผ
# A ์ง์ ํต๊ณผ
# 1
# E ์ง์ ํต๊ณผ
# B ์ง์ ํต๊ณผ
# 2
# F ์ง์ ํต๊ณผ
# C ์ง์ ํต๊ณผ
# ---------------------------------------------------------------------------
# StopIteration Traceback (most recent call last)
# ~\AppData\Local\Temp\ipykernel_21056\1173299310.py in <module>
# 17
# 18 print('F ์ง์ ํต๊ณผ')
# ---> 19 c = next(output)
# 20 print(c)
# 21
#
# StopIteration:
global a
a = 10
def func():
global a
print(a) # ํจ์ ๋ด๋ถ์์ ํจ์ ์ธ๋ถ์ ์๋ ๋ณ์๋ฅผ ์ถ๋ ฅ
a = 20 # ํจ์ ์ธ๋ถ์ ์๋ ๋ฒ์์ ๊ฐ์ ๊ต์ฒด
func()
print(a)
# ์คํ ๊ฒฐ๊ณผ:
# 10
# 20
# global ํค์๋๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฉด 'UnboundLocalError'๋ฅผ ๋ฐ์์ํด
- ์กฐ๊ฑด๋ฌธ ์ฌ์ฉ
userInputA = input('์ ์ ์
๋ ฅ')
if userInputA.isdigit(): # 0์ด์์ ์ ์๋ฅผ ์
๋ ฅ๋ฐ์์ ๋๋ง ์๋
numberInputA = int(userInputA)
print('์์ ๋ฐ์ง๋ฆ>>', numberInputA)
print('์์ ๋๋ >> {:.2f}'.format(2 * 3.14 * numberInputA))
print('์์ ๋์ด>> {:.2f}'.format(3.14 * numberInputA * numberInputA))
else:
print('์ ์๋ฅผ ์
๋ ฅํ์ง ์์์ต๋๋ค.')
# ---------------------------------------- #
- try except ๊ตฌ๋ฌธ
try:
numberInputA = int(input('์ ์ ์
๋ ฅ>>'))
print('์์ ๋ฐ์ง๋ฆ>>', numberInputA)
print('์์ ๋๋ >> {:.2f}'.format(2 * 3.14 * numberInputA))
print('์์ ๋์ด>> {:.2f}'.format(3.14 * numberInputA * numberInputA))
except:
print('๋ฌด์ธ๊ฐ ์๋ชป๋์์ต๋๋ค')
# ---------------------------------------- #
- try except else finally ๊ตฌ๋ฌธ
- ํ์:
try:
์์ธ๊ฐ ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ์๋ ์ฝ๋
except:
์์ธ๊ฐ ๋ฐ์ํ์ ๋ ์คํํ ์ฝ๋
else:
์์ธ๊ฐ ๋ฐ์ํ์ง ์์์ ๋ ์คํํ ์ฝ๋
finally:
๋ฌด์กฐ๊ฑด ์คํํ ์ฝ๋
try:
numberInputA = int(input('์ ์ ์
๋ ฅ>>'))
print('์์ ๋ฐ์ง๋ฆ>>', numberInputA)
print('์์ ๋๋ >> {:.2f}'.format(2 * 3.14 * numberInputA))
print('์์ ๋์ด>> {:.2f}'.format(3.14 * numberInputA * numberInputA))
except:
print('์ ์๋ฅผ ์
๋ ฅํ์ง ์์์ต๋๋ค!')
else:
print('์์ธ๊ฐ ๋ฐ์ํ์ง ์์์ต๋๋ค.')
finally:
print('์ผ๋จ ํ๋ก๊ทธ๋จ์ด ์ด๋ป๊ฒ๋ ๋๋ฌ์ต๋๋ค.')
# ๊ท์น:
# 1. try ๊ตฌ๋ฌธ์ ๋จ๋
์ผ๋ก ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ๋ฐ๋์ except ๊ตฌ๋ฌธ ๋๋ finally ๊ตฌ๋ฌธ๊ณผ ํจ๊ป ์ฌ์ฉ
# 2. else ๊ตฌ๋ฌธ์ ๋ฐ๋์ except ๊ตฌ๋ฌธ ๋ค์ ์ฌ์ฉ
# (์ฆ, try else ๊ตฌ๋ฌธ์ ๋ถ๊ฐ๋ฅ)
# finally ํค์๋๋ ๊ตณ์ด ์์ด๋ ๋์ง๋ง ์ฝ๋ ์ ๋ฆฌ๋ฅผ ์ํด ์ฌ์ฉํ๋ค
# try ๊ตฌ๋ฌธ ๋ด๋ถ์์ return ํค์๋๋ก ์ค๊ฐ์์ ํ์ถํด๋ finally ๊ตฌ๋ฌธ์ ๋ฐ๋์ ์คํ
def test():
print('1st line of test()')
try:
print('try')
return
print('next to try')
except:
print('except')
else:
print('else')
finally:
print('finally')
print('end line of test()')
test()
# ์คํ ๊ฒฐ๊ณผ:
# 1st line of test()
# try
# finally
#์์ธ ๊ฐ์ฒด
try:
numberInputA = int(input('์ ์ ์
๋ ฅ>>'))
print('์์ ๋ฐ์ง๋ฆ>>', numberInputA)
print('์์ ๋๋ >> {:.2f}'.format(2 * 3.14 * numberInputA))
print('์์ ๋์ด>> {:.2f}'.format(3.14 * numberInputA * numberInputA))
except Exception as exception:
print('type(exception):', type(exception))
print('exception:', exception)
# ์คํ ๊ฒฐ๊ณผ:
# ์ ์ ์
๋ ฅ>> yes
# type(exception): <class 'ValueError'>
# exception: invalid literal for int() with base 10: 'yes'
# ---------------------------------------- #
# ์์ธ ๊ตฌ๋ถํ๊ธฐ
listNumber = [52, 273, 32, 72, 100]
try:
numberInput = int(input('์ ์ ์
๋ ฅ>>'))
print('{}๋ฒ์งธ ์์: {}'.format(numberInput, listNumber[numberInput]))
except ValueError:
print('์ ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์!')
except IndexError:
print('๋ฆฌ์คํธ์ ์ธ๋ฑ์ค๋ฅผ ๋ฒ์ด๋จ.')
# ---------------------------------------- #
# ๋ชจ๋ ์์ธ ์ก๊ธฐ
listNumber = [52, 273, 32, 72, 100]
try:
numberInput = int(input('์ ์ ์
๋ ฅ>>'))
print('{}๋ฒ์งธ ์์: {}'.format(numberInput, listNumber[numberInput]))
์์ธ.๋ฐ์ํด์ฃผ์ธ์()
except ValueError as exception:
print('์ ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์!')
print(type(exception), excpetion)
except IndexError as exception:
print('๋ฆฌ์คํธ์ ์ธ๋ฑ์ค๋ฅผ ๋ฒ์ด๋จ.')
print(type(exception), excpetion)
except Exception as exception:
print('๋ฏธ๋ฆฌ ํ์
ํ์ง ๋ชปํ ์์ธ๊ฐ ๋ฐ์ํ์ต๋๋ค.')
print(type(exception), exception)