set dictionary keys python code example

Example 1: how to set a key of dict in python

# declaring dict
dictionary={'Hello':3,'World!':4}
# creating function
def dict_add(obj,key,value):
  obj[key]=value
  reutrn obj
# using the functiom
dict_add(dictionary,'Python',10)
print (dictionary) # output --> { 'Hello':3 ,'World!':4 , 'Python':10 }

Example 2: python dictoinary add value

student_scores = {'Simon': 45  }
print(student_scores)
# {'Simon': 45}
student_scores['Sara'] = 63
print(student_scores)
# {'Simon': 45, 'Sara': 63}

Example 3: append to dictionary python

# Append to Dictionary in Python

# Let's say we had the following dictionary:

languages = {'#1': "Python", "#2": "Javascript", "#3": "HTML"}

# There are two ways to add a key-and-value set to this dictionary

# Number 1: By .update() method

languages.update({"#4": "C#"}) # Adds a #4 key-and-value set

#--------------------------------------------

# Number 2: The define-key method

# This is the easier one

languages['#4'] = 'C#'

# ^^ Just updates a key of #4 to C#, or adds it in this case