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

Experiment-8 Python notes

The document explains the concept of dictionaries in Python, highlighting their unordered nature and the use of key-value pairs. It details how to create dictionaries, access their elements, and emphasizes that keys must be unique and case-sensitive. Additionally, it compares dictionaries with other data types, noting that both sets and dictionaries do not allow duplicate elements, unlike lists and tuples.

Uploaded by

singhrohit200315
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Experiment-8 Python notes

The document explains the concept of dictionaries in Python, highlighting their unordered nature and the use of key-value pairs. It details how to create dictionaries, access their elements, and emphasizes that keys must be unique and case-sensitive. Additionally, it compares dictionaries with other data types, noting that both sets and dictionaries do not allow duplicate elements, unlike lists and tuples.

Uploaded by

singhrohit200315
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment-8: Dictionaries and their properties

 Dictionary

Dictionary is an unordered collection of datatypes, used to store data values. Unlike other Data Types
that hold only single value as an element, Dictionary holds key:value pair. Each key-value pair in a
Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’.

Creating Dictionary
In Python, a Dictionary can be created by placing a sequence of elements within curly {} braces,
separated by ‘comma’. Values in a dictionary can be of any datatype and can be duplicated, whereas
keys can’t be repeated and must be immutable. Dictionary can also be created by the built-in
function dict(). An empty dictionary can be created by just placing it to curly braces{}.

Note – Dictionary’s keys are case sensitive, So, same name but different cases of Key will be treated
distinctly.

Eg-

# Creating an empty Dictionary


Dict = {}
print("Empty Dictionary: ")
print(Dict)

# Creating a Dictionary with Integer Keys


Dict = {1: 'Hello', 2: 'For', 3: 'World'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)

# Creating a Dictionary with Mixed keys


Dict = {'Name': 'Rajesh', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)

# Creating a Dictionary using dict( ) built-in function


Dict = dict({1: 'Hello', 2: 'For', 3:'World'})
print("\nDictionary with the use of dict( ): ")
print(Dict)
# Creating a Dictionary
Dict = {1: 'Hello', 'Name': 'Rajesh', 'NAME': 'Rahul'}
print("\nDictionary : ")
print(Dict)

# Note – Dictionary’s keys are case sensitive. So, same name but different cases of Key will be treated distinctly.

O/P:
Empty Dictionary:
{}
Dictionary with the use of Integer Keys:
{1: 'Hello', 2: 'For', 3: 'World'}
Dictionary with the use of Mixed Keys:
{1: [1, 2, 3, 4], 'Name': 'Rajesh'}
Dictionary with the use of dict():
{1: 'Hello', 2: 'For', 3: 'World'}
Dictionary :
{1: 'Hello', 'Name': 'Rajesh', 'NAME': 'Rahul'}

- Accessing elements of Dictionary :-

In order to access the items of a dictionary refer to its key name. Key can be used inside square brackets.
There is also a method called get() that will also help in accessing the element from a dictionary.

Eg-

# Python program to demonstrate accessing a element from a Dictionary


# Creating a Dictionary
Dict = {1: 'Hello', 'name': 'For', 3: 'World'}

# Accessing a element using key


print("Accessing a element using key:")
print(Dict['name'])

# Accessing a element using get( ) built-in function


print("Accessing a element using get:")
print(Dict.get(3))
O/P:-
Accessing a element using key:
For
Accessing a element using get:
World

Conclusion:-

Both Set and Dictionary datatypes do not allow duplicate elements (But List and tuple datatypes allow
duplicate elements).

Eg-
tuple1 = (1, 2, 33, 44, 6,2)
print("tuple datatype allows duplicate elements \n",tuple1)

List1 = ["Hello","World","Hello"]
print("List datatype also allows duplicate elements \n",List1)

dict = {1: 'jj', 2: 'For', 1: 'jj'}


print("Dictionary datatype does not allow duplicate elements \n",dict)

set1 = {1,2,3,4,5,1}
print("Set datatype does not allows duplicate elements \n",set1)

O/P:-
tuple datatype allows duplicate elements
(1, 2, 33, 44, 6, 2)
List datatype also allows duplicate elements
['Hello', 'World', 'Hello']
Dictionary datatype does not allow duplicate elements
{1: 'jj', 2: 'For'}
Set datatype does not allows duplicate elements
{1, 2, 3, 4, 5}

You might also like