You are on page 1of 1

Experiment 7

Q1. Perform all Python set and Dictionary operations

In [108…
# Set Operations in Python
first = {1, 2, 3, 4, 5, 6}
second = {4, 5, 6, 7, 8, 9}

print("Union: ",first | second)


print("Intersection: ",first & second)
print("Difference: ",first - second)
print("Difference: ",second - first)
print("Symmetric difference :", first ^ second)
print("Symmetric difference :", second ^ first)

Union: {1, 2, 3, 4, 5, 6, 7, 8, 9}
Intersection: {4, 5, 6}
Difference: {1, 2, 3}
Difference: {8, 9, 7}
Symmetric difference : {1, 2, 3, 7, 8, 9}
Symmetric difference : {1, 2, 3, 7, 8, 9}

In [109…
# Dictionaries
dict = {'math': 45, 'english': 60, 'science': 65,

'computer science': 70}

x = dict['science']
print(x)
dict['english'] = 80
print(dict)
del dict['math']
print(dict)
x = 'english' in dict
y = 'hindi' not in dict
print(x)
print(y)

d3 = {'a': 5, 'b': 7, 'c': 9, 'e': 3}


d4 = {'c': 9, 'a': 5, 'b': 7, 'e': 3}
x0 = d3 == d4
y0 = d3 != d4
print(x0)
print(y0)

65
{'math': 45, 'english': 80, 'science': 65, 'computer science': 70}
{'english': 80, 'science': 65, 'computer science': 70}
True
True
True
False
Q2.Perform all python built in set and Dictionary methods. Set

In [110…
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)

{'cherry', 'orange', 'banana', 'apple'}

In [111…
x=fruits.copy()
print(x)

{'cherry', 'orange', 'banana', 'apple'}

In [112…
x.clear()
print(x)

set()

In [113…
y={"google", "microsoft", "apple"}

print(fruits.difference(y))

{'cherry', 'orange', 'banana'}

In [114…
fruits.difference_update(y)
print(fruits)

fruits={'cherry', 'banana', 'orange', 'apple'}

print(fruits.intersection(y))

fruits.discard('banana')
fruits

x = {"apple", "banana", "cherry"}


z = {"google", "microsoft", "facebook"}
print(x.isdisjoint(z))

x = {"a", "b", "c"}


z = {"f", "e", "d", "c", "b", "a"}
print(x.issubset(z))

print(fruits.pop())
print(fruits)

fruits.remove("apple")
print(fruits)

print(x.symmetric_difference(y))

{'cherry', 'orange', 'banana'}


{'apple'}
True
True
cherry
{'orange', 'apple'}
{'orange'}
{'microsoft', 'c', 'apple', 'google', 'b', 'a'}

In [115…
# Dictionaries

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

car.clear()

print(car)

{}

In [116…
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.copy()

print(x)

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

In [117…
x = ('key1', 'key2', 'key3')
y = 0

thisdict = dict.fromkeys(x, y)

print(thisdict)

{'key1': 0, 'key2': 0, 'key3': 0}

In [118…
x = car.get("model")

print(x)

Mustang

In [119…
x = car.items()

print(x)

dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])

In [120…
x = car.keys()

print(x)

dict_keys(['brand', 'model', 'year'])

In [121…
car.pop("model")

print(car)

{'brand': 'Ford', 'year': 1964}

In [122…
car.popitem()

print(car)

{'brand': 'Ford'}

In [123…
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.setdefault("model", "Bronco")

print(x)

car.update({"color": "White"})

print(car)

x = car.values()

print(x)

Mustang
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'White'}
dict_values(['Ford', 'Mustang', 1964, 'White'])
Q3. Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for 'From ' lines and takes the
second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times
they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.

In [1]:
fname = input("Enter file:")
if len(fname) < 1 : name = "mbox-short.txt"
hand = open(fname)

lst = list()

for line in hand:


if not line.startswith("From:"): continue
line = line.split()
lst.append(line[1])

counts = dict()
for word in lst:
counts[word] = counts.get(word,0) + 1

bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigcount = count
bigword = word

print (bigword,bigcount)

Enter file:mbox-short.txt
cwen@iupui.edu 5

You might also like