๐Ÿง Programming/Python

1) Python - Return๋ฌธ (1) and/or ์—ฐ์‚ฐ์ž ํ™œ์šฉ in Return Python์—์„œ๋Š” ์กฐ๊ฑด๋ฌธ์— ๋น„๊ต ์—ฐ์‚ฐ์‹ ๋Œ€์‹ ์— ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋…ํŠนํ•œ ๋ฌธ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š”๋ฐ (์ด ๊ฒฝ์šฐ์—๋Š” ๋ณ€์ˆ˜ ์ž์ฒด๊ฐ€ ๋…ผ๋ฆฌ์‹์ด ๋œ๋‹ค) ๋งŒ์•ฝ ํ•จ์ˆ˜์˜ Return๋ฌธ์—์„œ ๋น„๊ต ์—ฐ์‚ฐ์‹ (and, or)๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ๋ณ€์ˆ˜ ์ž์ฒด๋ฅผ ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ํ—ท๊ฐˆ๋ฆฌ๊ฒŒ ๋  ์—ฌ์ง€๊ฐ€ ๋งŽ๋‹ค. ์ˆซ์ž๋Š” 0์ด ์•„๋‹ˆ๋ฉด True, 0์ด๋ฉด False, ๋ฌธ์ž์—ด์€ ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด True, ๋น„์–ด์žˆ์œผ๋ฉด False, ๊ทธ ์ด์™ธ์—๋„ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ์…‹, ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ์˜ ๊ฐ์ฒด ๋˜ํ•œ ๋ฌธ์ž์—ด๊ณผ ๋น„์Šทํ•œ ๋งฅ๋ฝ์œผ๋กœ ๊ฐ์ฒด์˜ ๊ฐ’์ด ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด True, ๋น„์–ด์žˆ์œผ๋ฉด False๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ๋‹ค๋งŒ, ์กฐ๊ฑด๋ฌธ์ด ์•„๋‹Œ Return๋ฌธ์—์„œ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด True๋‚˜ Fal..
1) ๋‚ด์žฅ ํ•จ์ˆ˜ (1) any(iterable) any() ํ•จ์ˆ˜๋Š” ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ iterable ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›์œผ๋ฉฐ, ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ ์ค‘ ์–ด๋Š ํ•˜๋‚˜๊ฐ€ True ๋ผ๋ฉด ํ•ญ์ƒ True๋กœ ์ถœ๋ ฅํ•œ๋‹ค. → ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์˜ OR ์—ฐ์‚ฐ์ž! (2) all(iterable) all() ํ•จ์ˆ˜๋Š” ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ iterable ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›์œผ๋ฉฐ, all() ํ•จ์ˆ˜๋Š” ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ ๋ชจ๋‘ True ์ธ ๊ฒฝ์šฐ, True๋กœ ์ถœ๋ ฅํ•œ๋‹ค. → ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์˜ AND ์—ฐ์‚ฐ์ž! (3) ์˜ˆ์ œ >>> any( [True, True, False, False, True] ) True >>> all( [True, True, False, False, True] ) False >>> any( [ [1,2], [], [3,1] ] ) True >>> all( [ ..
1) ๋‚ด์žฅ ํ•จ์ˆ˜ (1) zip (iterable, iterable, ...) zip() ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ iterable ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ , ๊ฐ ๊ฐ์ฒด๊ฐ€ ๋‹ด๊ณ  ์žˆ๋Š” ์›์†Œ๋ฅผ ํŠœํ”Œ์˜ ํ˜•ํƒœ๋กœ ์ฐจ๋ก€๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ณต์ž (iterator)๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. [์˜ˆ์‹œ] numbers = [1, 2, 3] letters = [“A”, “B”, “C”] for pair in zip(numbers, letters): print(pair) [๊ฒฐ๊ณผ] >>> (1, 'A') >>> (2, 'B') >>> (3, 'C') → numbers, letters ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ๋ฅผ ๊บผ๋‚ด์–ด ํŠœํ”Œ์˜ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ [๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ] for number, upper, lower in zip("12345", "ABCDE", "abcde"..
1) ๋‚ด์žฅ ํ•จ์ˆ˜ (1) map(func, iterable) iterable ์š”์†Œ๋“ค์„ ํ•ด๋‹น ํ•จ์ˆ˜์— ์ ์šฉ์‹œ์ผœ Map ํ˜•ํƒœ๋กœ Return ํ•˜๊ธฐ ๋•Œ๋ฌธ์— list ํ˜น์€ tuple ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€ํ™˜์‹œ์ผœ ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ iterable์—๋Š” ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ, ๋ฌธ์ž์—ด ๋“ฑ์ด ์‚ฌ์šฉ๋˜์–ด์ง„๋‹ค. ์ฆ‰, map() ํ•จ์ˆ˜๋Š” function์„ iterable์˜ ๋ชจ๋“  ์š”์†Œ์— ๋Œ€ํ•ด ์ ์šฉํ•˜๊ณ  function์— ์˜ํ•ด ๋ณ€๊ฒฝ๋œ iterator๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค [์˜ˆ์‹œ] L = [1,2] x, y = map(str, L) (2) filter(func, iterable) iterable ์š”์†Œ๋“ค์„ ํ•ด๋‹น ํ•จ์ˆ˜์— ์ ์šฉ์‹œ์ผœ filter ํ˜•ํƒœ๋กœ Return ํ•˜๊ธฐ ๋•Œ๋ฌธ์— list ํ˜น์€ tuple ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€ํ™˜์‹œ์ผœ ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ iterable์—๋Š” ๋ฆฌ..
1) ํ•จ์ˆ˜ (Function) (1) ํ•จ์ˆ˜ ์„ ์–ธ def ํ•จ์ˆ˜๋ช…(์ธ์ž): ์ฒ˜๋ฆฌํ•  ๋ช…๋ น ์ฒ˜๋ฆฌํ•  ๋ช…๋ น ... return ... ํ•จ์ˆ˜ ์•ˆ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ(์ธ์ž)๋Š” ์žˆ์„ ์ˆ˜๋„ ์žˆ๊ณ  ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค. [์ „๋‹ฌ๊ฐ’ ์œ ๋ฌด] return๋ฌธ์€ ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ ๋ฆฌํ„ดํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์‚ฌ์šฉํ•˜๊ณ  ํŠน๋ณ„ํžˆ return ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ์—†๋‹ค๋ฉด ์ ์ง€ ์•Š๋Š”๋‹ค. [๋ฐ˜ํ™˜๊ฐ’ ์œ ๋ฌด] (2) ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ธฐ๋ณธ๊ฐ’ def testFunc(name, age, lang=“python”): print(name, age, lang) testFunc(“joe”, 18) → ์ด์ฒ˜๋Ÿผ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ, ํ•ด๋‹น ์ธ์ž๊ฐ’์„ ๋„ฃ์–ด์ฃผ์ง€ ์•Š์•„๋„ ๊ธฐ๋ณธ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์–ด → testFunc() ํ•จ์ˆ˜๊ฐ€ ์ž˜ ์ž‘๋™๋จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ํ•จ์ˆ˜ ์„ ์–ธ ์‹œ์— “testFunc(n..
1) ๊ธฐ์กด ๋ณตํ•ฉ๋ฌธ (if, for) # First Case numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] odd_numbers = [] for i in numbers: if numbers[i] % 2 == 0: odd_numbers.append(i) print(odd_numbers) # Second Case L = [1, 2, 3, 4, 5] for n in L: if n % 2 == 0: print(“odd”) else: print(“even”) 2) ํ•œ ์ค„ ๋ณตํ•ฉ๋ฌธ (if, for) # First Case L = [] [L.append(i) for i in range(10) if i % 2 == 0] print(L) # Second Case [print(“odd”) i..
1) ๊ธฐ์กด for๋ฌธ # First Case L = [] for i in range(10): L.append(i) print(L) # Second Case students = ["Mark", "Joe", "Jay"] for s in range(len(students): students[i] = len(students[i]) # Third Case L = [] for i in range(5): for j in range(5): L.append(i * j) print(L) 2) ํ•œ ์ค„ for๋ฌธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฐ•๋ ฅํ•˜๊ณ  ๊ฐ„๊ฒฐํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ž์ฃผ ์“ฐ์ด๋Š” ๋ฌธ๋ฒ•์ด๋ฏ€๋กœ ๊ผญ ์•Œ์•„๋‘๋„๋ก ํ•˜์ž! # First Case L = [ i for i in range(10) ] print(*L) or [ print(i, end=" ")..
1) ๊ธฐ์กด if๋ฌธ int a = 3 if a == 3: print("a๋Š” 3 ์ž…๋‹ˆ๋‹ค.") else: print("a๋Š” 3์ด ์•„๋‹™๋‹ˆ๋‹ค.") 2) ํ•œ ์ค„ if๋ฌธ print( "a๋Š” 3์ž…๋‹ˆ๋‹ค." if a == 3 else "a๋Š” 3์ด ์•„๋‹™๋‹ˆ๋‹ค." ) ๋ฌธ๋ฒ• : (True์ผ ์‹œ, ์ฒ˜๋ฆฌํ•  ๋ช…๋ น) if [์กฐ๊ฑด์‹] else [False์ผ ์‹œ, ์ฒ˜๋ฆฌํ•  ๋ช…๋ น] ๋งŒ์•ฝ์—, elif๋ฌธ์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ if๋ฅผ elif๋ฌธ ๊ฐฏ์ˆ˜๋งŒํผ ์ถ”๊ฐ€๋กœ ๋” ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ์ค„๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. [๊ธฐ์กด] int a = 3 if a > 3: print("a>3") elif a == 3: print("a==3") else: print("a3" if a > 3 else print("a==3") if a == 3 else print("a
Dev_sHu
'๐Ÿง Programming/Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก