0% found this document useful (0 votes)
3 views6 pages

Python Dictionary Operations Guide

The document contains various Python dictionary operations, including creation, updating, deletion, and iteration. It also demonstrates how to handle nested dictionaries and perform operations like sorting, finding maximum and minimum values, and counting character frequencies. Additionally, it showcases how to manage student records and their corresponding results based on marks.

Uploaded by

mail2kabilan.m
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Python Dictionary Operations Guide

The document contains various Python dictionary operations, including creation, updating, deletion, and iteration. It also demonstrates how to handle nested dictionaries and perform operations like sorting, finding maximum and minimum values, and counting character frequencies. Additionally, it showcases how to manage student records and their corresponding results based on marks.

Uploaded by

mail2kabilan.m
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

d = {"name": "Srimathi", "age": 20, "course": "EAI"}

print(d)

{'name': 'Srimathi', 'age': 20, 'course': 'EAI'}

d = {}
d["city"] = "Chennai"
print(d)

{'city': 'Chennai'}

d = {"a": 10, "b": 20}


print(d["a"])

10

d = {"marks": 80}
d["marks"] = 90
print(d)

{'marks': 90}

d = {"x": 1, "y": 2}
del d["x"]
print(d)

{'y': 2}

d = {"name": "srimathi"}
print("name" in d)

True

d = {1:10, 2:20, 3:30}


print(len(d))

d = {"a":1, "b":2}
for k in d:
print(k)

a
b

d = {"a":1, "b":2}
for v in [Link]():
print(v)

1
2
d = {"a":1, "b":2}
for k,v in [Link]():
print(k, v)

a 1
b 2

d1 = {1:10}
d2 = [Link]()
print(d2)

{1: 10}

d = {1:10, 2:20}
[Link]()
print(d)

{}

d1 = {1:10}
d2 = {2:20}
[Link](d2)
print(d1)

{1: 10, 2: 20}

s = "python"
freq = {}
for c in s:
freq[c] = [Link](c, 0) + 1
print(freq)

{'p': 1, 'y': 1, 't': 1, 'h': 1, 'o': 1, 'n': 1}

d = {"a":10, "b":20}
print(sum([Link]()))

30

d = {"a":10, "b":50}
print(max([Link]()))

50

d = {"a":10, "b":50}
print(min([Link]()))

10

d = {3:30, 1:10, 2:20}


print(dict(sorted([Link]())))

{1: 10, 2: 20, 3: 30}


d = {"a":30, "b":10}
print(dict(sorted([Link](), key=lambda x:x[1])))

{'b': 10, 'a': 30}

d = {}
n = int(input("Enter number of items: "))
for i in range(n):
k = input("Key: ")
v = input("Value: ")
d[k] = v
print(d)

Enter number of items: 3


Key: name
Value: srimathi
Key: age
Value: 20
Key: course
Value: EAI

{'name': 'srimathi', 'age': '20', 'course': 'EAI'}

marks = {"bhuvaneshwari":80, "srimathi":90}


for s,m in [Link]():
print(s, m)

bhuvaneshwari 80
srimathi 90

d = {"a":10, "b":50}
print(max(d, key=[Link]))

d = {x:x*x for x in range(1,6)}


print(d)

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

keys = ["a","b","c"]
values = [1,2,3]
d = dict(zip(keys, values))
print(d)

{'a': 1, 'b': 2, 'c': 3}

student = {"id":1, "marks":{"math":90, "cs":95}}


print(student)

{'id': 1, 'marks': {'math': 90, 'cs': 95}}


d = {"a":1, "b":2}
print(list([Link]()))

['a', 'b']

d = {"a":1, "b":2}
print(list([Link]()))

[1, 2]

t = tuple(map(int, input("Enter numbers: ").split()))

unique = list(set(t)) # remove duplicates


[Link](reverse=True)

print("Third Biggest Number:", unique[2])

Enter numbers: 10 45 20 45 30 50

Third Biggest Number: 30

t = tuple(map(int, input().split()))

first = second = third = -1

for num in t:
if num > first:
third = second
second = first
first = num
elif num > second and num != first:
third = second
second = num
elif num > third and num != second and num != first:
third = num

print("Third Biggest Number:", third)

10 45 20 30 50

Third Biggest Number: 30

students = {
101: {"name": "bhuvaneshwari", "age": 20, "course": "EAI",
"marks": 75},
102: {"name": "Srimathi", "age": 21, "course": "EAI", "marks":
85},
103: {"name": "priya", "age": 22, "course": "EAI", "marks": 70}
}

print("---- STUDENT DETAILS---------")


for roll, details in [Link]():
print("\nRoll No:", roll)
for key, value in [Link]():
print(key, ":", value)

if 101 in students: students[101]


["marks"] = 88

students[104] = {
"name": "priya",
"age": 22,
"course": "EAI",
"marks": 78
}

if 103 in students:
del students[103]

print("\n---- UPDATED STUDENT LIST---------")


for roll, details in [Link]():
print(roll, ":", details)

print("\n---- RESULT STATUS---------")


for roll, details in [Link]():
if [Link]("marks", 0) >= 80:
print(details["name"], "PASSED")
else:
print(details["name"], "FAILED")
---- STUDENT DETAILS ----

Roll No: 101


name : bhuvaneshwari
age : 20
course : EAI
marks : 75

Roll No: 102


name :
Srimathi age :
21 course :
EAI marks : 85

Roll No: 103


name : priya
age : 22
course : EAI
marks : 70

---- UPDATED STUDENT LIST ----


101 : {'name': 'bhuvaneshwari', 'age': 20, 'course': 'EAI', 'marks':
88}
102 : {'name': 'Srimathi', 'age': 21, 'course': 'EAI', 'marks': 85}
104 : {'name': 'priya', 'age': 22, 'course': 'EAI', 'marks': 78}

---- RESULT STATUS ----


bhuvaneshwari PASSED
Srimathi PASSED
priya FAILED

You might also like