์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ)
<์ด์ ๊ธ>
https://silvercoding.tistory.com/34
[python ๊ธฐ์ด] 2. ๋ฐ์ดํฐ ํ์ Data Type - ์ซ์ํ, ๋ฌธ์์ด
์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ) <์ด์ ๊ธ> https://silvercoding.tistory.com/33 https://www.python-course.eu/python3_formatted_output.php Python Tutorial: Formatted Output Even though it..
silvercoding.tistory.com
1. ๋ฆฌ์คํธ (list)
: ์์ O, ์ค๋ณต O, ์ญ์ O
- ์ ์ธ
a = []
b = list()
c = [1, 2, 3, 4]
d = [10, 100, 'Pen', 'Banana', 'Orange']
e = [10, 100, ['Pen', 'Banana', 'Orange']]
์ฌ๋ฌ ๋ฐฉ๋ฒ์ผ๋ก ๋ฆฌ์คํธ๋ฅผ ์ ์ธ ํด ์ค ์ ์๋ค.
- ์ธ๋ฑ์ฑ
print(d[3])
print(d[-2])
print(d[0] + d[1])
print(e[2][1])
print(e[-1][-2])
Banana
Banana
110
Banana
Banana
- ์ฌ๋ผ์ด์ฑ
print(d[0:3])
print(e[2][1:3])
[10, 100, 'Pen']
['Banana', 'Orange']
- ์ฐ์ฐ
print(c + d)
print(c * 3)
print(str(c[0]) + 'hi')
[1, 2, 3, 4, 10, 100, 'Pen', 'Banana', 'Orange']
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
1hi
- ๋ฆฌ์คํธ ์์ , ์ญ์
c[0] = 77
print(c)
[77, 2, 3, 4]
0๋ฒ์จฐ ์ธ๋ฑ์ค์ ๊ฐ ๋ฃ์ด์ฃผ๊ธฐ
c[1:2] = [100, 1000, 10000]
print(c) # ์์๊ฐ ๋ค์ด๊ฐ
[77, 100, 1000, 10000, 3, 4]
์ฌ๋ผ์ด์ฑ์ ์ด์ฉํ์ฌ ์ฌ๋ฌ ๊ฐ์ ๋ฃ์ด์ค ์ ์๋ค.
c[1] = ['a', 'b', 'c']
print(c) # ๋ฆฌ์คํธ๊ฐ ๋ค์ด๊ฐ
[77, ['a', 'b', 'c'], 1000, 10000, 3, 4]
๋ฆฌ์คํธ ์์ฒด๋ฅผ ๋ฃ์ด์ฃผ๋ ค๋ฉด ์ธ๋ฑ์ฑ์ ์ฌ์ฉํ๋ฉด ๋๋ค.
del c[1]
print(c)
[77, 1000, 10000, 3, 4]
del์ ์ด์ฉํ์ฌ ๋ฆฌ์คํธ์ ๊ฐ์ ์ญ์ ํ ์ ์๋ค.
del c[-1]
print(c)
[77, 1000, 10000, 3]
-๋ฆฌ์คํธ ๊ด๋ จ ํจ์
(1) append
y = [5, 2, 3, 1, 4]
print(y)
y.append(6) # ๋์ ์ถ๊ฐ
print(y)
[5, 2, 3, 1, 4]
[5, 2, 3, 1, 4, 6]
๋ฆฌ์คํธ์ ๊ฐ์ฅ ๋์ ๊ฐ์ ์ถ๊ฐํด ์ค๋ค.
(2) sort
y.sort() # ์ ๋ ฌ
print(y)
[1, 2, 3, 4, 5, 6]
๋ฆฌ์คํธ๋ฅผ ์ ๋ ฌํด ์ค๋ค.
(3) reverse
y.reverse()
print(y)
[6, 5, 4, 3, 2, 1]
๋ฆฌ์คํธ๋ฅผ ๋ค์ง์ด ์ค๋ค.
(4) insert
y.insert(2, 7)
print(y)
[6, 5, 7, 4, 3, 2, 1]
append์๋ ๋ค๋ฅด๊ฒ ์ํ๋ ์ธ๋ฑ์ค์ ๊ฐ์ ์ถ๊ฐํด์ค ์ ์๋ค.
(5) remove
y.remove(2)
y.remove(7)
print(y) # ์์ ๊ฐ์ ์ง์ด๋ค.
[6, 5, 4, 3, 1]
del์ ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ์ฌ ์ง์์ฃผ์๋๋ฐ, removeํจ์๋ ๊ฐ์ ๋ฃ์ผ๋ฉด ๊ทธ ๊ฐ์ ์ง์์ค๋ค.
(7) pop
y.pop()
print(y) # LIFO ์คํ ์๋ฃ๊ตฌ์กฐ
[6, 5, 4, 3]
pop์ ์ฌ์ฉํ๋ฉด ๊ฐ์ฅ ๋์ ์๋ ๊ฐ์ ์ง์์ค๋ค. LIFO(Last In First Out) ์ ์๋ฃ๊ตฌ์กฐ๋ก ์ฌ์ฉ๋๋ค.
(8) extend
ex = [88, 77]
# y.append(ex) # ๋ฆฌ์คํธ ์์ฒด๋ฅผ ์ถ๊ฐ
y.extend(ex) # ๊ฐ์ ์ถ๊ฐ
print(y)
[6, 5, 4, 3, 88, 77]
๋ฆฌ์คํธ ์์ ๋ฆฌ์คํธ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ด ์๋๋ผ, ๋ฆฌ์คํธ ์์ ์ฌ๋ฌ ๊ฐ์ ํ ๋ฒ์ ๋ฃ์ด์ฃผ๊ณ ์ถ์ ๋ extendํจ์๋ฅผ ์ฌ์ฉํ๋ค.
* ๋ฆฌ์คํธ ์ญ์ ์ด์ ๋ฆฌ
del, remove, pop ( : ์์ธ ๋ฐ์ ์ฃผ์)
2. ํํ (tuple)
: ์์ O, ์ค๋ณต O, ์์ X, ์ญ์ X
- ์ ์ธ
a = ()
b = (1,)
c = (1, 2, 3, 4)
d = (10, 100, ('a', 'b', 'c'))
์ด์ ๊ฐ์ด ์ฌ๋ฌ ๋ฐฉ๋ฒ์ผ๋ก ํํ์ ์ ์ธํ ์ ์๋ค.
- ์ญ์ : ๋ถ๊ฐ
del c[2] # -> ์ญ์ ๋ถ๊ฐ
TypeError: 'tuple' object doesn't support item deletion
ํํ์ ์์ ์ด๋ ์ญ์ ๋ฅผ ํ ์ ์๋ค.
- ์ธ๋ฑ์ฑ
print(c[2])
print(c[3])
print(d[2][2])
3
4
c
- ์ฌ๋ผ์ด์ฑ
print(d[2:])
print(d[2][0:2])
(('a', 'b', 'c'),)
('a', 'b')
- ์ฐ์ฐ
print(c + d)
print(c * 3)
(1, 2, 3, 4, 10, 100, ('a', 'b', 'c'))
(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
- ํํ ๊ด๋ จ ํจ์
z = (5, 2, 1, 3, 1)
print(z)
print(3 in z)
print(z.index(5)) # ์ธ๋ฑ์ค ๊ฐ์ ๋ฐํ
print(z.count(1)) # 1 ์ด ๋ช๊ฐ๊ฐ ์๋
(5, 2, 1, 3, 1)
True
0
2
'ํ๋ก๊ทธ๋๋ฐ ์ธ์ด > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python ๊ธฐ์ด] 5. ํ์ด์ฌ ํ๋ฆ์ ์ด (์ ์ด๋ฌธ) - ์กฐ๊ฑด๋ฌธ if (0) | 2021.07.31 |
---|---|
[python ๊ธฐ์ด] 4. ๋ฐ์ดํฐ ํ์ (2) (์๋ฃํ) - dictionary, set (0) | 2021.07.31 |
[python ๊ธฐ์ด] 2. ๋ฐ์ดํฐ ํ์ Data Type - ์ซ์ํ, ๋ฌธ์์ด (0) | 2021.07.30 |
[python ๊ธฐ์ด] 1. print ํจ์ (0) | 2021.07.30 |
[python ์ฌํ] 15. Asyncio, async await (0) | 2021.07.24 |