Coding/Code in the Morning

211027_Code in the morning

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 10. 27. 10:33

Q1

์ฃผ์–ด์ง„ ์ž์—ฐ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ธ์ง€ ์ง์ˆ˜์ธ์ง€ ํŒ๋ณ„ํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜(is_odd)๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž.

 

ํ’€์ด: 

๋”๋ณด๊ธฐ
def is_odd(num):
    if num % 2 == 1:
        return True
    else :
        return False

 

Q2

์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์ˆ˜์˜ ํ‰๊ท  ๊ฐ’์„ ๊ณ„์‚ฐํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์ž. (๋‹จ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ์ˆ˜์˜ ๊ฐœ์ˆ˜๋Š” ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š๋‹ค.)

โ€ป ํ‰๊ท  ๊ฐ’์„ ๊ตฌํ•  ๋•Œ len ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์ž.

ํ’€์ด: 

๋”๋ณด๊ธฐ
def avg_num(*args) :
    result = 0
    for i in args:
        result += i
        result = result/len(args)
    return result

 

Q3

๋‹ค์Œ์€ ๋‘ ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋”ํ•˜์—ฌ ๋Œ๋ ค์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค.

input1 = input("์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”:") input2 = input("๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”:") total = input1 + input2 print("๋‘ ์ˆ˜์˜ ํ•ฉ์€ %s ์ž…๋‹ˆ๋‹ค" % total)

์ด ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜ํ–‰ํ•ด ๋ณด์ž.

์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”:3 ๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”:6 ๋‘ ์ˆ˜์˜ ํ•ฉ์€ 36 ์ž…๋‹ˆ๋‹ค

3๊ณผ 6์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ 9๊ฐ€ ์•„๋‹Œ 36์ด๋ผ๋Š” ๊ฒฐ๊ด๊ฐ’์„ ๋Œ๋ ค์ฃผ์—ˆ๋‹ค. ์ด ํ”„๋กœ๊ทธ๋žจ์˜ ์˜ค๋ฅ˜๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด์ž.

โ€ป int ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์ž.

๋”๋ณด๊ธฐ
input1 = input("์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”:")
input2 = input("๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”:")

total = int(input1) + int(input2)
print("๋‘ ์ˆ˜์˜ ํ•ฉ์€ %s ์ž…๋‹ˆ๋‹ค" % total)

Q4

๋‹ค์Œ ์ค‘ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ ํ•œ ๊ฐœ๋ฅผ ๊ณจ๋ผ ๋ณด์ž.

  1. print("you" "need" "python")
  2. print("you"+"need"+"python")
  3. print("you", "need", "python")
  4. print("".join(["you", "need", "python"]))

ํ’€์ด: 3๋ฒˆ

3๋ฒˆ์€ ๋„์–ด์“ฐ๊ธฐ ๋˜์–ด์„œ ๋‚˜์˜ด.

'Coding > Code in the Morning' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

211101_Code in the morning  (0) 2021.11.01
211029_Code in the morning  (0) 2021.10.29
211026_Code in the morning  (0) 2021.10.26
211024_Code in the Morning  (0) 2021.10.24
211023_Code in the Morning  (0) 2021.10.23