Social Icons

Python Dictionary Exercises

Exercise 101 Solution

We consider the three Pythons dictionaries which includes all the computer hardwares:
dicPC = {"HP": 11, "Acer": 7, "Lenovo": 17, "Del": 23}
dicPhone = {"Sumsung": 22, "Iphone": 9, "Other": 13}
dicTablet = {"Sumsung": 15, "Other": 13}
Write a Python program that combines by concatenating these three dictionaries into one.

Exrcise 102 Solution

We consider the following dictionary whose keys are the names of the students and the key values are the general averages obtained by passing the final exam:
students = {"student_1" : 13 , "student_2" : 17 , "student_3" : 9 , "student_4" : 15 , 
			 "student_5" : 8 , "student_6" : 14 , "student_7" : 16 , "student_8" : 12 , 
			 "student_9" : 13 , "student_10" : 15 , "student_11" : 14 , "student_112" : 9 , 
			 "student_13" : 10 , "student_14" : 12 , "student_15" : 13 , "student_16" : 7 ,
			 "student_17" : 12 , "student_18" : 15 , "student_19" : 9 , "student_20" : 17 ,}
Write a Python program that partitions this dictionary into two sub-dictionaries:
  1. admittedStudents whose keys are the admitted students and the key values are the averages obtained (average greater than or equal to 10).
  2. nonAdmittedStudents whose keys are non-admitted students and the key values are the averages obtained (average less than or equal to 10).

Exercise 103 || Solution

Write a Python program that creates from a keyboard-typed integer n, a dictionary whose keys are integers from 1 to n and key values are their squares. Example for n = 7 the dictionary will be of the form:
{1: 1, 2: 4, 3: 9, 4:16, 5:25, 6:36, 7:49}

Exercise 104 || Solution

Write a program in Python that asks the user to enter a string, and return him a dictionary whose keys are the characters in the string entered and the values are the number of occurrences of the characters in the string. Example if the entered string is s = 'language', the program returns the dictionary:
d = {'l': 1, 'a': 2, 'n': 1, 'g': 2, 'e': 1}

Exercise 105 || Solution

Write a function in Python that takes a list of integers as a parameter and returns a dictionary whose keys are the list integers and whose values are "even" or "odd" depending on the number parity.

Exercise 106 || Solution

Write a program in Python that asks the user to enter ten integers of their choice and return them a dictionary whose keys are the entered integers and whose values are 'prime' or 'not prime' depending on the entered integer.

Exercise 107 || Solution

Write a program in Python that asks the user to enter ten integers of their choice and return them a dictionary whose keys are the integers entered and whose values ​​are the lists of divisors of the numbers entered. Example if the user enters the numbers: 2, 7, 11, 5, 3, 19, 14, 9, 1, 4, the program returns the dictionary:
d = {2: [1,2], 7: [1,7], 14: [1,2,7,14],
     9: [1,3,9], 11: [1,11], 5: [1,5],
     3: [1,3], 19: [1,19], 1: [1], 4: [1,2,4]}

Exercise 108 || Solution

Write a python program that asks the user to enter an integer n and return a dictionary whose keys are integers 1, 2, 3, ... n and whose values ​​are 1! , 2! , 3! , … , n!

Exercise 109 || Solution

Write a program in Python that asks the user to enter an integer n and return a dictionary whose keys are the integers 1, 2, 3, ... n and whose values ​​are the sums 1, 1+ 2, 1 + 2 + 3,…, 1 + 2 + 3 +… + n.

Exercise 110 || Solution

Write a Python program that asks the user to enter a text and return a dictionary whose keys are the words of the text entered and the values are the lengths of the words that make up the text. Example for the text T = "Python is a programming language", the program must return the dictionary:
d = {'Python': 6, 'is': 3, 'a': 3, 'language': 7, 'de': 2, 'programming': 13}

Exercise 111 || Solution

Write a Python program that asks the user to enter a text and return him a dictionary whose keys are the words of the text entered and the values are the reverse of the words that make up the text. Example for the text T = "Python is easy", the program must return the dictionary:
d = {'Python': 'nohtyp', 'is': 'si', 'easy': 'ysae'}

