Python은 '문자열'이나 List, Tuple 등의 Collection에 Index를 사용하며 값을 추출할 수 있다.
s='hello'
s[1]
'e'
즉 idex의 시작은 맨좌측부터 S[0], S[1], S[2], S[3], S[4]로 지정된다.
|| || || || ||
같은방식으로 맨우측부터 S[-1], S[-2], S[-3], S[-4] S[-5]로 지정된다.
s='hello'
s[2]==s[-3]
True
li=[100, 3.14, 'hello']
li
[100, 3.14, 'hello']
문자열과 같은 방식으로 li[0]은 100이다.
2-1. Index를 이용한 list 요소의 추가와 삭제가 가능하다.
1) append() method는 마지막 위치에 요소를 추가
2) insert(index,요소) method는 원하는 위치에 요소를 추가
3) pop(index) 원하는 위치의 요소를 제거함. 단, pop()는 마지막 요소를 제거함.
score=[50,40,30]
score.append(100) #마지막 요소로 100을 추가
score
[50,40,30,100]
score.insert(0,90)
score
[90,50,40,30,100]
score.pop(1)
score
[90,40,30,100]
score=[50,40,30,20,10]
score[0:2] # score[0]~score[2] 까지 추출
[50,40,30]
score=[50,40,30,20,10]
score[:2] # ~score[2] 까지 추출
[50,40,30]
score=[50,40,30,20,10]
score[0:6:2] # score[0]에서 [6]까지 2항씩 증가시키며 항목 추출, Straide라는 기능임.
[50,30,10]
오늘은 여기까지 ^^