0% found this document useful (0 votes)
653 views7 pages

Class Notes Class 11 Computer Science (Dictionary and Tuple)

The document discusses dictionaries and tuples in Python. It defines dictionaries as unordered collections of key-value pairs where keys must be unique and immutable. It describes how to create, access, update, add and delete dictionary elements. It also defines tuples as immutable sequences that are enclosed in parentheses and cannot be changed unlike lists. It explains how to create, access, iterate through and perform basic operations on tuples.

Uploaded by

prateekurmaliya5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
653 views7 pages

Class Notes Class 11 Computer Science (Dictionary and Tuple)

The document discusses dictionaries and tuples in Python. It defines dictionaries as unordered collections of key-value pairs where keys must be unique and immutable. It describes how to create, access, update, add and delete dictionary elements. It also defines tuples as immutable sequences that are enclosed in parentheses and cannot be changed unlike lists. It explains how to create, access, iterate through and perform basic operations on tuples.

Uploaded by

prateekurmaliya5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Class notes class 11 computer science (python)

Dictionary and tuple

Dictionary in Python Class 11 Notes

What is Dictionary in Python?

Python’s dictionaries are kind of hash table type. They work like associative arrays or hashes found in
Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually
numbers or strings. Values, on the other hand, can be any arbitrary Python object.

It is an unordered collection of items where each item consists of a key and a value. It is mutable
(can modify its contents) but Key must be unique and immutable.

Creating Dictionary

It is enclosed in curly braces { } and each item is separated from other item by a comma (,).Within
each item, key and value are separated by a colon(:).

dictonary_name={key:value, key: value, ……..keyN:valueN}

Example :

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}

print day

Output:

{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'} >>>

Accessing Dictionary Item

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}

print day

print day[1]

print day[3]
Output:

{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}

Monday

Wednesday

>>>

In above code we are accessing dictionary using key.

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}

print(day.get(1))

Output

Monday

>>>

Iterating Through A Dictionary

Following example will show how dictionary items can be accessed through loop.

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}

for i in day:

print(day[i])

Output:

Monday

Tuesday

Wednesday

>>>

Updating Dictionary Elements

We can change the individual element of dictionary.


day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}

day[1]='Sunday'

print(day)

OUTPUT

{1: 'Sunday', 2: 'Tuesday', 3: 'Wednesday'}

>>>

Adding New Element in dictionary:

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}

day[4]='Thursday'

day[5]='Friday'

day[6]='Saturday'

day[7]='Sunday'

print day

Output:

{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', 7: 'Sunday'}

>>>

Deleting Dictionary Elements:

del, pop() and clear() statement are used to remove elements from the dictionary.

Using del function:

day {1:'Monday',2:'Tuesday',3:'Wednesday',4:'Thursday',5:'Friday'}

del day[1]

print day

Output:
{2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday'}

>>>

In above code we deleted only single element.

Built-in Dictionary Functions

♦ len(dict) Gives the total length of the dictionary. It is equal to the number of items in the
dictionary.

♦ str(dict) Return a printable string representation of a dictionary

♦ type(variable) If variable is dictionary,then it would return a dictionary type.

Built-in Dictionary Methods

♦ clear() Removes all elements of dictionary dict

♦ copy() Returns a shallow copy of dictionary dict

♦ items()Returns a list of dict’s (key, value) tuple pairs

♦ keys()Returns list of dictionary dict’s keys

♦ setdefault(key,default=None) Similar to get (), but will set dict [key] = default if key is not already
in dict

♦ update(dict2)Adds dictionary dict2’s key-values pairs to dict

♦ values() Returns list of dictionary dict’s values

Important points to remember

1. Dictionary is a collection of elements which is unordered, changeable and indexed.

2. Dictionary has keys and values.

3. Doesn’t have index for values. Keys work as indexes.

4. Dictionary doesn’t have duplicate member means no duplicate key.

5. Dictionaries are enclosed by curly braces { }

6. The key-value pairs are separated by commas ( , )

7. A dictionary key can be almost any Python type, but are usually numbers or strings.

8. Values can be assigned and accessed using square brackets [ ].

9. Keys of a dictionary must be of immutable types, such as string, number, tuple.


10. A dictionary operation that takes a key and finds the corresponding value, is called lookup.

11. There are two methods to delete elements from a dictionary: (i) using del statement (ii) using
pop( ) method

12. To check the existence of a key in dictionary, two operators are used (i) in (ii) not in

What is Tuple in Python

Python Tuples

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of
values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their
elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be
updated.

Tuple: It is a sequence of immutable objects. It is just like list. Difference between the tuples and the
lists is that the tuples cannot be changed unlike lists. Lists uses square bracket whereas tuples use
parentheses.

Creating A Tuple:
A tuple is enclosed in parentheses () for creation and each item is separated by a comma.

tup1 = (‘comp sc’, ‘info practices’, 2017, 2018)


tup2 = (5,11,22,44)

Indexing of tuple is just similar to indexing of list.

Accessing Values from Tuples


Use the square brackets for slicing along with the index or indices to obtain the value available at
that index.

tup1 = (“comp sc”, “info practices”, 2017, 2018)


tup2 = (5,11,22,44,9,66)
print (“tup1[0]: “, tup1[0])
print (“tup2[1:5]: “, tup2[1:5])
Output
(‘tup1[0]: ‘, ‘comp sc’)
(‘tup2[1:5]: ‘, (11, 22, 44, 9))

Iterating Through A Tuple

Element of the tuple can be accessed sequentially using loop.


tup = (5,11,22)
for i in range(0,len(tup)):
print(tup[i])
Output
5
11
22

Updating Tuples
Tuples are immutable,that’s why we can’t change the content of tuple.It’s alternate way is to take
contents of existing tuple and create another tuple with these contents as well as new content.

tup1 = (1, 2)
tup2 = (‘a’, ‘b’)
tup3 = tup1 + tup2
print (tup3)
Output
(1, 2, ‘a’, ‘b’)

Delete Tuple Elements

Direct deletion of tuple element is not possible but shifting of required content after discard of
unwanted content to another tuple.

tup1 = (1, 2,3)


tup3 = tup1[0:1] + tup1[2:]
print (tup3)
Output (1, 3)

NOTE : Entire tuple can be deleted using del statement.

del tup1

Python Expression Results Description

len((1, 2, 3, 4)) 4 Length

(1, 2, 3)+(4,5,6) (1,2,3,4,5,6) Concatenation

(cbsepython.in)*2 (‘cbsepython.in’,’cbsepython.in’) Repetition

4 in (1, 2, 3, 4) True Membership

for i in (1,2,3) : 1 Iteration


print (i,end=”) 2
3

Basic Tuples Operations

S.No. Function Description

1 tuple(seq) Converts a list into tuple.

2 min(tuple) Returns item from the tuple with min value.

3 max(tuple) Returns item from the tuple with max value.

4 len(tuple) Gives the total length of the tuple.

5 cmp (tuple1, tuple2) Compares elements of both tuples.

Tuple Functions

You might also like