Exercise 112 || Solution

Given a dictionary d whose key values are lists. Write a Python program that transforms the dictionary d by sorting the lists. Example for the dictionary:
d = {'a1': [21, 17, 22, 3], 'a2': [11, 15, 8, 13], 'a3': [7, 13, 2, 11], 'a4': [22,14,7,9]}
The program should return the dictionary:
d = {'a1': [3, 17, 21, 22], 'a2': [8, 11, 13, 15], 'a3': [2, 7, 11, 13], 'a4': [7, 9, 14, 22]}

Exercise 113 || Solution

Write a Python program that allows from a given directory to create a dictionary whose keys are the names of the text files located in this directory and the keys are the numbers of the lines of the files.

Exercise 114 || Solution

We consider the Pythons dictionary which contains a personal data :
personalData = {'Name' : 'David' , 'Email' : 'david@gmail.com' , 'Phone' : 33354587 , age' : 27 }
Write a Python program that save data contained in personalData into a file called data.txt .

Exercise 115 || Solution

1) - Write a python program which creates a file named data.txt and write the following lines in this file: 

Name: David Email:
david@gmail.com
Phone: 33354587 age: 27
 

2) - Write a Python program that reads the data.txt file and exports its content to a dictionary:

d = {'Name': 'David', 'Email': 'david@gmail.com', 'Phone': 33354587, age ': 27}

Exercise 116 || Solution

From the following two lists, create a dictionary by two different methods:
keys = ['Name', 'Email', 'Phone']
values = ['Albert', 'albert@gmail.com', 2225668877]
The output should be:
data = {'Name': 'Albert', 'Email': 'albert@gmail.com', 'Phone': 2225668877}

Exercise 117 || Solution

Write a python algorithm as function that verify if a key is present in given dictionary or note.

Exercise 118 || Solution

The following dictionary contains a student's math scores:
scores = {'score1' : 16, 'score2' : 14, 'score3' : 17}
update the notes dictionary by adding the average of scores.

Exercice 119 || Solution

We cosider the following dictionary of length N containing the students names as keys and their score lists as key values:
d = {'student1': listNote1, 'students2': listNotes2, ..., 'studentN': listNoteN}
Write a Python program that transforms this dictionary by replacing the score lists with their averages. Example if
d = {'student1': [14, 16, 18], 'students2': [12, 15, 17], 'student3': [16, 16, 13]}
, the program returns:
d = {'student1': 16.0, 'students2': 14.666666666666666, 'student3': 15.0}

Exercise 120 || Solution

Write a python program that transforms a dictionary into an html table and saves the content to an html file called 'convert_dictionary.html'. Example for the dictionary:
name_email = {'david': 'david@gmail.com', 'hafid': 'hafid@gmail.com', 'nathalie': 'nathalie@gmail.com', 'najib': 'najib @ gmail.com '}

the program should create an html file that displays the content in an html table: 




Younes Derfoufi
my-courses.net

No comments:

Post a Comment

Python Exercises With Solutions

  1. Exercises on Python Strings
  2. Exercises on Python lists
  3. OOP Exercices with solutions
  4. Exercises on Python File I/O
  5. Python Dictionary Exercises
  6. Python Sets Exercises
  1. Python Arithmetic Exercises
  2. Equations & System Of Equations
  3. Polynomials and Functions
  4. Python Arithmetic Exercises 
  5. Numpy, Matplolib, Sympy , Scipy - Exercises
  6. Python Arithmetic Exercises
  1. Python GUI Tkinter Exercises



Younes Derfoufi

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (2) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (256) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) Bootstrap CSS (1) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (9) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (35) Python BeautifulSoup (1) Python For Data Science (2) Python PyQt (4) Python Reference (1) Python-Books (6) Python-DVD-Training (1) Python-Exercises (263) Python-Framework (1) Python-IDE (1) Python-Kivy-Framework (2) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates