-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.py
45 lines (36 loc) · 984 Bytes
/
dictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 17 18:08:51 2023
@author: Teja Ram Pooniya
@Program: Topic: Dictionary in Python How to Program
"""
"""
Discription:
Dictionaries:
A dictionary is an unordered collection of key-value pairs. Each key must be unique,
and keys are used to access corresponding values.
Dictionaries are defined using curly braces {}.
"""
# Creating a dictionary
person = {
'name': 'Teja',
'age': 34,
'city': 'Jodhpur'
}
# Accessing dictionary values
print(person['name']) # Output: 'John'
# Modifying dictionary values
person['age'] = 31
# Adding new key-value pairs
person['occupation'] = 'developer'
# Removing key-value pairs
del person['city']
# Checking key existence
if 'name' in person:
print("Name exists in the dictionary.")
# Looping through dictionary keys
for key in person:
print(key, person[key])
# Looping through dictionary items (key-value pairs)
for key, value in person.items():
print(key, value)