Coding/Python 27

[python] list Comprehension, ์‚ผํ•ญ ์—ฐ์‚ฐ์ž

List Comprehension List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด์ž. Without list comprehension you will have to write a for statement with a conditional test inside: Example fruits = ["apple", "banana", "cherry", "kiwi", "mango"] newlist = [] for x in fruits: if "a" in x: newlist.append(x) print(newlist) With list com..

Coding/Python 2021.12.29

[python] ๋ชจ๋“ˆ

#๋ชจ๋“ˆ #๋ชจ๋“ˆ์€ ํ•„์š”ํ•œ ๊ฒƒ๋“ค๋ผ๋ฆฌ ๋ถ€ํ’ˆ์ฒ˜๋Ÿผ ๋งŒ๋“ค์–ด์ง„ ํŒŒ์ผ # 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))) ์—ฌ๊ธฐ..

Coding/Python 2021.11.10

[python] ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ€ด์ฆˆ

#์ž๋™ ์ฃผ๋ฌธ ์‹œ์Šคํ…œ: ์‹œ์Šคํ…œ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜๊ณ  ์ ์ ˆํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ตฌ๋ฌธ์„ ๋„ฃ์œผ์‹œ์˜ค # ์กฐ๊ฑด1: 1๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ์ž…๋ ฅ๊ฐ’์ด ๋“ค์–ด์˜ฌ๋•Œ๋Š” ValueError๋กœ ์ฒ˜๋ฆฌ # ์ถœ๋ ฅ ๋ฉ”์‹œ์ง€ : "์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. " # ์กฐ๊ฑด2: ๋Œ€๊ธฐ ์†๋‹˜์ด ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์น˜ํ‚จ๋Ÿ‰์€ 10๋งˆ๋ฆฌ๋กœ ํ•œ์ • # ์น˜ํ‚จ ์†Œ์ง„์‹œ ์‚ฌ์šฉ์ž ์ •์˜ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ # ์ถœ๋ ฅ ๋ฉ”์‹œ์ง€: "์žฌ๊ณ ๊ฐ€ ์†Œ์ง„๋˜์–ด ๋” ์ด์ƒ ์ฃผ๋ฌธ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค." class TooManyChickenError(Exception): pass chicken = 10 waiting = 1 #ํ™€ ์•ˆ์—๋Š” ๋งŒ์„ ๋Œ€๊ธฐ๋ฒˆํ˜ธ 1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘ while(True): try: print("๋‚จ์€ ์น˜ํ‚จ : {0}".format(chicken)) order = int(input("์น˜..

Coding/Python 2021.11.04

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

try: print("๋‚˜๋ˆ„๊ธฐ ์ „์šฉ ๊ณ„์‚ฐ๊ธฐ์ž…๋‹ˆ๋‹ค/") nums = [] nums.append(int(input("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” :"))) nums.append(int(input("๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” :"))) nums.append(int(nums[0]/nums[1])) print("{0} / {1} = {2}".format(nums[0], nums[1], nums[2])) except ValueError: print("์—๋Ÿฌ! ์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.") except ZeroDivisionError as err: print(err) except Exception as err: print("์•Œ ์ˆ˜ ์—†๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ") print(err) #ํ•œ ์ž๋ฆฌ ์ˆซ์ž ์ „์šฉ ๋‚˜๋ˆ—์…ˆ ๊ณ„์‚ฐ๊ธฐ ..

Coding/Python 2021.11.04

[python] ํด๋ž˜์Šค ํ€ด์ฆˆ

# ์ด 3๋Œ€์˜ ๋งค๋ฌผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. # ๊ฐ•๋‚จ ์•„ํŒŒํŠธ ๋งค๋งค 10์–ต 2010๋…„ # ๋งˆํฌ ์˜คํ”ผ์Šคํ…” ์ „์„ธ 5์–ต 2007๋…„ # ์†กํŒŒ ๋นŒ๋ผ ์›”์„ธ 500/50 2000๋…„ # class House: #๋งค๋ฌผ ์ดˆ๊ธฐํ™” def __init__(self, location, housetype, deal_type, price, completion_year): self.location = location self.housetype = housetype self.deal_type = deal_type self.price = price self.completion_year = completion_year def show_detail(self): print(self.location, self.housetype, self.deal_type\ ..

Coding/Python 2021.11.03

[python] ํŒŒ์ด์ฌ์œผ๋กœ ๋งŒ๋“  ์Šคํƒ€ํฌ๋ž˜ํ”„ํŠธ

๊ทธ๋ ‡๋‹ค,, ๊ณ ๋Œ€๋กœ ๋”ฐ๋ผ์ณค๋Š”๋ฐ ์—๋Ÿฌ๋‚˜๋Š” ๋ฏธ์Šคํ…Œ๋ฆฌ,, ์™œ? ์–ธ์  ๊ฐ€ ์ดํ•ดํ•ด์„œ ๋š๋”ฑ๋š๋”ฑ ๊ณ ์น  ์ˆ˜ ์žˆ๋Š” ๋‚  ์˜ค๊ธธ ๋ฐ”๋ผ๋ฉฐ,,,, ์Šคํƒ€๋„ ๋ชจ๋ฅด๊ฒ ๊ณ  ํŒŒ์ด์ฌ๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค~~~~~~~~~ from random import * #์ผ๋ฐ˜์œ  class Unit: #๋ถ€๋ชจ ํด๋ž˜์Šค def __init__(self, name, hp, speed): self.name = name self.hp = hp self.speed = speed print("{0} ์œ ๋‹›์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.".format(name)) def move(self, location): print("[์ง€์ƒ ์œ ๋‹› ์ด๋™]") print("{0} : {1} ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. [์†๋„ {2}]"\ .format(self.name, location, self.speed)) def d..

