관리 메뉴

보리차

[파이썬] Tuple(튜플)/Dictionary 본문

파이썬

[파이썬] Tuple(튜플)/Dictionary

보리콩 2021. 5. 10. 13:56

Tuple

여러 자료형을 담을 수 있지만 값을 바꿀 수 없는 자료형? -> 튜플!

Tuple(튜플) : 여러 자료를 함께 담을 수 있는 자료형. ( ) - 소괄호로 묶어서 표현

    tuple_zer0 = ()

    tuple_one = (1, )   #원소가 하나 뿐!

    tuple_ = (1, 2, 3, 4, 5)

    tuple_ = 1, 2, 3, 4, 5

 

Tuple의 특징 : 시퀀스 자료형으로 Index를 이용한 인덱싱, 슬라이싱이 가능 

but 자료 추가, 삭제, 변경 불가. 한 번 만들어지면 고정! (append, remove, 대입 등이 불가능)

 

Dictionary

Dictionary: 짝꿍이 있는 자료형. {key:value}의 형식. key를 알면 value를 알 수 있음

    dict_zero = { }

    person = {'name':'michael', 'age':10}

Dictionary[key] : Dictionary에서 자료를 꺼내기, 추가하기

    print(person['name'])   # Michael

    print(person['age'])   # 10

    person['hometown'] = Seoul   # person = {'name':'michael', 'age':10, 'hometown':'Seoul}

del : del함수로 Dictionary의 원소 삭제

    del person['age']

    print(person)   #{'name':'michael'}

dictionary.values()

dictionary.keys()

dictionary.items()   # key와 value를 동시에 받아올 수 있다.

 

Dictionary의 특징 : key는 변할 수 없는 자료형 -> 리스트는 안되고 튜플은 된다.

    datas = {[1, 2, 3]:'Alphabet'}   # Error

    datas = {(1, 2, 3):'Number'}   #OK