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

Dictionary

The document discusses dictionaries in Python. It explains that a dictionary consists of key-value pairs where keys must be unique and immutable, like strings or numbers. It provides the syntax for defining a dictionary using curly braces and separating key-value pairs with colons. It also notes that dictionaries are unordered, mutable, and can be nested. The document demonstrates how to access dictionary items by key, add items, delete items, check if a key exists, and lists some common dictionary methods.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views5 pages

Dictionary

The document discusses dictionaries in Python. It explains that a dictionary consists of key-value pairs where keys must be unique and immutable, like strings or numbers. It provides the syntax for defining a dictionary using curly braces and separating key-value pairs with colons. It also notes that dictionaries are unordered, mutable, and can be nested. The document demonstrates how to access dictionary items by key, add items, delete items, check if a key exists, and lists some common dictionary methods.

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 PDF, TXT or read online on Scribd
You are on page 1/ 5

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>
}

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.

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

print(d)

Output:

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:

• 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:
Syntax:
Variable[key] = value
d = {}
d[1] = 100
d[2] = 300

print(d)

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)

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

length()

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

You might also like