|
| 1 | +# Dictionary Comprehension = create dictionaries using an expression can replace for loops |
| 2 | +# and certain lambda fucntion |
| 3 | + |
| 4 | + |
| 5 | +# dictionary = {key: expression for (key,value) in iterable} |
| 6 | +# dictionary = {key: expression for (key,value) in iterable if conditional } |
| 7 | +# dictionary = {key: (if/else) for (key,value) in iterable } |
| 8 | +# dictionary = {key: function(value) for (key,value) in iterable } |
| 9 | + |
| 10 | +# cities_temperture = {'Bhubaneswar':34,'Bhadrak':36,'Cuttack':30,'Anugul':23} |
| 11 | +# print(cities_temperture) |
| 12 | +# print('-' * 50) |
| 13 | +# cities_temperture_in_F = {key: round((value -32) * (5/9)) for (key,value) in cities_temperture.items()} |
| 14 | +# print(cities_temperture_in_F) |
| 15 | +# print('-' * 50) |
| 16 | + |
| 17 | +# ----------------------------------------------------------------- |
| 18 | +# weather = {'Bhubaneswar':'cloudy','Bhadrak':'snowing','Cuttack':'rain','Anugul':'cloudy'} |
| 19 | +# |
| 20 | +# rainy_cities = {key: value for key,value in weather.items() if value == 'rain'} |
| 21 | +# cloudy_cities = {key: value for key,value in weather.items() if value == 'cloudy'} |
| 22 | +# snowing_cities = {key:value for key,value in weather.items() if value == 'snowing'} |
| 23 | +# |
| 24 | +# |
| 25 | +# print(snowing_cities) |
| 26 | +# print(cloudy_cities) |
| 27 | +# print(rainy_cities) |
| 28 | +# --------------------------------------------------------- |
| 29 | +# cities_temp = {'Bhubaneswar':34,'Bhadrak':24,'Cuttack':45,'Anugul':40} |
| 30 | +# |
| 31 | +# Helthy_weather = {key: ('Good'if value <= 30 else 'bad' ) for (key,value) in cities_temp.items()} |
| 32 | +# print(Helthy_weather) |
| 33 | +# |
| 34 | +# warm_cold = {key:('cold' if value <= 30 else 'warm') for (key,value) in cities_temp.items()} |
| 35 | +# print(warm_cold) |
| 36 | +# ------------------------------------------------ |
| 37 | +cities_temp = {'Bhubaneswar':34,'Bhadrak':24,'Cuttack':45,'Anugul':40} |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +def add(temp): |
| 42 | + if temp >= 30: |
| 43 | + temp = temp - 5 |
| 44 | + else: |
| 45 | + temp += 2 |
| 46 | + return temp |
| 47 | + |
| 48 | +add_temp = {key: add(value) for (key,value) in cities_temp.items()} |
| 49 | + |
| 50 | + |
| 51 | +print(add_temp) |
| 52 | +print(cities_temp) |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
0 commit comments