์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/39

 

[python ๊ธฐ์ดˆ] 7. ํ•จ์ˆ˜ ๋ฐ ๋žŒ๋‹ค (lambda)

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/38 https://silvercoding.tistory.com/37 https://silvercoding.tistory.com/36 https://silvercoding.tistory.com/35..

silvercoding.tistory.com

 

 

- ๋ณธ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃฐ ๋‚ด์šฉ ์š”์•ฝ 

* ํด๋ž˜์Šค ๋ณ€์ˆ˜ & ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์ฐจ์ด (์ค‘์š”)

* ํด๋ž˜์Šค ๋ณ€์ˆ˜ : ์ง์ ‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅ, ๊ฐ์ฒด๋ณด๋‹ค ๋จผ์ € ์ƒ์„ฑ 

* ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ : ๊ฐ์ฒด ๋งˆ๋‹ค ๋ณ„๋„๋กœ ์กด์žฌ, ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ํ›„ ์‚ฌ์šฉ 

* ๋„ค์ž„์ŠคํŽ˜์ด์Šค : ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•  ๋•Œ ์ €์žฅ๋œ ๊ณต๊ฐ„ ( (ex)์ด๋ฆ„, ์ฃผ์†Œ, .... -> ๋…๋ฆฝ์ ์ธ ๊ณต๊ฐ„ )   

* ํด๋ž˜์Šค ์ƒ์†๊ณผ ๋‹ค์ค‘ ์ƒ์† 

 

 

* ํด๋ž˜์Šค ์„ ์–ธ

class ํด๋ž˜์Šค๋ช…:

    ํ•จ์ˆ˜

    ํ•จ์ˆ˜

    ํ•จ์ˆ˜ 

 

 

 

 

 [ํด๋ž˜์Šค ๊ธฐ๋ณธ ์‚ฌ์šฉ] 

- ์˜ˆ์ œ 1 

class UserInfo:
    # ์†์„ฑ, ๋ฉ”์†Œ๋“œ 
    def __init__(self, name):
        self.name = name
        print("์ดˆ๊ธฐํ™”", name)

    def user_info_p(self):
        print('Name : ', self.name)

์ดˆ๊ธฐ์— name์„ ๋ฐ›๋Š” ํด๋ž˜์Šค UserInfo๋ฅผ ์„ ์–ธํ•œ๋‹ค. user_info_p๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด self.name์ด ์ถœ๋ ฅ๋œ๋‹ค. 

user1 = UserInfo('Silver')
print(user1.name)
user1.user_info_p()

 ์ดˆ๊ธฐํ™” Silver 
 Silver 
 Name :  Silver 

user2 = UserInfo('Park')
print(user1.name)
user2.user_info_p()

 ์ดˆ๊ธฐํ™” Park 
 Park 
 Name :  Park 

print(id(user1))
print(id(user2))

 2535817860096 
 2535817860544 

๊ฐ™์€ ํด๋ž˜์Šค์—์„œ ๋‚˜์™”์ง€๋งŒ user1๊ณผ user2 ๋Š” ์„œ๋กœ ๋‹ค๋ฅด๋‹ค. id๊ฐ’์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์œผ๋กœ ์ฆ๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

* ๋„ค์ž„์ŠคํŽ˜์ด์Šค ํ™•์ธ 

print(user1.__dict__)
print(user2.__dict__)

 {'name': 'Silver'} 
 {'name': 'Park'} 

 __dict__ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ €์žฅ๋œ ๋ณ€์ˆ˜์™€ ๊ฐ’์ด key, valueํ˜•์‹์ธ dictionary๋กœ ์ถœ๋ ฅ๋œ๋‹ค. 

 

 

 

 

 

 

 

- ์˜ˆ์ œ 2 

* self ์˜ ์ดํ•ด (ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ vs ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ) 

<์ฐธ๊ณ  ํฌ์ŠคํŒ…> 

https://silvercoding.tistory.com/20?category=957523 

 

[python ์‹ฌํ™”] 3. ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ, ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ, ์Šคํ…Œ์ดํ‹ฑ ๋ฉ”์†Œ๋“œ

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/19 https://silvercoding.tistory.com/18 [python ์‹ฌํ™”] 1. ๊ฐ์ฒด์ง€ํ–ฅ(OOP), ํด๋ž˜์Šค ๊ธฐ์ดˆ ์‚ฌ์šฉ ์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ..

silvercoding.tistory.com

class SelfTest():
    def function1():
        print('function1 called ! ')

    def function2(self):
        print(id(self))
        print('function2 called ! ')

๋ฉ”์„œ๋“œ๋ฅผ 2๊ฐœ ์ƒ์„ฑํ•ด ์ค€๋‹ค. functionn1์€ self๋ฅผ ์ธ์ž๋กœ ๋ฐ›์ง€ ์•Š๊ณ , function2๋Š” self๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค. ์ด ๊ฒฝ์šฐ์— ๊ฐ ๋‘๊ฐœ์˜ ๋ฉ”์„œ๋“œ์— ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•˜๋Š”์ง€ ์‹คํ—˜ํ•ด ๋ณธ๋‹ค. 

self_test = SelfTest()
self_test.function1()

 TypeError: function1() takes 0 positional arguments but 1 was given 

function1 ํ•จ์ˆ˜์— ์ธ์Šคํ„ด์Šค๋กœ ์ ‘๊ทผํ–ˆ๋”๋‹ˆ TypeError๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค. self๋ฅผ ์ธ์ž๋กœ ๋ฐ›์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ์ด๊ณ , ๋ˆ„๊ตฌ(์–ด๋–ค ์ธ์Šคํ„ด์Šค)์˜ function1 ํ•จ์ˆ˜์ธ์ง€ ๋ชจ๋ฅด๋Š” ๊ฒƒ์ด๋‹ค. 

SelfTest.function1() # ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ

 function1 called ! 

๋”ฐ๋ผ์„œ ์ด๋ ‡๊ฒŒ ํด๋ž˜์Šค ์ž์ฒด๋กœ ์ ‘๊ทผํ•ด ์ฃผ์–ด์•ผ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

self_test.function2() # ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ 
print(id(self_test))

 1805035034104 
 function2 called ! 
 1805035034104 

๋ฐ˜๋ฉด self๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•˜๋˜ function2 ๋ฉ”์†Œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•˜๋‹ค. self์˜ id๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ์ƒ์„ฑ ํ–ˆ์—ˆ๋Š”๋ฐ, self_test ์ธ์Šคํ„ด์Šค์™€ id๊ฐ’์ด ๊ฐ™์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ self๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์˜ ์ธ์ž์ธ self์—๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 

SelfTest.function2() # -> ์˜ˆ์™ธ

 TypeError: function2() missing 1 required positional argument: 'self' 

์ด์ฒ˜๋Ÿผ ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค ์ž์ฒด๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋˜‘๊ฐ™์ด TypeError๊ฐ€ ๋‚˜๊ฒŒ ๋œ๋‹ค. ์˜ค๋ฅ˜ ๋‚ด์šฉ์— ๋”ฐ๋ฅด๋ฉด ์ธ์Šคํ„ด์Šค๋ฅผ ์ธ์ž์— ๋„ฃ์–ด์ฃผ๋ฉด ์‹คํ–‰๋  ๊ฒƒ์ด๋‹ค. 

SelfTest.function2(self_test)  # ์ธ์ž์— ์ธ์Šคํ„ด์Šค ๋„ฃ์–ด์ฃผ๋ฉด ํด๋ž˜์Šค์ž์ฒด๋กœ ํ˜ธ์ถœ ๊ฐ€๋Šฅ

 2385469756864 
 function2 called ! 

 

 

 

 

 

 

 

 

- ์˜ˆ์ œ 3 

: ํด๋ž˜์Šค ๋ณ€์ˆ˜ vs ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ 

<์ฐธ๊ณ  ํฌ์ŠคํŒ…> 

https://silvercoding.tistory.com/19?category=957523 

 

[python ์‹ฌํ™”] 2. ํด๋ž˜์Šค ๊ธฐ์ดˆ, ํด๋ž˜์Šค ๋ณ€์ˆ˜ , ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/18 [python ์‹ฌํ™”] 1. ๊ฐ์ฒด์ง€ํ–ฅ(OOP), ํด๋ž˜์Šค ๊ธฐ์ดˆ ์‚ฌ์šฉ ์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  1. ๊ฐ์ฒด์ง€ํ–ฅ vs ์ ˆ์ฐจ์ง€ํ–ฅ ๊ฐ„๋‹จํ•œ..

silvercoding.tistory.com

class WareHouse: 
    # ํด๋ž˜์Šค ๋ณ€์ˆ˜ 
    stock_num = 0 
    def __init__(self, name): 
    	# ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜
        self.name = name 
        WareHouse.stock_num += 1 

    def __del__ (self):
        WareHouse.stock_num -= 1

ํด๋ž˜์Šค ๋ณ€์ˆ˜์™€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ๊ฐ๊ฐ ์ƒ์„ฑํ•ด ์ฃผ์—ˆ๋‹ค. 

user1 = WareHouse('Kim')
user2 = WareHouse("Park")
user3 = WareHouse("Lee")

์ธ์Šคํ„ด์Šค๋ฅผ 3๊ฐœ ๋งŒ๋“ค์–ด ์ค€๋‹ค. 

 

 

* ์ธ์Šคํ„ด์Šค ๋„ค์ž„์ŠคํŽ˜์ด์Šค, ํด๋ž˜์Šค ๋ณ€์ˆ˜ (๋ชจ๋“  ์ธ์Šคํ„ด์Šค์— ๊ณต์œ ) 

print(user1.__dict__)
print(user2.__dict__)
print(user3.__dict__)

 {'name': 'Kim'} 
 {'name': 'Park'} 
 {'name': 'Lee'} 

์œ„์™€ ๊ฐ™์ด ์ธ์Šคํ„ด์Šค์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด name์ด ์ œ๋Œ€๋กœ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

print(user1.name)
print(user2.name)
print(user3.name)

 Kim 
 Park 
 Lee 

 

 

* ํด๋ž˜์Šค ๋„ค์ž„์ŠคํŽ˜์ด์Šค, ํด๋ž˜์Šค ๋ณ€์ˆ˜ (๋ชจ๋“  ์ธ์Šคํ„ด์Šค์— ๊ณต์œ ) 

print(WareHouse.__dict__)  # ---> ํด๋ž˜์Šค ์ž์ฒด์˜ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค์—์„œ ํด๋ž˜์Šค๋ณ€์ˆ˜ ํ™•์ธ ๊ฐ€๋Šฅ

 {'__module__': '__main__', 'stock_num': 3, '__init__': <function WareHouse.__init__  
 at 0x0000022B69124510>, '__del__': <function WareHouse.__del__ at 0x0000022B69124598>, '__dict__': <attribute  '__di ct__' of 'WareHouse' objects>, '__weakref__': <attribute '__weakref__' of 'WareHouse' objects>, '__doc__': None} 

์ด๋ ‡๊ฒŒ ํด๋ž˜์Šค ๋ณ€์ˆ˜๋Š” ํด๋ž˜์Šค์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์ฐ์–ด๋ณด๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

print(user1.stock_num) # ์ž์‹ ์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์—†์œผ๋ฉด ํด๋ž˜์Šค์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์—์„œ ์ฐพ๊ณ , ํด๋ž˜์Šค์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์—๋„ ์—†์œผ๋ฉด ๊ทธ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค. 
print(user2.stock_num)
print(user3.stock_num)

 3 
 3 
 3 

ํด๋ž˜์Šค ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•˜์—ฌ ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ž๊ธฐ ์ž์‹ ์˜ ์ธ์Šคํ„ด์Šค ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์ฐพ๋Š” ๋ณ€์ˆ˜๊ฐ€ ์—†์œผ๋ฉด ํด๋ž˜์Šค์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์—์„œ ์ฐพ๊ณ  , ํด๋ž˜์Šค์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์—๋„ ํ•ด๋‹น ๋ณ€์ˆ˜๊ฐ€ ์—†์œผ๋ฉด ๊ทธ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

del user1

del๋กœ user1์„ ์ง€์šฐ๋ฉด, ์œ„์— ์„ ์–ธํ•ด ๋†“์€ ๋Œ€๋กœ sock_num ๋„ -1์ด ๋  ๊ฒƒ์ด๋‹ค. 

# print(user1.stock_num)
print(user2.stock_num)
print(user3.stock_num)

 2 
 2 

 

 

 

 

 

 

 

 

 

 

 

 

 [ํด๋ž˜์Šค ์ƒ์†] 

- ์˜ˆ์ œ 1 

* Car : ๋ถ€๋ชจ ํด๋ž˜์Šค 

class Car: 
    """"Parent Class"""
    def __init__(self, tp, color):
        self.type = tp
        self.color = color

    def show(self):
        return 'Car Class "Show Method!"'

* BmwCar : ์ž์‹ ํด๋ž˜์Šค 1

class BmwCar(Car):
    """Sub Class"""
    def __init__(self, car_name, tp, color):
        super().__init__(tp, color)
        self.car_name = car_name

    def show_model(self) -> None:
        return "Your Car Name : %s" % self.car_name

* BenzCar : ์ž์‹ ํด๋ž˜์Šค 2

class BenzCar(Car):
    """Sub Class"""
    def __init__(self, car_name, tp, color):
        super().__init__(tp, color)
        self.car_name = car_name

    def show_model(self) -> None:
        return "Your Car Name : %s" % self.car_name

    def show(self):
        print(super().show())
        return 'Car Info: %s %s %s' % (self.car_name, self.type, self.color)

 

* ์‚ฌ์šฉ 

model1 = BmwCar('520d', 'sedan', 'red')

์šฐ์„  ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค. 

print(model1.color) # Super 
print(model1.type)  # Super
print(model1.car_name) # Sub
print(model1.show()) # Super    
print(model1.show_model()) # Sub
print(model1.__dict__)

 red 
 sedan 
 520d 
 Car Class "Show Method!" 
 Your Car Name : 520d 
 {'type': 'sedan', 'color': 'red', 'car_name': '520d'} 

๋ถ€๋ชจํด๋ž˜์Šค์—์„œ ์„ ์–ธํ•ด ๋†“์•˜๋˜ color, type๋ณ€์ˆ˜์™€ show() ๋ฉ”์†Œ๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ธ์Šคํ„ด์Šค์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์—๋„ ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด ๊ฐ€ ์žˆ๋‹ค. 

 

 

# Method Overriding (์˜ค๋ฒ„๋ผ์ด๋“œ : ์˜ฌ๋ผํƒ€๋‹ค)

model2 = BenzCar("220d", 'suv', 'black')
print(model2.show())

 Car Class "Show Method!" 
 Car Info: 220d suv black 

# Parent Method Call 
model3 = BenzCar('350s', 'sedan', 'silver')
print(model3.show())

 Car Class "Show Method!" 
 Car Info: 350s sedan silver 

BenzCar ํด๋ž˜์Šค์˜ show() ๋ฉ”์†Œ๋“œ์—์„œ super.show()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ€๋ชจํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•˜์˜€๋‹ค. ๋”ฐ๋ผ์„œ ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ์„ ์–ธํ•˜์˜€๋˜ ๋‚ด์šฉ๋„ ํ•จ๊ป˜ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

# Inheritance Info (์ƒ์†์ •๋ณด ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋‚˜์˜ด)

print(BmwCar.mro())  # ์ƒ์† ๊ด€๊ณ„๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. 
print(BenzCar.mro())
# ๋ชจ๋“  ํด๋ž˜์Šค๋Š” object๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.

 [<class '__main__.BmwCar'>, <class '__main__.Car'>, <class 'object'>] 
 [<class '__main__.BenzCar'>, <class '__main__.Car'>, <class 'object'>] 

mro ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์† ๊ด€๊ณ„๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“  ํด๋ž˜์Šค๋Š” object๋ฅผ ์ƒ์† ๋ฐ›๋Š” ๊ฒƒ ๋˜ํ•œ ์•Œ์•„๋‘๊ธฐ ! 

 

 

 

 

 

 

- ์˜ˆ์ œ 2 : ๋‹ค์ค‘ ์ƒ์† 

class X:
    pass

class Y:
    pass

class Z:
    pass

class A(X, Y):
    pass

class B(Y, Z):
    pass

class M(B, A, Z):
    pass

์šฐ์„  ํด๋ž˜์Šค X, Y, Z๋ฅผ ์„ ์–ธํ•˜๊ณ , A๋Š” X, Y๋ฅผ , B๋Š” Y, Z ๋ฅผ , M์€ B, A, Z ๋ฅผ ๋‹ค์ค‘์ƒ์† ๋ฐ›๋Š”๋‹ค. ์ด ๋•Œ์˜ Inheritance Info๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด์ž. 

