Dictionary Element Value in Python

To retrieve the value of an element from a dictionary, you need to access its key.

Let us have the following dictionary:

pythondct = {
  'a': 1,
  'b': 2,
  'c': 3
}

Let's derive the first element from it. To do this, after the dictionary name, we specify the key name in square brackets:

pythonprint(dct['a']) # 1

If you access a key that is not in the dictionary, an error will be returned:

pythonprint(dct['x']) # will display an error

You can also store the dictionary key in a variable:

pythonkey = 'a'
print(dct[el]) # 1
Задание 1

Given a dictionary:

pythondct = {
  'x': 1,
  'y': 2,
  'z': 3
}

Print each element of this dictionary.

Задание 2

Given a dictionary:

pythondct = {
  'a': 1,
  'b': 2,
  'c': 3
}

Derive from it the number 2.

Задание 3

Given a dictionary:

pythondct = {
  'x': 1,
  'y': 2,
  'z': 3
}

Given a variable:

pythonkey = 'x'

Print the element whose key is stored in the variable.

Задание 4

Given a dictionary:

pythondct = {
  'a': 5,
  'b': 10,
  'c': 15
}

Sequentially derive all the values ​​from it and get their sum.

Задание 5

Given a dictionary:

pythondct = {
  1: 'a',
  2: 'b',
  3: 'c'
}

Write the code to get the following string:

python'abc'