l = []
l = [1, 2, "Great"]
print(l[0], l[-1])
print(l[1:3], l[:])
print()
L = range(10)
print(L[::2])
print(list(L)[::2])
print()
print(l * 2)
print(l + [3, 4, 5])
print(len(l))
print()
print(4 in L)
1 Great [2, 'Great'] [1, 2, 'Great'] range(0, 10, 2) [0, 2, 4, 6, 8] [1, 2, 'Great', 1, 2, 'Great'] [1, 2, 'Great', 3, 4, 5] 3 True
a = ['spam', 'eggs', 100, 1234]
a[2] = a[2] + 23
print(a)
['spam', 'eggs', 123, 1234]
a = ['spam', 'eggs', 123, 1234]
a[0:2] = [1, 12] # 동일한 크기에 대한 슬라이스 치환
print(a)
a[0:2] = [1] # 서로 다른 크기에 대한 슬라이스 치환
print(a)
a[0:1] = [1, 2, 3] # 서로 다른 크기에 대한 슬라이스 치환
print(a)
[1, 12, 123, 1234] [1, 123, 1234] [1, 2, 3, 123, 1234]
a = [1, 12, 123, 1234]
a[0:2] = []
print(a)
[123, 1234]
a = [123, 1234]
a[1:1] = ['spam', 'ham'] # 1번째 인덱스에 삽입
print(a)
b = [123, 1234]
b[1:2] = ['spam', 'ham'] # 1번째 원소에 대한 치환
print(b)
a[:0] = a # 리스트 맨 앞에 자기 자신을 삽입
print(a)
[123, 'spam', 'ham', 1234] [123, 'spam', 'ham'] [123, 'spam', 'ham', 1234, 123, 'spam', 'ham', 1234]
a = [1, 2, 3, 4]
del a[0]
print(a)
del a[1:]
print(a)
[2, 3, 4] [2]
# a = range(4)
a = list(range(4))
print(a)
print(a[::2])
del a[::2]
print(a)
[0, 1, 2, 3] [0, 2] [1, 3]
a = range(5)
del a
print(a)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-10-a1e3c3afe17a> in <module>() 1 a = range(5) 2 del a ----> 3 print(a) NameError: name 'a' is not defined
s = [1, 2, 3]
t = ['begin', s, 'end']
print(t)
print(t[1][1])
['begin', [1, 2, 3], 'end'] 2
s[1] = 100
print(t)
['begin', [1, 100, 3], 'end']
L = [1, ['a', ['x', 'y'], 'b'], 3]
print(L[0])
print(L[1])
print(L[1][1])
print(L[1][1][1])
1 ['a', ['x', 'y'], 'b'] ['x', 'y'] y
print(range(10)) # 0(Included)부터 10(Excluded)까지
print(range(5, 15)) # 5(Included)부터 15(Excluded)까지
print(range(5, 15, 2)) # 0(Included)부터 10(Excluded)까지, Step: 2
range(0, 10) range(5, 15) range(5, 15, 2)
print(list(range(10))) # 0(Included)부터 10(Excluded)까지
print(list(range(5, 15))) # 5(Included)부터 15(Excluded)까지
print(list(range(5, 15, 2))) # 0(Included)부터 10(Excluded)까지, Step: 2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [5, 7, 9, 11, 13]
for el in range(10):
print(el, 'inch =', el * 2.54, 'centi')
0 inch = 0.0 centi 1 inch = 2.54 centi 2 inch = 5.08 centi 3 inch = 7.62 centi 4 inch = 10.16 centi 5 inch = 12.7 centi 6 inch = 15.24 centi 7 inch = 17.78 centi 8 inch = 20.32 centi 9 inch = 22.86 centi
for el in range(10):
print("%d inch = %.2f centi" % (el, el * 2.54))
0 inch = 0.00 centi 1 inch = 2.54 centi 2 inch = 5.08 centi 3 inch = 7.62 centi 4 inch = 10.16 centi 5 inch = 12.70 centi 6 inch = 15.24 centi 7 inch = 17.78 centi 8 inch = 20.32 centi 9 inch = 22.86 centi
for el in range(10):
print("{0} inch = {1:.2f} centi".format(el, el * 2.54))
0 inch = 0.00 centi 1 inch = 2.54 centi 2 inch = 5.08 centi 3 inch = 7.62 centi 4 inch = 10.16 centi 5 inch = 12.70 centi 6 inch = 15.24 centi 7 inch = 17.78 centi 8 inch = 20.32 centi 9 inch = 22.86 centi
sun, mon, tue, wed, thu, fri, sat = range(7)
print(sun, mon, tue, wed, thu, fri, sat)
0 1 2 3 4 5 6
lt = [('one', 1), ('two', 2), ('three', 3)]
for t in lt:
print('name =', t[0] ,', num =', t[1])
name = one , num = 1 name = two , num = 2 name = three , num = 3
lt = [('one', 1), ('two', 2), ('three', 3)]
for t in lt:
print('name = %s, num = %s' % t)
name = one, num = 1 name = two, num = 2 name = three, num = 3
lt = [('one', 1), ('two', 2), ('three', 3)]
for t in lt:
print('name = {0}, num = {1}'.format(t[0], t[1]))
name = one, num = 1 name = two, num = 2 name = three, num = 3
lt = [('one', 1), ('two', 2), ('three', 3)]
for name, num in lt:
print(name, num)
one 1 two 2 three 3
LL = [['one', 1], ['two', 2], ['three', 3]]
for name, num in LL:
print(name, num)
one 1 two 2 three 3
s = [1, 2, 3]
s.append(5) # 리스트 맨 마지막에 정수 값 5 추가
print(s)
s.insert(3, 4) # 3 인덱스 위치에 정수 값 4 추가
print(s)
[1, 2, 3, 5] [1, 2, 3, 4, 5]
s = [1, 2, 3, 4, 5]
print(s.index(3)) # 값 3의 인덱스 반환
print(s.count(2)) # 값 2의 개수 반환
s = [1, 2, 2, 2, 2, 2, 3, 4, 5]
print(s.count(2))
2 1 5
s = [1, 2, -10, -7, 100]
k = s.reverse() # 자료의 순서를 뒤집기 (반환값 없음 --> 리스트 자체의 내부 원소 배열이 변경됨)
print(k)
print(s)
k = s.sort() # 정렬 (반환값 없음 --> 리스트 자체의 내부 원소 배열이 변경됨)
print(k)
print(s)
None [100, -7, -10, 2, 1] None [-10, -7, 1, 2, 100]
s = [10, 20, 30, 40, 50]
s.remove(10) # 자료 값 10 삭제
print(s)
s = [10, 20, 30, 20, 40, 50] # 자료 값이 여러개 존재하면 첫번째 것만 삭제
s.remove(20)
print(s)
s.extend([60, 70]) # 새로운 리스트 [60, 70]를 기존 리스트 s 뒤에 병합
print(s)
s.append([60, 70]) # 주의: append로 새로운 리스트를 추가하면 하나의 자료 요소로서 추가
print(s)
[20, 30, 40, 50] [10, 30, 20, 40, 50] [10, 30, 20, 40, 50, 60, 70] [10, 30, 20, 40, 50, 60, 70, [60, 70]]
s = [10, 20, 30, 40, 50]
s.append(60)
print(s)
# If no index is specified, a.pop() removes and returns the last item in the list.
print(s.pop())
print(s)
[10, 20, 30, 40, 50, 60] 60 [10, 20, 30, 40, 50]
s = [10, 20, 30, 40, 50]
print(s.pop(0)) #0 번째 인덱스 값을 꺼낸다.
print(s)
print(s.pop(1)) #1 번째 인덱스 값을 꺼낸다.
print(s)
10 [20, 30, 40, 50] 30 [20, 40, 50]
q = [10, 20, 30, 40, 50]
q.append(60)
print(q.pop(0))
print(q)
10 [20, 30, 40, 50, 60]
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일