Sunday, April 21, 2024

📑 Exploration of the Function locals()

📑 Task

1) What will the program print?
2) How can we change the 3rd line of the program so that
the function takes elements of the same dictionaries as arguments
but the program prints out only keys of these dictionaries?

📑 Answer

1) Two rows: True and ({'a': 1, 'b': 2}, {'b': 3})
2)

📑 Dictionary Comprehensions

📑 Task

1) What two numbers will the program print?
2) Replace the first five lines of the program with a single line
containing a dictionary comprehension

📑 Answer

1) 10 29
The first number is the sum of the keys of the resulting dictionary
The second number is the sum of its values
2)

📑 Boolean Values of Objects

📑 Task

1) What will be printed
2) Explain the result with a code snippet, then remove
one punctuation character so the program result would be 0

📑 Answer

1) 1
2)

📑 Nested Functions for Polynomials

📑 Task

1) What will be the result of the calculations?
2) Can you write this code in a form of an anonymous function?
3) Can you make this function more universal so that
it builds a polynomial of any degree using arguments as coefficients?

📑 Answer

1) $polynomials(1, 1, 1)(0)$ builds the function $f(x) = x^2 + x + 1$
and finds its value $f(x) = 1$ at $x = 0$ twice
So the program will print out $ 1 $
2)

3)

📑 Curly Braces inside F-strings

📑 Task

1) Will the code print out the elements of the interval?
2) What minimal changes can be made to the code to see
the elements of this interval?

📑 Answer

1) The code will only display the command text
{*map(str, numbers)} {*numbers}
2) It needs to add spaces between the curly braces
or replace the inner curly braces with square braces

📑 F-strings and Nested Lists

📑 Task

1) What will the program print?
2) How can you print the elements of the nested list:
the entire list [0, 1, 2] and the character with index 1 of the string 'abc'?

📑 Answer

1) [0, 1, 2] abc - elements of a nested list with indexes 1 and 0
b 0 - fragments of these elements list_[0][1] and list[1][0]
2)