You are on page 1of 8

COMPUTER SCIENCE

with
PYTHON

Computer Science Department(Krishna Public School)


CHAPTER- 1
Sujeet Tiwari
Mr. Liju K John Review of Python Basics: Dictionary
Mrs Rubeena Mirza
Mrs Disha Dhupar
(PART-VIII)
Topics
1. Flow of Execution
2. String
3. List
4. Tuples
5. Dictionary
6. Sorting Techniques

Computer Science with PYTHON class XII Chaper-1: Dictionary 2


5. Dictionary
Definition: A kind of list type data storing technique in which each
element is accessible through unique key.
A key can be any number, String or tuple.
Values into dictionary are mutable, mean can be changed but key in
dictionary are immutable, it mean once key declared can’t be change.
Syntax:
Dict_Var={key1:value, key2:value,…}
Ex: d1={1:”Value1”, “2”:”Value2”}

Computer Science with PYTHON class XII Chaper-1: Dictionary 3


5. Dictionary: Creating, Inserting and modifying value
Let create a program to create a dictionary which hold items and price
Creating a
>>> d1={"Keyboard":350, "Mouse":150}
new dict.

Hands
on IDLE

Adding data to
>>> d1[“Monitor”]=4500
existing Dict

Hands
on IDLE
>>>Key=input(“Enter Product Name”) Adding New data
>>>val=int(input(“Enter product price”) to existing by
>>>d1[key]=val taking user input

Hands on IDLE:
TRY AND CHECK
OUTPUT Computer Science with PYTHON class XII Chaper-1: Dictionary 4
5. Dictionary: Creating, Inserting and modifying value
Modifying Existing value

Computer Science with PYTHON class XII Chaper-1: Dictionary 5


5. Dictionary: Built-in Functions and Methods
Sr Function Description
.
1 len(Dict) Return the total number
of items in dict.
2 str(dict) Produce a printable
string representation of a
dict

Computer Science with PYTHON class XII Chaper-1: Dictionary 6


5. Dictionary: Built-in Functions and Methods
Sr. Method Description D1=[1:”Apple”,2:”Banana”,3:”Mango”]
1 clear() Remove all elements from dictionary >>>D1.clear()
2 copy() A shallow copy of dictionary is created >>>D2=D1.copy()
Note: it s different from D2=D1
3 item() Returns a list of dictionary’s >>>D1.item()
(key,value) tuple pairs
4 keys() Returns the list of dictionary’s key >>>D1.keys()
5 setdefault(key,Value) Return key’s value when key exist into >>>D1.setdefault(1)
dictionary otherwise add key along >>>D1.setdefault(4)
with value into dict. If no value >>>print(D1)
specified default value is “None” >>>D1.setdefault(5,”Orange”)
>>>print(D1)
6 update() Marge another dictionary >>>D2={“a”:”Keyboard”, ”b”:”Mouse”}
>>>D1.update(D2)
7 values() Returns the list of dicti’s value >>>D1.values()

Computer Science with PYTHON class XII Chaper-1: Dictionary 7


Thankyou
Computer Science Department
(Krishna Public School, Koni, Bilaspur)
Sujeet Tiwari
Mr. Liju K John
Mrs Rubeena Mirza
Mrs Disha Dhupar

Computer Science with PYTHON class XII Chaper-1: Dictionary 8

You might also like