Coding/Python 2021.11.03

[python] ํด๋ž˜์Šค(pass, super)

#pass #๊ฑด๋ฌผ class BuildingUnit(Unit): def __init__(self, name, hp, location): pass #์ผ๋‹จ ๋„˜์–ด๊ฐ„๋‹ค. #์„œํ”Œ๋ผ์ด ๋””ํฟ : ๊ฑด๋ฌผ 1๊ฐœ ์žˆ์œผ๋ฉด 8 ์œ ๋‹› ์ƒ์„ฑ supply_depot = BuildingUnit("์„œํ”Œ๋ผ์ด ๋””ํฟ", 500, "7์‹œ") #Another example def game_start(): print("[์•Œ๋ฆผ] ์ƒˆ๋กœ์šด ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.") def game_over(): pass ============ Super class Flyable: def __int__(self): print("FlyableFF ์ƒ์„ฑ์ž") class Unit: def __init__(self): print("Unit ์ƒ์„ฑ์ž") class FlyableUni..

Coding/Python 2021.11.03

[python] ํด๋ž˜์Šค(๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ)

๊ณต๋ถ€ํ•  ๊ฒƒ์€ ๋งŽ๊ณ , ์ดํ•ด๋Š” ์ž˜ ์•ˆ๋˜๊ณ , ์ •๋ฆฌํ•˜๊ธฐ์—” ์–ด๋ ต๊ธฐ์—, ํ•„๊ธฐ๋ฅผ ๊ทธ๋ƒฅ ๋ณต๋ถ™ํ•ฉ๋‹ˆ๋‹ค. #์ผ๋ฐ˜์œ ๋‹› class Unit: #๋ถ€๋ชจ ํด๋ž˜์Šค def __init__(self, name, hp, speed): self.name = name self.hp = hp self.speed = speed def move(self, location): print("[์ง€์ƒ ์œ ๋‹› ์ด๋™]") print("{0} : {1} ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. [์†๋„ {2}]"\ .format(self.name, location, self.speed)) class AttackUnit(Unit): #์ž์‹ ํด๋ž˜์Šค def __init__(self, name, hp, speed, damage): Unit.__init__(self, name, hp, spe..

Coding/Python 2021.11.03

[python] ํด๋ž˜์Šค(๋‹ค์ค‘์ƒ์†)

#์ผ๋ฐ˜์œ  class Unit: #๋ถ€๋ชจ ํด๋ž˜์Šค def __init__(self, name, hp): self.name = name self.hp = hp class AttackUnit(Unit): #์ž์‹ ํด๋ž˜์Šค def __init__(self, name, hp, damage): Unit.__init__(self, name, hp) self.damage = damage def attack(self, location): print("{0} : {1} ๋ฐฉํ–ฅ์œผ๋กœ ์ ๊ตฐ์„ ๊ณต๊ฒฉํ•ฉ๋‹ˆ๋‹ค. [๊ณต๊ฒฉ๋ ฅ {2}]"\ .format(self.name, location, self.damage)) def damaged(self, damage): print("{0} : {1} ๋ฐ๋ฏธ์ง€๋ฅผ ์ž…์—ˆ์Šต๋‹ˆ๋‹ค.".format(self.name, d..

Coding/Python 2021.11.03

[python] ํด๋ž˜์Šค(ํด๋ž˜์Šค, __init__, ๋ฉค๋ฒ„๋ณ€์ˆ˜)

ํด๋ž˜์Šค, __inti__ class Unit: def __init__(self, name, hp, damage): self.name = name self.hp = hp self.damage = damage print("{0} ์œ ๋‹›์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.".format(self.name)) print("์ฒด๋ ฅ {0}, ๊ณต๊ฒฉ๋ ฅ {1}".format(self.hp,self.damage)) marine1 = Unit("๋งˆ๋ฆฐ", 30, 6) marine2 = Unit("๋งˆ๋ฆฐ", 30, 7) ๋ฉค๋ฒ„๋ณ€์ˆ˜ #๋ฉค๋ฒ„๋ณ€์ˆ˜ #์–ด๋–ค ํด๋ž˜์Šค ๋‚ด์—์„œ ์ •์˜๋œ ๋ณ€์ˆ˜ #๋ ˆ์ด์Šค : ๊ณต์ค‘ ์œ ๋‹›, ๋น„ํ–‰๊ธฐ, ํด๋กœํ‚น (์ƒ๋Œ€๋ฐฉ์—๊ฒŒ ๋ณด์ด์ง€ ์•Š์Œ) wraith1 = Unit("๋ ˆ์ด์Šค", 80, 5) print("์œ ๋‹› ์ด๋ฆ„ : {0}, ๊ณต๊ฒฉ๋ ฅ : {1}".fo..

Coding/Python 2021.11.01