|
189 | 189 | "print(tuple_2)"
|
190 | 190 | ]
|
191 | 191 | },
|
| 192 | + { |
| 193 | + "cell_type": "code", |
| 194 | + "execution_count": 1, |
| 195 | + "metadata": {}, |
| 196 | + "outputs": [ |
| 197 | + { |
| 198 | + "name": "stdout", |
| 199 | + "output_type": "stream", |
| 200 | + "text": [ |
| 201 | + "NA\n" |
| 202 | + ] |
| 203 | + } |
| 204 | + ], |
| 205 | + "source": [ |
| 206 | + "'''\n", |
| 207 | + "Dict_Error\n", |
| 208 | + "\n", |
| 209 | + "Description\n", |
| 210 | + "\n", |
| 211 | + "From a Dictionary input_dict={'Name': 'Monty', 'Profession': 'Singer' }, get the value of a key ‘Label’ which is not a part of the dictionary, in such a way that Python doesn't hit an error. If the key does not exist in the dictionary, Python should return 'NA'.\n", |
| 212 | + "'''\n", |
| 213 | + "\n", |
| 214 | + "import ast,sys\n", |
| 215 | + "input_dict = {'Name' : 'Monty' , 'Profession' : 'Singer' }\n", |
| 216 | + "\n", |
| 217 | + "answer = input_dict.get('Label' , 'NA')\n", |
| 218 | + "print(answer)" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "code", |
| 223 | + "execution_count": 3, |
| 224 | + "metadata": {}, |
| 225 | + "outputs": [ |
| 226 | + { |
| 227 | + "name": "stdout", |
| 228 | + "output_type": "stream", |
| 229 | + "text": [ |
| 230 | + "Apple\n" |
| 231 | + ] |
| 232 | + } |
| 233 | + ], |
| 234 | + "source": [ |
| 235 | + "'''\n", |
| 236 | + "Getting a Value from a Dictionary.\n", |
| 237 | + "\n", |
| 238 | + "Description\n", |
| 239 | + "\n", |
| 240 | + "Extract the company headed by Tim Cook from the dictionary {'Jack Dorsey': 'Twitter', 'Tim Cook': 'Apple','Jeff Bezos': 'Amazon','Mukesh Ambani': 'RJIO'}\n", |
| 241 | + "'''\n", |
| 242 | + "\n", |
| 243 | + "import ast,sys\n", |
| 244 | + "input_dict = {'Jack Dorsey' : 'Twitter' , 'Tim Cook' : 'Apple','Jeff Bezos' : 'Amazon' ,'Mukesh Ambani' : 'RJIO'}\n", |
| 245 | + "\n", |
| 246 | + "name = input_dict['Tim Cook']\n", |
| 247 | + "print(name)" |
| 248 | + ] |
| 249 | + }, |
| 250 | + { |
| 251 | + "cell_type": "code", |
| 252 | + "execution_count": 4, |
| 253 | + "metadata": {}, |
| 254 | + "outputs": [ |
| 255 | + { |
| 256 | + "name": "stdout", |
| 257 | + "output_type": "stream", |
| 258 | + "text": [ |
| 259 | + "['Amazon', 'Apple', 'RJIO', 'Twitter']\n" |
| 260 | + ] |
| 261 | + } |
| 262 | + ], |
| 263 | + "source": [ |
| 264 | + "'''\n", |
| 265 | + "List of Values in a Dictionary.\n", |
| 266 | + "\n", |
| 267 | + "Description\n", |
| 268 | + "\n", |
| 269 | + "Create a SORTED list of all values from the dictionary input_dict = {'Jack Dorsey' : 'Twitter' , 'Tim Cook' : 'Apple','Jeff Bezos' : 'Amazon' ,'Mukesh Ambani' : 'RJIO'}\n", |
| 270 | + "'''\n", |
| 271 | + "\n", |
| 272 | + "import ast,sys\n", |
| 273 | + "input_dict = {'Jack Dorsey' : 'Twitter' , 'Tim Cook' : 'Apple','Jeff Bezos' : 'Amazon' ,'Mukesh Ambani' : 'RJIO'}\n", |
| 274 | + "\n", |
| 275 | + "value_list = list(input_dict.values())\n", |
| 276 | + "print(sorted(value_list))" |
| 277 | + ] |
| 278 | + }, |
| 279 | + { |
| 280 | + "cell_type": "code", |
| 281 | + "execution_count": 18, |
| 282 | + "metadata": {}, |
| 283 | + "outputs": [ |
| 284 | + { |
| 285 | + "name": "stdout", |
| 286 | + "output_type": "stream", |
| 287 | + "text": [ |
| 288 | + "[1]\n", |
| 289 | + "[1, 7, 8, 9]\n" |
| 290 | + ] |
| 291 | + } |
| 292 | + ], |
| 293 | + "source": [ |
| 294 | + "'''\n", |
| 295 | + "Set_diff\n", |
| 296 | + "\n", |
| 297 | + "Description\n", |
| 298 | + "\n", |
| 299 | + "Find the difference, using difference and symmetric_difference, between two given lists - list1 and list2.\n", |
| 300 | + "\n", |
| 301 | + "First, convert the lists into sets and store them as set_1 and set_2. Then store the difference and symmetric difference in answer_1 and answer_2 respectively. Print both the answers as sorted lists, i.e. convert the final sets to lists, sort it and then return it.\n", |
| 302 | + "'''\n", |
| 303 | + "\n", |
| 304 | + "import ast,sys\n", |
| 305 | + "input_list = [[1,2,3,4,5,6],[2,3,4,5,6,7,8,9]]\n", |
| 306 | + "list_1 = input_list[0]\n", |
| 307 | + "list_2 = input_list[1]\n", |
| 308 | + "\n", |
| 309 | + "set_1 = set(list_1)\n", |
| 310 | + "set_2 = set(list_2)\n", |
| 311 | + "answer_1 = sorted(list(set_1.difference(set_2)))\n", |
| 312 | + "answer_2 = sorted(list(set_1.union(set_2).difference(set_1.intersection(set_2))))\n", |
| 313 | + "\n", |
| 314 | + "print(answer_1)\n", |
| 315 | + "print(answer_2)\n" |
| 316 | + ] |
| 317 | + }, |
192 | 318 | {
|
193 | 319 | "cell_type": "code",
|
194 | 320 | "execution_count": null,
|
|
0 commit comments