print(M.mro())
print(A.mro())

 [<class '__main__.M'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.X'>, <class '__main__.Y'>, <class '__main__.Z'>, <class 'object'>] 
 [<class '__main__.A'>, <class '__main__.X'>, <class '__main__.Y'>, <class 'object'>] 

์ด์ฒ˜๋Ÿผ ๋‹ค์ค‘์ƒ์†์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ , ๋„ˆ๋ฌด ๋งŽ์ด ์ƒ์† ๋ฐ›๊ฒŒ ๋˜๋ฉด ์ฝ”๋“œ๋ฅผ ํ•ด์„ํ•˜๊ธฐ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ค์–ด์งˆ ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋œ๋‹ค. 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/38

 

[python ๊ธฐ์ดˆ] 6. ํŒŒ์ด์ฌ ํ๋ฆ„์ œ์–ด (๋ฐ˜๋ณต๋ฌธ) - for, while

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/37 https://silvercoding.tistory.com/36 https://silvercoding.tistory.com/35 https://silvercoding.tistory.com/34..

silvercoding.tistory.com

 

 

 

* ํ•จ์ˆ˜ ์ •์˜ ๋ฐฉ๋ฒ• 

def ํ•จ์ˆ˜๋ช…(parameter):

    code 

 

 

 

- ์˜ˆ์ œ 1 : ์ถœ๋ ฅ(print) 

def hello(world):
    print('Hello', world)
hello('Silver !')
hello(77777)

 Hello Silver ! 
 Hello 77777 

ํ•จ์ˆ˜๋ช…(parameter) ์˜ ํ˜•ํƒœ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 2 : ๋ฆฌํ„ด(return) 

def hello_return(world):
    val = "Hello" + str(world)
    return val
str = hello_return("Silver !!!!!")
print(str)

 HelloSilver !!!!! 

ํ•จ์ˆ˜ ์•ˆ์—์„œ returnํ•˜์—ฌ ๋ณ€์ˆ˜๋กœ ๋ฆฌํ„ด๊ฐ’์„ ๋ฐ›์•„์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 3 : ์—ฌ๋Ÿฌ ๊ฐ’ ๋ฆฌํ„ด 

def func_mul(x):
    y1 = x * 100 
    y2 = x * 200 
    y3 = x * 300 
    return y1, y2, y3
val1, val2, val3 = func_mul(100)
print(type(val1), val1, val2, val3)

 <class 'int'> 10000 20000 30000 

์—ฌ๋Ÿฌ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜์—ฌ ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋ฐ›์•„์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 4 : ๋ฆฌ์ŠคํŠธ ๋ฆฌํ„ด 

def func_mul2(x):
    y1 = x * 100 
    y2 = x * 200 
    y3 = x * 300 
    return [y1, y2, y3]
lt = func_mul2(100)
print(lt, type(lt))

 [10000, 20000, 30000] <class 'list'> 

 

 

 

- ์˜ˆ์ œ 5 : ๊ฐ€๋ณ€ ์ธ์ž ๋ฐ›๊ธฐ (*args / **kwargs) 

(1) *args 

: ํŠœํ”Œ ํ˜•ํƒœ๋กœ ๋ฐ›์Œ ( ๊ฐ€๋ณ€ ํŠœํ”Œ ) 

def args_func(*args):
    print(args)

 

 

def args_func2(*args):
    for t in args:
        print(t)
def args_func3(*args):
    for i, v in enumerate(args):
        print(i, v)
args_func('Kim')
args_func('Kim', 'Park')
args_func2('Kim', 'Park', 'Lee')
args_func3('Kim', 'Park', 'Lee')

 ('Kim',) 

 ('Kim', 'Park') 

์œ„์™€ ๊ฐ™์ด ํŒŒ๋ผ๋ฏธํ„ฐ์— ์ž…๋ ฅํ•œ ๊ฐœ์ˆ˜ ๋งŒํผ  ํŠœํ”Œ์•ˆ์˜ ๊ฐ’์ด ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 
 Kim 
 Park 

 Lee 

ํŠœํ”Œ์ด๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ์—์„œ ์‚ฌ์šฉํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 
 0 Kim 
 1 Park 
 2 Lee 

์ธ๋ฑ์Šค์™€ ๊ฐ’์„ ๋ชจ๋‘ iterable ํ•˜๊ฒŒ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” enumerate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์ด๋‹ค. 

 

 

(2) **kwargs

: dictionary๋กœ ๋ฐ›์Œ ( ๊ฐ€๋ณ€ ๋”•์…”๋„ˆ๋ฆฌ ) 

def kwargs_func(**kwargs):
    print(kwargs)
def kwargs_func2(**kwargs):
    for k, v in kwargs.items():
        print(k, v)
kwargs_func(name1='Kim')
kwargs_func(name1='Kim', name2='Park', name3='Lee')
kwargs_func2(name1='Kim', name2='Park', name3='Lee')

 {'name1': 'Kim'} 
 {'name1': 'Kim', 'name2': 'Park', 'name3': 'Lee'} 

์ธ์ž์— key = value ํ˜•ํƒœ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๊ทธ ๊ฐœ์ˆ˜ ๋Œ€๋กœ ๋”•์…”๋„ˆ๋ฆฌ ๊ฐ’์ด ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 
 name1 Kim 
 name2 Park 
 name3 Lee 

dictionary ๊ฐ์ฒด์ด๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

(3) ์ „์ฒด ํ˜ผํ•ฉ 

def example_mul(arg1, arg2, *args, **kwargs):
    print(arg1, arg2, args, kwargs)

์ผ๋ฐ˜, ๊ฐ€๋ณ€ ํŠœํ”Œ, ๊ฐ€๋ณ€ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋ฐ›๋Š” ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค.  

example_mul(10, 20)
example_mul(10, 20, 'park', 'kim')
example_mul(10, 20, 'park', 'kim', age1=24, age2=35)

 10 20 () {} 
 10 20 ('park', 'kim') {} 
 10 20 ('park', 'kim') {'age1': 24, 'age2': 35} 

 

 

 

- ์˜ˆ์ œ 6 : ์ถฉ์ฒฉํ•ฉ์ˆ˜ (ํด๋กœ์ €) - ํšจ์œจ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๊ฐ€๋Šฅ 

def nested_func(num):
    def func_in_func(num):
        print('>>>', num)
    print('in func')
    func_in_func(num + 10000)
nested_func(10000)

 in func 
 >>> 20000 

nested_fun(10000)์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด print๋ฌธ์ด ์‹คํ–‰๋˜๊ณ , func_in_func(10000 + 10000)์ด ํ˜ธ์ถœ์ด ๋˜์–ด func_in_func์•ˆ์˜ print๋ฌธ์ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

<์ฐธ๊ณ  : ํด๋กœ์ € ํฌ์ŠคํŒ…>

https://silvercoding.tistory.com/26?category=957523 

 

[python ์‹ฌํ™”] 9. ๋ณ€์ˆ˜ ๋ฒ”์œ„, Closure, Decorator

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/25 ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ์–ด๋ ต๋‹ค๊ณ  ๋Š๋ผ๋Š” ๊ฒƒ์ด ๋‹น์—ฐํ•˜๋‹ค. ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ

silvercoding.tistory.com

 

 

- ์˜ˆ์ œ 7 : hint -> ์ธ์ž ํƒ€์ž…๊ณผ ๋ฆฌํ„ด ํƒ€์ž… ๋ช…์‹œํ•ด์ฃผ๊ธฐ 

def func_mul3(x: int) -> list:
    y1 = x * 100 
    y2 = x * 200 
    y3 = x * 300 
    return [y1, y2, y3]
print(func_mul3(5))

 [500, 1000, 1500] 

์ธ์ž x๋Š” int, ๋ฆฌํ„ดํƒ€์ž…์€ list๋ผ๊ณ  ๊ธฐ์ž…ํ•ด์ฃผ์–ด ๋ช…ํ™•ํžˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 8 : lambda 

* ๋žŒ๋‹ค์‹ : ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ, ๊ฐ€๋…์„ฑ ํ–ฅ์ƒ, ์ฝ”๋“œ ๊ฐ„๊ฒฐ 

* ํ•จ์ˆ˜ : ๊ฐ์ฒด ์ƒ์„ฑ -> ๋ฆฌ์†Œ์Šค (๋ฉ”๋ชจ๋ฆฌ) ํ• ๋‹น vs ๋žŒ๋‹ค : ์ฆ‰์‹œ ์‹คํ–‰ (Heap ์ดˆ๊ธฐํ™”) -> ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ธฐํ™” 

 

(1) ์ผ๋ฐ˜์  ํ•จ์ˆ˜ : ๋ณ€์ˆ˜ ํ• ๋‹น 

def mul_10(num: int) -> int:
    return num * 10
val_func = mul_10   # ---> ๊ฐ์ฒด ์ƒ์„ฑ -> ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น 
print(val_func) 
print(type(val_func))

print(val_func(10))

 <function mul_10 at 0x00000212CEA148C8> 
 <class 'function'> 
 100 

 

 

 

(2) ๋žŒ๋‹ค์‹ 

lambda_mul_10 = lambda num : num * 10 
print(">>>", lambda_mul_10(10))

 >>> 100 

 

 

* ํ•จ์ˆ˜์˜ ์ธ์ž์— ํ•จ์ˆ˜ ๋„ฃ๊ธฐ 

def func_final(x, y, func):
    print(x * y * func(10))

์ธ์ž์— ํ•จ์ˆ˜๋ฅผ ๋„ฃ๋Š” ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค. 

func_final(10, 10, lambda_mul_10)

 10000 

 

print(func_final(10, 10, lambda x: x * 1000))

 1000000 
 None 

์ด๋ ‡๊ฒŒ ๋ฐ”๋กœ lambda ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ์–ด๋„ ๋œ๋‹ค. 

 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/37

 

[python ๊ธฐ์ดˆ] 5. ํŒŒ์ด์ฌ ํ๋ฆ„์ œ์–ด (์ œ์–ด๋ฌธ) - ์กฐ๊ฑด๋ฌธ if

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/36 https://silvercoding.tistory.com/35 https://silvercoding.tistory.com/34 https://silvercoding.tistory.com/33..

silvercoding.tistory.com

 

 

 

(1) while ๋ฌธ 

- ์˜ˆ์ œ 1

v1 = 1
while v1 < 11:
    print("v1 is : ", v1)
    v1 += 1

 v1 is :  1 
 v1 is :  2 
 v1 is :  3 
 v1 is :  4 
 v1 is :  5 
 v1 is :  6 
 v1 is :  7 
 v1 is :  8 
 v1 is :  9 
 v1 is :  10 

while๋ฌธ์€ ์กฐ๊ฑด์ด ์ฐธ์ผ ๋™์•ˆ ๊ณ„์† ๋ฐ˜๋ณตํ•œ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” 11๋ณด๋‹ค ์ž‘์„ ๋•Œ, ์ฆ‰ 10๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. 

 

 

- ์˜ˆ์ œ 2 

sum1 = 0 
cnt1 = 1
while cnt1 <= 100: 
    sum1 += cnt1
    cnt1 += 1
print('1 ~ 100: ', sum1)

 1 ~ 100:  5050 

cnt1์ด 100 ์ดํ•˜์ผ ๋™์•ˆ ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค. sum1์— 1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” cnt1์„ ๊ณ„์† ๋”ํ•ด์คŒ์œผ๋กœ์จ 1๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. 

 

 

* ์ฐธ๊ณ  - ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ sum(), range()์ด์šฉ

print('1 ~ 100: ', sum(range(1, 101)))
print('1 ~ 100: ', sum(range(1, 101, 2)))

 1 ~ 100:  5050 
 1 ~ 100:  2500 

๋” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ฉ์„ ๊ตฌํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 1๋ถ€ํ„ฐ 100๊นŒ์ง€ ํ™€์ˆ˜์˜ ํ•ฉ๋งŒ์„ ๊ตฌํ•˜๋ ค๋ฉด range(1, 101, 2) ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

(2) for ๋ฌธ 

* ์‹œํ€€์Šค (์ˆœ์„œ๊ฐ€ ์žˆ๋Š”) ์ž๋ฃŒํ˜• ๋ฐ˜๋ณต : ๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ์ง‘ํ•ฉ, ์‚ฌ์ „ 

* iterable ๋ฆฌํ„ด ํ•จ์ˆ˜ : range. reversed, enumerate, filter, map, zip 

 

- ์˜ˆ์ œ1 - range() 

for v3 in range(1, 11):
    print('v3 is : ', v3)

 v3 is :  1 
 v3 is :  2 
 v3 is :  3 
 v3 is :  4 
 v3 is :  5 
 v3 is :  6 
 v3 is :  7 
 v3 is :  8 
 v3 is :  9 
 v3 is :  10 

์‹œํ€€์Šค ์ž๋ฃŒํ˜•์„ ๋ฐ˜๋ณตํ•œ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—์„œ๋Š” 1๋ถ€ํ„ฐ 10๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ v3์— ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ถœ๋ ฅํ•ด ์ค€๋‹ค. 

 

 

- ์˜ˆ์ œ2 - ๋ฆฌ์ŠคํŠธ (ilst)

names = ['Kim', 'Park', 'Cho', 'Choi', 'Yoo']
for v in names: 
    print('You are: ', v)

 You are:  Kim 
 You are:  Park 
 You are:  Cho 
 You are:  Choi 
 You are:  Yoo 

๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์„ ๋„ฃ์–ด์ฃผ์–ด๋„ ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•˜๋‹ค! 

 

 

- ์˜ˆ์ œ3 - ๋ฌธ์ž์—ด (str)

word = "dreams"
for s in word:
    print("Word: ", s)

 Word:  d 
 Word:  r 
 Word:  e 
 Word:  a 
 Word:  m 
 Word:  s 

๋ฌธ์ž์—ด๋„ ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

- ์˜ˆ์ œ4 - ๋”•์…”๋„ˆ๋ฆฌ (dictionary) 

my_info = {
    'name' : 'kim',
    'age' : 33, 
    'city' : 'Seoul'
}

์šฐ์„  ๋”•์…”๋„ˆ๋ฆฌ ์„ ์–ธํ•˜๊ธฐ 

 

(1) ๊ธฐ๋ณธ : key

for key in my_info:
    print('my_info', key)

 my_info name 
 my_info age 
 my_info city 
๋”•์…”๋„ˆ๋ฆฌ ์ž์ฒด๋ฅผ ๋ฐ˜๋ณต๋ฌธ์— ๋„ฃ์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ํ‚ค๊ฐ€ ๋ฐ˜๋ณต๋œ๋‹ค. 

 

 

(2) values

for key in my_info.values():
    print('my_info', key)

 my_info kim 
 my_info 33 
 my_info Seoul 

 

 

 

(3) keys

for key in my_info.keys():
    print('my_info', key)

 my_info name 
 my_info age 
 my_info city 

 

(4) items 

for key, value in my_info.items():
    print('my_info', key, value)

 my_info name kim 
 my_info age 33 
 my_info city Seoul 

 

 

- ์˜ˆ์ œ5 : ๋ฐ˜๋ณต๋ฌธ + ์กฐ๊ฑด๋ฌธ 

name = 'KennRY'
name2 = ""

KennRY ์—์„œ ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ, ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊พธ๊ณ , ์ตœ์ข…์ ์œผ๋กœ name2์— ๋ฐ”๋€ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. 

for n in name:
    if n.isupper():
        print(n.lower())
        name2+=n.lower()
    else:
        print(n.upper())
        name2+=n.upper()

 k 
 E 
 N 
 N 
 r 
 y 

print(name2)

 kENNry 

๋ฌธ์ž์—ด์„ ๋ฐ˜๋ณตํ•˜์—ฌ, ๊ธ€์ž๊ฐ€ ๋Œ€๋ฌธ์ž๋ฉด ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๊ณ  name2์— ๊ธ€์ž๋ฅผ ๋”ํ•ด์ค€๋‹ค. ๊ทธ ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ๋„ ๋™์ผํ•˜๊ฒŒ ํ•ด์ค€๋‹ค. ๊ทธ๋Ÿผ ์ด์ฒ˜๋Ÿผ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ6 : break

numbers = [14, 3, 4, 7, 10, 24, 17, 2, 33, 35, 36, 38]
numbers2 = [14, 3, 4, 7, 10, 24, 17, 2, 37, 35, 36, 38]
for num in numbers: 
    if num == 33:
        print('found : 33 !')
        break
    else:
        print('not found : 33!')

 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 found : 33 ! 

33์„ ๋ฐœ๊ฒฌํ•˜๋ฉด break๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ์„ ํƒˆ์ถœํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. ์‹ค์ œ๋กœ 33 ์ฐจ๋ก€๊ฐ€ ์™”์„ ๋•Œ, found : 33 ! ์ด ์ถœ๋ ฅ๋˜๊ณ , ๊ทธ ์ดํ›„๋กœ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ7 : for-else ๊ตฌ๋ฌธ 
: ๋ฐ˜๋ณต๋ฌธ์ด ์ •์ƒ์ ์œผ๋กœ ์ˆ˜ํ–‰(๋ชจ๋“  ๊ฐ’ ์ˆœํšŒ ) ๋œ ๊ฒฝ์šฐ else ๋ธ”๋Ÿญ ์ˆ˜ํ–‰ 

for num in numbers2: 
    if num == 33:
        print('found : 33 !')
        break
    else:
        print('not found : 33!')
else:
    print('Not found 33 ...........')

 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 not found : 33! 
 Not found 33 ........... 

break๋ฌธ ๋“ฑ์œผ๋กœ ์ธํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ์ด ์ •์ƒ์ ์œผ๋กœ ๋ชจ๋“  ๊ฐ’์„ ์ˆœํšŒํ•˜์ง€ ๋ชปํ•  ๊ฒฝ์šฐ else๋ธ”๋Ÿญ์€ ์ˆ˜ํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. ์˜ˆ์ œ7๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” 33์ด ๋ฆฌ์ŠคํŠธ์— ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ •์ƒ์ ์œผ๋กœ ๋ชจ๋“  ๊ฐ’์„ ์ˆœํšŒํ•˜์—ฌ else๋ธ”๋Ÿญ์ด ์ˆ˜ํ–‰๋˜์—ˆ๋‹ค. 

 

 

 

- ์˜ˆ์ œ8 : continue

lt = ['1', 2, 5, True, 4.3, complex(4)]
for v in lt: 
    if type(v) is float:
        continue
    print('ํƒ€์ž… : ', type(v))

 ํƒ€์ž… :  <class 'str'> 
 ํƒ€์ž… :  <class 'int'>
 ํƒ€์ž… :  <class 'int'> 
 ํƒ€์ž… :  <class 'bool'> 
 ํƒ€์ž… :  <class 'complex'> 

continue๋ฅผ ๋งŒ๋‚˜๋ฉด ๊ทธ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ๊ณง๋ฐ”๋กœ for๋ฌธ์˜ ์ฒซ๋ฒˆ์งธ๋กœ ๋˜๋Œ์•„ ๊ฐ€ ๋ฐ˜๋ณต์„ ๊ทธ๋Œ€๋กœ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์˜ˆ์ œ8์—์„œ๋Š” floatํƒ€์ž…์ด๋ฉด continue๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. float ํƒ€์ž…์ธ 4.3์„ ์ค‘๊ฐ„์— ๋„ฃ์–ด๋†“์•˜๋”๋‹ˆ ๊ทธ ํ›„์˜ print๋ฌธ์€ ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  complexํƒ€์ž…์€ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

- ์˜ˆ์ œ9 : reversed ํ•จ์ˆ˜ 

name = 'Niceman'
print(reversed(name))
print(list(reversed(name)))
print(tuple(reversed(name)))

 <reversed object at 0x000001C1CC70D898> 
 ['n', 'a', 'm', 'e', 'c', 'i', 'N'] 
 ('n', 'a', 'm', 'e', 'c', 'i', 'N') 

reversed ํ•จ์ˆ˜๋Š” ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•ด์„œ ๋ณด๊ธฐ ์œ„ํ•ด list๋‚˜ tuple๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

for i in reversed(name):
    print(i)

 n 
 a 
 m 
 e 
 c 
 i 
 N 

ํ•˜์ง€๋งŒ iterable์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— for๋ฌธ์—์„œ๋Š” ๊ณง๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/36

 

[python ๊ธฐ์ดˆ] 4. ๋ฐ์ดํ„ฐ ํƒ€์ž… (2) (์ž๋ฃŒํ˜•) - dictionary, set

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/35 https://silvercoding.tistory.com/34 https://silvercoding.tistory.com/33 https://www.python-course.eu/python..

silvercoding.tistory.com

 

* ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฐ๊ฒฝ์ง€์‹ & ๊ธฐ์ดˆ if๋ฌธ ์ž‘์„ฑํ•ด๋ณด๊ธฐ

- ์ฐธ(True) , ๊ฑฐ์ง“(False) ์ข…๋ฅ˜ 

(1) ์ฐธ : "๋‚ด์šฉ", [๋‚ด์šฉ], (๋‚ด์šฉ), {๋‚ด์šฉ}, 1, True

(2) ๊ฑฐ์ง“ : "", [], (), {}, 0, False 

๊ธฐ๋ณธ์ ์œผ๋กœ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ํƒ€์ž…์—์„œ ๋‚ด์šฉ์ด ์ฑ„์›Œ์ ธ ์žˆ์œผ๋ฉด ์ฐธ, ๋น„์–ด ์žˆ์œผ๋ฉด ๊ฑฐ์ง“์ด๋‹ค. 

# ์˜ˆ 1
if True:
    print('Yes')

# ์˜ˆ 2
if False:
    print('No')

# ์˜ˆ 3
if False:
    print('No')
else:
    print('Yes2')

 Yes 
 Yes2 

์šฐ์„  ์ด์ฒ˜๋Ÿผ if๋ฌธ์€ True์ผ ๋•Œ ์‹คํ–‰๋˜๊ณ , False์ผ ๋•Œ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์œ„์—์„œ ๋ฐฐ์šด ์ฐธ๊ณผ ๊ฑฐ์ง“์„ ์‚ฌ์šฉํ•˜์—ฌ if๋ฌธ์„ ํ™œ์šฉํ•œ๋‹ค. 

city = ""

if city:
    print('>>>>True')
else:
    print('>>>>False')

 >>>>False 

์ด์ฒ˜๋Ÿผ ๋น„์–ด์žˆ๋Š” ๋ฌธ์ž์—ด์ผ ๊ฒฝ์šฐ False๋กœ ์ธ์‹ํ•˜์—ฌ else๋กœ ๋„˜์–ด๊ฐ€ else์˜ ๋ถ€๋ถ„์ด ์‹คํ–‰ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

 

- ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž 

: >, >=, <, <=, ==, != 

a = 10 
b = 0
print(a == b)
print(a != b)
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)

 False 
 True 
 True 

 True 

 False 
 False 

 

 

 

- ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž 

: and, or, not

a = 100 
b = 60 
c = 15
print('and: ', a > b and b > c)
print('or: ', a > b or c > b)
print('not: ', not a > b)
print(not True)
print(not False)

 and:  True 
 or:  True 
 not:  False 
 False 
 True 

 

 

* ์‚ฐ์ˆ , ๊ด€๊ณ„ , ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž ์ ์šฉ ์ˆœ์„œ 

: ์‚ฐ์ˆ  > ๊ด€๊ณ„ > ๋…ผ๋ฆฌ 

print('ex1: ', 5 + 10 > 0 and not 7 + 3 == 10)

 ex1:  False 

 

 

 

 

 

 

 

* if ๋ฌธ ์‚ฌ์šฉ 

score1 = 90
score2 = 'A'
if score1 >= 90 and score2 == 'A':
    print('ํ•ฉ๊ฒฉํ•˜์…จ์Šต๋‹ˆ๋‹ค. ')
else:
    print('๋” ๊ณต๋ถ€ํ•˜์„ธ์š”. ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค. ')

 ํ•ฉ๊ฒฉํ•˜์…จ์Šต๋‹ˆ๋‹ค. 

score1์ด 90์ด์ƒ์ด๋ฉด์„œ score2๊ฐ€ A์ด๋ฏ€๋กœ (True & True = True) if๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค. 

 

 

- ๋‹ค์ค‘ ์กฐ๊ฑด๋ฌธ 

num = 82
if num >= 90:
    print("num ๋“ฑ๊ธ‰ A", num)
elif num >= 80:
    print("num ๋“ฑ๊ธ‰ B", num)
elif num >= 70:
    print("num ๋“ฑ๊ธ‰ C", num)
else:
    print("๊ฝ")

 num ๋“ฑ๊ธ‰ B 82 

num ์ด 82 ์ด๋ฏ€๋กœ ๋“ฑ๊ธ‰ B๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. elif๋Š” else if ์˜ ์ค„์ž„๋ง์ด๋‹ค. if๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์œผ๋ฉด elif๋กœ ๊ฐ€๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ ์œ„์˜ ๊ฒฝ์šฐ์—์„œ elif ๋Œ€์‹  if๋ฅผ ์“ด๋‹ค๋ฉด ,

 num ๋“ฑ๊ธ‰ B 82 
 num ๋“ฑ๊ธ‰ C 82 

์ด์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค. ์œ—์ค„์˜ if๋ฌธ์ด ์‹คํ–‰์ด ๋˜๋“  ์•ˆ๋˜๋“  ๋ชจ๋“  if๋ฌธ์„ ๊ฒ€์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๊ฒƒ์ด elif๊ฐ€ ํ•„์š”ํ•œ ์ด์œ ๋‹ค. 

 

- ์ค‘์ฒฉ ์กฐ๊ฑด๋ฌธ 

age = 27 
height = 175
if age >= 20: 
    if height >= 170:
        print('A ์ง€๋ง ์ง€์› ๊ฐ€๋Šฅ')
    elif height > 160:
        print('B ์ง€๋ง ์ง€์› ๊ฐ€๋Šฅ')
    else:
        print('์ง€์› ๋ถˆ๊ฐ€')

else:
    print('20์„ธ ์ด์ƒ ์ง€์› ๊ฐ€๋Šฅ')

 A ์ง€๋ง ์ง€์› ๊ฐ€๋Šฅ 

์ด์ฒ˜๋Ÿผ if๋ฌธ ์•ˆ์— if๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. age๊ฐ€ 20 ์ด์ƒ์ผ ๋•Œ , height ๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ์ค‘์ฒฉ ์กฐ๊ฑด๋ฌธ์ด๋‹ค.  

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/35

 

[python ๊ธฐ์ดˆ] 3. ๋ฐ์ดํ„ฐ ํƒ€์ž… (1) (์ž๋ฃŒํ˜•) - list, tuple

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/34 https://silvercoding.tistory.com/33 https://www.python-course.eu/python3_formatted_output.php Python Tutori..

silvercoding.tistory.com

 

1. ๋”•์…”๋„ˆ๋ฆฌ (Dictionary) 

: ์ˆœ์„œ X, ์ค‘๋ณต X, ์ˆ˜์ • O, ์‚ญ์ œ O 

: Key, Value ํ˜•์‹์œผ๋กœ ๋˜์–ด ์žˆ์Œ. (Json/ MongoDB) 

 

- ์„ ์–ธ 

a = {'name': 'kIM', 'Phone': '010-7777-7777', 'birth': 990101}
b = {0: 'Hello Python', 1: 'Hello Coding'}
c = {'arr': [1, 2, 3, 4, 5]}
print(type(a))

 <class 'dict'> 

๋”•์…”๋„ˆ๋ฆฌ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•ด ์ค€๋‹ค. 

 

 

- ์ถœ๋ ฅ 

print(a['name'])
print(a.get('name'))
print(a.get('adress'))  # ์•ˆ์ „ํ•œ ์ฝ”๋”ฉ ๋ฐฉ์‹ : ์ด๊ฑฐ ์“ฐ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ. ์—๋Ÿฌ๊ฐ€ ์•ˆ๋‚˜๊ธฐ ๋•Œ๋ฌธ 
print(c['arr'][1:3])

 kIM 
 kIM 
 None 
 [2, 3] 

๋”•์…”๋„ˆ๋ฆฌ์—์„œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. 1. ํ‚ค์— ์ง์ ‘ ์ ‘๊ทผ , 2. get() ์‚ฌ์šฉ 

1๋ฒˆ ๋ฐฉ๋ฒ•์ด ํŽธ๋ฆฌํ•˜์ง€๋งŒ, ์—†๋Š” ํ‚ค ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ฒŒ ๋œ๋‹ค. 2๋ฒˆ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์—†๋Š” ํ‚ค ๊ฐ’์„ ๋„ฃ๋”๋ผ๋„ None์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋” ์•ˆ์ „ํ•œ ์ฝ”๋”ฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

- ๋”•์…”๋„ˆ๋ฆฌ ์ถ”๊ฐ€ 

a['adress'] = 'Seoul'
print(a)
a['rank'] = [1, 3, 4]
a['rank2'] = (1, 2, 3)
print(a)

 {'name': 'kIM', 'Phone': '010-7777-7777', 'birth': 990101, 'adress': 'Seoul'} 
 {'name': 'kIM', 'Phone': '010-7777-7777', 'birth': 990101, 'adress': 'Seoul', 'rank': [1, 3, 4], 'rank2': (1, 2, 3)} 

key์™€ value๋ฅผ ์ง์ ‘ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค. ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ ๋˜ํ•œ ์ถ”๊ฐ€๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

- keys, values, items 

# print(a.keys()[0]) # ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผ ๋ถˆ๊ฐ€ 
temp = list(a.keys()) # ---> ํ˜•๋ณ€ํ™˜ ํ•ด์ฃผ์–ด์•ผ ํ•จ 
print(temp)
print(temp[1:3])

 ['name', 'Phone', 'birth', 'adress', 'rank', 'rank2'] 
 ['Phone', 'birth'] 

a.keys() ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋”•์…”๋„ˆ๋ฆฌ a์˜ key๊ฐ’๋“ค๋งŒ ๋ชจ์•„์„œ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ์–ธ๋œป ๋ณด๋ฉด ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ์ƒ๊ฒผ์ง€๋งŒ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ์ธ๋ฑ์‹ฑ์ด๋‚˜ ์Šฌ๋ผ์ด์‹ฑ์„ ํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ list()๋ฅผ ์ด์šฉํ•˜์—ฌ ํ˜•๋ณ€ํ™˜์„ ํ•ด์ฃผ๋ฉด ํ‚ค ๊ฐ’๋“ค์ด ๋‹ด๊ธด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

print(a.values())
print(list(a.values()))

print(a.items())
print(list(a.items()))

 dict_values(['kIM', '010-7777-7777', 990101, 'Seoul', [1, 3, 4], (1, 2, 3)]) 
 ['kIM', '010-7777-7777', 990101, 'Seoul', [1, 3, 4], (1, 2, 3)] 

 dict_items([('name', 'kIM'), ('Phone', '010-7777- 7777'), ('birth', 990101), ('adress', 'Seoul'), ('rank', [1, 3, 4]), ('rank2', (1, 2, 3))]) 
 [('name', 'kIM'), ('Phone', '010-7777-7777'), ('birth', 990101), ('adress', 'Seoul'), ('rank', [1, 3, 4]), ('rank2', (1, 2, 3))] 

values()์™€ items()๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค! 

 

 

- ํ‚ค ์กด์žฌ ์—ฌ๋ถ€ 

print(1 in b)
print(2 in b)
print('name' in a)

 True 
 False 
 True 

์œ„์™€ ๊ฐ™์ด ๋”•์…”๋„ˆ๋ฆฌ์—์„œ in์„ ์ด์šฉํ•˜๋ฉด key๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋ฅผ boolean ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค. 

 

 

 

 

 

 

 

 

 

2. ์ง‘ํ•ฉ (Set) 

: ์ˆœ์„œ X, ์ค‘๋ณต X 

 

- ์„ ์–ธ 

a = set()
b = set([1, 2, 3, 4])
c = set([1, 4, 6, 6, 6])

* ์ฃผ์˜ํ•  ์ ์€ a = { } ์™€ ๊ฐ™์ด ์„ ์–ธํ•˜๋ฉด ๋”•์…”๋„ˆ๋ฆฌ ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด ์ง€๋ฏ€๋กœ ๋นˆ ์ง‘ํ•ฉ์„ ๋งŒ๋“ค ๊ฒฝ์šฐ์—” a = set() ์˜ ํ˜•ํƒœ๋กœ ์„ ์–ธํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

print(type(a))
print(c)

 <class 'set'> 
 {1, 4, 6} 

์œ„์™€ ๊ฐ™์ด ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค

 

 

- ํ˜•๋ณ€ํ™˜ 

: Set ์ž๋ฃŒํ˜•์€ ์ฃผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค. 

t = tuple(b)
print(t)
l = list(b)
print(l)

 (1, 2, 3, 4) 
 [1, 2, 3, 4] 

์ค‘๋ณต ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š๋„๋ก set์„ ์ƒ์„ฑํ•œ ํ›„ ํŠœํ”Œ์ด๋‚˜ ๋ฆฌ์ŠคํŠธ ๋“ฑ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ์ผ์ด ๋งŽ๋‹ค๊ณ  ํ•œ๋‹ค.  

 

 

 

 

* ์ง‘ํ•ฉ ์—ฐ์‚ฐ (๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ) 

s1 = set([1, 2, 3, 4, 5, 6])
s2 = set([4, 5, 6, 7, 8, 9])

์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์ง‘ํ•ฉ์„ ์„ ์–ธํ•œ๋‹ค. 

 

 

- ๊ต์ง‘ํ•ฉ : intersection / &

print(s1.intersection(s2))
print(s1 & s2)

 {4, 5, 6} 
 {4, 5, 6} 

 

 

- ํ•ฉ์ง‘ํ•ฉ : union / |

print(s1.union(s2))
print(s1 | s2)

 {1, 2, 3, 4, 5, 6, 7, 8, 9} 
 {1, 2, 3, 4, 5, 6, 7, 8, 9} 

 

 

- ์ฐจ์ง‘ํ•ฉ : difference / -

print(s1.difference(s2))
print(s1 - s2)

 {1, 2, 3, 4, 5, 6, 7, 8, 9} 
 {1, 2, 3} 
 {1, 2, 3} 

 

 

 

- ์ถ”๊ฐ€ & ์ œ๊ฑฐ 

s3 = set([7, 8, 10, 15])
s3.add(18)
s3.add(7)

print(s3)

 {7, 8, 10, 15, 18} 

addํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

s3.remove(15)
print(s3)

 {7, 8, 10, 18} 

removeํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’ ์ œ๊ฑฐ๊นŒ์ง€ ๊ฐ€๋Šฅํ•˜๋‹ค. 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/34

 

[python ๊ธฐ์ดˆ] 2. ๋ฐ์ดํ„ฐ ํƒ€์ž… Data Type - ์ˆซ์žํ˜•, ๋ฌธ์ž์—ด

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/33 https://www.python-course.eu/python3_formatted_output.php Python Tutorial: Formatted Output Even though it..

silvercoding.tistory.com

 

 

 

 

 

1. ๋ฆฌ์ŠคํŠธ (list) 

: ์ˆœ์„œ O, ์ค‘๋ณต O, ์‚ญ์ œ O 

- ์„ ์–ธ 

a = []
b = list()
c = [1, 2, 3, 4]
d = [10, 100, 'Pen', 'Banana', 'Orange']
e = [10, 100, ['Pen', 'Banana', 'Orange']]

์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ์–ธ ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์ธ๋ฑ์‹ฑ

print(d[3])
print(d[-2])
print(d[0] + d[1])
print(e[2][1])
print(e[-1][-2])

 Banana 
 Banana 
 110 
 Banana 
 Banana 

 

 

 

- ์Šฌ๋ผ์ด์‹ฑ 

print(d[0:3])
print(e[2][1:3])

 [10, 100, 'Pen'] 
 ['Banana', 'Orange'] 

 

 

 

- ์—ฐ์‚ฐ 

print(c + d)
print(c * 3)
print(str(c[0]) + 'hi')

 [1, 2, 3, 4, 10, 100, 'Pen', 'Banana', 'Orange'] 
 [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4] 
 1hi 

 

 

 

- ๋ฆฌ์ŠคํŠธ ์ˆ˜์ •, ์‚ญ์ œ 

c[0] = 77
print(c)

 [77, 2, 3, 4] 

0๋ฒˆ์จฐ ์ธ๋ฑ์Šค์— ๊ฐ’ ๋„ฃ์–ด์ฃผ๊ธฐ 

c[1:2] = [100, 1000, 10000]
print(c) # ์›์†Œ๊ฐ€ ๋“ค์–ด๊ฐ

 [77, 100, 1000, 10000, 3, 4] 

์Šฌ๋ผ์ด์‹ฑ์„ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

c[1] = ['a', 'b', 'c']
print(c) # ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋“ค์–ด๊ฐ

 [77, ['a', 'b', 'c'], 1000, 10000, 3, 4] 

๋ฆฌ์ŠคํŠธ ์ž์ฒด๋ฅผ ๋„ฃ์–ด์ฃผ๋ ค๋ฉด ์ธ๋ฑ์‹ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

del c[1]
print(c)

 [77, 1000, 10000, 3, 4] 

del์„ ์ด์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ’์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค. 

del c[-1]
print(c)

 [77, 1000, 10000, 3] 

 

 

 

 

 

-๋ฆฌ์ŠคํŠธ ๊ด€๋ จ ํ•จ์ˆ˜ 

(1) append 

y = [5, 2, 3, 1, 4]
print(y)
y.append(6) # ๋์— ์ถ”๊ฐ€ 
print(y)

 [5, 2, 3, 1, 4] 
 [5, 2, 3, 1, 4, 6] 

๋ฆฌ์ŠคํŠธ์˜ ๊ฐ€์žฅ ๋์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค. 

 

(2) sort

y.sort() # ์ •๋ ฌ 
print(y)

 [1, 2, 3, 4, 5, 6] 

๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•ด ์ค€๋‹ค. 

 

(3) reverse

y.reverse()
print(y)

 [6, 5, 4, 3, 2, 1] 

๋ฆฌ์ŠคํŠธ๋ฅผ ๋’ค์ง‘์–ด ์ค€๋‹ค. 

 

(4) insert

y.insert(2, 7)
print(y)

 [6, 5, 7, 4, 3, 2, 1] 

append์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์›ํ•˜๋Š” ์ธ๋ฑ์Šค์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

(5) remove

y.remove(2)
y.remove(7)
print(y) # ์›์†Œ ๊ฐ’์„ ์ง€์šด๋‹ค.

 [6, 5, 4, 3, 1] 

del์€ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง€์›Œ์ฃผ์—ˆ๋Š”๋ฐ, removeํ•จ์ˆ˜๋Š” ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๊ทธ ๊ฐ’์„ ์ง€์›Œ์ค€๋‹ค. 

 

(7) pop

y.pop()
print(y) # LIFO ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ

 [6, 5, 4, 3]  

pop์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€์žฅ ๋์— ์žˆ๋Š” ๊ฐ’์„ ์ง€์›Œ์ค€๋‹ค. LIFO(Last In First Out) ์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. 

 

(8) extend

ex = [88, 77]
# y.append(ex) # ๋ฆฌ์ŠคํŠธ ์ž์ฒด๋ฅผ ์ถ”๊ฐ€
y.extend(ex) # ๊ฐ’์„ ์ถ”๊ฐ€ 
print(y)

 [6, 5, 4, 3, 88, 77] 

๋ฆฌ์ŠคํŠธ ์•ˆ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐ’์„ ํ•œ ๋ฒˆ์— ๋„ฃ์–ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ extendํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

 

 

 

* ๋ฆฌ์ŠคํŠธ ์‚ญ์ œ ์ด์ •๋ฆฌ 

del, remove, pop ( : ์˜ˆ์™ธ ๋ฐœ์ƒ ์ฃผ์˜) 

 

 

 

 

 

 

2. ํŠœํ”Œ (tuple) 

: ์ˆœ์„œ O, ์ค‘๋ณต O, ์ˆ˜์ • X, ์‚ญ์ œ X 

- ์„ ์–ธ 

a = ()
b = (1,)
c = (1, 2, 3, 4)
d = (10, 100, ('a', 'b', 'c'))

์ด์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์œผ๋กœ ํŠœํ”Œ์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์‚ญ์ œ : ๋ถˆ๊ฐ€ 

del c[2] # -> ์‚ญ์ œ ๋ถˆ๊ฐ€

 TypeError: 'tuple' object doesn't support item deletion 

ํŠœํ”Œ์€ ์ˆ˜์ •์ด๋‚˜ ์‚ญ์ œ๋ฅผ ํ•  ์ˆ˜ ์—†๋‹ค. 

 

 

- ์ธ๋ฑ์‹ฑ 

print(c[2])
print(c[3])
print(d[2][2])

 3 
 4 
 c 

 

 

- ์Šฌ๋ผ์ด์‹ฑ 

print(d[2:])
print(d[2][0:2])

 (('a', 'b', 'c'),) 
 ('a', 'b') 

 

 

- ์—ฐ์‚ฐ 

print(c + d)
print(c * 3)

 (1, 2, 3, 4, 10, 100, ('a', 'b', 'c')) 
 (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4) 

 

 

- ํŠœํ”Œ ๊ด€๋ จ ํ•จ์ˆ˜ 

z = (5, 2, 1, 3, 1)
print(z)
print(3 in z)
print(z.index(5))  # ์ธ๋ฑ์Šค ๊ฐ’์„ ๋ฐ˜ํ™˜ 
print(z.count(1))  # 1 ์ด ๋ช‡๊ฐœ๊ฐ€ ์žˆ๋ƒ

 (5, 2, 1, 3, 1) 
 True 
 0 
 2 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/33

 

[python ๊ธฐ์ดˆ] 1. print ํ•จ์ˆ˜

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ฐธ๊ณ  ๋งํฌ> https://www.python-course.eu/python3_formatted_output.php Python Tutorial: Formatted Output Even though it may look so, the formatting is not..

silvercoding.tistory.com

 

* ๋ฐ์ดํ„ฐ ํƒ€์ž… ํ•œ๋ˆˆ์— ๋ณด๊ธฐ 

v_str = "Niceman"
v_bool = True
v_str2 = "Goodboy"
v_float = 10.3
v_int = 7
v_dict = {
    "name" : "Kim", 
    "age" : 25
}
v_list = [3, 5, 7]
v_tuple = 3, 5, 7
v_set = {7, 8, 9}
print(type(v_str))
print(type(v_bool))
print(type(v_str2))
print(type(v_float))
print(type(v_int))
print(type(v_dict))
print(type(v_list))
print(type(v_tuple))
print(type(v_set))

 <class 'str'> 
 <class 'bool'> 
 <class 'str'>  
 <class 'float'> 
 <class 'int'> 
 <class 'dict'> 
 <class 'list'> 
 <class 'tuple'> 
 <class 'set'> 

 

 

 

 

 

1. ์ˆซ์žํ˜• 

<์ˆซ์žํ˜• ์—ฐ์‚ฐ์ž ์ฐธ๊ณ > 

 https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex

 

Built-in Types — Python 3.9.6 documentation

The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some collection classes are mutable. The methods that add, subtract,

docs.python.org

i1 = 39 
i2 = 939 
big_int1 = 999999999999999999999999999999999999999
big_int2 = 777777777777777777777777777777777777777
f1 = 1.234 
f2 = 3.1415
f3 = .5
f4 = 10.
print(i1 * i2)
print(big_int1 * big_int2)
print(f1 ** f2)
print(f3 + i2)

 36621 
 777777777777777777777777777777777777776222222222222222222222222222222222222223       
 1.93582713947248 
 939.5 

 

result = f3 + i2 
print(result, type(result))

 939.5 <class 'float'> 

int + float๋ฅผ ํ•˜๋ฉด ์ž๋™์ ์œผ๋กœ float ํƒ€์ž…์ด ๋œ๋‹ค. 

 

 

a = 5. 
b = 4
c = 10
print(type(a), type(b))
result2 = a + b 
print(result2)

 <class 'float'> <class 'int'> 
 9.0 

 

 

- ์ˆซ์žํ˜• ํ˜•๋ณ€ํ™˜ (์œ ์—ฐํ•œ ํ˜•๋ณ€ํ™˜ ๊ฐ€๋Šฅ) 

* int, float, complex(๋ณต์†Œ์ˆ˜ : ์ž˜ ์•ˆ์”€) 

print(int(result2))
print(float(c))
print(complex(3))
print(int(True))
print(int(False))
print(int('3'))
print(complex(False))

 9 
 10.0 
 (3+0j) 
 1 
 0 

True, False๋Š” ๊ฐ๊ฐ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด 1, 0 ์ด ๋œ๋‹ค. 
 3 
 0j 

์ด์ฒ˜๋Ÿผ ํŒŒ์ด์ฌ์€ ์œ ์—ฐํ•œ ํ˜•๋ณ€ํ™˜์„ ์ง€์›ํ•œ๋‹ค .

y = 100 
y += 100 
print(y)

 200 

 

 

 

 

<์ˆ˜์น˜ ์—ฐ์‚ฐ ํ•จ์ˆ˜ ์ฐธ๊ณ > 

https://docs.python.org/3/library/math.html

 

math — Mathematical functions — Python 3.9.6 documentation

math — Mathematical functions This module provides access to the mathematical functions defined by the C standard. These functions cannot be used with complex numbers; use the functions of the same name from the cmath module if you require support for co

docs.python.org

print(abs(-7))
n, m = divmod(100, 8)  # ๋ชซ, ๋‚˜๋จธ์ง€ 
print(n, m)

 7 

abs() ํ•จ์ˆ˜๋Š” ์ ˆ๋Œ“๊ฐ’์„ ์”Œ์›Œ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. 
 12 4 

divmod()ํ•จ์ˆ˜๋Š” ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ๋™์‹œ์— ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค. 

 

import math
print(math.ceil(5.1))  # ์ด ์ˆซ์ž๋ณด๋‹ค ํฌ๋ฉด์„œ ๊ฐ€์žฅ ์ž‘์€ ์ •์ˆ˜ 
print(math.floor(3.874))  # ์ด ์ˆซ์ž๋ณด๋‹ค ์ž‘์œผ๋ฉด์„œ ๊ฐ€์žฅ ํฐ ์ •์ˆ˜

 6 
 3 

math.ceil() : ์ธ์ž์— ๋“ค์–ด๊ฐ„ ์ˆซ์ž๋ณด๋‹ค ํฌ๋ฉด์„œ ๊ฐ€์žฅ ์ž‘์€ ์ •์ˆ˜ 

math.floor : ์ธ์ž์— ๋“ค์–ด๊ฐ„ ์ˆซ์ž๋ณด๋‹ค ์ž‘์œผ๋ฉด์„œ ๊ฐ€์žฅ ํฐ ์ •์ˆ˜ 

 

 

 

 

 

2. ๋ฌธ์ž์—ด 

str1 = "I am a geak!"
str2 = "NiceGirl"
str3 = ""
str4 = str()
print(len(str1), len(str2), len(str3), len(str4))  # ๊ณต๋ฐฑ ํฌํ•จ

 12 8 0 0 

๊ณต๋ฐฑ์„ ํฌํ•จํ•˜์—ฌ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” len() ํ•จ์ˆ˜

 

 

