Creative Code

Chapter4(dic,set,메모리) 본문

Python Programming

Chapter4(dic,set,메모리)

빛하루 2023. 10. 13. 12:34
# 빈 딕셔너리를 생성하고 데이터 타입과 내용을 출력합니다.
d = dict()
print(type(d))  # 딕셔너리의 데이터 타입을 출력합니다.
print(d)

# 초기 데이터가 있는 딕셔너리를 생성하고 딕셔너리의 내용을 출력합니다.
dic = {'name': 'pey',
       'phone': '010-9999-1234',
       'birth': '1118',
       'address': '서울'}
print(dic)
print(dic['name'])  # 딕셔너리에서 'name' 키의 값을 출력합니다.

# 딕셔너리에 새로운 키-값 쌍을 추가하고 값을 변경합니다.
dic['etc'] = ''  # 'etc' 키를 추가하고 빈 문자열을 값으로 설정합니다.
print(dic)
dic['name'] = '홍길동'  # 'name' 키의 값을 '홍길동'으로 변경합니다.
print(dic)

# 딕셔너리에서 항목을 삭제하고, 키, 값, 키-값 쌍을 출력합니다.
del dic['etc']  # 'etc' 키와 해당 값 삭제
print(dic)
print(dic.keys())  # 모든 키를 출력합니다.
print(dic.values())  # 모든 값들을 출력합니다.
print(dic.items())  # 모든 키-값 쌍을 출력합니다.

# 딕셔너리의 키, 값, 키-값 쌍을 순회하며 출력합니다.
for i in dic.keys():
    print(i)

for i in dic.values():
    print(i)

for i in dic.items():
    print(i)

for i in dic:
    print(dic[i])

# 딕셔너리에서 특정 키의 값을 얻고, 특정 키의 존재 여부를 확인합니다.
print(dic.get('name'))  # 'name' 키의 값을 얻습니다.
print('name' in dic)  # 'name' 키가 딕셔너리에 있는지 확인합니다.
print('이름' in dic)  # '이름' 키가 딕셔너리에 있는지 확인합니다.

# 빈 집합을 생성하고 출력합니다.
a = set()
print(a)
print(type(a))

# 리스트를 생성하고 리스트가 빌 때까지 요소를 팝하여 출력합니다.
a = [1, 2, 3, 4]
while a:
    print(a.pop())

# 변수 a와 b의 아이디를 출력하고 두 변수가 동일한 객체를 참조하는지 확인합니다.
a = 1
b = 1
print(id(a), id(b))
print(a is b)

# 리스트 c와 d의 아이디를 출력하고 두 리스트가 동일한 객체를 참조하는지 확인합니다.
c = [1, 2, 3]
d = [1, 2, 3]
print(id(c), id(d))
print(c is d)

'Python Programming' 카테고리의 다른 글

Chapter5(제어문)  (0) 2023.10.13
Chapter3(리스트, 튜플)  (0) 2023.10.13
chapter2(문자열 자료형)  (0) 2023.10.12
chapter01(숫자형)  (0) 2023.10.12