딕셔너리(dictionary)는 파이썬에서 키-값 쌍을 구현하는 데 사용됩니다.
딕셔너리(dictionary)는 파이썬의 데이터 유형으로 특정 키에 대해 특정 값이 존재하는 실제 데이터 배열을 시뮬레이션 할 수 있습니다.
딕셔너리(dictionary)는 키-불변 파이썬 객체, 즉 숫자, 문자열 또는 튜플 인 반면 값은 임의의 파이썬 객체가 될 수있는 키-값 쌍의 모음이라고 말할 수 있습니다.
# 딕셔너리(dictionary) 생성하기
작은 괄호 ()로 묶고 콜론 (:)으로 구분 된 여러 키-값 쌍을 사용하여 dictionary를 작성할 수 있습니다.
키-값 쌍의 콜렉션은 중괄호 ({})로 묶습니다.
dictionary를 정의하는 구문은 다음과 같습니다.
Dict = {"Name": "Ayush","Age": 22}
# 딕셔너리(dictionary) 생성 및 출력
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
print(type(Employee))
print("printing Employee data .... ")
print(Employee)
# 결과
<class 'dict'>
printing Employee data ....
{'Age': 29, 'salary': 25000, 'Name': 'John', 'Company': 'GOOGLE'}
# 딕셔너리(dictionary) 값에 액세스
인덱싱을 사용하여 목록 및 튜플에서 데이터에 액세스하는 방법에 대해 설명했습니다.
그러나 dictionary에서 키가 고유하므로 키를 사용하여 dictionary에서 값에 액세스 할 수 있습니다.
dictionary 값은 다음과 같은 방식으로 액세스 할 수 있습니다.
# 예시
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
print(type(Employee))
print("printing Employee data .... ")
print("Name : %s" %Employee["Name"])
print("Age : %d" %Employee["Age"])
print("Salary : %d" %Employee["salary"])
print("Company : %s" %Employee["Company"])
# 결과
<class 'dict'>
printing Employee data ....
Name : John
Age : 29
Salary : 25000
Company : GOOGLE
dictionary는 변경 가능한 데이터 유형이며 특정 키를 사용하여 값을 업데이트 할 수 있습니다.
# 예시
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
print(type(Employee))
print("printing Employee data .... ")
print(Employee)
print("Enter the details of the new employee....");
Employee["Name"] = input("Name: ");
Employee["Age"] = int(input("Age: "));
Employee["salary"] = int(input("Salary: "));
Employee["Company"] = input("Company:");
print("printing the new data");
print(Employee)
# 결과
<class 'dict'>
printing Employee data ....
{'Name': 'John', 'salary': 25000, 'Company': 'GOOGLE', 'Age': 29}
Enter the details of the new employee....
Name: David
Age: 19
Salary: 8900
Company:JTP
printing the new data
{'Name': 'David', 'salary': 8900, 'Company': 'JTP', 'Age': 19}
'파이썬' 카테고리의 다른 글
파이썬 세트(set) (0) | 2019.08.22 |
---|---|
파이썬 튜플(tuple) (0) | 2019.08.21 |
파이썬 리스트(list) (0) | 2019.08.20 |
파이썬 문자열(String) (0) | 2019.08.20 |
파이썬 continue 문 (0) | 2019.08.20 |