obrazky
-

JSON

  1. Save the following dictionary into a JSON file called data.json, then load it from the file and print the names of all movies.

     movies = {
         "movies": [
             {"title": "Matrix", "year": 1999},
             {"title": "Inception", "year": 2010},
             {"title": "Interstellar", "year": 2014}
         ]
     }
    
  2. Download the file ludia.json and create a program that:

  • searches for people by name
  • searches for people by age
  • searches for people by part of their name
  • searches for people with the same name and age
  • prints a list of people with the same interests
  • prints this list of people sorted in alphabetical order

Method Description

json.dumps() Converts a Python object into a JSON string.

json.dump() Writes a Python object into a JSON file.

json.loads() Converts a JSON string into a Python object.

json.load() Reads JSON data from a file and converts it into a Python object.

dict.keys() Returns all dictionary keys.

dict.values() Returns all dictionary values.

dict.items() Returns all key-value pairs.

dict.get() Gets a value using a key.

dict.update() Updates a dictionary with new values.

dict.pop() Removes an item using its key.

dict.clear() Removes all dictionary items.

list.append() Adds an element to a list.

list.remove() Removes a specified element.

list.pop() Removes an element by index (last by default).

list.sort() Sorts a list.

Function with parameters

  1. Create a new program squared_parameter.py. Copy the following code into it and complete the function output, which uses the parameter x to print the value of x and its square:

     def output(x):
         print('Number', ...)
         print('Squared equals', ............)
     output(1)
     output(2)
     output(3)
    

The program should print:

    Number 1
    Squared equals 1
    Number 2
    Squared equals 4
    Number 3
    Squared equals 9
  1. Create a new program circle_parameter.py. Copy the following code into it and complete it so that it draws circles with center at point 200, 150 and radius r, which will be the parameter of the function:

     import tkinter
     canvas = tkinter.Canvas()
     canvas.pack()
    
     def circle(r):
         canvas.create_oval(......., ......., ......., .......)
    
     circle(10)
     circle(100)
     circle(50)
    
  2. Create a new program square_parameter.py and define a function square with parameter a, which specifies the side length of a square. The function should work so that it draws a square only for positive values of a, and for negative values it prints the message “impossible” in the center of the canvas. The upper-left corner of the drawn square will have coordinates [10, 10]. Test the program.

  3. Create a new program quiz.py, which will work as a simple addition quiz. At the start, the computer generates two random numbers from 1 to 10, prints them, and we must answer by calling the function check() from the command line. The computer then checks whether our answer was correct:

     =========== RESTART ===========
     How much 10 + 7 ?
     >>> check(5)
     Incorrect, it should be 17
     >>>
     =========== RESTART ===========
     How much is 4 + 9 ?
     >>> check(13)
     Correct!
     >>>
    
  4. We want to explore how often the number 6 appears when rolling a die multiple times. Create a program luck.py with a function luck that has parameter n, which simulates n rolls of a standard die. The function generates a random number 1–6 n times, and whenever a six appears, it increases a counter by 1. At the end, the function prints a message in following format:

     The probability of winning with 10 rolls: 0.2
    

Use the program to print the probability of winning for 10, 100, 1000, 10000, 100000 rolls.

-
Copyright © 2008-2026 Miroslava Valíková