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

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/43

 

[python ๊ธฐ์ดˆ] 11. ์˜ˆ์™ธ ์ข…๋ฅ˜์™€ ์ฒ˜๋ฆฌ , try , except , else, raise

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

silvercoding.tistory.com

 

 

 

import csv

์šฐ์„  ํŒŒ์ด์ฌ์—์„œ csv๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด import ํ•ด์ค€๋‹ค. 

 

 

 

 

- ์˜ˆ์ œ 1 

with open('./resource/sample1.csv', 'r') as f:
    reader = csv.reader(f)
    # next(reader) # Header ์Šคํ‚ต
    
    # ํ™•์ธ 
    print(reader)
    print(type(reader))
    print(dir(reader))
    print()

    for c in reader:
        print(c)  # ํ•˜๋‚˜์˜ row๊ฐ€ ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ๋‚˜์˜จ๋‹ค.

<_csv.reader object at 0x0000013CD5F8E868>
<class '_csv.reader'>
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'dialect', 'line_num']

 

['๋ฒˆํ˜ธ', '์ด๋ฆ„', '๊ฐ€์ž…์ผ์‹œ', '๋‚˜์ด']
['1', '๊น€์ •์ˆ˜', '2017-01-19 11:30:00', '25']
['2', '๋ฐ•๋ฏผ๊ตฌ', '2017-02-07 10:22:00', '35']
['3', '์ •์ˆœ๋ฏธ', '2017-01-22 09:10:00', '33']
['4', '๊น€์ •ํ˜„', '2017-02-22 14:09:00', '45']
['5', 'ํ™๋ฏธ์ง„', '2017-04-01 18:00:00', '17']
['6', '๊น€์ˆœ์ฒ ', '2017-05-14 22:33:07', '22']
['7', '์ด๋™์ฒ ', '2017-03-01 23:44:45', '27']
['8', '๋ฐ•์ง€์ˆ™', '2017-01-11 06:04:18', '30']
['9', '๊น€์€๋ฏธ', '2017-02-08 07:44:33', '51']
['10', '์žฅํ˜์ฒ ', '2017-12-01 13:01:11', '16']

์—ฌ๊ธฐ์„œ header๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์œ„์˜ ์ฃผ์„์œผ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ๋Š” next(reader)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

 

 

 

- ์˜ˆ์ œ 2 : delimiter 

vs๋กœ ์—ฌ๋‹ˆ๊นŒ ๊ธ€์”จ๊ฐ€ ๊นจ์ง€์ง€๋งŒ ์ฝค๋งˆ๊ฐ€ ์•„๋‹Œ |๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋Š” csvํŒŒ์ผ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌ๋ถ„์ž๋ฅผ , ์—์„œ | ๋กœ ๋ฐ”๊พธ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

with open('./resource/sample2.csv', 'r') as f:
    reader = csv.reader(f, delimiter='|')
    # next(reader) # Header ์Šคํ‚ต

    # ํ™•์ธ 
    print(reader)
    print(type(reader))
    print(dir(reader))
    print()

    for c in reader:
        print(c)

<_csv.reader object at 0x0000013CD5EA5798>
<class '_csv.reader'>
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'dialect', 'line_num']

['๋ฒˆํ˜ธ', '์ด๋ฆ„', '๊ฐ€์ž…์ผ์‹œ', '๋‚˜์ด']
['1', '๊น€์ •์ˆ˜', '2017-01-19 11:30:00', '25']
['2', '๋ฐ•๋ฏผ๊ตฌ', '2017-02-07 10:22:00', '35']
['3', '์ •์ˆœ๋ฏธ', '2017-01-22 09:10:00', '33']
['4', '๊น€์ •ํ˜„', '2017-02-22 14:09:00', '45']
['5', 'ํ™๋ฏธ์ง„', '2017-04-01 18:00:00', '17']
['6', '๊น€์ˆœ์ฒ ', '2017-05-14 22:33:07', '22']
['7', '์ด๋™์ฒ ', '2017-03-01 23:44:45', '27']
['8', '๋ฐ•์ง€์ˆ™', '2017-01-11 06:04:18', '30']
['9', '๊น€์€๋ฏธ', '2017-02-08 07:44:33', '51']
['10', '์žฅํ˜์ฒ ', '2017-12-01 13:01:11', '16']

delimiter = '|' ๋ฅผ ์‚ฌ์šฉํ–ˆ๋”๋‹ˆ ์ž˜ ๋‚˜๋ˆ„์–ด์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

- ์˜ˆ์ œ 3 : dictionary๋กœ ์ฝ์–ด์˜ค๊ธฐ => DictReader

with open('./resource/sample1.csv', 'r') as f:
    reader = csv.DictReader(f)

    for c in reader:
        for k, v in c.items():
            print(k, v)
        print("------------------")

