Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Please write a Python 3 program for the follwing. Please show all output. Add

ID: 3788544 • Letter: 1

Question

1. Please write a Python 3 program for the follwing. Please show all output.

Add the file to your clone of the repository and commit changes frequently while working on the following tasks. When you are done, push your changes to GitHub and issue a pull request.

(if you are struggling with git – just write the code for now)

When the script is run, it should accomplish the following four series of actions:

Create a dictionary containing “name”, “city”, and “cake” for “Chris” from “Seattle” who likes “Chocolate”.

Display the dictionary.

Delete the entry for “cake”.

Display the dictionary.

Add an entry for “fruit” with “Mango” and display the dictionary.

Display the dictionary keys.

Display the dictionary values.

Display whether or not “cake” is a key in the dictionary (i.e. False) (now).

Display whether or not “Mango” is a value in the dictionary (i.e. True).

Using the dictionary from item 1: Make a dictionary using the same keys but with the number of ‘t’s in each value as the value. (upper and lower case?).

Create sets s2, s3 and s4 that contain numbers from zero through twenty, divisible 2, 3 and 4.

Display the sets.

Display if s3 is a subset of s2 (False)

and if s4 is a subset of s2 (True).

Create a set with the letters in ‘Python’ and add ‘i’ to the set.

Create a frozenset with the letters in ‘marathon’

display the union and intersection of the two sets.

Explanation / Answer

Python program

import sys

dict = {}

# adding value in dictionary
dict['Chris'] = {}
dict['Chris']['Seattle'] = 'Choclate'

#printing dictionary
print 'dictionary = ',dict


#adding fruit and mango in dictionary
dict['fruit'] = 'Mango'

#printing dictionary keys and values
for key,value in dict.items():
   print "key",key
   print "value",value


#check if key 'choclate' exist or not
print 'Choclate' in dict.keys()
  

#check if value 'Mango' exist or not
print 'Mango' in dict.values()

#creating new dictionary with 't' values
dict2 = {}
for key in dict:
   value = str(dict[key])
   count1 = value.count('t')
   count2 = value.count('T')
   totalcount = count1+count2
   dict2[key] = totalcount
  

# creating sets
s2 = set([2,4,6,8,10,12,14,16,18,20])
s3 = set([3,6,9,12,15,18])
s4 = set([4,8,12,16,20])

print s2
print s3
print s4

#check if s3 is subset of s2
print set(s3) < set(s2)

#check if s4 is subset of s2
print set(s4) < set(s2)

#creating python set
python = set(['P','y','t','h','o','n'])
python.add('i')

frozenset = set(['m','a','r','a','t','h','o','n'])

#intersection and union of sets
print python.intersection(frozenset)
print python.union(frozenset)