0% found this document useful (0 votes)
19 views13 pages

CSE 136-Presentation

Uploaded by

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

CSE 136-Presentation

Uploaded by

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

Hello!

Shreya Das
Student ID:203-15-14489
Section - B
DICTIONARY
Introduction:
In Python, a dictionary is a built-in data structure that stores a
collection of key-value pairs. Each key-value pair maps the key to its
associated value. Dictionaries are unordered, meaning there is no
guarantee of the order of the items stored within them.

Dictionaries: Key-Value Data Structures


my_dict = { "apple": 10, "banana": 5,
"orange":
student = 8{"name":
} "Alice", "age": 25,
"grade": "A"}
Operations on Dictionaries
To add new items to a dictionary, simply assign a value
to a new key.
student = {"name": "Alice", "age": 25,
"grade": "A"}
student["city"] = "New York"
student

student = {"name": "Alice", "age": 25,


"grade": "A", "city": "New York"}
Operations on Dictionaries
Assign a new value to an existing key to update existing items
in a dictionary.
student = {"name": "Alice", "age": 25,
"grade": "A"}
student["age"] = 26
student

student = {"name": "Alice", "age": 26,


"grade": "A", "city": "New York"}
Operations on Dictionaries

To remove items from a dictionary,


either the del keyword or pop() method
is used.

del student["grade"]
student.pop("grade")
Advanced Dictionary Techniques
Dictionary Comprehension:
Dictionary comprehension is a concise and efficient way
to create dictionaries in Python.

{key_expression: value_expression for item in


iterable}

numbers = [1, 2, 3, 4, 5]
square_dict = {num: num**2 for num in numbers}
print(square_dict)

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}


Advanced Dictionary Techniques
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Double each value in the dictionary
double_dict = {key: value * 2 for key, value in
my_dict.items()} print(double_dict)

{'a': 2, 'b': 4, 'c': 6}


Advanced Dictionary Techniques
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Double each value in the dictionary
double_values_dict = {key: value * 2 for key, value in
my_dict.items()}
print(double_values_dict)

{'a': 2, 'b': 4, 'c': 6}


Advanced Dictionary Techniques
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Double both keys and values in the dictionary
double_dict = {key*2: value*2 for key, value in
my_dict.items()}
print(double_dict)

{'a': 2, 'b': 4, 'c': 6}


 Reading Materials provided by Course Instructor
Hasnur Jahan.
 AI based technology.

You might also like