Skip to content

Commit 3c0abf4

Browse files
committed
Dict
1 parent 11c82e9 commit 3c0abf4

File tree

1 file changed

+204
-13
lines changed

1 file changed

+204
-13
lines changed

Diff for: 08.Dictionary .ipynb

+204-13
Original file line numberDiff line numberDiff line change
@@ -513,31 +513,222 @@
513513
},
514514
{
515515
"cell_type": "code",
516-
"execution_count": 31,
516+
"execution_count": 32,
517517
"metadata": {},
518518
"outputs": [
519519
{
520520
"name": "stdout",
521521
"output_type": "stream",
522522
"text": [
523-
"Salary: None\n"
523+
"{'a': 'vowel', 'u': 'vowel', 'i': 'vowel', 'o': 'vowel', 'e': 'vowel'}\n"
524524
]
525-
},
525+
}
526+
],
527+
"source": [
528+
"keys = {'a', 'e', 'i', 'o', 'u' }\n",
529+
"value = 'vowel'\n",
530+
"\n",
531+
"vowels = dict.fromkeys(keys, value)\n",
532+
"print(vowels)"
533+
]
534+
},
535+
{
536+
"cell_type": "code",
537+
"execution_count": 33,
538+
"metadata": {},
539+
"outputs": [
526540
{
527-
"ename": "KeyError",
528-
"evalue": "'salary'",
529-
"output_type": "error",
530-
"traceback": [
531-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
532-
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
533-
"\u001b[0;32m<ipython-input-31-b00495ba314f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Salary: '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mperson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'salary'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mperson\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'salary'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
534-
"\u001b[0;31mKeyError\u001b[0m: 'salary'"
541+
"name": "stdout",
542+
"output_type": "stream",
543+
"text": [
544+
"{'a': [1], 'u': [1], 'i': [1], 'o': [1], 'e': [1]}\n",
545+
"{'a': [1, 2], 'u': [1, 2], 'i': [1, 2], 'o': [1, 2], 'e': [1, 2]}\n"
535546
]
536547
}
537548
],
538549
"source": [
539-
"print('Salary: ', person.get('salary'))\n",
540-
"print(person['salary'])"
550+
"keys = {'a', 'e', 'i', 'o', 'u' }\n",
551+
"value = [1]\n",
552+
"\n",
553+
"vowels = dict.fromkeys(keys, value)\n",
554+
"print(vowels)\n",
555+
"\n",
556+
"value.append(2)\n",
557+
"print(vowels)"
558+
]
559+
},
560+
{
561+
"cell_type": "code",
562+
"execution_count": 34,
563+
"metadata": {},
564+
"outputs": [
565+
{
566+
"name": "stdout",
567+
"output_type": "stream",
568+
"text": [
569+
"{'a': [1], 'u': [1], 'i': [1], 'o': [1], 'e': [1]}\n",
570+
"{'a': [1], 'u': [1], 'i': [1], 'o': [1], 'e': [1]}\n"
571+
]
572+
}
573+
],
574+
"source": [
575+
"keys = {'a', 'e', 'i', 'o', 'u' }\n",
576+
"value = [1]\n",
577+
"\n",
578+
"vowels = { key : list(value) for key in keys }\n",
579+
"\n",
580+
"print(vowels)\n",
581+
"\n",
582+
"\n",
583+
"value.append(2)\n",
584+
"print(vowels)"
585+
]
586+
},
587+
{
588+
"cell_type": "code",
589+
"execution_count": 35,
590+
"metadata": {},
591+
"outputs": [
592+
{
593+
"name": "stdout",
594+
"output_type": "stream",
595+
"text": [
596+
"{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n"
597+
]
598+
}
599+
],
600+
"source": [
601+
"squares = {x: x*x for x in range(6)}\n",
602+
"\n",
603+
"print(squares)"
604+
]
605+
},
606+
{
607+
"cell_type": "code",
608+
"execution_count": 36,
609+
"metadata": {},
610+
"outputs": [],
611+
"source": [
612+
"squares = {}\n",
613+
"for x in range(6):\n",
614+
" squares[x] = x*x"
615+
]
616+
},
617+
{
618+
"cell_type": "code",
619+
"execution_count": 37,
620+
"metadata": {},
621+
"outputs": [
622+
{
623+
"data": {
624+
"text/plain": [
625+
"{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}"
626+
]
627+
},
628+
"execution_count": 37,
629+
"metadata": {},
630+
"output_type": "execute_result"
631+
}
632+
],
633+
"source": [
634+
"squares"
635+
]
636+
},
637+
{
638+
"cell_type": "code",
639+
"execution_count": 38,
640+
"metadata": {},
641+
"outputs": [
642+
{
643+
"name": "stdout",
644+
"output_type": "stream",
645+
"text": [
646+
"{1: 1, 3: 9, 5: 25, 7: 49, 9: 81}\n"
647+
]
648+
}
649+
],
650+
"source": [
651+
"odd_squares = {x: x*x for x in range(11) if x%2 == 1}\n",
652+
"\n",
653+
"print(odd_squares)"
654+
]
655+
},
656+
{
657+
"cell_type": "code",
658+
"execution_count": 39,
659+
"metadata": {},
660+
"outputs": [
661+
{
662+
"name": "stdout",
663+
"output_type": "stream",
664+
"text": [
665+
"True\n"
666+
]
667+
}
668+
],
669+
"source": [
670+
"squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}\n",
671+
"\n",
672+
"print(1 in squares)\n"
673+
]
674+
},
675+
{
676+
"cell_type": "code",
677+
"execution_count": 41,
678+
"metadata": {},
679+
"outputs": [
680+
{
681+
"name": "stdout",
682+
"output_type": "stream",
683+
"text": [
684+
"True\n"
685+
]
686+
}
687+
],
688+
"source": [
689+
"\n",
690+
"print(2 not in squares)\n"
691+
]
692+
},
693+
{
694+
"cell_type": "code",
695+
"execution_count": 42,
696+
"metadata": {},
697+
"outputs": [
698+
{
699+
"name": "stdout",
700+
"output_type": "stream",
701+
"text": [
702+
"False\n"
703+
]
704+
}
705+
],
706+
"source": [
707+
"\n",
708+
"print(49 in squares)"
709+
]
710+
},
711+
{
712+
"cell_type": "code",
713+
"execution_count": 43,
714+
"metadata": {},
715+
"outputs": [
716+
{
717+
"name": "stdout",
718+
"output_type": "stream",
719+
"text": [
720+
"1\n",
721+
"9\n",
722+
"25\n",
723+
"49\n",
724+
"81\n"
725+
]
726+
}
727+
],
728+
"source": [
729+
"squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}\n",
730+
"for i in squares:\n",
731+
" print(squares[i])"
541732
]
542733
},
543734
{

0 commit comments

Comments
 (0)