์ „์ฒด๋ณด๊ธฐ 182

ํŒŒ์ด์ฌ 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) . . .

ํŒŒ์ด์ฌ for๋ฌธ ๊ณผ range, dict

1. for ๋ฌธ ๊ฐ’์˜ ๋ฒ”์œ„๋‚˜ ํšŸ์ˆ˜๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•œ ๋ฐ˜๋ณต๋ฌธ while๋ฌธ ๋ณด๋‹ค ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค. for ๋ณ€์ˆ˜ in ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด: ๋ฐ˜๋ณต์‹คํ–‰๋ฌธ #pwd = 'abcdefg55' pwd = input('๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” >>> ') ch_count = 0 num_count = 0 for ch in pwd: if ch.isalpha(): #๋ฌธ์ž ์—ฌ๋ถ€ ํŒ๋‹จ bool type, ํ•ด์„ํ•˜์ž๋ฉด pwd์—์„œ a๊ฐ€ ๋ฌธ์ž๋ผ๋ฉด ์•„๋ž˜์ค„๋กœ ๋„˜์–ด๊ฐ€์„œ ch_count์— 1์„ ์ ๋ฆฝ ch_count += 1 #7 elif ch.isnumeric(): #์ˆซ์ž ์—ฌ๋ถ€ ํŒ๋‹จ bool type, ํ•ด์„ํ•˜์ž๋ฉด pwd์—์„œ 5๊ฐ€ ์ˆซ์ž๋ฉด ์•„๋ž˜์ค„๋กœ ๋„˜์–ด๊ฐ€์„œ num_count์— 1์„ ์ ๋ฆฝ num_count += 1 #2 if ch_coun..

ํŒŒ์ด์ฌ While ๋ฐ˜๋ณต๋ฌธ

๋ฐ˜๋ณต๋ฌธ ์–ด๋–ค ์ˆ˜ํ–‰ ์ž‘์—…์„ ํ•œ ๋ฒˆ์ด ์•„๋‹ˆ๋ผ ๊ณ„์†ํ•ด์„œ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋ฐ˜๋ณต๋ฌธ ์ข…๋ฅ˜ while, for๋ฌธ while๋ฌธ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋™์•ˆ ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ while ์กฐ๊ฑด์‹: ๋ฐ˜๋ณต ์‹คํ–‰์ฝ”๋“œ ์˜ˆ์ œ1) 10๋ถ€ํ„ฐ 1๊นŒ์ง€ ๋‚˜์—ดํ•˜๊ธฐ n = 10 while n >= 1: print(n) n -= 1 # n = n - 1 print("while๋ฌธ ๋๋‚˜๊ณ  n ๊ฐ’ : {}".format(n)) ์˜ˆ์ œ2) list์— ์ˆซ์ž ๋ชจ์œผ๊ธฐ1 my_list = [] n = int(input('์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(์ข…๋ฃŒ๋Š” 0์ž…๋‹ˆ๋‹ค.)>>>')) # input ํ•จ์ˆ˜๋กœ ์ž…๋ ฅ ๋ฐ›์€ ๊ฐ’์„ n์— ๋„ฃ์Œ while n != 0: # n์ด 0์ด ์•„๋‹ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต my_list.append(n) # my_list์— n ์ถ”๊ฐ€ n = int(..

ํŒŒ์ด์ฌ if๋ฌธ

์กฐ๊ฑด๋ฌธ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋‹ฌ๋ผ์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ์˜ ๋ฒ”์œ„ ์ •์˜. 1. ๊ธฐ๋ณธํ˜• if(์กฐ๊ฑด์‹): (์กฐ๊ฑด์‹์ด ์ฐธ)์‹คํ–‰ํ•  ์ฝ”๋“œ 2. else if(์กฐ๊ฑด์‹): (์กฐ๊ฑด์‹์ด ์ฐธ)์‹คํ–‰ํ•  ์ฝ”๋“œ else: (์กฐ๊ฑด์‹์ด ๊ฑฐ์ง“)์‹คํ–‰ํ•  ์ฝ”๋“œ 3. elif if(์กฐ๊ฑด์‹): (์กฐ๊ฑด์‹1 ์ฐธ)์‹คํ–‰ํ•  ์ฝ”๋“œ elif(์กฐ๊ฑด์‹2): (์กฐ๊ฑด์‹2 ์ฐธ)์‹คํ–‰ํ•  ์ฝ”๋“œ elif(์กฐ๊ฑด์‹3): (์กฐ๊ฑด์‹3 ์ฐธ)์‹คํ–‰ํ•  ์ฝ”๋“œ elif(์กฐ๊ฑด์‹N): (์กฐ๊ฑด์‹N ์ฐธ)์‹คํ–‰ํ•  ์ฝ”๋“œ else: (์กฐ๊ฑด์‹ ๋ชจ๋‘ ๊ฑฐ์ง“)์‹คํ–‰ํ•  ์ฝ”๋“œ # if(์กฐ๊ฑด์‹) a = 100 b = 200 if b > a: print("b๋Š” a๋ณด๋‹ค ํฌ๋‹ค") # if(์กฐ๊ฑด์‹) else a = 7 b = 4 if b >= a: print("b๋Š” a๋ณด๋‹ค ..

๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž(logical operator)

๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋Š” ์—ฐ์‚ฐ์ž๋กœ 2๊ฐœ ์ด์ƒ์˜ ํ•ญ์„ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์—ฐ๊ฒฐํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ex) and, or, not a = 10 b = 0 print('{} > 0 and {} > 0 : {}'.format(a, b, a > 0 and b > 0)) #๋‘ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑ์‹œํ‚ค์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ฑฐ์ง“ print('{} > 0 or {} > 0 : {}'.format(a, b, a > 0 or b > 0)) #๋‘ ์กฐ๊ฑด ์ค‘ ํ•˜๋‚˜๋ฅผ ๋งŒ์กฑ์‹œํ‚ค๋ฏ€๋กœ ์ฐธ print('not {} : {}'.format(a, not a)) #๊ฐ’์ด ์žˆ์œผ๋‹ˆ๊นŒ true์ธ๋ฐ not์ด ๋ถ™์–ด์„œ false print('not {} : {}'.format(b, not b)) #0์€ false๋‹ˆ๊นŒ 0์˜ not์€ true

ํŒŒ์ด์ฌ ๋Œ€์ž… ์—ฐ์‚ฐ์ž(assignment operator)

๋Œ€์ž… ์—ฐ์‚ฐ์ž ๋ณ€์ˆ˜์— ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์ž… ์—ฐ์‚ฐ์ž print ํ˜•์‹๋ฌธ์ž %d : ์ˆซ์ž ๋ฐ์ดํ„ฐ %f : ์‹ค์ˆ˜ ๋ฐ์ดํ„ฐ %o : 8์ง„์ˆ˜ ๋ฐ์ดํ„ฐ %x : 16์ง„์ˆ˜ ๋ฐ์ดํ„ฐ %s : ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ %c : ๋ฌธ์ž ํ•˜๋‚˜ ๋ฐ์ดํ„ฐ a, b = 10, 20 print('a = %d, b = %d ' % (a, b)) a, b = b, a print('a = %d, b = %d ' % (a, b)) ๋ณตํ•ฉ ๋Œ€์ž…์—ฐ์‚ฐ์ž += ex) x += 3 ๊ณผ x = x + 3 ๊ฐ™๋‹ค -= ex) x += 3 ๊ณผ x = x - 3 ๊ฐ™๋‹ค piggy_bank = 0 money = 10000 piggy_bank += money # piggy_bank = piggy_bank + money print('์ €๊ธˆํ†ต์— ์šฉ๋ˆ {}์›์„ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค.'.fo..

ํŒŒ์ด์ฌ ์—ฐ์‚ฐ์ž arithmetic

์—ฐ์‚ฐ์ž ํŠน์ •ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐํ˜ธ๋ฅผ ์˜๋ฏธ 1. ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž +, -, *, **, /, //, % ์ˆซ์ž ๊ณ„์‚ฐ์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์—ฐ์‚ฐ์ž. a = 7 b = 2 # ๋ง์…ˆ print('{} + {} = {}'.format(a, b, a + b)) # ๋บ„์…ˆ print('{} - {} = {}'.format(a, b, a - b)) # ๊ณฑ์…ˆ print('{} * {} = {}'.format(a, b, a * b)) # ๊ฑฐ๋“ญ์ œ๊ณฑ print('{} ** {} = {}'.format(a, b, a ** b)) # ๋‚˜๋ˆ„๊ธฐ print('{} / {} = {}'.format(a, b, a / b)) # ๋ชซ print('{} // {} = {}'.format(a, b, a // b)) # ๋‚˜๋จธ์ง€ print(..