๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Python

[Python] ์—ฐ์Šต๋ฌธ์ œ 2

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 11.
๋ฐ˜์‘ํ˜•

Q1. ์ด์ˆœ์‹  ์”จ์˜ ๊ณผ๋ชฉ๋ณ„ ์ ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ด์ˆœ์‹  ์”จ์˜ ํ‰๊ท  ์ ์ˆ˜๋ฅผ ๊ตฌํ•ด ๋ณด์ž.

๊ณผ๋ชฉ

์ ์ˆ˜

๊ตญ์–ด

80

์˜์–ด

75

์ˆ˜ํ•™

55

โ€‹A)

>>> a = 80
>>> b = 75
>>> c = 55
>>> (a+b+c)/3
70.0

 

 

Q2. ์ž์—ฐ์ˆ˜ 13์ด ํ™€์ˆ˜์ธ์ง€ ์ง์ˆ˜์ธ์ง€ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋งํ•ด ๋ณด์ž.

A)

>>> 13 % 2
1

 

 

Q3. ์ด์ˆœ์‹  ์”จ์˜ ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ๋Š” 881120-1068234์ด๋‹ค. ์ด์ˆœ์‹  ์”จ์˜ ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ๋ฅผ ์—ฐ์›”์ผ(yyyymmdd) ๋ถ€๋ถ„๊ณผ ๊ทธ ๋’ค์˜ ์ˆซ์ž ๋ถ€๋ถ„(num) ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ถœ๋ ฅํ•ด ๋ณด์ž.

A)

>>> pin = "881120-1068234"
>>> yyyymmdd = pin[:6]
>>> num = pin[7:]
>>> print(yyyymmdd)
881120
>>> print(num)
1068234

 

 

Q4. ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ ๋’ท์ž๋ฆฌ์˜ ๋งจ ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋Š” ์„ฑ๋ณ„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ์—์„œ ์„ฑ๋ณ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•ด๋ณด์ž.

A)

>>> pin = "881120-1068234"
>>> print(pin[7])
1

 

 

Q5. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ž์—ด a:b:c:d๊ฐ€ ์žˆ๋‹ค. ๋ฌธ์ž์—ด์˜ replace ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ a#b#c#d๋กœ ๋ฐ”๊ฟ”์„œ ์ถœ๋ ฅํ•ด ๋ณด์ž.

A)

>>> a = "a:b:c:d"
>>> b = a.replace(":", "#")
>>> print(b)
a#b#c#d

 

 

Q6. [1, 3, 5, 4, 2] ๋ฆฌ์ŠคํŠธ๋ฅผ [5, 4, 3, 2, 1]๋กœ ๋งŒ๋“ค์–ด ๋ณด์ž.

A)

>>> a = [1, 3, 5, 4, 2]
>>> a.sort()
>>> a.reverse()
>>> print(a)
[5, 4, 3, 2, 1]

๋ฆฌ์ŠคํŠธ์˜ ๋‚ด์žฅ ํ•จ์ˆ˜์ธ sort๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ๊ฐ’๋“ค์„ ๋จผ์ € ์ •๋ ฌํ•œ ํ›„ reverse ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœ์„œ๋ฅผ ๋’ค์ง‘๋Š”๋‹ค.

 

 

Q7. ['Life', 'is', 'too', 'short'] ๋ฆฌ์ŠคํŠธ๋ฅผ Life is too short ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด ์ถœ๋ ฅํ•ด ๋ณด์ž.

A)

>>> a = ['Life', 'is', 'too', 'short']
>>> result = " ".join(a)
>>> print(result)
Life is too short

a ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ๋‹จ์–ด๋“ค์„ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ์กฐ๋ฆฝํ•  ๋•Œ ๋‹จ์–ด๋“ค ์‚ฌ์ด๋งˆ๋‹ค ๊ณต๋ฐฑ์„ ๋„ฃ์–ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. 1๊ฐœ์˜ ๊ณต๋ฐฑ๋ฌธ์ž(" ")๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ joinํ•œ๋‹ค.

 

 

Q8. (1, 2, 3) ํŠœํ”Œ์— ๊ฐ’ 4๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ (1, 2, 3, 4)๋ฅผ ๋งŒ๋“ค์–ด ์ถœ๋ ฅํ•ด ๋ณด์ž.

A)

>>> a = (1, 2, 3)
>>> a = a + (4,)
>>> print(a)
(1, 2, 3, 4)

a ํŠœํ”Œ์— (4, ) ๋ผ๋Š” ํŠœํ”Œ์„ ๋”ํ•˜๋ฉด ๋œ๋‹ค. ๋‹จ, ์ด๋•Œ ๋งŒ๋“ค์–ด์ง€๋Š” a + (4, )์˜ ๊ฒฐ๊ณผ๋Š” a ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ(ํŠœํ”Œ์€ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค) ์ƒˆ๋กœ์šด ํŠœํ”Œ์ด ์ƒ์„ฑ๋˜๊ณ  ๊ทธ ๊ฐ’์ด a ๋ณ€์ˆ˜์— ๋Œ€์ž…๋˜๋Š” ๊ฒƒ์ž„.

โ˜… ๋ฆฌ์ŠคํŠธ์— ํŠœํ”Œ์„ ์ง‘์–ด๋„ฃ์œผ๋ ค๋ฉด (n, ) ์ด๋ผ๋Š” ๋ฌธ๋ฒ•์„ ์จ์•ผํ•˜๋Š”๋“ฏ

โ˜… ํŠœํ”Œ์ด ์ถ”๊ฐ€๋˜๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ๊ณ ์œ ์ฃผ์†Œ๊ฐ’์ด ๋ฐ”๋€œ

 

 

Q9. ๋”•์…”๋„ˆ๋ฆฌ a์—์„œ 'B'์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์„ ์ถ”์ถœํ•ด ๋ณด์ž.

a = {'A':90, 'B':80, 'C':70}
result =
print(a)
print(result)

 

A)

>>> a = {'A':90, 'B':80, 'C':70}
>>> result = a.pop('B')
>>> print(a)
{'A': 90, 'C': 70}
>>> print(result)
80

๋”•์…”๋„ˆ๋ฆฌ๋„ ๋ฆฌ์ŠคํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ pop ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

'B' ํ‚ค ๊ฐ’์— ํ•ด๋‹น๋˜๋Š” ๊ฐ’์ด ๋ฆฌํ„ด๋˜๊ณ  ๋”•์…”๋„ˆ๋ฆฌ a์—์„œ๋Š” ๊ทธ ๊ฐ’์ด ์ œ๊ฑฐ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

Q10. a ๋ฆฌ์ŠคํŠธ์—์„œ ์ค‘๋ณต ์ˆซ์ž๋ฅผ ์ œ๊ฑฐํ•ด ๋ณด์ž.

a = [1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5]
aSet =
b = 
print(b)

 

A)

>>> a = [1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5]
>>> aSet = set(a)   ← a ๋ฆฌ์ŠคํŠธ๋ฅผ ์ง‘ํ•ฉ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€ํ™˜
>>> b = list(aSet)   ← ์ง‘ํ•ฉ์ž๋ฃŒํ˜•์„ ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์œผ๋กœ ๋‹ค์‹œ ๋ณ€ํ™˜
>>> print(b)
[1, 2, 3, 4, 5]

๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์ด ์ง‘ํ•ฉ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€ํ™˜๋˜๋ฉด์„œ ์ค‘๋ณต๋œ ๊ฐ’๋“ค์€ ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค. ์ด์™€ ๊ฐ™์€ ์„ฑ์งˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ ๋‚ด์— ์ค‘๋ณต๋œ ๊ฐ’์„ ์‰ฝ๊ฒŒ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.

โ€‹

 

Q11. ํŒŒ์ด์ฌ์€ ๋‹ค์Œ์ฒ˜๋Ÿผ ๋™์ผํ•œ ๊ฐ’์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด a, b ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ ํ›„ a์˜ ๋‘ ๋ฒˆ์งธ ์š”์†Œ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด b ๊ฐ’์€ ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ์ด์œ ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด ๋ณด์ž.

>>> a = b = [1, 2, 3]
>>> a[1] = 4
>>> print(b)

 

A)

[1, 4, 3]์ด ์ถœ๋ ฅ๋œ๋‹ค. a์™€ b ๋ณ€์ˆ˜๋Š” ๋ชจ๋‘ ๋™์ผํ•œ [1, 2, 3]์ด๋ผ๋Š” ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋ฐ˜์‘ํ˜•

'Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Python] while๋ฌธ  (0) 2021.02.11
[Python] if๋ฌธ  (0) 2021.02.11
[Python] ๋ณ€์ˆ˜  (0) 2021.02.10
[Python] ๋ถˆ ์ž๋ฃŒํ˜•  (0) 2021.02.10
[Python] ์ง‘ํ•ฉ ์ž๋ฃŒํ˜•  (0) 2021.02.10

๋Œ“๊ธ€