π Python
9. λͺ¨λ
sa1t
2022. 6. 9. 10:54
λ°μν
# λͺ¨λ
# λͺ¨λ : ν¨μμ μ§ν© vs ν¨ν€μ§ : λ€μ΄λ°μμ μ°λκ±°
# νμ€ λͺ¨λ : νμ΄μ¬μμ μ 곡νλ λͺ¨λ
# μ¬μ©μ μ μ λͺ¨λ : μ§μ λ§λ€μ΄μ μ¬μ©
# μλ νν° (3rd party) : νμ΄μ¬, μ¬μ©μκ° μλ μΈλΆ νμ¬λ λ¨μ²΄μμ μ 곡
# import sys
# print(sys.builtin_module_names) # νμ€ λͺ¨λ νμΈ λͺ
λ Ήμ΄
import module
module.func1()
module.func2()
# λ¨μ νλ¦°νΈ κ΅¬λ¬Έ
sum = module.plus(50,30)
print(sum)
sum2 = module.para_func(10,20)
print(sum2)
# ν¨μ λͺ
μΌλ‘λ§ νΈμΆ
# from λͺ¨λλͺ
import ν¨μλͺ
1, ν¨μλͺ
2 ...
from module import func1, func2, para_func, plus
func1()
func2()
# νμΌ(λͺ¨λ)μ μλ λͺ¨λ ν¨μ λ€
# λ‘λλ μ¦κ°, μ¬μ©νκΈ° νΈν΄μ§
from module import *
func1()
func2()
# Quiz) model νμΌ μλ‘ λ§λ€κΈ°
# μ¬μΉμ°μ°, λ‘λ λ²νΈ, λ€μ΄μλͺ¬λ, κ΅¬κ΅¬λ¨ μΆλ ₯
from model import *
calc(11,23,'*')
lottonum()
dia(5)
dan2(66)
# μ¬μΉ μ°μ°
def calc(v1,v2,op) :
result= 0 # νΉμ λͺ°λΌμ
if op=="+":
result=v1+v2
print("%d %s %d = %d" %(v1,op,v2,result))
elif op=="-":
result=v1-v2
print("%d %s %d = %d" %(v1,op,v2,result))
elif op=="*":
result=v1*v2
print("%d %s %d = %d" %(v1,op,v2,result))
elif op=="/":
result=v1/v2
print("%d %s %d = %d" %(v1,op,v2,result))
else:
print("μλͺ» μ
λ ₯νμ
¨μ΅λλ€.")
return result
# λ‘λ λ²νΈ
def lottonum():
import random
def getNumber():
return random.randrange(1, 46)
lotto = []
num = 0
print("##### λ‘λ λ²νΈ μΆμ²¨ ##### \n")
while True:
num = getNumber()
if lotto.count(num) == 0:
lotto.append(num)
if len(lotto) >= 7:
break
print("μΆμ²¨ λ‘λ λ²νΈ : ", end='')
ll6 = lotto[0:6]
ll6.sort()
print(ll6)
print("μΆμ²¨ λ‘λ 보λμ€ λ²νΈ : ", end='')
print(lotto[6])
# λ€μ΄μλͺ¬λ
def dia(di):
a = str("*")
i = 0
if di % 2 == 0:
print(" μ«μλ₯Ό λ€μ μ
λ ₯ν΄ μ£ΌμΈμ ")
else:
for i in range(0, di + 1, 2):
print(("*" * (i + 1)).center(di))
for i in range(di - 2, 0, -2):
print(("*" * (i)).center(di))
# κ΅¬κ΅¬λ¨ μΆλ ₯ ν¨μ
def dan2(dan):
i = 0
for i in range(1, 10): # μ¦κ° κ° μλ΅νλ©΄ 1μ΄ λν΄νΈ
print("%d * %d = %d" % (dan, i, dan * i))
print("λ")
λ°μν