BSIS-1M-Muhmammad Talha-Assignment 6

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

❖ The Islamia University Of Bahawalpur

❖ Department of Information Security


❖ Name : Muhammad Talha Munir
❖ Assignment # 6
❖ Class: BS(IS) – Semester 1(MOR)
❖ Course Instructor: Dr. Shahzada Khurram
❖ Roll no: F20BINFS1M02046

Q 1.
What is the difference between a set and a list?
SET LIST
Set is a collection which is A list is a collection where you can
unordered and unindexed, and put your values using square
does not allow duplicates. In brackets, which
Python, sets are written with curly is ordered and changeable. You
brackets. can put several data-types in a list,
• You cannot access items in a such as integers, strings, and
set by referring to an index booleans.
• Sets are mutable
• You access the list items by
• They are useful for checking
referring to the index number
for duplicates
• Lists are mutable
Example of Set:
Set OF String = {"one", "two", "three"}
Example Of List:
list =[8, -30, "apple", "20"]
Q 2.
What is the difference between a list and a dictionary?
LIST DICTIONARY
A list is a collection where you can
A dictionary maps hashable values
put your values using square to arbitrary objects. A dictionary
brackets, which is a mutable object. The main
is ordered and changeable. You operations on a dictionary are
can put several data-types in a storing a value with some key and
list, such as integers, strings, and
extracting the value given the
booleans. key..
Example Of Dictionary:
• You access the list items by "age": 24,
referring to the index number "favorite_fruit": "apple"
• Lists are mutable my_dictionary["age"]

Example Of List:
list =[8, -30, "apple", "20"]

Q 3. A school web site keeps a collection of web sites that are


blocked at student computers. Should the program that checks
for blocked sites use a list, set, or dictionary for storing the site
addresses? Explain your answer
Dictionary is used for sorting the addresses of websites.
Q 4. An invoice contains a collection of purchased items.
Should that collection be implemented as a list, set, or
dictionary? Explain your answer
Yes,the collection should be implemented as a list,set or
dictionary
Q 5.
Can a dictionary have two keys with the same value? Two
values with the same key?
No, each key in a dictionary should be unique. You can’t have
two keys with the same value. Attempting to use the same key
again will just overwrite the previous value stored.
Q 6. Define a dictionary with five entries that maps student
identification numbers to their full names:
ANS: A dictionary is a collection which is unordered,
changeable and does not allow duplicates.
Dictionaries are written with curly brackets, and have keys and
values:
Dict = {“1”:”Ali”,”2”:”Hamza”,”3”:”Ahmed”,”4”:”Khizar”}
Q 7. Given the set definitions below, answer the following
questions:
set1 = { 1, 2, 3, 4, 5 }
set2 = { 2, 4, 6, 8 }
set3 = { 1, 5, 9, 13, 17 }
a) Is the intersection of set1 and set3 empty?
NO
b) What is the result of performing set union on set1 and set2?
{1,2,3,4,5,6,8}
c) What is the result of performing set intersection on set2 and set3?
{}
d) What is the result of performing set intersection on all three sets?
{}
e) What is the result of performing the set difference on set1 and set2
(set1 – set2)?
{1,3,5}
f) What is the result of the instruction set1.discard(5)?
{1, 2, 3, 4}
g) What is the result of the instruction set2.discard(5)?
{8, 2, 4, 6}

Q 8.
Given a dictionary?
gradeCounts = { "A": 8, "D": 3, "B": 15, "F": 2, "C": 6 }
write the Python statement(s) to print:
write the Python statement(s) to print:
a) all the keys.
gradeCounts = { "A": 8, "D": 3, "B": 15, "F": 2, "C": 6 }
key =gradeCounts.keys()
print(key)
output
dict_keys(['A', 'D', 'B', 'F', 'C'])
b) all the values.
gradeCounts = { "A": 8, "D": 3, "B": 15, "F": 2, "C": 6 }
print(gradeCounts)
output
{'A': 8, 'D': 3, 'B': 15, 'F': 2, 'C': 6}

c) all the key and value pairs.


gradecounts= { "A": 8, "D": 3, "B": 15, "F": 2, "C": 6 }
print(*gradecounts.items(), sep='\n')
output
('A', 8)
('D', 3)
('B', 15)
('F', 2)
('C', 6)
d) all of the key and value pairs in key order.
gradecounts= { "A": 8, "D": 3, "B": 15, "F": 2, "C": 6 }
gradecounts = sorted(gradecounts)
print(gradecounts)
output
['A', 'B', 'C', 'D', 'F']
e) the average value.
For print average value the integer values are required.

gradeCounts = { "A": 8, "D": 3, "B": 15, "F": 2, "C": 6 }


z={print(gradeCounts.values())}
length=len(z)
print(length)
total=sum(gradeCounts.values())
avg=total/length
print(avg)

Output:-
dict_values([8, 3, 15, 2, 6])
1
34.0

You might also like