This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dict_YTL_currency = {'Pound': 0.08, 'Euro': 0.09, 'Usd': 0.12} | |
| # iterating using .values() | |
| for value in dict_YTL_currency.values(): | |
| print(value) | |
| # iterating using .keys() | |
| for key in dict_YTL_currency.keys(): | |
| print(key) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import collections | |
| # sort a dictionary by value | |
| currency = {'Pound': 0.087, 'Euro': 0.09, 'Usd': 0.12, 'Bangladeshi_Taka': 10.0, 'Afghani': 9.12, 'Romanian_leu': 2.08, 'Quatari_rial': 2.34, 'Somali_Shilling': 0.01, 'Serbian_Dinar': 0.08} | |
| sorted_currency = {k: v for k, v in sorted(currency.items(), key=lambda item: item[1])} | |
| reversed_sorted_currency = {k: v for k, v in sorted(currency.items(), key=lambda item: item[1], reverse = True)} | |
| print("dict original :" + str(currency)) | |
| print("dict sorted by value :" + str(sorted_currency)) | |
| print("dict reversed sorted by value:" + str(reversed_sorted_currency)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| print("Python version") | |
| print (sys.version) | |
| currency_1 = {'Pound': 0.091, 'Euro': 0.093, 'Usd': 0.121} | |
| currency_2 = {'Pound': 0.083, 'Afghani': 9.12, 'Romanian_Leu': 2.08} | |
| # for Python 3.5 or greater | |
| # new dictionary is a shallowly merged dictionary of currency_1 and currency_2 | |
| # with values from currency_2 replacing those from currency_1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import copy | |
| # YTL Currency | |
| dict_YTL_currency = {'Pound': [0.091, 0.087], 'Euro': 0.093, 'Usd': 0.121} | |
| # create a shallow copy of the original dictionary | |
| newDict = dict_YTL_currency.copy() | |
| # modify the primitive datatype value in the new dictionary | |
| newDict["Euro"] = 0.081 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # YTL Currency | |
| dict_YTL_currency = {'Pound': 0.087, 'Euro': 0.09, 'Usd': 0.12} | |
| print ("dict original : " + str(dict_YTL_currency)) | |
| # using del to remove a dict | |
| del dict_YTL_currency['Pound'] | |
| print ("dict after removal : " + str(dict_YTL_currency)) | |
| # using del to remove a dict | |
| # raises exception if the key does not exist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # YTL Currency | |
| dict_YTL_currency = {'Pound': 0.091, 'Euro': 0.11, 'Usd': 0.12, 'Afghani': 9.15, 'Bangladeshi Taka': 10.0 } | |
| # simple lookup with [] | |
| print(dict_YTL_currency['Pound']) | |
| # simple lookup with [] when the key does not exist | |
| print(dict_YTL_currency['Dinar']) # trows KeyError | |
| # avoiding KeyError with get() method |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # YTL Currency | |
| dict_YTL_currency = {'Pound': 0.091, 'Euro': 0.11, 'Usd': 0.12, 'Afghani': 9.15, 'Bangladeshi Taka': 10.0 } | |
| # Adding If Conditions | |
| dict_YTL_strong = {k:v for (k,v) in dict_YTL_currency.items() if v>1} | |
| dict_YTL_weak = {k:v for (k,v) in dict_YTL_currency.items() if v<1} | |
| print(dict_YTL_strong) | |
| print(dict_YTL_weak) | |
| # Adding Multiple If Conditions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # GBP to YTL conversion | |
| GBP_to_YTL = 11.0 | |
| # item price in GBP | |
| dictionary_GBP = {'milk': 1.02, 'coffee': 2.5, 'bread': 1.5} | |
| # creating a new dictionary with dictionary comprehension | |
| dictionary_YTL = {key: value*GBP_to_YTL for (key, value) in dictionary_GBP.items()} | |
| print(dictionary_YTL) | |
| # output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # fromkeys() method returns a dictionary with the specified keys and the specified value. | |
| # iterable specifying the keys of the new dictionary | |
| keys = ["IST", "SAW", "STN"] | |
| # the value for all keys as an optional parameter | |
| # default value is None | |
| value ="to_be_defined" | |
| dictionary = dict.fromkeys(keys, value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # zip() function make an iterator that aggregates elements from each of the iterables | |
| # we can create a dictionary when it is used in dict() method | |
| # keys/values are available as a list | |
| keys = ["IST", "SAW", "STN"] | |
| values = ["Istanbul Airport", "Sabiha Gokcen International Airport", "London Stansted Airport"] | |
| dictionary = dict(zip(keys, values)) | |
| print(dictionary) |
NewerOlder