Coding/Python

[python] λͺ¨λ“ˆ

κΉ€μœ λ‹ˆμ½˜ 2021. 11. 10. 15:08

#λͺ¨λ“ˆ
#λͺ¨λ“ˆμ€ ν•„μš”ν•œ 것듀끼리 λΆ€ν’ˆμ²˜λŸΌ λ§Œλ“€μ–΄μ§„ 파일

# ex. μžλ™μ°¨ 이용 ν›„ 타이어 νŽ‘ν¬λ‚˜λ©΄ νƒ€μ΄μ–΄λ§Œ κ΅μ²΄ν•˜λ©΄ 됨

#λͺ¨λ“ˆμ˜ ν™•μž₯μžλŠ” .py

#ν˜„κΈˆλ§Œ λ°›λŠ” μ˜ν™”κ΄€, μž”λˆμ„ μ•ˆκ±°μŠ¬λŸ¬μ€Œ.

# 일반 가격
def price(people):
print("{0}λͺ… 가격은 {1}원 μž…λ‹ˆλ‹€.".format(people, people * 10000))

#쑰쑰할인 가격
def price_morning(people):
print(("{0}λͺ…μ˜ 쑰쑰할인 가격은 {1}원 μž…λ‹ˆλ‹€.".format(people, people * 6000)))


#ꡰ인 할인 가격

def price_soldier(people):
print(("{0}λͺ…μ˜ ꡰ인할인 가격은 {1}원 μž…λ‹ˆλ‹€.".format(people, people * 4000)))

 

μ—¬κΈ°κΉŒμ§€κ°€ theater_module

-----------------------------

μ΄ν•˜ λͺ¨λ“ˆμ„ μ‚¬μš© 

 

import theater_module
theater_module.price(3) #μ…‹μ΄μ„œ μ˜ν™” 가격
theater_module.price_morning(4)
theater_module.price_soldier(5)

 

 

 


import theater_module as mv #mv둜만 theater_module 호좜 κ°€λŠ₯ / λͺ¨λ“ˆλͺ… κΈΈλ•Œ μ€„μ—¬μ„œ
mv.price(3)
mv.price_morning(4)

 

 

from theater_module import *
#from random import *
price(3)
price_morning(4)

 

 


from theater_module import price, price_morning #μ–΄λ–€ ν•¨μˆ˜λ§Œ 갖닀쓸지 μ •μ˜ν•˜κΈ°
price(5)

from theater_module import price_soldier as price #별λͺ…을 price라고 ν• κ±°μ•Ό.
price(5) #ꡰ인 가격을 λ‚˜νƒ€λƒ„

 

 


 


# νŒ¨ν‚€μ§€ : λͺ¨λ“ˆμ„ λͺ¨μ•„λ‘” 집합, ν•˜λ‚˜μ˜ 디렉토리에 μ—¬λŸ¬ λͺ¨λ“ˆλ“€μ„ κ°–λ‹€λ‘” 것
# μ‹ κ·œ 여행사 ν”„λ‘œμ νŠΈ(νƒœκ΅­, λ² νŠΈλ‚¨ νŒ¨ν‚€μ§€ μƒν’ˆ 제곡)

# ν•œ 디렉토리에 λ§Œλ“€κ³ μžν•˜λŠ” λͺ¨λ“ˆκ³Ό __init__ λͺ¨λ“ˆμ„ λ§Œλ“ λ‹€

 

thailand.py

class ThailandPackage:
def detail(self):
print("[νƒœκ΅­ νŒ¨ν‚€μ§€ 3λ°• 5일] 방콕, νŒŒνƒ€μ•Ό μ—¬ν–‰(μ•Όμ‹œμž₯ νˆ¬μ–΄) 50만")

 

 

vietnam.py

class VietnamPackage:
def detail(self):
print("[λ² νŠΈλ‚¨ νŒ¨ν‚€μ§€ 3λ°• 5일] λ‹€λ‚­ νš¨λ„ μ—¬ν–‰ 60만")

 

 

 

practice

import travel.thailand #μž„ν¬νŠΈλŠ” λͺ¨λ“ˆμ΄λ‚˜ νŒ¨ν‚€μ§€λ§Œ κ°€λŠ₯, ν΄λž˜μŠ€λŠ” μ•ˆλ¨
trip_to = travel.thailand.ThailandPackage()
trip_to.detail()

from travel import vietnam
trip_to = vietnam.VietnamPackage()
trip_to.detail()

 

 

__init__

 

__all__ = ["vietnam", "thailand"]  #μ‚¬μš© κ°€λŠ₯ν•œ λ²”μœ„λ₯Ό μ„€μ •ν•΄μ£ΌλŠ” 것 

 

 

 

 

λͺ¨λ“ˆ 직접 μ‹€ν–‰

