Question

USING PYTHON! a) Programmatically (do not just write a definition!) create a dictionary whose keys are...

USING PYTHON!

  1. a) Programmatically (do not just write a definition!) create a dictionary whose keys are the integers 1 through 10 and whose respective values are the keys cubed (e.g. a key of 5 has a value of 125).

  2. b) Write a function that, given a user inputted string, outputs a dictionary where each letter is a key and each value is the letter’s position in the string. Note that you ​must handle cases where a word could contain the same letter more than once​, such as “mongoose” correctly having the letter “o” indexed more than once!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Question 1 :
d = {}
for i in range(1,11):
    d[i]=i*i*i
print(d)

Output:

{1: 1, 2: 8, 3: 27, 4: 64, 5: 125, 6: 216, 7: 343, 8: 512, 9: 729, 10: 1000}

#############################

Question 2 :

s = input("Enter input: ")
d = {}
for i in range(len(s)):
    x = s[i]
    if not d.get(x):
        d[x] = [i]
    else:
        tmp = d[x]
        tmp.append(i)
        d[x] = tmp
print(d)

Output:

Enter input: mongoose {m: [0], o: [1, 4, 5], n: [2], g: [3], s: [6], e: [7]} Process finished with exit code 0

Add a comment
Answer #2

Write a Python program to create a dictionary of student data by taking input from the user,  where each student data contains Rollno (to be considered as key), and marks in 3 subjects (to 

be considered as list of values). e.g. {1 : [45, 40, 35], 2 : [41, 38, 39], 3 : [35, 30, 37]}. Prepare  mark list in the following format:  

Roll No. Mark-1 Mark-2 Mark-3 Total  

 1 45 40 35 120 

stud = {}

mrk = []

print("ENTER ZERO NUMBER FOR EXIT !!!!!!!!!!!!")

print("ENTER STUDENT INFORMATION ------------------------ ")

while True :

    rno = int(input("enter roll no.. :: -- "))

    if rno == 0 :

        break

    else:

         for i in range(3):

             print("enter marks  ",i+1," :: --  ",end = " ")

             n = int(input())

             mrk.append(n)

    stud[rno] = mrk

    mrk = []

print("Records are ------ ",stud)

print("\nRollNo\t Mark1\t Mark2\t Mark3\t Total")

tot = 0

for r in stud:

    print(r,"\t",end=" ")

    for m in stud[r]:

        tot = tot + m

        print(m,"\t",end=" ")

    print(tot)

    tot = 0

