๐Ÿ“‚์›น ๊ฐœ๋ฐœ(Web)/๐ŸํŒŒ์ด์ฌ(Python) 44

ํŒŒ์ด์ฌ ๋ชจ๋“ˆ(module)

๋ชจ๋“ˆ(module) ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜ ๋˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋ชจ์•„ ๋†“์€ ํŒŒ์ผ์„ ๋ชจ๋“ˆ์ด๋ผ๊ณ  ํ•œ๋‹ค. **์‚ฌ์šฉํ•  conveter ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋‘”๋‹ค. MILES = 0.621371 POUND = 0.00220462 def kilometer_to_miles(kilometer): return kilometer * MILES def gram_to_pounds(gram): return gram * POUND 1. ๋ชจ๋“ˆ ์‚ฌ์šฉ๋ฒ• import ๋ชจ๋“ˆ๋ช… import convertermiles = converter.kilometer_to_miles(150) print('150km = {}miles'.format(miles)) pounds = converter.gram_to_pounds(1000) print('1000g={}pounds'.fo..

ํŒŒ์ด์ฌ ํ•จ์ˆ˜(function)

ํ•จ์ˆ˜(fuction) ํ•˜๋‚˜์˜ ํŠน๋ณ„ํ•œ ๋ชฉ์ ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋…๋ฆฝ์ ์œผ๋กœ ์„ค๊ณ„๋œ ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์˜ ์ง‘ํ•ฉ def ํ•จ์ˆ˜ ์ด๋ฆ„(๋งค๊ฐœ๋ณ€์ˆ˜) ์ฝ”๋“œ ์‹คํ–‰๋ฌธ return ๋ฐ˜ํ™˜๊ฐ’ 1. ๋งค๊ฐœ๋ณ€์ˆ˜ x ๋ฆฌํ„ด x # welcome() ํ•จ์ˆ˜ ์ •์˜(์‹คํ–‰ x) def welcome(): print('Hello Python') print('Nice to meet you') welcome() # ํ•จ์ˆ˜ ํ˜ธ์ถœ(์‹คํ–‰) 2. ๋งค๊ฐœ๋ณ€์ˆ˜ o ๋ฆฌํ„ด x def introduce(name, age): print('๋‚ด ์ด๋ฆ„์€ {}์ด๊ณ , ๋‚˜์ด๋Š” {}์‚ด ์ž…๋‹ˆ๋‹ค.'.format(name,age)) introduce('ํ‚น๋‰ด๋•ก', 30) ๊ฐ€๋ณ€ ๋งค๊ฐœ๋ณ€์ˆ˜ ํ•จ์ˆ˜ def show(*args): print(type(args)) for item in args: print..

ํŒŒ์ด์ฌ ๊ต์ง‘ํ•ฉ intersection() ํ•ฉ์ง‘ํ•ฉ union() ์ฐจ์ง‘ํ•ฉ difference()

1. ๊ต์ง‘ํ•ฉ intersection() s1 = {'apple', 'banana', 'cherry'} s2 = {'apple', 'banana', 'orange'} print(s1 & s2) result = s1.intersection(s2) print(result) 2. ํ•ฉ์ง‘ํ•ฉ union() s1 = {'apple', 'banana', 'cherry'} s2 = {'apple', 'banana', 'orange'} print(s1 | s2) result = s1.union(s2) print(result) 3. ์ฐจ์ง‘ํ•ฉ difference() s1 = {'apple', 'banana', 'cherry'} s12= {'apple', 'banana', 'orange'} print(s1 - s2) result ..

join ๋ฉ”์†Œ๋“œ /split ๋ฉ”์†Œ๋“œ /replace ๋ฉ”์†Œ๋“œ /strip(), lstrip(), rstrip() ๊ณต๋ฐฑ ์ œ๊ฑฐ

1. join() ๋ฉ”์†Œ๋“œ s = '-'.join('python') print(s) s = '+'.join(['a', 'b', 'c', 'd', 'e']) print(s) s = ''.join(['a', 'b', 'c', 'd', 'e']) print(s) 2. split() ๋ฉ”์†Œ๋“œ s = 'Life is too short' result = s.split() print(result) s = '010-1234-5678' result = s.split('-') print(result) print(result[2]) #๋’ท๋ฒˆํ˜ธ๋งŒ data = 'ํ‚น๋‰ด๋•ก|30|ํ•™์ƒ' result = data.split('|') print(result) print('์ด๋ฆ„ : {}'.format(result[0])) 3. replace(..

ํŒŒ์ด์ฌ ๋ฉ”์†Œ๋“œ(method)/string๊ฐ์ฒด format ๋ฉ”์†Œ๋“œ/find ๋ฉ”์†Œ๋“œ/count ๋ฉ”์†Œ๋“œ/find ๋ฉ”์†Œ๋“œ/ index ๋ฉ”์†Œ๋“œ

๋ฉ”์†Œ๋“œ(method) ํŠน์ • ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ๋งŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ 1. String ๊ฐ์ฒด format ๋ฉ”์†Œ๋“œ print("10์ž๋ฆฌ ํญ ์™ผ ์ชฝ ์ •๋ ฌ ' {:10d}".format(123)) print("10์ž๋ฆฌ ํญ ๊ฐ€์šด๋ฐ ์ •๋ ฌ ์ฑ„์›€๋ฌธ์ž ' {:*^10d}".format(123)) 2. count() ๋ฉ”์†Œ๋“œ ์ฒซ ๋ฒˆ์งธ : ๋ฌธ์ž์—ด.count(x)๋Š” ๋ฌธ์ž์—ด ์ „ ๋ฒ”์œ„์—์„œ x๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ํšŸ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ ๋‘ ๋ฒˆ์งธ : ๋ฌธ์ž์—ด.count(x, start)๋Š” ๋ฌธ์ž์—ด start index ๋ถ€๋ถ„์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋ฌธ์ž์—ด ๋๊นŒ์ง€ x๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ํšŸ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ ์„ธ ๋ฒˆ์งธ : ๋ฌธ์ž์—ด.count(x, start, end)๋Š” ๋ฌธ์ž์—ด start index ๋ถ€๋ถ„์—์„œ end index ๋ถ€๋ถ„๊นŒ์ง€์˜..

python ๋‚ด์žฅ ํ•จ์ˆ˜ ๋ฌธ์ž์—ด/์ฒœ๋‹จ์œ„ ์‰ผํ‘œ/์ ˆ๋Œ€๊ฐ’/์ตœ๋Œ€๊ฐ’/์ตœ์†Œ๊ฐ’/๊ฑฐ๋“ญ์ œ๊ณฑ/์˜ค๋ฆ„์ฐจ์ˆœ์ •๋ ฌ/์—ญ์ •๋ ฌ/zipํ•จ์ˆ˜

๋‚ด์žฅํ•จ์ˆ˜ ๋ณด์ถฉ 1. format์œผ๋กœ ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ result = format(10000) #str(10000)๊ณผ ๊ฐ™๋‹ค print(type(result)) 2. ์ฒœ ๋‹จ์œ„ ์‰ผํ‘œ result = format(10000, ',') print(result) 3. abs() ์ ˆ๋Œ€๊ฐ’ result = abs(10) print(result) result = abs(-10) print(result) 4. max() ์ตœ๋Œ€๊ฐ’ ๋ฐ˜ํ™˜ result = max(1,10) print(result) li = [5, 3, 7, 4, 2, 1] result = max(li) print(result) 5. min() ์ตœ์†Œ๊ฐ’ ๋ฐ˜ํ™˜ result = min(li) print(result) 6. pow() ๊ฑฐ๋“ญ์ œ๊ณฑ ํ•จ์ˆ˜ result = pow(1..

ํŒŒ์ด์ฌ enumerate() ๋‚ด์žฅ ํ•จ์ˆ˜

enumerate() List, Tuple, String ๋“ฑ ์ž๋ฃŒํ˜•์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ธ๋ฑ์Šค ๊ฐ’์„ ํฌํ•จํ•˜๋Š” enumerate ๊ฐ์ฒด๋ฅผ ๋Œ๋ ค์ค€๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ์— ์ˆœ์„œ๊ฐ’์„ ๋ถ€์—ฌํ•ด์ฃผ๋Š” ํ•จ์ˆ˜. months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] print(len(months)) for month, day in enumerate(months): print('{}์›” = {}์ผ'.format(month+1, day))

ํŒŒ์ด์ฌ divmod() ํ•จ์ˆ˜

divmod() ํ•จ์ˆ˜ ๋‘ ์ˆซ์ž๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ ๋ฐ›์•„ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋‚˜๋ˆˆ ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ tuple ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. money = 10000 price = 3000 result = divmod(money, price) print('๋นต์„ {}๊ฐœ ์‚ฌ๊ณ  {}์›์ด ๋‚จ์•˜์Šต๋‹ˆ๋‹ค.' #๋ชซ๊ณผ ๋‚˜๋จธ์ง€ .format(result[0], result[1])) #์ธ๋ฑ์Šค๋กœ ํ‘œํ˜„

ํŒŒ์ด์ฌ eval ํ•จ์ˆ˜

eval() ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ expression(=์‹)์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ›์•„์„œ ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜ expr = input('๊ณ„์‚ฐ์‹์„ ์ž…๋ ฅํ•˜์„ธ์š” >>> ') result = eval(expr) print(expr + '=' + str(result)) # ๊ณ„์‚ฐ์‹์„ ๊ทธ๋Œ€๋กœ ๋ฐ›์•„์„œ ์‹คํ–‰ํ•œ๋‹ค ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„๋˜์–ด ์žˆ๋Š” "1+2"๋ผ๋Š” ํ‘œํ˜„์‹์˜ ๊ฐ’์„ ํ‰๊ฐ€ํ•ด์„œ result๋กœ ๋ฆฌํ„ดํ•ด์คฌ๋‹ค.

ํŒŒ์ด์ฌ continue

continue while๋ฌธ์ด๋‚˜ for๋ฌธ๊ณผ ๊ฐ™์€ ๋ฐ˜๋ณต๋ฌธ์„ ๊ฐ•์ œ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ (์•„๋ž˜ ์ฝ”๋“œ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค) total = 0 for a in range(1 , 101): # 1๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ๊ฐ’์„ a์— ๋„ฃ์–ด์คŒ if a % 3 == 0: # 3์˜ ๋ฐฐ์ˆ˜๋ฉด continue #์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. print('a: {}, total {}'.format(a, total)) total += a # 3์˜ ๋ฐฐ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋”ํ•˜๊ธฐ print(total) . . .