List와 Tuple

이재문·2021년 5월 28일
0

List

[ ] 대괄호 형식으로 이루어져있다.
[ ] 안에 데이터를 입력하고 추가와 삭제가 가능하다.

fruit = ["apple", "orange", "grape", "peach", "tomato"]

이런 형식으로 list를 만들 수 있다.
list의 작업으로

fruit.append("strawberry")
print(fruit)
# fruit 이라는 이름의 list에 append를 이용하여 "strawberry"를 추가한다.

print(fruit[3])
# list에서 3번째 있는 항목을 가져온다.
# list는 괄호 안의 순서대로 0,1,2,3... 순으로 셀 수 있다.

fruit.remove("tomato")
print(fruit)
# list에서 "tomato"를 지운다.

fruit.insert(1, "melon")
print(fruit)
# list의 첫번째 자리에 "melon"을 삽입한다.

fruit.reverse()
print(fruit)
# list의 순서를 뒤집는다.

fruit.clear()
print(fruit)
#list를 전체 삭제한다.

등이 있다.

Tuple

list와 같이 데이터를 저장 할 수 있지만
tuple 내의 정보를 삽입, 삭제, 수정 할 수 없다.

사용방법은 list와 비슷하지만 [ ] 가 아닌 ( )를 이용한다.

profile
이제부터 백엔드 개발자

0개의 댓글