제로베이스 자료구조(20~25)

ningbbang·2023년 3월 31일
0

Zerobase DS13

목록 보기
14/48

1. 튜플(Tuple)
1) 선언 : tuple = (아이템1, 아이템2 ---)
list와의 차이점은 tuple은 아이템을 수정할 수 없음
tuple안에 또 다른 tuple 삽입

2) 아이템 조회 : tuple[인덱스]
리스트와 마찬가지로 [인덱스]로 아이템 조회 가능

datas = ('a', 'b', 'c', 'd', 'e')
print(datas[1])
print(datas[2:])

2. in 과 not in

1) if data in tuple : data가 tuple에 있으면 True
2) if data not in tuple : data가 tuple에 없으면 True
3) 문자열에도 사용 가능

data = 'python pytho1 ypdjdwjasd'
if 'ypd' in data:
    print(True)
else:
    print(False)
#result : True

3. Tuple 길이
len(tuple) : 튜플의 아이템 개수

4. Tuple 연장
tuple1 + tuple2
extend함수는 사용 불가능 (tuple은 값을 수정할 수 없으므로)
변수를 튜플로 변환하려면 : (변수 또는 상수, )

datas = ('a', 'b', 'c', 'd', 'e')
print(datas)

n = 'f'
datas2 = datas + (n, )
print(datas2)

5. Tuple 슬라이싱
tuple[n:m] : 인덱스n 부터 인덱스m-1 까지의 아이템 호출
단, 리스트와 다르게 슬라이싱으로 아이템을 변경할 수는 없음

profile
HR Anaylist!

0개의 댓글