5. Dictionaries

Type of data key - value

5.1. Defining

>>> workers = {1: 'Adam', 3: 'Tomasz', 4: 'Kasia'}
>>> print(workers) 

5.2. Checking type

>>> type(workers)
<class 'dict'>

5.3. Operations on dictionary

  • Checking length of dictionary

>>> len(workers)
3
  • Checking element occurrences

>>> 3000 in workers
False
>>> 1 in workers
True

Employee with id 1 exists inside of dictionary

  • Adding element to the list

>>> workers[15] = "Marek"
>>> print(workers) 
>>> print(len(workers))
4

5.4. Exercises

  1. Create dictionary with capitals of:

  • France,

  • Germany,

  • Poland,

  • Czech republic

  1. Get capital of Uk - in case of not having capital within dictionary print "unknown capital

  2. Remove capital of Czech republic from dicionary,

Hint

Look for the method which is giving you some text in case of not having specific key inside of dict