0% found this document useful (0 votes)
9 views

Creating New Dictionary Python Dictionary in Python Is An Unordered Collection of Data Values, Used To Store

The document discusses how to create dictionaries in Python by placing key-value pairs within curly braces. It shows examples of creating dictionaries with integer keys and mixed keys, and explains that dictionary keys must be unique and immutable while values can be of any type.

Uploaded by

clarkkent aligan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Creating New Dictionary Python Dictionary in Python Is An Unordered Collection of Data Values, Used To Store

The document discusses how to create dictionaries in Python by placing key-value pairs within curly braces. It shows examples of creating dictionaries with integer keys and mixed keys, and explains that dictionary keys must be unique and immutable while values can be of any type.

Uploaded by

clarkkent aligan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Creating New Dictionary Python

Dictionary in Python is an unordered collection of data values, used to store


data values like a map, which, unlike other Data Types that hold only a single
value as an element, Dictionary holds key:value pair. Key-value is provided in
the dictionary to make it more optimized. 
In Python, a Dictionary can be created by placing a sequence of elements
within curly {} braces, separated by ‘comma’. Dictionary holds pairs of values,
one being the Key and the other corresponding pair element being
its Key:value. Values in a dictionary can be of any data type and can be
duplicated, whereas keys can’t be repeated and must be immutable. 

# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)

# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)

Accessing Values in Dictionary


Each key is separated from its value by a colon (:), the items are separated by
commas, and the whole thing is enclosed in curly braces. An empty dictionary without
any items is written with just two curly braces, like this: {}.
Keys are unique within a dictionary while values may not be. The values of a dictionary
can be of any type, but the keys must be of an immutable data type such as strings,
numbers, or tuples.
Accessing Values in Dictionary
To access dictionary elements, you can use the familiar square brackets along with the
key to obtain its value. Following is a simple example –

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}


print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']

You might also like