escape_str1 = "Do you have a \"big collection\"?"
print(escape_str1)
escape_str2 = "Tab\tTab\tTab"
print(escape_str2)

 Do you have a "big collection"? 

๊ฐ™์€ ์ข…๋ฅ˜ ๋”ฐ์˜ดํ‘œ๋ฅผ ์•ˆ์— ๋„ฃ๊ธฐ ์œ„ํ•˜์—ฌ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 

 Tab     Tab     Tab 

 

 

 

 

- Raw String

raw_s1 = r'C:\Programs\Test\Bin'
print(raw_s1)
raw_s2 = r"\\a\\a"
print(raw_s2)

 C:\Programs\Test\Bin 
 \\a\\a 

rwa string์œผ๋กœ ์ด์Šค์ผ€์ดํ”„๋ฌธ ์—†์ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

 

 

- ๋ฉ€ํ‹ฐ๋ผ์ธ : ๋‹ค์Œ์ค„๊ณผ ์ด์–ด์ง„๋‹ค. 

multi = \
"""
๋ฌธ์ž์—ด 
๋ฉ€ํ‹ฐ๋ผ์ธ 
ํ…Œ์ŠคํŠธ
"""
print(multi)

 ๋ฌธ์ž์—ด 
 ๋ฉ€ํ‹ฐ๋ผ์ธ 
 ํ…Œ์ŠคํŠธ 

 

 

 

 

 

- ๋ฌธ์ž์—ด ์—ฐ์‚ฐ 

str_o1 = '*'
str_o2 = 'abc'
str_o3 = 'def'
str_o4 = 'Niceman'
print(str_o1 * 100)
print(str_o2 + str_o3)
print(str_o1 * 3)
print('a' in str_o4)
print('f' in str_o4)
print('z' not in str_o4)

 **************************************************************************************************** 
 abcdef 
 *** 
 True 
 False 
 True 

๋ฌธ์ž์—ด์— ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๊ฑฐ๋‚˜, ๋ฌธ์ž์—ด๋ผ๋ฆฌ ๋”ํ•˜๋Š” ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

 

 

- ๋ฌธ์ž์—ด ํ˜•๋ณ€ํ™˜ 

print(str(77) + 'a')
print(str(10.4))

 77a 
 10.4 

 

 

 

 

 

 

 

- ๋ฌธ์ž์—ด ํ•จ์ˆ˜

<๋ฌธ์ž์—ด ํ•จ์ˆ˜ ์ฐธ๊ณ > 

https://www.w3schools.com/python/python_ref_string.asp

 

Python String Methods

Python String Methods Python has a set of built-in methods that you can use on strings. Note: All string methods returns new values. They do not change the original string. Method Description capitalize()Converts the first character to upper case casefold(

www.w3schools.com

 

a = 'niceman'
b = 'orange'
print(a.islower())  # ์†Œ๋ฌธ์ž์ธ์ง€ : Bool ๋ฐ˜ํ™˜
print(b.endswith('e'))  # e๋กœ ๋๋‚˜๋Š”์ง€ : Bool ๋ฐ˜ํ™˜
print(a.capitalize())  # ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ 
print(a.replace('nice', 'good'))  # nice๋ฅผ good์œผ๋กœ ๋Œ€์ฒดํ•ด๋ผ
print(list(reversed(b)))  # ๋’ค์ง‘์–ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•˜๋ผ

 True 

 True 
 Niceman 
 goodman 
 ['e', 'g', 'n', 'a', 'r', 'o'] 

 

 

 

 

 

 

 

- ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ 

a = 'niceman'
b = 'orange'
print(a[0:3])
print(a[0:4])
print(a[0:len(a)])
print(a[:4])
print(b[0:4:2])
print(b[1:-2])
print(b[::-1])

 nic 
 nice 
 niceman 
 nice 
 oa 
 ran 
 egnaro 

 a[0:n] ์ผ ๋•Œ 0๋ฒˆ์งธ ๊ธ€์ž๋ถ€ํ„ฐ n-1๋ฒˆ์งธ ๊ธ€์ž๊นŒ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

 

 

 

 

 

 

 

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ฐธ๊ณ  ๋งํฌ>

https://www.python-course.eu/python3_formatted_output.php

 

Python Tutorial: Formatted Output

Even though it may look so, the formatting is not part of the print function. If you have a closer look at our examples, you will see that we passed a formatted string to the print function. Or to put it in other words: If the string modulo operator is app

www.python-course.eu

 

 

 

 

1. ๊ธฐ๋ณธ ์ถœ๋ ฅ 

print('Hello Python!')
print("Hello Python!")
print("""Hello Python!""")
print('''Hello Python!''')

 Hello Python! 
 Hello Python! 
 Hello Python! 
 Hello Python! 

์ž‘์€๋”ฐ์˜ดํ‘œ, ํฐ๋”ฐ์˜ดํ‘œ 1๊ฐœ ํ˜น์€ 3๊ฐœ๋กœ ๊ฐ์‹ธ์ค€๋‹ค. 

 

 

 

 

 

 

 

2. seperator ์˜ต์…˜ ์‚ฌ์šฉ 

print('T', 'E', 'S', 'T', sep='')
print('2019', '02', '19', sep='-')
print('niceman', 'google.com', sep="@")

 TEST 
 2019-02-19 
 niceman@google.com 

printํ•จ์ˆ˜์˜ ์ธ์ž์— sep= ์˜ต์…˜์„ ๋„ฃ์–ด์ฃผ๋ฉด sep์— ๋“ค์–ด๊ฐ€๋Š” ๋ฌธ์ž๊ฐ€ ์ค‘๊ฐ„์— ๋“ค์–ด ๊ฐ€ ๋ฌธ์ž์—ด์„ ์ด์–ด ์ค€๋‹ค. 

 

 

 

 

 

 

 

3. end ์˜ต์…˜ ์‚ฌ์šฉ 

print('Welcom To', end=' ')
print('the black prade', end=' ')
print('piano notes')

 Welcom To the black prade piano notes 

end์˜ต์…˜์€ defalut๊ฐ€ \n์œผ๋กœ ๋“ค์–ด ๊ฐ€ ์žˆ์–ด ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ ์ค„๋ฐ”๊ฟˆ์ด ๋์—ˆ๋‹ค. end=' ' ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๊ทธ ๋‹ค์Œ ์ค„์ด ํ˜ธ์ถœ๋  ๋•Œ ๋„์–ด ์“ฐ๊ธฐ ํ•œ ๋ฒˆ ํ›„ ์ถœ๋ ฅ์ด ๋œ๋‹ค. 

 

 

 

 

 

 

 

4. format ์‚ฌ์šฉ  { }. ( ) 

print('{} and {}'.format('You', 'Me'))
print("{0} and {1} and {0}".format('You', 'Me'))
print('{a} and {b}'.format(a='You', b='Me'))

 You and Me 
 You and Me and You 
 You and Me 

format ํ•จ์ˆ˜๋กœ ๋ฌธ์ž์—ด์— ๊ฐ’์„ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

print("%s favorite number is %d" % ('Silver', 7))
print("Test1: %5d, Price: %4.2f" % (776, 6534.123))

 Silver favorite number is 7 
 Test1:   776, Price: 6534.12 

* %s : ๋ฌธ์ž, %d : ์ •์ˆ˜, %f : ์‹ค์ˆ˜

%๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€์ž์ˆ˜๋‚˜, type์— ๋Œ€ํ•ด ๋ช…์‹œ์ ์œผ๋กœ ๊ธฐ์ž…ํ•ด ๋†“์„ ์ˆ˜ ์žˆ๋‹ค. 

 

print("Test1: {0: 5d}, Price: {1: 4.2f}".format(776, 6534.123))
print('Test1: {a: 5d}, Price: {b: 4.2f}'.format(a=776, b=6534.123))

 Test1:   776, Price:  6534.12 
 Test1:   776, Price:  6534.12 

์œ„์™€ ๊ฐ™์ด format์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€์ž์ˆ˜์™€ type์„ ์ง€์ •ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

5. ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž 

print("'you'")
print('\'you\'')
print('"you"')
print("""'you'""")
print('\\you\\\n')
print('\t\t\ttest')

 'you' 

์ข…๋ฅ˜๊ฐ€ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด์ฒ˜๋Ÿผ ์•ˆ์— ์žˆ๋Š” ๋”ฐ์˜ดํ‘œ๋กœ ๋‚˜์˜ค๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 'you' 

์ข…๋ฅ˜๊ฐ€ ๊ฐ™์€ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. 

 "you" 
 'you' 

 \you\ 

                              
                        test 

\\, \n, \t ์„ ์‚ฌ์šฉํ•˜์—ฌ \, ์ค„๋ฐ”๊ฟˆ, ํƒญ์„ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋”ฐ. 

 

 

* ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž 

\n : ๊ฐœํ–‰

\t : ํƒญ

\\ : ๋ฌธ์ž

\' : ๋ฌธ์ž

\" : ๋ฌธ์ž

\r : ์บ๋ฆฌ์ง€ ๋ฆฌํ„ด

\f : ํผ ํ”ผ๋“œ

\a : ๋ฒจ ์†Œ๋ฆฌ

\b : ๋ฐฑ ์ŠคํŽ˜์ด์Šค

\000 : ๋„ ๋ฌธ์ž

...

 

 

 

 

 

 

 

 

+ Recent posts