if __name__ == "__main__":
print("Thailand λͺ¨λ“ˆμ„ 직접 μ‹€ν–‰")
print("이 λ¬Έμž₯은 λͺ¨λ“ˆμ„ 직접 μ‹€ν–‰ν•  λ•Œλ§Œ μ‹€ν–‰λΌμš”")
trip_to = ThailandPackage()
trip_to.detail()
else:
print("Thailand μ™ΈλΆ€μ—μ„œ λͺ¨λ“ˆ 호좜")

 

 

νŒ¨ν‚€μ§€, λͺ¨λ“ˆ μœ„μΉ˜

import inspect
import random
print(inspect.getfile(random))
print(inspect.getfile(thailand))

 

 

λ‚΄μž₯ν•¨μˆ˜

# dir : μ–΄λ–€ 객체λ₯Ό λ„˜κ²¨μ€¬μ„ λ•Œ κ·Έ 객체가 μ–΄λ–€ λ³€μˆ˜μ™€ ν•¨μˆ˜λ₯Ό 가지고 μžˆλŠ”μ§€ ν‘œμ‹œ
print(dir())


import random
print(dir())

# print(dir(random))
lst = [1, 2, 3]
print(dir(lst))


name = "Jim"
print(dir(name))

list of python builtins 검색

 

 

 

μ™Έμž₯ν•¨μˆ˜

# νŒŒμ΄μ¬μ—μ„œ 수 λ§Žμ€ μ™Έμž₯ν•¨μˆ˜ 제곡 쀑
# list of python modules κ²€μƒ‰ν•΄μ„œ λͺ¨λ“ˆ 검색 κ°€λŠ₯

# glob : 경둜 λ‚΄μ˜ 폴더 / 파일 λͺ©λ‘ 쑰회 (μœˆλ„μš° dir)
import glob
print(glob.glob("*.py")) #ν™•μž₯μžκ°€ py인 λͺ¨λ“  파일

# os : μš΄μ˜μ²΄μ œμ—μ„œ μ œκ³΅ν•˜λŠ” κΈ°λ³Έ κΈ°λŠ₯
import os
print(os.getcwd()) #ν˜„μž¬ 디렉토리

folder = "sample_dir"

if os.path.exists(folder):
print("이미 μ‘΄μž¬ν•˜λŠ” ν΄λ”μž…λ‹ˆλ‹€")
os.rmdir(folder)
print(folder, "폴더λ₯Ό μ‚­μ œν•˜μ˜€μŠ΅λ‹ˆλ‹€.")
else:
os.makedirs(folder) #폴더 생성
print(folder, "폴더λ₯Ό μƒμ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.")


# time : μ‹œκ°„ κ΄€λ ¨ ν•¨μˆ˜
import time
print(time.localtime())
print(time.strftime("%Y-%m-%d %H:%M:%S"))

import datetime
print("였늘 λ‚ μ§œλŠ” ", datetime.date.today())


# timedelta : 두 λ‚ μ§œ μ‚¬μ΄μ˜ 간격
today = datetime.date.today() #였늘 λ‚ μ§œ μ €μž₯
td = datetime.timedelta(days=100) #100일 μ €μž₯
print("μš°λ¦¬κ°€ λ§Œλ‚œμ§€ 100일은", today + td)

 

 

# Quiz) ν”„λ‘œμ νŠΈ 내에 λ‚˜λ§Œμ˜ μ‹œκ·Έλ‹ˆμ²˜λ₯Ό λ‚¨κΈ°λŠ” λͺ¨λ“ˆμ„ λ§Œλ“œμ‹œμ˜€
#
# 쑰건 : λͺ¨λ“ˆ 파일λͺ…은 byme.py둜 μž‘μ„±
#
# (λͺ¨λ“ˆ μ‚¬μš© 예제)
import byme
byme.sign()

# (좜λ ₯ 예제)
# 이 ν”„λ‘œκ·Έλž¨μ€ iris에 μ˜ν•΄ λ§Œλ“€μ–΄μ‘ŒμŠ΅λ‹ˆλ‹€.
# 유튜브 : www.youtube.com
# 이메일 : saltedcaramel.macarons@gmail.com

 

 

μ‹€ν–‰νŒŒμΌ.py

import byme
byme.sign()

 

 

 

byme.py

def sign():
print("이 ν”„λ‘œκ·Έλž¨μ€ iris에 μ˜ν•΄ λ§Œλ“€μ–΄μ‘ŒμŠ΅λ‹ˆλ‹€.")
print("유튜브 : www.youtube.com")
print("이메일 : saltedcaramel.macarons@gmail.com")

'Coding > Python' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[Python] *args, **kwargs  (0) 2021.12.30
[python] list Comprehension, μ‚Όν•­ μ—°μ‚°μž  (0) 2021.12.29
[python] μ˜ˆμ™Έμ²˜λ¦¬ ν€΄μ¦ˆ  (0) 2021.11.04
[python] μ˜ˆμ™Έ 처리  (0) 2021.11.04
[python] 클래슀 ν€΄μ¦ˆ  (0) 2021.11.03