dictionary python

 # dictionary is used to store data values in key:value pairs

# aftrer 3.7 version-> ordered , mutable, do not allow duplicate keys
# key only be immutable dat type.

# student={
#     "name":"ved",
#     "subject":["python","java","c"],
#     "chapter":(1,2,3,4,5),
#     "roll_no":98,
#     "gender":"male",
#     "percentage":91.87,
#      1:520
# }

# print(student)
# print(type(student))

# access value through key-> if key is not present and we want to access then error
# print(student["name"])
# print(student[1])

# change value ->
# student["name"]="sam"
# print(student["name"])

# add another key value->
# student["surname"]="varshney"
# print(student)

# empty dictionary->
# null_dict={}
# print(type(null_dict))
# null_dict["name"]="ved"
# print(null_dict)

# nested dictionary
# student={
#     "name":"ved",
#     "age":22,
#     "roll":98,
#     "subjects":{
#         "phy":98,
#         "math":100,
#         "hindi":89,
#         "english":94
#     }
# }
# print(student["subjects"]["hindi"])

# methods of dictionary->
# 1- keys()->used to access all keys
# print(student.keys())

# 2- len()->
# print(len(student))

# 3- values()->used to access all values
# print(student.values())

# 4- items()-> returns all (key,value) pairs as tuples
# print(list(student.items()))          

# 5- get()-> retuen value of given key
# if enter wrong key in get() method , no error , get None.
# print(student.get("name"))

# 6- update(pair/new_dict)-> inserts the new_dict ot pair in dictioanry
# student.update({"city":"ghaziabad"})
# print(student)

# new_dict={
#     "capital":"up"
# }
# student.update(new_dict)
# print(student)

#  pop()-> remove item from dictionary
# student.pop("name")
# print(student)

# popitem()-> removes last added item from dictionary
# student.popitem()
# print(student)

# clear()-> removes all data
# student.clear()
# print(student)

dict={
    "a":100,
    "b":200,
    "c":300
}
sum=0
for x in dict:
    sum+=dict.get(x)
print(sum)

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.