You are on page 1of 11

6/27/2021

Lecture # 5&6
By,
Syed Faheem Haider

Dictionary

▶ 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 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 sequence of
elements within curly {} braces, separated by ‘comma’. Dictionary
holds a pair of values, one being the Key and the other
corresponding pair element being its Key:value. Values in a
dictionary can be of any datatype and can be duplicated,
whereas keys can’t be repeated and must be immutable.

1
6/27/2021

Dictionary

Dictionary

2
6/27/2021

Nested Dictionary

Nested Dictionary

3
6/27/2021

Adding elements to a
Dictionary

Adding elements to a
Dictionary

4
6/27/2021

Accessing elements from a


Dictionary

Accessing element of a nested


dictionary

5
6/27/2021

Removing Elements from


Dictionary

11

Dictionary Methods

6
6/27/2021

Dictionary Methods

13

Dictionary Methods

7
6/27/2021

Python Arrays

▶ An array is a collection of items stored at contiguous memory locations. The


idea is to store multiple items of the same type together. This makes it easier
to calculate the position of each element by simply adding an offset to a
base value, i.e., the memory location of the first element of the array
(generally denoted by the name of the array).
▶ For simplicity, we can think of an array a fleet of stairs where on each step is
placed a value (let’s say one of your friends). Here, you can identify the
location of any of your friends by simply knowing the count of the step they
are on. Array can be handled in Python by a module named array. They
can be useful when we have to manipulate only a specific data type
values. A user can treat lists as arrays. However, user cannot constraint the
type of elements stored in a list. If you create arrays using the array module,
all elements of the array must be of the same type.

15

Python Arrays

8
6/27/2021

Creating a Array

17

Adding Elements to a Array


▶ Elements can be added to the Array by using built-in insert() function. Insert
is used to insert one or more data elements into an array. Based on the
requirement, a new element can be added at the beginning, end, or any
given index of array. append() is also used to add the value mentioned in
its arguments at the end of the array.

9
6/27/2021

Adding Elements to a Array

19

Accessing elements from the


Array

20

10
6/27/2021

Next Topics

▶ Removing Elements from the Array


▶ Slicing of a Array
▶ Searching element in a Array
▶ Updating Elements in a Array

21

11

You might also like