5.1CONCEPT(MUTABLE)
Everything in Python is an object. All objects in Python can be either mutable or immutable.
An object's mutability is determined by its type. Some of these objects like lists and dictionaries are
mutable, means their content can be changed.
In Python, immutable objects are those whose value cannot be changed in place after assignment or
initialization. A tuple is an immutable linear data structure. Thus, in contrast to lists, once atuple is
defined, it cannot be altered or changed.
Adictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are
written with curly brackets, and they have keys and values.
Dictionaries are optimized to retrieve values when the key is known.
Dictionaries are Python's implementation of a data structure that is more generally known as an
Associative Array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps
the key toits associated value.
Comparison of Dictionaries and Lists:
Both are mutable.
Both are dynamic. They can grow and shrink as needed.
Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A
dictionary can also contain a list, and vice versa.
List elements are accessed by their position in the list, via indexing while Dictionary elenments are
accessed via keys.
5.1|
, Python Programming (BCA Sclence: VI) Python Dition
CREATING AND ACCESSING VALUES IN ADICTIONARY
values.
As we know, dictionary is delete and update old
mutable, we can add new valuesand
5.2.1 Creation of Dictionary
Creating a dictionary is as simple as placing items inside curly braces ) separated by comma. An iten
has a key and the corresponding value expressed as a pair, key: value.
Each key is separated from its value by a coBon (), the items are separated by commas, and the whole
,eniosed in curly braces. An empty dictionary without any items is Written Withjust two CUrl.
braces, like this: ).
y uuque within a dictionary while values may not be.The values of a dictionary can be of ah
ype, but the keys must be of an immtable data tvpe such as strings, numbers, or tuples.
Syntax:
dictionary name = {
<key>: <value>,
<key>: <value>,
<key>: <value>
Or
dictionary_name = dict ([
(<key>, <value>),
(<key>, <value),
(<key>, <value>)
where dict is built-in function.
Example: -
# empty dictionary with name dict
dict =
# dictionary with integer keys
dict = {1: 'apple', 2: 'orange'}
# dictionary with mixed keys
dict = {'name': 'abc', I: (2, 4, 3]}
# using dict()
dict = dict({1:'apple', 2:'orange'})
# from sequence having each item as a pair
dict = dict ([(1, 'apple'), (2, 'orange')])
5.2
Everything in Python is an object. All objects in Python can be either mutable or immutable.
An object's mutability is determined by its type. Some of these objects like lists and dictionaries are
mutable, means their content can be changed.
In Python, immutable objects are those whose value cannot be changed in place after assignment or
initialization. A tuple is an immutable linear data structure. Thus, in contrast to lists, once atuple is
defined, it cannot be altered or changed.
Adictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are
written with curly brackets, and they have keys and values.
Dictionaries are optimized to retrieve values when the key is known.
Dictionaries are Python's implementation of a data structure that is more generally known as an
Associative Array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps
the key toits associated value.
Comparison of Dictionaries and Lists:
Both are mutable.
Both are dynamic. They can grow and shrink as needed.
Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A
dictionary can also contain a list, and vice versa.
List elements are accessed by their position in the list, via indexing while Dictionary elenments are
accessed via keys.
5.1|
, Python Programming (BCA Sclence: VI) Python Dition
CREATING AND ACCESSING VALUES IN ADICTIONARY
values.
As we know, dictionary is delete and update old
mutable, we can add new valuesand
5.2.1 Creation of Dictionary
Creating a dictionary is as simple as placing items inside curly braces ) separated by comma. An iten
has a key and the corresponding value expressed as a pair, key: value.
Each key is separated from its value by a coBon (), the items are separated by commas, and the whole
,eniosed in curly braces. An empty dictionary without any items is Written Withjust two CUrl.
braces, like this: ).
y uuque within a dictionary while values may not be.The values of a dictionary can be of ah
ype, but the keys must be of an immtable data tvpe such as strings, numbers, or tuples.
Syntax:
dictionary name = {
<key>: <value>,
<key>: <value>,
<key>: <value>
Or
dictionary_name = dict ([
(<key>, <value>),
(<key>, <value),
(<key>, <value>)
where dict is built-in function.
Example: -
# empty dictionary with name dict
dict =
# dictionary with integer keys
dict = {1: 'apple', 2: 'orange'}
# dictionary with mixed keys
dict = {'name': 'abc', I: (2, 4, 3]}
# using dict()
dict = dict({1:'apple', 2:'orange'})
# from sequence having each item as a pair
dict = dict ([(1, 'apple'), (2, 'orange')])
5.2