Page 196 - Data Science Algorithms in a Week
P. 196
Python Reference
Dictionary
A dictionary is a data structure that can store values by their keys.
Input:
# source_code/appendix_c_python/example08_dictionary.py
dictionary_names_heights = {}
dictionary_names_heights['Adam'] = 180.
dictionary_names_heights['Benjamin'] = 187
dictionary_names_heights['Eva'] = 169
print 'The height of Eva is', dictionary_names_heights['Eva'], 'cm.'
Output:
$ python example08_dictionary.py
The height of Eva is 169 cm.
Flow control
Conditionals, We can make certain amount of the code to be executed only upon a certain
condition met using the if statement. If the condition is not met, then we can execute the
code following the else statement. If the first condition is not met, we can set the next
condition for the code to be executed using the elif statement.
Input:
# source_code/appendix_c_python/example09_if_else_elif.py
x = 10
if x == 10:
print 'The variable x is equal to 10.'
if x > 20:
print 'The variable x is greater than 20.'
else:
print 'The variable x is not greater than 20.'
if x > 10:
print 'The variable x is greater than 10.'
elif x > 5:
print 'The variable x is not greater than 10, but greater ' +
'than 5.'
else:
print 'The variable x is not greater than 5 or 10.'
[ 184 ]