๋ฒˆํ˜ธ 1
์ด๋ฆ„ ๊น€์ •์ˆ˜
๊ฐ€์ž…์ผ์‹œ 2017-01-19 11:30:00
๋‚˜์ด 25
------------------
๋ฒˆํ˜ธ 2
์ด๋ฆ„ ๋ฐ•๋ฏผ๊ตฌ
๊ฐ€์ž…์ผ์‹œ 2017-02-07 10:22:00
๋‚˜์ด 35
------------------
๋ฒˆํ˜ธ 3
์ด๋ฆ„ ์ •์ˆœ๋ฏธ
๊ฐ€์ž…์ผ์‹œ 2017-01-22 09:10:00
๋‚˜์ด 33
------------------
๋ฒˆํ˜ธ 4
์ด๋ฆ„ ๊น€์ •ํ˜„
๊ฐ€์ž…์ผ์‹œ 2017-02-22 14:09:00
๋‚˜์ด 45
------------------
๋ฒˆํ˜ธ 5
์ด๋ฆ„ ํ™๋ฏธ์ง„
๊ฐ€์ž…์ผ์‹œ 2017-04-01 18:00:00
๋‚˜์ด 17
------------------
๋ฒˆํ˜ธ 6
์ด๋ฆ„ ๊น€์ˆœ์ฒ 
๊ฐ€์ž…์ผ์‹œ 2017-05-14 22:33:07
๋‚˜์ด 22
------------------
๋ฒˆํ˜ธ 7
์ด๋ฆ„ ์ด๋™์ฒ 
๊ฐ€์ž…์ผ์‹œ 2017-03-01 23:44:45
๋‚˜์ด 27
------------------
๋ฒˆํ˜ธ 8
์ด๋ฆ„ ๋ฐ•์ง€์ˆ™
๊ฐ€์ž…์ผ์‹œ 2017-01-11 06:04:18
๋‚˜์ด 30
------------------
๋ฒˆํ˜ธ 9
์ด๋ฆ„ ๊น€์€๋ฏธ
๊ฐ€์ž…์ผ์‹œ 2017-02-08 07:44:33
๋‚˜์ด 51
------------------

DictReader๋ฅผ ํ†ตํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ฝ์–ด์˜ค๋ฉด , 

OrderedDict([('๋ฒˆํ˜ธ', '1'), ('์ด๋ฆ„', '๊น€์ •์ˆ˜'), ('๊ฐ€์ž…์ผ์‹œ', '2017-01-19 11:30:00'), ('๋‚˜์ด', '25')])

์ด๋Ÿฐ ์‹์œผ๋กœ ํ•˜๋‚˜์˜ ์ค„๋งˆ๋‹ค OrderedDict๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋กœ key์™€ value๋ฅผ ๋ฝ‘์•„๋‚ด์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. 

 

 

 

- ์˜ˆ์ œ 4 : writerow

w = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]

with open('./resource/sample3.csv', 'w', newline='') as f:
    wt = csv.writer(f)

    for v in w:
        wt.writerow(v)  # ํ•˜๋‚˜ํ•˜๋‚˜ ๊ฒ€์ˆ˜ํ•ด์„œ ์“ธ ๋–„ (if๊ฐ™์€ ์กฐ๊ฑด์ด ๋“ค์–ด๊ฐˆ ๋•Œ )

writerow๋กœ ํ•œ์ค„์”ฉ ๊ฒ€์ˆ˜ํ•ด๊ฐ€๋ฉฐ ์ž‘์„ฑ์„ ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

- ์˜ˆ์ œ 5 : writerows 

with open('./resource/sample4.csv', 'w', newline='') as f:
    wt = csv.writer(f)
    wt.writerows(w)  # ์•„์— ํ•œ๋ฒˆ์— ์“ฐ๋Š” ๊ฒƒ

ํ•œ๋ฒˆ์— ์“ฐ๋Š” writerowsํ•จ์ˆ˜๋„ ์กด์žฌํ•œ๋‹ค. for๋ฌธ์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. 

 

 

 

- ์˜ˆ์ œ 6 : excel

* XSL, XLSX 

: openpyxl, xlswriter, xlrd, xlwt, xlutils 

: pandas ๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉ (openpyxl, xlrd) 

 

* ์„ค์น˜ (pandas, openpyxl, xlrd) 

- pip install xlrd

- pip install openpyxl

- pip install pandas 

 

 

* ๋ถˆ๋Ÿฌ์˜ค๊ธฐ 

import pandas as pd 

xlsx = pd.read_excel('./resource/sample.xlsx')

pandas๋ฅผ importํ•˜๊ณ , read_excel์„ ์ด์šฉํ•˜์—ฌ ์—‘์…€ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์™€ ์ค€๋‹ค. 

 

 

* ์ƒ์œ„ ๋ฐ์ดํ„ฐ ํ™•์ธ 

print(xlsx.head())

  Sap Co.      ๋Œ€๋ฆฌ์  ์˜์—…์‚ฌ์›       ์ „์›”       ๊ธˆ์›”  TEAM  ์ด ํŒ๋งค์ˆ˜๋Ÿ‰
0  KI1316  ๊ฒฝ๊ธฐ์ˆ˜์›๋Œ€๋ฆฌ์   ์ด๊ธฐ์ •  1720000  2952000     1     123
1  KI1451  ์ถฉ์ฒญํ™์„ฑ๋Œ€๋ฆฌ์   ์ •๋ฏธ์ง„  4080000  2706000     2     220
2  KI1534  ๊ฒฝ๊ธฐํ™”์„ฑ๋Œ€๋ฆฌ์   ๊ฒฝ์ธ์„    600000  2214000     1     320
3  KI1636  ๊ฐ•์›์†์ดˆ๋Œ€๋ฆฌ์   ์ด๋™๊ถŒ  3720000  2870000     3     110
4  KI1735  ๊ฒฝ๊ธฐ์•ˆ์–‘๋Œ€๋ฆฌ์   ๊ฐ•์ค€์„  4800000  2296000     1     134

๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ€์žฅ ์œ„์˜ 5๊ฐœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚˜์˜จ๋‹ค. 

 

 

* ํ•˜์œ„ ๋ฐ์ดํ„ฐ ํ™•์ธ 

print(xlsx.tail())

   Sap Co.       ๋Œ€๋ฆฌ์  ์˜์—…์‚ฌ์›       ์ „์›”       ๊ธˆ์›”  TEAM  ์ด ํŒ๋งค์ˆ˜๋Ÿ‰
15  KI2870  ๊ฒฝ๊ธฐ๊ตฌ๋ฆฌ์‹œ๋Œ€๋ฆฌ์   ๋ฐ•์ง„ํ˜•  6000000  3400000     2     143
16  KI2910   ๊ฐ•์›์ถ˜์ฒœ๋Œ€๋ฆฌ์   ๊น€์€ํ–ฅ  4800000  4896000     1     176
17  KI3030   ๊ฐ•์›์˜๋™๋Œ€๋ฆฌ์   ์ „์ˆ˜์ฐฝ  4560000  3128000     2      98
18  KI3131   ๊ฒฝ๊ธฐํ•˜๋‚จ๋Œ€๋ฆฌ์   ๊น€๋ฏผ์ •  2750000  7268000     3     293
19  KI3252   ๊ฐ•์›ํฌ์ฒœ๋Œ€๋ฆฌ์   ์„œ๊ฐ€์€  2420000  4740000     4     240

ํ•˜์œ„ 5๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. 

 

 

* ๋ฐ์ดํ„ฐ์˜ ๊ตฌ์กฐ 

print(xlsx.shape) # ํ–‰, ์—ด

 (20, 7) 

shape๋ฅผ ์ด์šฉํ•˜์—ฌ ํ–‰๊ณผ ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

* ์—‘์…€ or CSV ๋‹ค์‹œ ์“ฐ๊ธฐ 

xlsx.to_excel('./resource/result.xlsx', index=False)
xlsx.to_csv('./resource/result.csv', index=False)

์ด๋ ‡๊ฒŒ to_excel๊ณผ to_csv๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 

 

 

 

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

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/42

 

[python ๊ธฐ์ดˆ] 10. ํŒŒ์ผ ์ฝ๊ธฐ , ์“ฐ๊ธฐ (open)

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

silvercoding.tistory.com

 

 

* ๋ฌธ๋ฒ•์ ์œผ๋กœ ์—๋Ÿฌ๊ฐ€ ์—†์ง€๋งŒ, ์ฝ”๋“œ ์‹คํ–‰ (๋Ÿฐํƒ€์ž„) ํ”„๋กœ์„ธ์Šค์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋„ ์ค‘์š”ํ•˜๋‹ค. 

* (cf) linter : ์ฝ”๋“œ ์Šคํƒ€์ผ, ๋ฌธ๋ฒ• ์ฒดํฌ 

 

 

์˜ค๋Š˜์€ ๋ฐ”๋ณด์ฝ”๋”ฉ์„ ํ•˜๋ฉด์„œ ์˜ค๋ฅ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž! 

 

 

 [ ์˜ˆ์™ธ ์ข…๋ฅ˜ ] 

(1) SyntaxError : ์ž˜๋ชป๋œ ๋ฌธ๋ฒ• 