Q.6. Write a Python program to take input of a string from the user and then create a dictionary  containing each character of the string along with their frequencies. (e.g. if the string is ‘banana’  then output should be {'b': 1, 'a': 3, 'n': 2}.

 

d = dict()

user = input ("enter a string ::-- ")

lst = list(user)

l = len(lst)

for i in range(l) :

    c = 0

    for j in range(l) :

        if lst[i] == lst[j ]:

            c += 1

    d[lst[i]] = c

print("dictionary is  :: ",d)

Q.7. Write a Python program to create a list of strings by taking input from the user and then create  a dictionary containing each string along with their frequencies. (e.g. if the list is [‘apple’,  ‘banana’, ‘fig’, ‘apple’, ‘fig’, ‘banana’, ‘grapes’, ‘fig’, ‘grapes’, ‘apple’] then output should be  {'apple': 3, 'banana': 2, 'fig': 3, 'grapes': 2}.  

 

lst = []

d = dict()

print("ENTER ZERO NUMBER FOR EXIT !!!!!!!!!!!!")

while True:

    user = input('enter string element :: -- ')

    if user == "0":

        break

    else:

        lst.append(user)

print("LIST ELEMENR ARE :: ",lst)

l = len(lst)

for i in range(l) :

    c = 0

    for j in range(l) :

        if lst[i] == lst[j ]:

            c += 1

    d[lst[i]] = c

print("dictionary is  :: ",d)

Q.8. Write a Python program to input a string that is a list of words separated by commas. Construct  a dictionary that contains all these words as keys and their frequencies as values. Then display  the words with their quantities.  

 

lst = []

d = dict()

user = input ("enter a string ::-- ")

lst = user.split(',')

print("LIST ELEMENR ARE :: ",lst)

l = len(lst)

for i in range(l) :

    c = 0

    for j in range(l) :

        if lst[i] == lst[j ]:

            c += 1

    d[lst[i]] = c

print("dictionary is  :: ",d)


answered by: VISHAKHA AGARWAL
Add a comment
Know the answer?
Add Answer to:
USING PYTHON! a) Programmatically (do not just write a definition!) create a dictionary whose keys are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • using python Write a function that takes a dictionary and a list of keys as 2...

    using python Write a function that takes a dictionary and a list of keys as 2 parameters. It removes the entries associated with the keys from the given dictionary Your function should not print or return anything. The given dictionary should be modified after the function is called Note: it means that you can't create another dictionary in your function. Since dictionaries are mutable, you can modify them directly def remove_keys (dict, key_list) "" "Remove the key, value pair from...

  • Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys()...

    Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys() member function that returns a list of a dictionary's keys. You can check if a particular key is in a dictionary by using the in test with the keys list. For example, if our dictionary is MyDict = { 1: 'One', 2: 'Two', 3: 'Three' } Then ( 2 in MyDict.keys() ) evaluates to True, indicating that 2 is a valid key for MyDict....

  • PYTHON PROGRAM by using functions & dictionary or set. - must create a variable to hold...

    PYTHON PROGRAM by using functions & dictionary or set. - must create a variable to hold a dictionary or sets - must define a function that accepts dictionaries or sets as an argument A dictionary maps a set of objects (keys) to another set of objects (values). A Python dictionary is a mapping of unique keys to values. For e.g.: a dictionary defined as: released = { "iphone" : 2007, "iphone 3G": 2008, "iphone 3GS": 2009, "iphone 4" : 2010,...

  • Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary....

    Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary. Each key in starDictionary is the name of a star (a string). Its value is a dictionary with two keys: the string 'distance' and the string 'type'. The value associated with key 'distance' is the distance from our solar system in light years. The value associated with key 'type' is a classification such as 'supergiant' or 'dwarf'. 2. a number, maxDistance 3. a string,...

  • 3 Dn Ent Write a python program to create a Dictionary named grade with the given...

    3 Dn Ent Write a python program to create a Dictionary named grade with the given items and do the below operations: i. 15 i. Create Dictionary with keys-{'Monday', 'Tuesday', 'Wednesday Thursday', 'Friday') ii. values-{1, 2, 3, 4, 5) ii. Now append another item with key-'Saturday') and value-(63 to the already created Dictionary and print it.

  • Python 3.7 Coding assignment This Program should first tell users that this is a word analysis...

    Python 3.7 Coding assignment This Program should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file....

  • This script will create a dictionary whose keys are all the directories listed in thePATH system...

    This script will create a dictionary whose keys are all the directories listed in thePATH system variable, and whose values are the number of files in each of these directories. The script will also print each directory entry sorted by directory name. The script will use the following five functions to get the required results get_environment_variable_value() get_dirs_from_path() get_file_count() get_file_count_for_dir_list() print_sorted_dictionary() get_environment_variable_value() The header for this function must be This function must accept a shell variable as its only parameter The...

  • PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as...

    PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as parameters. The function randomly chooses n key-value pairs and removes them from the dictionary d. If n is greater than the number of elements in the dictionary, the function prints a message but does not make any changes to d. If n is equal to the number of elements in d, the function clears the dictionary. Otherwise the function goes through n rounds in...

  • USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key:...

    USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key: int, value: int) Description: You're done with stats, but you still have other math homework to go! You're currently learning about the Fibonacci sequence in math class. The Fibonacci sequence is a series of numbers in the pattern 112 3 5 8 13 21 ..., where the next number is found by adding up the two numbers before it. Write a function that takes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT