[자료구조] List와 Tuple

신은지·2024년 8월 3일

Data Structure

목록 보기
21/31
post-thumbnail

List VS Tuple

차이점

  • Tuple은 List와 달리 아이템 추가, 변경, 삭제가 불가하다.
  • Tuple은 선언 시, 괄호 생략이 가능하다.
# List
studentsList = ['짱구', '철수', '훈이', '맹구' , '유리']
print(studentsList)
print(type(studentsList))

# Tuple
studentsTuple = ('짱구', '철수', '훈이', '맹구' , '유리')
print(studentsTuple)
print(type(studentsTuple))

# Tuple
studentsTuple2 = '짱구', '철수', '훈이', '맹구' , '유리'
print(studentsTuple2)
print(type(studentsTuple2))
  • List와 Tuple은 자료형 변환이 가능하다.
students = ['짱구', '철수', '훈이', '맹구' , '유리']
print(students)
print(type(students))

# List >>> Tuple
students = tuple(students)
print(students)
print(type(students))

# Tuple >>> List
students = list(students)
print(students)
print(type(students))

💡Python으로 Tuple을 이용하여 출력하기

# Tuple을 이용한 점수표에서 최저, 최고 점수를 삭제한 후 총점과 평균을 출력
'''
점수 1: 9.5,
점수 2: 8.9,
점수 3: 9.2,
점수 4: 9.8,
점수 5: 8.8,
점수 6: 9.0
'''
scoreTuple = (9.5, 8.9, 9.2, 9.8, 8.8, 9.0)
print(type(scoreTuple))
print(scoreTuple)
print('--- Tuple에서 List로 형 변환 ---')
scoreList = list(scoreTuple)
print(type(scoreList))
print(scoreList)
print()

scoreList.sort()
print('최저 점수: {}'.format(scoreList.pop(0)))
print('최고 점수: {}'.format(scoreList.pop(len(scoreList) - 1)))
print('최저/최고 점수 제거 후: {}'.format(scoreList))
print()

print('총점: {}, 평균: {:.2f}'.format(sum(scoreList), (sum(scoreList)/len(scoreList))))





* 이 글은 제로베이스 데이터 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다.

profile
I believe there is no best, only better

0개의 댓글