1 분 소요

PYTHON PROGRAMMING 기초

DICTIONARIES AND BOOLEANS

DICTIONARIES

  • my_dict = {‘key1’:’value1’, ‘key2’:’value2’, ‘key3’:’value3’}
  • 딕쳐너리는 키 밸류의 쌍으로 되어 있다.
  • 키는, 딕셔너리 안에 유일한 값으로 되어 있다. 따라서 키가 같은 값을 가질 수 없다. 그러나 밸류는 같은 값이 여러개 있어도 상관없다.
  • 리스트는 인덱스의 오프셋으로 접근하지만, 딕셔너리는 키로 접근한다.
dict()
{}
my_phone = {'brand' : 'apple','model' : 'iphone X', 'year' : 2018}
my_phone
{'brand': 'apple', 'model': 'iphone X', 'year': 2018}

한쌍 : item , 아이템 왼쪽은 key , 오른쪽은 value

my_phone['brand']
'apple'
my_phone['model']
'iphone X'
my_phone['year']
2018
my_phone.get('brand')
'apple'
my_phone.get('color')
'red'
my_phone
{'brand': 'apple', 'model': 'iphone X', 'year': 2018}
my_phone['model'] = "iphone 12"
my_phone
{'brand': 'apple', 'model': 'iphone 12', 'year': 2018}
my_phone['year'] = 2021
my_phone
{'brand': 'apple', 'model': 'iphone 12', 'year': 2021}
my_phone['color'] = 'red'
my_phone
{'brand': 'apple', 'model': 'iphone 12', 'year': 2021, 'color': 'red'}
del my_phone['model']
my_phone
{'brand': 'apple', 'year': 2021, 'color': 'red'}

키,벨류로 매칭값 찾기

my_phone.keys()
dict_keys(['brand', 'year', 'color'])
my_phone.values()
dict_values(['apple', 2021, 'red'])
my_phone.items()
dict_items([('brand', 'apple'), ('year', 2021), ('color', 'red')])

항목 찾기

'brand' in my_phone
True
'brand' not in my_phone
False
'apple' in my_phone.values()
True

clear() 함수 호출을 이용하여 내용지우기

my_list = [1,2,3,4,5]
my_list.clear()
my_list
[]
my_phone.clear()
my_phone
{}

BOOLEANS

  • Boolean 은 다음 2가지의 오브젝트로 나타낸다. “False” and “True”.
  • 숫자 0과 1과 같은 의미이다.
True or False
True

댓글남기기