Python dictionaries.docx - Python dictionaries last...

This preview shows page 1 - 4 out of 14 pages.

The preview shows page 2 - 4 out of 14 pages.
Python dictionarieslast modified July 6, 2020In this part of the Python programming tutorial, we cover Pythondictionaries in more detail.Pythondictionaryis a container of key-value pairs. It is mutableand can contain mixed types. A dictionary is an unorderedcollection. Python dictionaries are called associative arrays orhash tables in other languages. The keys in a dictionary must beimmutable objects like strings or numbers. They must also beunique within a dictionary.Python dictionary creationFirst, we show how to create Python dictionaries.create_dict.py#!/usr/bin/env python# create_dict.pyweekend = { "Sun": "Sunday", "Mon": "Monday" }vals = dict(one=1, two=2)capitals = {}capitals["svk"] = "Bratislava"capitals["deu"] = "Berlin"capitals["dnk"] = "Copenhagen"d = { i: object() for i in range(4) }print(weekend)print(vals)print(capitals)print(d)In the example, we create four dictionaries in four different ways.Later we print the contents of these dictionaries to the console.weekend = { "Sun": "Sunday", "Mon": "Monday" }We create a weekend dictionary using dictionary literal notation.The key-value pairs are enclosed by curly brackets. The pairs are
separated by commas. The first value of a pair is a key, which isfollowed by a colon character and a value. The"Sun"string is a keyand the"Sunday"string is a value.vals = dict(one=1, two=2)Dictionaries can be created using thedict()function.capitals = {}capitals["svk"] = "Bratislava"capitals["deu"] = "Berlin"capitals["dnk"] = "Copenhagen"In the third way an empty capitals dictionary is created. Threepairs are added to the dictionary. The keys are inside the squarebrackets, the values are located on the right side of theassignment.d = { i: object() for i in range(4) }A dictionary is created using a dictionary comprehension. Thecomprehension has two parts. The first part is thei:object()expression, which is executed for each cycle of a loop. Thesecond part is thefor i in range(4)loop. The dictionarycomprehension creates a dictionary having four pairs, where thekeys are numbers 0, 1, 2, and 3 and the values are simple objects.$ ./create_dict.py{'Sun': 'Sunday', 'Mon': 'Monday'}{'two': 2, 'one': 1}{'svk': 'Bratislava', 'dnk': 'Copenhagen', 'deu': 'Berlin'}{0: <object object at 0xb76cb4a8>, 1: <object object at 0xb76cb4b0>,2: <object object at 0xb76cb4b8>, 3: <object object at 0xb76cb4c0>}This is the example output.Python dictionary comprehensionAdictionary comprehensionis a syntactic construct which createsa dictionary based on existing dictionary.D = { expression for variable in sequence [if condition] }A dictionary comprehension is placed between two curly brackets;it has three parts: for loop, condition, and expression.
In the for loop, we go through the dictionary. The optional ifcondition specifies a condition which must be met. In the end, theexpression is evaluated. The expression produces elements of theoutput dictionary from members of the input sequence that satisfythe condition.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 14 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Two
Professor
N/A
Tags
Object Oriented Programming, Subroutine, container of key value pairs

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture