Coding/Code in the Morning

211026_Code in the morning

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 10. 26. 07:57

Q5

A ํ•™๊ธ‰์— ์ด 10๋ช…์˜ ํ•™์ƒ์ด ์žˆ๋‹ค. ์ด ํ•™์ƒ๋“ค์˜ ์ค‘๊ฐ„๊ณ ์‚ฌ ์ ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

[70, 60, 55, 75, 95, 90, 80, 80, 85, 100]

for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ A ํ•™๊ธ‰์˜ ํ‰๊ท  ์ ์ˆ˜๋ฅผ ๊ตฌํ•ด ๋ณด์ž.

 

ํ’€์ด: 

๋”๋ณด๊ธฐ

A

A = [70, 60, 55, 75, 95, 90, 80, 80, 85, 100]
total = 0
for score in A:
    total += score
average = total / len(A)
print(average)

Q6

๋ฆฌ์ŠคํŠธ ์ค‘์—์„œ ํ™€์ˆ˜์—๋งŒ 2๋ฅผ ๊ณฑํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค.

numbers = [1, 2, 3, 4, 5]

result = [] for n in numbers:

     if n % 2 == 1:

             result.append(n*2)

์œ„ ์ฝ”๋“œ๋ฅผ ๋ฆฌ์ŠคํŠธ ๋‚ดํฌ(list comprehension)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„ํ•ด ๋ณด์ž.

 

ํ’€์ด

๋”๋ณด๊ธฐ
numbers = [1, 2, 3, 4, 5]
result = [num * 2 for num in numbers if num%2 ==1]
print(result)

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

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