๐Ÿข ํŒŒ์ด์ฌ ๊ธฐ์ดˆ์˜ ๋ชจ๋“  ๊ฒƒ ๐Ÿข

niraaahยท2023๋…„ 10์›” 13์ผ
0

ํ˜ผ์žํ•˜๋Š” ์Šคํ„ฐ๋””

๋ชฉ๋ก ๋ณด๊ธฐ
25/25
post-thumbnail

[๋ฆฌ์ŠคํŠธ] : โ€˜์ธ๋ฑ์Šคโ€™๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐ’์„ ์ €์žฅ


# 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

[break / continue]

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]

global a
a = 10

def func():
    global a 
    print(a) # ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ํ•จ์ˆ˜ ์™ธ๋ถ€์— ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ถœ๋ ฅ
    a = 20 # ํ•จ์ˆ˜ ์™ธ๋ถ€์— ์žˆ๋Š” ๋ฒˆ์ˆ˜์˜ ๊ฐ’์„ ๊ต์ฒด
    
func()
print(a)

# ์‹คํ–‰ ๊ฒฐ๊ณผ:
# 10
# 20

# global ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด 'UnboundLocalError'๋ฅผ ๋ฐœ์ƒ์‹œํ‚ด

[์˜ˆ์™ธ ์ฒ˜๋ฆฌ]

  • ์˜ˆ์™ธ / ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜: ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘์— ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜
  • ๋ฐฉ๋ฒ•
    • ์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉ
    • try ๊ตฌ๋ฌธ ์‚ฌ์šฉ
- ์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉ

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)
profile
์ฝ”๋”ฉ์ฒœ์žฌ

0๊ฐœ์˜ ๋Œ“๊ธ€