print('Test)

 SyntaxError: EOL while scanning string literal 

if True
    pass

 SyntaxError: invalid syntax 

x => y

 SyntaxError: invalid syntax 

 

 

(2) NameError : ์ฐธ์กฐ๋ณ€์ˆ˜ ์—†์Œ 

a = 10 
b = 15 

print(c)

 NameError: name 'c' is not defined 

 

 

(3) ZeroDivisionError : 0 ๋‚˜๋ˆ„๊ธฐ ์—๋Ÿฌ 

print(10 / 0)

 ZeroDivisionError: division by zero 

 

 

(4) IndexError : ์ธ๋ฑ์Šค ๋ฒ”์œ„ ์˜ค๋ฒ„ 

x = [10, 20, 30]
print(x[0])
print(x[3]) # ์˜ˆ์™ธ ๋ฐœ์ƒ

 10 

 IndexError: list index out of range 

 

 

(5) KeyError : ์ฃผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ๋ฐœ์ƒ 

dic = {'name': 'Kim', 'Age': 33, 'city': 'Seoul'}
print(dic['hobby'])

 KeyError: 'hobby' 

print(dic.get('hobby'))  # ๊ถŒ์žฅ

 None 

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  None์„ ๋ฐ˜ํ™˜ํ•˜๋Š” getํ•จ์ˆ˜ ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•œ๋‹ค. 

 

 

(6) AttributeError : ๋ชจ๋“ˆ , ํด๋ž˜์Šค์— ์žˆ๋Š” ์ž˜๋ชป๋œ ์†์„ฑ ์‚ฌ์šฉ ์‹œ 

import time
print(time.time())
print(time.month())

 1627887313.5666506 
Traceback (most recent call last):
  File "c:/Users/./Desktop/python_study/python_easy/section10.py", line 45, in <module>
    print(time.month())
 AttributeError: module 'time' has no attribute 'month' 

 

 

(7) ValueError

x = [1, 5, 9]
x.remove(10)

 ValueError: list.remove(x): x not in list 

x.index(10)

 ValueError: 10 is not in list 

 

 

(8) FileNotFoundError 

f = open('test.txt', 'r')  # ์˜ˆ์™ธ ๋ฐœ์ƒ

 FileNotFoundError: [Errno 2] No such file or directory: 'test.txt' 

 

 

(9) TypeError 

x = [1, 2]
y = (1, 2)
z = 'test'

print(x + y)

 TypeError: can only concatenate list (not "tuple") to list 

print(x + z)

 TypeError: can only concatenate list (not "str") to list 

print(x + list(y))

 [1, 2, 1, 2] 

๋ฆฌ์ŠคํŠธ๋Š” ๋ฆฌ์ŠคํŠธ๋ผ๋ฆฌ๋งŒ ํ•ฉ์น  ์ˆ˜ ์žˆ๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๊ณ , ์œ„์™€ ๊ฐ™์ด list๋กœ ํ˜•๋ณ€ํ™˜์„ ํ•ด์ฃผ๋ฉด ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. 

 

 

 

 

 

* ํ•ญ์ƒ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•˜๊ณ  ๋จผ์ € ์ฝ”๋”ฉ 

-> ๊ทธ ํ›„ ๋Ÿฐํƒ€์ž„ ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฝ”๋”ฉ ๊ถŒ์žฅ ( EAFP ์ฝ”๋”ฉ ์Šคํƒ€์ผ ) 

 

 

 

 

 

[ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ธฐ๋ณธ ] 

* try : ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ์ฝ”๋“œ ์‹คํ–‰ (์—ฌ๊ธฐ์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋ฉด except๋กœ ๊ฐ„๋‹ค. ) 

* except : ์—๋Ÿฌ๋ช… 

* else : ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ์‹คํ–‰ 

* finally : ํ•ญ์ƒ ์‹คํ–‰ 

 

- ์˜ˆ์ œ 1 

name = ['Kim', 'Lee', 'Park']

try: 
    z = 'Kim'  # Cho : ์˜ˆ์™ธ ๋ฐœ์ƒ 
    x = name.index(z)
    print('{} Found it! in name'.format(z, x + 1))

except ValueError:
    print('Not found it! - Occured ValueError!')
else:
    print('OK! else!')

 Kim Found it! in name 

 OK! else! 

name = ['Kim', 'Lee', 'Park']

try: 
    z = 'Cho' 
    x = name.index(z)
    print('{} Found it! in name'.format(z, x + 1))

except ValueError:
    print('Not found it! - Occured ValueError!')
else:
    print('OK! else!')

 Not found it! - Occured ValueError! 

 

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— except ๋ธ”๋Ÿญ์ด ์‹คํ–‰๋˜์—ˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 2 

try: 
    z = 'jim' 
    x = name.index(z)
    print('{} Found it! in name'.format(z, x + 1))

except:
    print('Not found it! - Occured Error!')
else:
    print('OK! else!')

 Not found it! - Occured Error! 

 

 

- ์˜ˆ์ œ 3 

try: 
    z = 'Kim' 
    x = name.index(z)
    print('{} Found it! in name'.format(z, x + 1))

except:
    print('Not found it! - Occured Error!')
else:
    print('OK! else!')
finally:
    print("finally ok !")

 Kim Found it! in name 
 OK! else! 
 finally ok ! 

finally๋Š” ๋ฌด์กฐ๊ฑด ์‹คํ–‰๋œ๋‹ค. 

 

 

- ์˜ˆ์ œ 4 : ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ํ•˜์ง€ ์•Š์ง€๋งŒ , ๋ฌด์กฐ๊ฑด ์ˆ˜ํ–‰๋˜๋Š” ์ฝ”๋”ฉ ํŒจํ„ด 

try:
    print('try')
finally:
    print("ok finally!!!")

 try 
 ok finally!!! 

 

 

- ์˜ˆ์ œ 5 : ์—ฌ๋Ÿฌ ๊ฐœ์˜ except ๋ธ”๋Ÿญ 

try: 
    z = 'Kim' 
    x = name.index(z)
    print('{} Found it! in name'.format(z, x + 1))
# except Exception:  # ์ด๊ฑธ ์ฒ˜์Œ์— ๋†“์œผ๋ฉด ์•ˆ๋จ ! 
#     print('Not found it! - Occured Error!')
except ValueError:
    print('Not found it! - Occured ValueError!')
except IndexError:
    print('Not found it! - Occured IndexError!')
except Exception:
    print('Not found it! - Occured Error!')
else:
    print('OK! else!')
finally:
    print("finally ok !")

 Kim Found it! in name 
 OK! else! 
 finally ok ! 

์œ„์™€ ๊ฐ™์ด ์—๋Ÿฌ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์žก์•„์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด ๋•Œ Exception์„ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฐ‘์— ์ž์„ธํ•œ ์—๋Ÿฌ๋Š” ๋‚˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์œ„์น˜ ์„ ์ •์„ ์ฃผ์˜ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ( Exception์ด ํฐ ๋ฒ”์œ„์ด๊ธฐ ๋•Œ๋ฌธ ) 

 

 

- ์˜ˆ์ œ 6 

* ์˜ˆ์™ธ ๋ฐœ์ƒ : raise ( raise ํ‚ค์›Œ๋“œ๋กœ ์˜ˆ์™ธ ์ง์ ‘ ๋ฐœ์ƒ ) 

try: 
    a = 'Jim'
    if a == 'Kim':
        print('OK ํ—ˆ๊ฐ€!')
    else:
        raise ValueError
except ValueError:
    print('๋ฌธ์ œ๋ฐœ์ƒ!')
except Exception as f: 
    print(f)
else:
    print('oK!')

 ๋ฌธ์ œ๋ฐœ์ƒ! 

raise๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง์ ‘ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” ValueError๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ผ๊ณ  ํ•˜์˜€์œผ๋ฏ€๋กœ except ValueError๋ถ€๋ถ„์ด ์‹คํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

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

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/41

 

[python ๊ธฐ์ดˆ] 9. ๋ชจ๋“ˆ๊ณผ ํŒจํ‚ค์ง€

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

silvercoding.tistory.com

 

 

<์ฐธ๊ณ > 

https://docs.python.org/3.7/library/functions.html#open

 

Built-in Functions — Python 3.7.11 documentation

Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order. abs(x) Return the absolute value of a number. The argument may be an integer or a floating po

docs.python.org

 

 

 

* ์ฝ๊ธฐ๋ชจ๋“œ : r / ์“ฐ๊ธฐ๋ชจ๋“œ (๊ธฐ์กด ํŒŒ์ผ ์‚ญ์ œ) : w / ์ถ”๊ฐ€๋ชจ๋“œ (ํŒŒ์ผ ์ƒ์„ฑ ๋˜๋Š” ์ถ”๊ฐ€ ) 

* ๋ณธ ํฌ์ŠคํŒ…์˜ txt ํŒŒ์ผ : ํŒจ์ŠคํŠธ ์บ ํผ์Šค ์ œ๊ณต (resource - *.txt) 

 

 

 

1. ํŒŒ์ผ ์ฝ๊ธฐ 

- ์˜ˆ์ œ 1 

f = open('./resource/review.txt', 'r')
content = f.read()
print(content)
print(dir(f))
# ๋ฐ˜๋“œ์‹œ close ๋ฆฌ์†Œ์Šค ๋ฐ˜ํ™˜ 
f.close()

The film, projected in the form of animation, 
imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, 
which eventually paves the path for gaining a fresh perspective on an age-old problem. 
The story also happens to centre around two parallel characters, Shundi King and Hundi King, 
who are twins, but they constantly fight over unresolved issues planted in their minds 
by external forces from within their very own units. 

 ['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', 
'__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 'writelines'] 

open์˜ ์ธ์ž์— 'r'์„ ์ž‘์„ฑํ•˜์—ฌ ์ฝ๊ธฐ๋ชจ๋“œ๋กœ txtํŒŒ์ผ์„ ๊ฐ€์ ธ ์˜จ๋‹ค. dir์„ ์ถœ๋ ฅํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋“ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 2 : close ์ƒ๋žต ๊ฐ€๋Šฅ 

with open('./resource/review.txt', 'r') as f:
    c = f.read()
    print(c)
    print(list(c))
    print(iter(c))

The film, projected in the form of animation,
imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues,
which eventually paves the path for gaining a fresh perspective on an age-old problem.
The story also happens to centre around two parallel characters, Shundi King and Hundi King,
who are twins, but they constantly fight over unresolved issues planted in their minds
by external forces from within their very own units.
['T', 'h', 'e', ' ', 'f', 'i', 'l', 'm', ',', ' ', 'p', 'r', 'o', 'j', 'e', 'c', 't', 'e', 'd', ' ', 'i', 'n', ' ', 't', 'h', 'e', ' ', 'f', 'o', 'r', 'm', ' ', 'o', 'f', ' ', 'a', 'n', 'i', 'm', 'a', 't', 'i', 'o', 'n', ',', '\n', 'i', 'm', 'p', 'a', 
'r', 't', 's', ' ', 't', 'h', 'e', ' ', 'l', 'e', 's', 's', 'o', 'n', ' ', 'o', 'f', ' ', 'h', 'o', 'w', ' ', 'w', 'a', 'r', 's', ' ', 'c', 'a', 'n', ' ', 'b', 'e', ' ', 'e', 'l', 'u', 'd', 'e', 'd', ' ', 't', 'h', 'r', 'o', 'u', 'g', 'h', ' ', 'r', 'e', 'a', 's', 'o', 'n', 'i', 'n', 'g', ' ', 'a', 'n', 'd', ' ', 'p', 'e', 'a', 'c', 'e', 'f', 'u', 'l', ' ', 'd', 'i', 'a', 'l', 'o', 'g', 'u', 'e', 's', ',', '\n', 'w', 'h', 'i', 'c', 'h', ' ', 'e', 'v', 'e', 'n', 't', 'u', 'a', 'l', 'l', 'y', ' ', 'p', 'a', 'v', 'e', 's', ' ', 't', 'h', 'e', ' ', 'p', 'a', 't', 'h', ' ', 'f', 'o', 'r', ' ', 'g', 'a', 'i', 'n', 'i', 'n', 'g', ' ', 'a', ' ', 'f', 'r', 'e', 's', 'h', ' ', 'p', 'e', 'r', 's', 'p', 'e', 'c', 't', 'i', 'v', 'e', ' ', 'o', 'n', ' ', 'a', 
'n', ' ', 'a', 'g', 'e', '-', 'o', 'l', 'd', ' ', 'p', 'r', 'o', 'b', 'l', 'e', 'm', '.', '\n', 'T', 'h', 'e', ' ', 's', 't', 'o', 'r', 'y', ' ', 'a', 'l', 's', 'o', ' 
', 'h', 'a', 'p', 'p', 'e', 'n', 's', ' ', 't', 'o', ' ', 'c', 'e', 'n', 't', 'r', 'e', ' ', 'a', 'r', 'o', 'u', 'n', 'd', ' ', 't', 'w', 'o', ' ', 'p', 'a', 'r', 'a', 
'l', 'l', 'e', 'l', ' ', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's', ',', ' ', 'S', 'h', 'u', 'n', 'd', 'i', ' ', 'K', 'i', 'n', 'g', ' ', 'a', 'n', 'd', ' ', 'H', 'u', 'n', 'd', 'i', ' ', 'K', 'i', 'n', 'g', ',', '\n', 'w', 'h', 'o', ' ', 'a', 'r', 'e', ' ', 't', 'w', 'i', 'n', 's', ',', ' ', 'b', 'u', 't', ' ', 't', 'h', 'e', 
'y', ' ', 'c', 'o', 'n', 's', 't', 'a', 'n', 't', 'l', 'y', ' ', 'f', 'i', 'g', 'h', 't', ' ', 'o', 'v', 'e', 'r', ' ', 'u', 'n', 'r', 'e', 's', 'o', 'l', 'v', 'e', 'd', ' ', 'i', 's', 's', 'u', 'e', 's', ' ', 'p', 'l', 'a', 'n', 't', 'e', 'd', ' ', 'i', 'n', ' ', 't', 'h', 'e', 'i', 'r', ' ', 'm', 'i', 'n', 'd', 's', '\n', 'b', 'y', 
' ', 'e', 'x', 't', 'e', 'r', 'n', 'a', 'l', ' ', 'f', 'o', 'r', 'c', 'e', 's', ' ', 'f', 'r', 'o', 'm', ' ', 'w', 'i', 't', 'h', 'i', 'n', ' ', 't', 'h', 'e', 'i', 'r', ' ', 'v', 'e', 'r', 'y', ' ', 'o', 'w', 'n', ' ', 'u', 'n', 'i', 't', 's', '.']   
 <str_iterator object at 0x00000230D67D2160> 

์œ„์™€ ๊ฐ™์ด with๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด close๋ฅผ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค. with๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด close๋ฅผ ๊ผญ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง๊ธฐ. 

 

 

- ์˜ˆ์ œ 3 

with open('./resource/review.txt', 'r') as f:
    for c in f:
        print(c.strip())

 The film, projected in the form of animation, 
 imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, 
 which eventually paves the path for gaining a fresh perspective on an age-old problem. 
 The story also happens to centre around two parallel characters, Shundi King and Hundi King, 
 who are twins, but they constantly fight over unresolved issues planted in their minds 
 by external forces from within their very own units. 

for๋ฌธ์œผ๋กœ ์ถœ๋ ฅํ•ด๋ณผ ์ˆ˜ ๋„ ์žˆ๋‹ค. stripํ•จ์ˆ˜๋กœ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•œ ๋’ค ์ถœ๋ ฅํ•ด ์ค€๋‹ค. 

 

 

- ์˜ˆ์ œ 4 

with open('./resource/review.txt', 'r') as f:
    content = f.read()
    print('>>>', content)
    content = f.read()  # ๋‚ด์šฉ ์—†์Œ 
    print('>>>', content)

 >>> The film, projected in the form of animation, 
 imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, 
 which eventually paves the path for gaining a fresh perspective on an age-old problem. 
 The story also happens to centre around two parallel characters, Shundi King and Hundi King, 
 who are twins, but they constantly fight over unresolved issues planted in their minds 
 by external forces from within their very own units. 
 >>> 

์ด๋ ‡๊ฒŒ ๋ชจ๋“  line์„ ์ถœ๋ ฅํ•˜๊ณ  ๋‚˜๋ฉด, ์ค‘๋ณตํ•ด์„œ line์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†๋‹ค. ์ด๋ฏธ ์ปค์„œ๊ฐ€ ๋๊นŒ์ง€ ๊ฐ”๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

 

- ์˜ˆ์ œ 5 : readline() 

with open('./resource/review.txt', 'r') as f:
    line = f.readline()
    # print(line)
    while line:
        print(line, end='####')
        line = f.readline()

The film, projected in the form of animation,
####imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues,
####which eventually paves the path for gaining a fresh perspective on an age-old problem.
####The story also happens to centre around two parallel characters, Shundi King and Hundi King,
####who are twins, but they constantly fight over unresolved issues planted in their minds
####by external forces from within their very own units.####

readline()์€ ํ•œ์ค„์”ฉ ๋ฐ˜ํ™˜ ํ•ด ์ค€๋‹ค. 

 

 

- ์˜ˆ์ œ 6 : readlines() 

with open('./resource/review.txt', 'r') as f:
    contents = f.readlines()
    print(contents)
    for c in contents:
        print(c, end=" ****** ")

['The film, projected in the form of animation,\n', 'imparts the lesson of how wars 
can be eluded through reasoning and peaceful dialogues,\n', 'which eventually paves 
the path for gaining a fresh perspective on an age-old problem.\n', 'The story also 
happens to centre around two parallel characters, Shundi King and Hundi King,\n', 'who are twins, but they constantly fight over unresolved issues planted in their minds\n', 'by external forces from within their very own units.']
The film, projected in the form of animation,
 ****** imparts the lesson of how wars can be eluded through reasoning and peaceful 
dialogues,
 ****** which eventually paves the path for gaining a fresh perspective on an age-old problem.
 ****** The story also happens to centre around two parallel characters, Shundi King and Hundi King,
 ****** who are twins, but they constantly fight over unresolved issues planted in their minds
 ****** by external forces from within their very own units. ******

readlines๋Š” ๋ชจ๋“  ๋ฌธ์žฅ์„ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ค€๋‹ค. 

 

 

- ์˜ˆ์ œ 7 

score = []

with open('./resource/score.txt', 'r') as f:
    for line in f:
        score.append(int(line))
    print(score)
    
print('Average : {:6.3}'.format(sum(score)/len(score)))

 [95, 78, 92, 89, 100, 66] 
 Average :   86.7 

ํ˜•๋ณ€ํ™˜์„ ํ•˜๊ณ , ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ด์„œ ํ‰๊ท ์„ ๊ตฌํ•ด์ฃผ๋Š” ์˜ˆ์ œ์ด๋‹ค. 

 

 

 

 

 

 

 

 

 

2. ํŒŒ์ผ ์“ฐ๊ธฐ 

- ์˜ˆ์ œ 1 

with open('./resource/text1.txt', 'w') as f:
    f.write("Nicegirl!\n")

open์˜ ์ธ์ž์— 'w'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์ƒ์„ฑ ๋ฐ ๊ธ€์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 2 

with open('./resource/text1.txt', 'a') as f:
    f.write("Goodgirl!")

open์˜ ์ธ์ž์— 'a'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€์”จ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 3 

from random import randint
with open('./resource/text2.txt', 'w') as f:
    for cnt in range(6):
        f.write(str(randint(1, 50)))
        f.write('\n')

for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ 6๊ฐœ์˜ ๋žœ๋ค ์ˆซ์ž๋ฅผ ๊ฐ ์ค„์— ํ•œ๊ฐœ์”ฉ ์ž‘์„ฑํ•ด ์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค. 

 

 

- ์˜ˆ์ œ 4 >>> writelines : ๋ฆฌ์ŠคํŠธ -> txt ํŒŒ์ผ๋กœ ์ €์žฅ 

# writelines : ๋ฆฌ์ŠคํŠธ -> ํŒŒ์ผ๋กœ ์ €์žฅ
with open('./resource/text3.txt', 'w') as f:
    list = ['Kim\n', 'Park\n', 'Cho\n']
    f.writelines(list)

writelines๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 5 : ํ”„๋ฆฐํŠธ ํ•จ์ˆ˜๋กœ ํŒŒ์ผ ์ƒ์„ฑ 

with open('./resource/text4.txt', 'w') as f:
    print('Test Contents1!', file=f)
    print('Test Contents2!', file=f)

printํ•จ์ˆ˜์˜ ์ธ์ž์— file=f ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ํŒŒ์ผ๋กœ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

 

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

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/40

 

[python ๊ธฐ์ดˆ] 8. ํด๋ž˜์Šค , ์ƒ์† , ๋‹ค์ค‘์ƒ์†

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

silvercoding.tistory.com

 

* ํด๋” - ํŒจํ‚ค์ง€, ํŒŒ์ผ - ๋ชจ๋“ˆ ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. 

* ์ƒ๋Œ€ ๊ฒฝ๋กœ - . : ๋ถ€๋ชจ ๋””๋ ‰ํ† ๋ฆฌ / .. : ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ 

 

 

 

์šฐ์„  ์˜ค๋Š˜ ์‹ค์Šต์„ ์œ„ํ•ด์„œ 

pkg ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๊ทธ ์•ˆ์— ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค. 

 

(1) __init__.py 

* ์šฉ๋„ : ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ํŒจํ‚ค์ง€์ž„์„ ์„ ์–ธ 

* Python 3.x : ํŒŒ์ผ์ด ์—†์–ด๋„ ํŒจํ‚ค์ง€๋ฅผ ์ธ์‹ ํ•˜์ง€๋งŒ , ํ•˜์œ„ ํ˜ธํ™˜์„ ์œ„ํ•ด ์ƒ์„ฑํ•ด ๋†“๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. 

 

 

(2) fibonacci.py 

class Fibonacci:
    def __init__(self, title='fibonacci'):
        self.title = title 

    def fib(n):  # ํ”ผ๋ณด๋‚˜์น˜ ์ถœ๋ ฅ
        a, b = 0, 1 
        while a < n:
            print(a, end=' ')
            a, b = b, a + b 
        print()

    def fib2(n): # ํ”ผ๋ณด๋‚˜์น˜ ๋ฆฌ์ŠคํŠธ ๋ฆฌํ„ด 
        result = []
        a, b = 0, 1 
        while a < n:
            result.append(a)
            a, b = b, a + b 

        return result

fib๋Š” ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ ์ถœ๋ ฅ, fib2๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. 

 

 

(3) calculations.py

def add(l, r):
    return l + r

def mul(l, r):
    return l * r

def div(l, r):
    return l / r

๋”ํ•˜๊ธฐ, ๊ณฑํ•˜๊ธฐ, ๋‚˜๋ˆ„๊ธฐ ๋‹จ์ˆœ ๊ณ„์‚ฐ ํ•จ์ˆ˜๋‹ค. 

 

 

(4) prints.py

def prt1():
    print("I'm NiceGirl!")


def prt2():
    print("I'm GoodGirl!")


# ๋‹จ์œ„ ์‹คํ–‰ (๋…๋ฆฝ์ ์œผ๋กœ ํŒŒ์ผ ์‹คํ–‰) : ํ•จ์ˆ˜๊ฐ€ ์ž˜ ๋งŒ๋“ค์–ด ์กŒ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ด ํŒŒ์ผ์—์„œ๋งŒ ์‹คํ–‰ํ•˜๋„๋ก ์„ค์ • 
if __name__ == "main":
    prt1()
    prt2()

๋‹จ์ˆœ ์ถœ๋ ฅ๋ฌธ์ด๊ณ , ๋‹จ์œ„์‹คํ–‰์œผ๋กœ ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ์ด ๋ชจ๋“ˆ์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์ถœ๋ ฅ๋˜์ง€ ์•Š๊ณ , ํ˜„์žฌ ํŒŒ์ผ์—์„œ๋งŒ ์‹คํ–‰์ด ๋˜๋„๋ก ํ•˜๋Š” if๋ฌธ์€ ์„ ์–ธํ•œ๋‹ค. 

 

 

 

 

- ์˜ˆ์ œ 1 : ํด๋ž˜์Šค 

from pkg.fibonacci import Fibonacci

์œ„์™€ ๊ฐ™์ด ํด๋ž˜์Šค๋ฅผ importํ•ด ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. 

Fibonacci.fib(300)

 0 1 1 2 3 5 8 13 21 34 55 89 144 233 

์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ์˜€์œผ๋ฏ€๋กœ ํด๋ž˜์Šค ์ž์ฒด๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

print("ex1 : ", Fibonacci.fib2(400))
print('ex1 : ', Fibonacci().title)  # ์ธ์Šคํ„ด์Šคํ™” ์‹œ์ผœ์•ผ ํ•จ.

 ex1 :  [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377] 
 ex1 :  fibonacci 

title์€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ์Šคํ„ด์Šคํ™” ์‹œ์ผœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 2 : ํด๋ž˜์Šค 2 

from pkg.fibonacci import *

๋ชจ๋“ˆ์˜ ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ์ฐจ์ง€ํ•˜๋ฏ€๋กœ ๊ถŒ์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค. ํ•„์š”ํ•œ ๊ฒƒ๋งŒ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ด๋‹ค. 

Fibonacci.fib(500)
print("ex2 : ", Fibonacci.fib2(600))
print('ex2 : ', Fibonacci().title)  # ์ธ์Šคํ„ด์Šคํ™” ์‹œ์ผœ์•ผ ํ•จ.

 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 
 ex2 :  [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377] 
 ex2 :  fibonacci 

 

 

- ์˜ˆ์ œ 3 : ํด๋ž˜์Šค - ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ• 

from pkg.fibonacci import Fibonacci as fb

์ด์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋งŽ์ด ๋ณด์•˜์„ ๊ฒƒ์ด๋‹ค. 

fb.fib(1000)
print("ex3 : ", fb.fib2(1600))
print('ex3 : ', fb().title)

 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 
 ex3 :  [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]     
 ex3 :  fibonacci 

 

 

- ์˜ˆ์ œ 4 : ํ•จ์ˆ˜ 

import pkg.calculations as c
print("ex4 : ", c.add(10, 100))
print("ex4 : ", c.mul(10, 100))

 ex4 :  110 
 ex4 :  1000 

 

 

 

- ์˜ˆ์ œ 5 : ํ•จ์ˆ˜ - ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ• (ํ•„์š”ํ•œ ๋งŒํผ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€ ์Šต๊ด€์ด๋‹ค. ) 

from pkg.calculations import div as d
print('ex5 : ', int(d(100, 10)))

 ex5 :  10 

์ด๋ ‡๊ฒŒ ํ•„์š”ํ•œ ํ•จ์ˆ˜๋งŒ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 6 

import pkg.prints as p
import builtins  # ํŒŒ์ด์ฌ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜๋“ค
p.prt1()
p.prt2()
print()
print(dir(builtins))

 I'm NiceGirl! 
 I'm GoodGirl! 

 ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip'] 

builtins์—๋Š” ํŒŒ์ด์ฌ์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜๋“ค์ธ๋ฐ, prints.py์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ __name__์ด ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

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

 

 

<์ด์ „ ๊ธ€>

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 ๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ์ค‘์ฒฉ ์กฐ๊ฑด๋ฌธ์ด๋‹ค.  

+ Recent posts