Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 4

27 bytes added, 19:25, 21 June 2017
PART 2 - String Formatting
</source>Now back to dictionaries. Using keyword arguments(sometimes referred to as '''kwargs''' in python). The dictionary can be quickly and easily expanded into these keyword arguments using the syntax '''**dictionary_name'''.<br><br>
:#Let's try this below. Pay close attention to the keys inside the dictionary and the values associated with each key:<source>
# Define a dictionary:
dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Province': 'ON'}
# Uses the dictionary's keyword arguments:print('{City} {Province} {Country}'.format(**dict_york)) # Uses the dictionary's Creates new keyword arguments:print('{City} {Province} {Country}'.format(City='Toronto', Province='ON', Country='Canada')) # Creates new keyword arguments
</source>
13,420
edits