Python ๐Ÿ์—์„œ ํŠœํ”Œ ๋‚ด๋ถ€์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•

7056 ๋‹จ์–ด beginnerstutorialprogrammingpython
์ด ๊ธฐ์‚ฌ์—์„œ๋Š” ๊ฐ€๋ณ€ ๋ฐ์ดํ„ฐ ์œ ํ˜•๊ณผ ๋ถˆ๋ณ€ ๋ฐ์ดํ„ฐ ์œ ํ˜•์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด ๋…ผ์˜ํ•˜๊ณ  ํŠœํ”Œ ๋‚ด๋ถ€์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Python์—๋Š” ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ์—ฌ๋Ÿฌ ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ 4๊ฐ€์ง€ ๊ธฐ๋ณธ ์ œ๊ณต ๋ฐ์ดํ„ฐ ์œ ํ˜•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ ๋ชฉ๋ก, ์‚ฌ์ „, ์„ธํŠธ ๋ฐ ํŠœํ”Œ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฒ”์ฃผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ณ€๊ฒฝ ๊ฐ€๋Šฅ ๋Œ€ ๋ถˆ๋ณ€ ๋ฐ์ดํ„ฐ ์œ ํ˜•



์ฃผ์š” ์ฐจ์ด์ ์€ ๋ชฉ๋ก์ด๋‚˜ ์‚ฌ์ „๊ณผ ๊ฐ™์€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ์‚ฌ์šฉํ•  ๋•Œ ํ• ๋‹น ํ›„ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด:

list_a = ["first", "second", "third"] # this is a list
print(list_a) # output: ["first", "second", "third"]

list_a[0] = "zero"
print(list_a) # output: ["zero", "second", "third"]


๊ทธ๋Ÿฌ๋‚˜ ํŠœํ”Œ์˜ ๊ฒฝ์šฐ ํŠœํ”Œ์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๋‚ด๋ถ€์˜ ์š”์†Œ๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŠœํ”Œ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•˜๋ฉด:

tuple_a = ("element1", "element2", "element3")
print(tuple_a) # output: ('element1', 'element2', 'element3')

tuple_a[0] = "element0"


์œ ํ˜• ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.


ํ•˜์ง€๋งŒ ๋•Œ๋•Œ๋กœ ์šฐ๋ฆฌ๋Š” ํŠœํ”Œ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค...

๊ทธ๋ ‡๋‹ค๋ฉด ์ด ๋ณ€ํ™”๋ฅผ ์œ„ํ•ด ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?



๊ฐ€์žฅ ์‰ฌ์šด ์†”๋ฃจ์…˜์€ ๊ธฐ๋ณธ ์ œ๊ณต ๋ณ€ํ™˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ž, 3๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์œ ํ˜•์ด ๋‚จ์•„ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋Š ๊ฒƒ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๊นŒ?

_dictionaries _์—๋Š” ํ‚ค, ๊ฐ’ ์Œ์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ์ด ๊ฒฝ์šฐ ์‚ฌ์ „์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€ ์ƒ๊ฐ์ด ์•„๋‹™๋‹ˆ๋‹ค.

์„ธํŠธ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธํŠธ๋Š” ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ํ• ๋‹น ํ›„ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•˜์ง€๋งŒ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ค‘๋ณต ๊ฐ’(์˜ˆ: ๋‘ ๊ฐœ์˜ ๋™์ผํ•œ ๋ฌธ์ž์—ด)์„ ํฌํ•จํ•˜๋Š” ํŠœํ”Œ์„ ์„ธํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด ํ”ผํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ ์†์‹ค์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ์˜ ๋งˆ์ง€๋ง‰ ํ›„๋ณด๋Š” ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๋ฉฐ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ์™„๋ฒฝํ•œ ์†”๋ฃจ์…˜ ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?



๋จผ์ € ๋‚ด์žฅ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠœํ”Œ์„ ๋ชฉ๋ก์œผ๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

tuple_a = ("element1", "element2", "element3")
tuple_a = list(tuple_a)

print(tuple_a) # output: ["element1", "element2", "element3"]
print(type(tuple_a)) # output: <class 'list'>


๋ณ€ํ™˜ ํ›„ ๋ชฉ๋ก์„ ์–ป์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ’์„ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

tuple_a = ("element1", "element2", "element3")
tuple_a = list(tuple_a)

print(tuple_a) # output: ["element1", "element2", "element3"]
print(type(tuple_a)) # output: <class 'list'>

tuple_a[0] = "element0"
print(tuple_a) # output: ["element0", "element2", "element3"]


๋งˆ์ง€๋ง‰์œผ๋กœ ๋ชฉ๋ก์„ ๋‹ค์‹œ ํŠœํ”Œ๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

tuple_a = ("element1", "element2", "element3")
tuple_a = list(tuple_a)

print(tuple_a) # output: ["element1", "element2", "element3"]
print(type(tuple_a)) # output: <class 'list'>

tuple_a[0] = "element0"
print(tuple_a) # output: ["element0", "element2", "element3"]

tuple_a = tuple(tuple_a)

print(tuple_a) # output: ("element0", "element2", "element3")
print(type(tuple_a)) # output: <class 'tuple'>


๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฒŒ ๋‹ค์•ผ! ํŠœํ”Œ ๋‚ด๋ถ€์˜ ๊ฐ’์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค! ๐Ÿฅณ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