Adding Dictionary Elements (डिक्शनरी में Elements जोड़ना) :-
Adding Dictionary Elements (डिक्शनरी में Elements जोड़ना) का अर्थ Dictionary में नया Key-Value Pair जोड़ना है। Python में Dictionary Mutable (परिवर्तनीय) होती है, इसलिए Dictionary बनाने के बाद उसमें नए Elements जोड़े जा सकते हैं।
English
Adding Dictionary Elements means adding a new Key-Value Pair to a Dictionary. In Python, a Dictionary is Mutable, so new Elements can be added after creating the Dictionary.
1. Using Square Brackets [] :-
Dictionary में नया Element जोड़ने का सबसे सामान्य तरीका Square Brackets [] का उपयोग करना है।
English
The most common way to add a new Element to a Dictionary is by using Square Brackets [].
Syntax -
dictionary_name[key] = value
Example -
student = {
"name": "Rahul",
"age": 20
}
student["course"] = "O Level"
print(student)
Output-
{'name': 'Rahul', 'age': 20, 'course': 'O Level'}
यहाँ:
"course" → New Key
"O Level" → New Value
इस प्रकार "course": "O Level" एक नया Key-Value Pair के रूप में Dictionary में Add हो गया।
2. Adding Multiple Elements :-
Dictionary में एक से अधिक Elements को एक-एक करके भी Add किया जा सकता है।
Example -
student = {
"name": "Rahul"
}
student["age"] = 20
student["course"] = "O Level"
student["city"] = "Mau"
print(student)
Output-
{'name': 'Rahul', 'age': 20, 'course': 'O Level', 'city': 'Mau'}
3. Using update() Method :-
Dictionary में एक या एक से अधिक Key-Value Pairs जोड़ने के लिए update() Method का उपयोग किया जा सकता है।
English
The update() Method can be used to add one or more Key-Value Pairs to a Dictionary.
Syntax -
dictionary_name.update({
key1: value1,
key2: value2
})
Example -
student = {
"name": "Rahul"
}
student.update({
"age": 20,
"course": "O Level",
"city": "Mau"
})
print(student)
Output-
{'name': 'Rahul', 'age': 20, 'course': 'O Level', 'city': 'Mau'}
4. Adding Elements to an Empty Dictionary :-
सबसे पहले एक Empty Dictionary बनाई जा सकती है और बाद में उसमें Elements Add किए जा सकते हैं।
English
First, an Empty Dictionary can be created, and then Elements can be added to it later.
Example -
student = {}
student["name"] = "Rahul"
student["age"] = 20
student["course"] = "O Level"
print(student)
Output-
{'name': 'Rahul', 'age': 20, 'course': 'O Level'}
5. Adding Different Types of Values :-
Dictionary में अलग-अलग Data Types की Values को Add किया जा सकता है।
Example -
student = {}
student["name"] = "Rahul"
student["age"] = 20
student["marks"] = 85.5
student["passed"] = True
print(student)
Output-
{'name': 'Rahul', 'age': 20, 'marks': 85.5, 'passed': True}
यहाँ:
"name" → String Value
"age" → Integer Value
"marks" → Float Value
"passed" → Boolean Value
6. Adding a List as a Value :-
Dictionary में किसी Key की Value के रूप में List भी Add की जा सकती है।
Example -
student = {
"name": "Rahul"
}
student["subjects"] = ["Python", "HTML", "CSS"]
print(student)
Output-
{'name': 'Rahul', 'subjects': ['Python', 'HTML', 'CSS']}
7. Adding a Dictionary as a Value :-
एक Dictionary को दूसरी Dictionary के अंदर Value के रूप में Add किया जा सकता है। इसे Nested Dictionary कहा जाता है।
Example -
student = {}
student["details"] = {
"age": 20,
"city": "Mau"
}
print(student)
Output-
{'details': {'age': 20, 'city': 'Mau'}}