Control Flow: The if
, elif
, else
Statements
Dictionary
A dictionary is a data structure that can be used as a key-value pair storage. Instead of providing index values as in tuples or lists, the dictionary elements can be accessed and modified by using non-consecutive or non-integer keys. Dictionaries are defined by using a pair of curly braces and their values can be modified after initialization.
bookshelves = {
"The Grapes of Wrath" : "A2-S5",
"Harry Potter and the Goblet of Fire" : "B7-S2",
"The Hobbit" : "B7-S2"
}
print(bookshelves["The Grapes of Wrath"])
The example above defines a bookshelf database by using the book name as the key and the shelf address as the dictionary value for each key. Each key needs to be a unique number, string literal, or object, however the same value can be used for multiple keys. The value can also be any data type such as numbers, string literals, lists, tuples, other dictionaries, etc.
There is no limitation on the type of keys or the respective values in Python, as long as the key is of an immutable hashable type such as a string, numeric, or tuple, etc. Lists or custom classes cannot be used as keys in a dictionary since their contents may change over time yielding a different hash. It is possible to use a mixed set of keys such as strings and numbers. Similarly the values can also be a mixed group of strings, numbers or data structures.
dictionary_of_everything = {
#the value can be a number
"my favorite number" : 8,
#the key can be number
3 : "Three",
# the value can even be another dictionary
"The bookshelf dictionary" : {
"The Grapes of Wrath" : "A2-S5",
"Harry Potter and the Goblet of Fire" : "B7-S2",
"The Hobbit" : "B7-S2"
}
}
print(dictionary_of_everything["my favorite number"])
print(dictionary_of_everything[3])
print(dictionary_of_everything["The bookshelf dictionary"])
The dictionaries can also be populated in the runtime of the application:
runtime_dictionary = {}
even_odd_dictionary = {}
for _ in range(10):
if(_ % 2 == 0):
runtime_dictionary[_] = 2 * _
even_odd_dictionary[_] = str(_) + " is an even number"
else:
runtime_dictionary[_] = 3 * _
even_odd_dictionary[_] = str(_) + " is an odd number"
print(runtime_dictionary)
print(even_odd_dictionary)
Similar to list comprehension, dictionary comprehension can be used to populate dictionaries as in the following example:
my_dict = {x : 2*x for x in range(10,50,2) }
print(my_dict)
which will retrieve a collection of numbers using the range
function and assign as much as 2*x
value to the each key x
in the dictionary while they are iterated by the for
statement. Note that this use of for
does not have a body or colon (:). See list comprehension in earlier sections.
The modified version of the code from https://code.sololearn.com/c7oSEHZTPmsV/#py can be used to print different shapes on the console:
# Example of Python code to
# print some simple symmetric shapes
# using "X" and spaces..
def xprint(x, line_width=40):
spaces = (line_width - x) // 2
print("|" + " " * spaces + "X" * x +
" " * spaces +"|")
# Some simple shapes using a dictionary of String: a tuple of numbers
shapes = {
"square": (0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0),
"triangle": (0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 0),
"diamond": (0, 1, 3, 5, 7, 9, 11, 13, 15, 15, 13, 11, 9, 7, 5, 3, 1, 0),
"sandglass": (0, 15, 13, 11, 9, 7, 5, 3, 3, 5, 7, 9, 11, 13, 15, 0),
"cross": (0, 10, 10, 10, 30, 30, 30, 10, 10, 10, 0),
"arrow": (0, 1, 3, 5, 7, 9, 11, 13, 7, 7, 7, 7, 7, 0)
}
for key in shapes.keys():
print(key)
shapeRowTuple = shapes[key]
for x in shapeRowTuple:
xprint(x)
square
| |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| |
triangle
| |
| X |
| XXX |
| XXXXX |
| XXXXXXX |
| XXXXXXXXX |
| XXXXXXXXXXX |
| XXXXXXXXXXXXX |
| XXXXXXXXXXXXXXX |
| XXXXXXXXXXXXXXXXX |
| XXXXXXXXXXXXXXXXXXX |
| |
diamond
| |
| X |
| XXX |
| XXXXX |
| XXXXXXX |
| XXXXXXXXX |
| XXXXXXXXXXX |
| XXXXXXXXXXXXX |
| XXXXXXXXXXXXXXX |
| XXXXXXXXXXXXXXX |
| XXXXXXXXXXXXX |
| XXXXXXXXXXX |
| XXXXXXXXX |
| XXXXXXX |
| XXXXX |
| XXX |
| X |
| |
sandglass
| |
| XXXXXXXXXXXXXXX |
| XXXXXXXXXXXXX |
| XXXXXXXXXXX |
| XXXXXXXXX |
| XXXXXXX |
| XXXXX |
| XXX |
| XXX |
| XXXXX |
| XXXXXXX |
| XXXXXXXXX |
| XXXXXXXXXXX |
| XXXXXXXXXXXXX |
| XXXXXXXXXXXXXXX |
| |
cross
| |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| XXXXXXXXXX |
| |
arrow
| |
| X |
| XXX |
| XXXXX |
| XXXXXXX |
| XXXXXXXXX |
| XXXXXXXXXXX |
| XXXXXXXXXXXXX |
| XXXXXXX |
| XXXXXXX |
| XXXXXXX |
| XXXXXXX |
| XXXXXXX |
| |
Exercise: Fix the alignment of the "|" characters at the end of the lines throughout the entire print sequence.