6. Dictionaries

Type of data key - value

6.1. Definiowanie

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

6.2. Checking type

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

6.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

6.4. Exercises

  1. Create dictionary with capitols of:

  • France,

  • Germany,

  • Poland,

  • Czech republic

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

  2. Remove capitol 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