Creating a Dictionary (डिक्शनरी बनाना) :-
Creating a Dictionary (डिक्शनरी बनाना) का अर्थ Python में एक ऐसी Dictionary तैयार करना है जिसमें Data को Key-Value Pair के रूप में Store किया जाता है।
Python में Dictionary को बनाने के लिए मुख्य रूप से Curly Braces {} तथा dict() Function का उपयोग किया जाता है।
English
Creating a Dictionary means creating a Dictionary in Python to store data in the form of Key-Value Pairs.
A Dictionary can mainly be created using Curly Braces {} and the dict() Function.
1. Using Curly Braces {} :-
Dictionary बनाने का सबसे सामान्य तरीका Curly Braces {} का उपयोग करना है।
English
The most common way to create a Dictionary is by using Curly Braces {}.
Syntax -
dictionary_name = {
key1: value1,
key2: value2,
key3: value3
}
Example -
student = {
"name": "Rahul",
"age": 20,
"course": "O Level"
}
print(student)
Output-
{'name': 'Rahul', 'age': 20, 'course': 'O Level'}
यहाँ:
"name", "age", "course" → Keys
"Rahul", 20, "O Level" → Values
2. Creating an Empty Dictionary :-
यदि हमें बिना किसी element की Dictionary बनानी हो, तो खाली {} का उपयोग किया जाता है।
English
If we want to create a Dictionary without any elements, an empty {} is used.
Example -
student = {}
print(student)
Output-
{}
बाद में इसमें elements add किए जा सकते हैं:
student = {}
student["name"] = "Rahul"
student["age"] = 20
print(student)
Output-
{'name': 'Rahul', 'age': 20}
3. Using dict() Function :-
Python में dict() built-in function का उपयोग करके भी Dictionary बनाई जा सकती है।
English
In Python, a Dictionary can also be created using the built-in dict() function.
Syntax -
dictionary_name = dict(key=value)
Example -
student = dict(name="Rahul", age=20, course="O Level")
print(student)
Output-
{'name': 'Rahul', 'age': 20, 'course': 'O Level'}
4. Creating Dictionary from Two Lists :-
दो Lists की सहायता से भी Dictionary बनाई जा सकती है। इसके लिए zip() का उपयोग किया जा सकता है।
English
A Dictionary can also be created with the help of two Lists. The zip() function can be used for this purpose.
Example -
keys = ["name", "age", "course"]
values = ["Rahul", 20, "O Level"]
student = dict(zip(keys, values))
print(student)
Output-
{'name': 'Rahul', 'age': 20, 'course': 'O Level'}
यहाँ:
Keys → name, age, course
Values → Rahul, 20, O Level