0% found this document useful (0 votes)
46 views5 pages

Dictionary

A dictionary in Python consists of key-value pairs where keys must be unique and immutable such as strings or numbers, and values can be of any type. Dictionaries are defined using curly braces {} and colons are used to separate keys from values. Keys are used to access values and dictionaries provide various methods to add, remove, check for keys and get information about the dictionary.

Uploaded by

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

Dictionary

A dictionary in Python consists of key-value pairs where keys must be unique and immutable such as strings or numbers, and values can be of any type. Dictionaries are defined using curly braces {} and colons are used to separate keys from values. Keys are used to access values and dictionaries provide various methods to add, remove, check for keys and get information about the dictionary.

Uploaded by

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

Dictionary

A dictionary consists of a collection of key-value pairs. Each key-value


pair maps the key to its associated value.
You can define a dictionary by enclosing a comma-separated list of key-
value pairs in curly braces - {}. A colon (:) separates each key from its
associated value:

Syntax:
d = {
<key>: <value>,
<key>: <value>,
.
.
.
<key>: <value>
}

Properties of Dictionary Keys

There are two important points while using dictionary keys

 More than one entry per key is not allowed (no duplicate key is
allowed)
 The values in the dictionary can be of any type, while the keys must
be immutable like numbers, tuples, or strings.
 Dictionary keys are case sensitive- Same key name but with the
different cases are treated as different keys in Python dictionaries.

Creating a Dictionary
d = {
"roll": 10,
"name": "Ramesh",
"address": "ctc"
}

print(d)

Output:

Accessing Elements

You can access the items of a dictionary by referring to its key name,
inside square brackets:
d = {
"roll": 10,
"name": "Ramesh",
"address": "ctc"
}

print("Roll Num:", d["roll"])


print("Name of the Student:", d["name"])

Output:

Characteristics of Dictionary

 Dictionaries are unordered, meaning that the items do not have a


defined order and cannot be accessed by using an index. However,
as of Python version 3.7, dictionaries are ordered by insertion order.
 Dictionaries are mutable, meaning that they can be changed, added
or removed items after they are created.
 Dictionaries do not allow duplicates, meaning that they cannot have
two items with the same key. If a duplicate key is given, the previous
value will be overwritten.
 Dictionaries can be nested, meaning that they can contain another
dictionary or other collections as values.
 Dictionaries can be created by using curly braces {}, by using the
dict() constructor, or by using comprehension syntax.

Write a program to input roll and name of n students.


stud = {}
n = int(input("Enter number of students: "))

for i in range(0, n):


roll = int(input("Enter Roll Number: "))
name = input("Enter name: ")
stud[roll] = name

print(stud)

Output:
Adding Element

Syntax:
Variable[key] = value
d = {}
d[1] = 100
d[2] = 300

print(d)

Removing Element

Syntax:
del variable[key]

d = {}
d[1] = 100
d[2] = 300

# Before Deletion
print(d)

del d[1]

# After Deletion
print(d)
Syntax:
Var.pop(key)

Checking Existence of Key

<Key> in <dict_variable>
Returns true if key is present
<Key> not in <dict_variable>
Returns true if key is not present

Dictionary Methods

length()

clear()
get()
items()
keys()
values()
update()

You might also like