python

ยทpython
*args (arguments) Python ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ, ํ•จ์ˆ˜์— ์ธ์ž(arguments)๊ฐ€ ๋ช‡๊ฐœ ์ž…๋ ฅ๋  ์ง€ ๋ชจ๋ฅด๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐ€์กฑ ๊ตฌ์„ฑ์›์„ ์ž…๋ ฅํ•˜๋ฉด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊ฐœ์ธ๋ณ„๋กœ ๊ฐ€์กฑ์˜ ๊ตฌ์„ฑ์› ์ˆ˜๋Š” ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ธ์ž๊ฐ€ ์ด ๋ช‡๊ฐœ ์ž…๋ ฅ๋ ์ง€๋Š” ์•Œ ์ˆ˜ ์—†๋‹ค. ์„ธ๊ณ„์˜ ๋ชจ๋“  ๊ฐ€์กฑ์ด 4๋ช…์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค๋ฉด, ์šฐ๋ฆฌ๊ฐ€ ์•Œ๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•จ์ˆ˜๋ฅผ ์งค ์ˆ˜ ์žˆ๋‹ค. def family(dad, mom, sibling, me): print(dad, mom, sibling, me) family("๊น€๋ฒ”์ˆ˜", "์ด์†Œ๋ผ", "๋ฐ•ํšจ์‹ ", "์žฅ๋ฒ”์ค€") # output: ๊น€๋ฒ”์ˆ˜ ์ด์†Œ๋ผ ๋ฐ•ํšจ์‹  ์žฅ๋ฒ”์ค€ ํ•˜์ง€๋งŒ ๋‹น์—ฐํžˆ ์„ธ์ƒ์˜ ๋ชจ๋“  ๊ฐ€์กฑ ๊ตฌ์„ฑ์›์€ ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๊ณ , ์ด๋Ÿด ๊ฒฝ์šฐ *args๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ..
ยทpython
๋ชจ๋‘๊ฐ€ ์•Œ๋‹ค์‹œํ”ผ python์˜ ์žฅ์  ์ค‘ ํ•˜๋‚˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ๋ฒ•์ด๋‹ค. ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ ๋งŽ์€ ์ค„์„ ์‚ฌ์šฉํ•ด์•ผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•œ ์ค„๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. list comprehension์„ ์ž˜ ์ด์šฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ด๋ป๋ณด์ธ๋‹ค. ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณด๊ฒ ๋‹ค ์ •์ˆ˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ ์š”์†Œ์— 1์„ ๋”ํ•œ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๋•Œ, list comprehension์„ ์ด์šฉํ•˜์ง€ ์•Š์œผ๋ฉด for๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. my_list = [1, 2, 3] new_list = [] for num in my_list: new_list.append(num + 1) print(new_list) # = [2, 3, 4] ํ•˜์ง€๋งŒ, list comprehension์„ ์ด์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค...
ยทpython
๋ฐฉ๋ฒ• 1 - Clear Previous Output option vscode ์‚ฌ์šฉ์‹œ code runner extension์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ํŒŒ์ผ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋‹ค ๋ณด๋ฉด ํ„ฐ๋ฏธ๋„์ด ์ง€์ €๋ถ„ํ•˜๊ณ  ๋ณต์žกํ•ด์ ธ์„œ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿด ๋•Œ ํŒŒ์ผ์„ ์‹คํ–‰ํ•  ๋•Œ ๋งˆ๋‹ค ํ„ฐ๋ฏธ๋„์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐฉ๋ฒ•์ผ ์ˆ˜ ์žˆ๋‹ค. 1. vscode ์„ค์ •์œผ๋กœ ๊ฐ„๋‹ค (โŒ˜ + ,) 2. ๊ฒ€์ƒ‰์ฐฝ์— 'Clear Previous Output'์„ ์ž…๋ ฅํ•œ๋‹ค 3. Code-runner: Clear Previous Output ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ•œ๋‹ค. ์ฒดํฌ ์™„๋ฃŒ ํ›„, ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ธฐ์กด ํ„ฐ๋ฏธ๋„์ด ์ดˆ๊ธฐํ™”๋œ ์ดํ›„์— ์ถœ๋ ฅ๋œ๋‹ค. ๋ฐฉ๋ฒ• 2 - settings.json ์ˆ˜์ • ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•˜๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์ถœ๋ ฅ ์‹œ outp..
ยทpython
Turtle graphics Module์„ ์‚ฌ์šฉํ•ด์„œ ๋ฑ€ ๊ฒŒ์ž„์„ ๋งŒ๋“ค์–ด๋ณด์ž. ๋จน์ด๋ฅผ ๋จน์œผ๋ฉด ๊ผฌ๋ฆฌ๊ฐ€ ํ•œ ์นธ์”ฉ ์ž๋ผ๊ณ , ๊ผฌ๋ฆฌ๋‚˜ ์ฐฝ ๋์— ๋ถ€๋”ชํžˆ๋ฉด ์ฃฝ๋Š” ๊ฒŒ์ž„์ด๋‹ค. main.py, snake.py, scoreBoard.py, food.py ์ด 4๊ฐ€์ง€ ํŒŒ์ผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. # main.py import time from turtle import Screen, Turtle from snake import Snake from food import Food from scoreBoard import ScoreBoard screen = Screen() screen.setup(width=600, height=600) screen.bgcolor("black") screen.title("Snake Game") screen.tr..
Chan Lee
'python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (2 Page)