\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mSport\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'baseball'\u001b[0m \u001b[1;31m# capital 'S'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msport\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# lowercase 's'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
150 | "\u001b[1;31mNameError\u001b[0m: name 'sport' is not defined"
151 | ]
152 | }
153 | ],
154 | "source": [
155 | "Sport = 'baseball' # capital 'S'\n",
156 | "print(sport) # lowercase 's'"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": 3,
162 | "metadata": {},
163 | "outputs": [
164 | {
165 | "name": "stdout",
166 | "output_type": "stream",
167 | "text": [
168 | "5 8.4\n"
169 | ]
170 | }
171 | ],
172 | "source": [
173 | "num1 = 5 # storing an integer into a variable\n",
174 | "\n",
175 | "num2 = 8.4 # storing a float into a variable\n",
176 | "\n",
177 | "print(num1, num2) # you can print multiple items using commas"
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": 5,
183 | "metadata": {},
184 | "outputs": [
185 | {
186 | "name": "stdout",
187 | "output_type": "stream",
188 | "text": [
189 | "True\n"
190 | ]
191 | }
192 | ],
193 | "source": [
194 | "# storing a boolean into a variable\n",
195 | "\n",
196 | "switch = True\n",
197 | "\n",
198 | "print(switch)"
199 | ]
200 | },
201 | {
202 | "cell_type": "code",
203 | "execution_count": 5,
204 | "metadata": {},
205 | "outputs": [
206 | {
207 | "name": "stdout",
208 | "output_type": "stream",
209 | "text": [
210 | "John Smith 9\n"
211 | ]
212 | }
213 | ],
214 | "source": [
215 | "# storing strings into a variable\n",
216 | "\n",
217 | "name = 'John Smith'\n",
218 | "\n",
219 | "fav_number = '9'\n",
220 | "\n",
221 | "print(name, fav_number) # will print 9 next to the name"
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "execution_count": 5,
227 | "metadata": {},
228 | "outputs": [
229 | {
230 | "name": "stdout",
231 | "output_type": "stream",
232 | "text": [
233 | "13.4\n"
234 | ]
235 | }
236 | ],
237 | "source": [
238 | "# using two variables to create another variable\n",
239 | "\n",
240 | "result = num1 + num2\n",
241 | "\n",
242 | "print(result)"
243 | ]
244 | },
245 | {
246 | "cell_type": "code",
247 | "execution_count": 6,
248 | "metadata": {},
249 | "outputs": [
250 | {
251 | "name": "stdout",
252 | "output_type": "stream",
253 | "text": [
254 | "14.4\n",
255 | "72.0\n"
256 | ]
257 | }
258 | ],
259 | "source": [
260 | "# adding, deleting, multiplying, dividing from a variable\n",
261 | "\n",
262 | "result += 1 # same as saying result = result + 1\n",
263 | "print(result)\n",
264 | "\n",
265 | "result *= num1 # same as saying result = result * num1\n",
266 | "print(result)"
267 | ]
268 | },
269 | {
270 | "cell_type": "code",
271 | "execution_count": 7,
272 | "metadata": {},
273 | "outputs": [
274 | {
275 | "name": "stdout",
276 | "output_type": "stream",
277 | "text": [
278 | "John\n",
279 | "Sam\n"
280 | ]
281 | }
282 | ],
283 | "source": [
284 | "# defining a variable and overwriting it's value\n",
285 | "\n",
286 | "name = 'John'\n",
287 | "print(name)\n",
288 | "\n",
289 | "name = 'Sam'\n",
290 | "print(name)"
291 | ]
292 | },
293 | {
294 | "cell_type": "markdown",
295 | "metadata": {},
296 | "source": [
297 | "# Working with Strings"
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "execution_count": 8,
303 | "metadata": {},
304 | "outputs": [
305 | {
306 | "name": "stdout",
307 | "output_type": "stream",
308 | "text": [
309 | "John Smith\n"
310 | ]
311 | }
312 | ],
313 | "source": [
314 | "# using the addition operator without variables\n",
315 | "\n",
316 | "name = 'John' + ' ' + 'Smith'\n",
317 | "\n",
318 | "print(name)"
319 | ]
320 | },
321 | {
322 | "cell_type": "code",
323 | "execution_count": 9,
324 | "metadata": {},
325 | "outputs": [
326 | {
327 | "name": "stdout",
328 | "output_type": "stream",
329 | "text": [
330 | "John Smith\n"
331 | ]
332 | }
333 | ],
334 | "source": [
335 | "# using the addition operator with variables\n",
336 | "\n",
337 | "first_name = 'John'\n",
338 | "last_name = 'Smith'\n",
339 | "\n",
340 | "full_name = first_name + ' ' + last_name\n",
341 | "\n",
342 | "print(full_name)"
343 | ]
344 | },
345 | {
346 | "cell_type": "code",
347 | "execution_count": 10,
348 | "metadata": {},
349 | "outputs": [
350 | {
351 | "name": "stdout",
352 | "output_type": "stream",
353 | "text": [
354 | "Hello John\n",
355 | "Hello John, you are 28 years old!\n"
356 | ]
357 | }
358 | ],
359 | "source": [
360 | "# injecting variables using the format method\n",
361 | "\n",
362 | "name = 'John'\n",
363 | "\n",
364 | "print('Hello {}'.format(name))\n",
365 | "\n",
366 | "print('Hello {}, you are {} years old!'.format(name, 28))"
367 | ]
368 | },
369 | {
370 | "cell_type": "code",
371 | "execution_count": 11,
372 | "metadata": {},
373 | "outputs": [
374 | {
375 | "name": "stdout",
376 | "output_type": "stream",
377 | "text": [
378 | "Hello John\n"
379 | ]
380 | }
381 | ],
382 | "source": [
383 | "# using the new f strings\n",
384 | "\n",
385 | "name = 'John'\n",
386 | "\n",
387 | "print(f'Hello {name}')"
388 | ]
389 | },
390 | {
391 | "cell_type": "code",
392 | "execution_count": 12,
393 | "metadata": {},
394 | "outputs": [
395 | {
396 | "name": "stdout",
397 | "output_type": "stream",
398 | "text": [
399 | "Hello, John\n"
400 | ]
401 | }
402 | ],
403 | "source": [
404 | "# one major difference between versions 2 & 3\n",
405 | "\n",
406 | "name = 'John'\n",
407 | "\n",
408 | "print('Hello, %s' % name)"
409 | ]
410 | },
411 | {
412 | "cell_type": "code",
413 | "execution_count": 6,
414 | "metadata": {},
415 | "outputs": [
416 | {
417 | "name": "stdout",
418 | "output_type": "stream",
419 | "text": [
420 | "Hello, John Smith\n"
421 | ]
422 | }
423 | ],
424 | "source": [
425 | "# python 2 multiple variable formatting\n",
426 | "\n",
427 | "first_name = \"John\"\n",
428 | "last_name = \"Smith\"\n",
429 | "\n",
430 | "print(\"Hello, %s %s\" % (first_name, last_name))"
431 | ]
432 | },
433 | {
434 | "cell_type": "code",
435 | "execution_count": 8,
436 | "metadata": {},
437 | "outputs": [
438 | {
439 | "name": "stdout",
440 | "output_type": "stream",
441 | "text": [
442 | "H\n",
443 | "e\n",
444 | "o\n"
445 | ]
446 | }
447 | ],
448 | "source": [
449 | "# using indexes to print each element\n",
450 | "\n",
451 | "word = 'Hello'\n",
452 | "\n",
453 | "print(word[0]) # will output 'H'\n",
454 | "print(word[1]) # will output 'e'\n",
455 | "print(word[-1]) # will output 'o'"
456 | ]
457 | },
458 | {
459 | "cell_type": "code",
460 | "execution_count": 14,
461 | "metadata": {},
462 | "outputs": [
463 | {
464 | "name": "stdout",
465 | "output_type": "stream",
466 | "text": [
467 | "He\n"
468 | ]
469 | }
470 | ],
471 | "source": [
472 | "print(word[0:2]) # will output 'He'"
473 | ]
474 | },
475 | {
476 | "cell_type": "code",
477 | "execution_count": 9,
478 | "metadata": {},
479 | "outputs": [
480 | {
481 | "name": "stdout",
482 | "output_type": "stream",
483 | "text": [
484 | "Hlo\n"
485 | ]
486 | }
487 | ],
488 | "source": [
489 | "print( word[ 0 : 5 : 2 ] )"
490 | ]
491 | },
492 | {
493 | "cell_type": "markdown",
494 | "metadata": {},
495 | "source": [
496 | "# Manipulating Strings"
497 | ]
498 | },
499 | {
500 | "cell_type": "code",
501 | "execution_count": 15,
502 | "metadata": {},
503 | "outputs": [
504 | {
505 | "name": "stdout",
506 | "output_type": "stream",
507 | "text": [
508 | "John Smith\n"
509 | ]
510 | }
511 | ],
512 | "source": [
513 | "# using the title method to capitalize a string\n",
514 | "\n",
515 | "name = 'john smith'\n",
516 | "\n",
517 | "print(name.title())"
518 | ]
519 | },
520 | {
521 | "cell_type": "code",
522 | "execution_count": 16,
523 | "metadata": {},
524 | "outputs": [
525 | {
526 | "name": "stdout",
527 | "output_type": "stream",
528 | "text": [
529 | "Hello there.\n"
530 | ]
531 | }
532 | ],
533 | "source": [
534 | "# replacing an exclamation point with a period\n",
535 | "\n",
536 | "words = 'Hello there!'\n",
537 | "\n",
538 | "print(words.replace('!', '.'))"
539 | ]
540 | },
541 | {
542 | "cell_type": "code",
543 | "execution_count": 17,
544 | "metadata": {},
545 | "outputs": [
546 | {
547 | "name": "stdout",
548 | "output_type": "stream",
549 | "text": [
550 | "5\n"
551 | ]
552 | }
553 | ],
554 | "source": [
555 | "# finding the starting index of our searched term\n",
556 | "\n",
557 | "s = 'Look over that way'\n",
558 | "\n",
559 | "print(s.find('over'))"
560 | ]
561 | },
562 | {
563 | "cell_type": "code",
564 | "execution_count": 12,
565 | "metadata": {},
566 | "outputs": [
567 | {
568 | "name": "stdout",
569 | "output_type": "stream",
570 | "text": [
571 | "john\n"
572 | ]
573 | }
574 | ],
575 | "source": [
576 | "# removing white space with strip\n",
577 | "\n",
578 | "name = ' john '\n",
579 | "\n",
580 | "print(name.strip( ))"
581 | ]
582 | },
583 | {
584 | "cell_type": "code",
585 | "execution_count": 19,
586 | "metadata": {},
587 | "outputs": [
588 | {
589 | "name": "stdout",
590 | "output_type": "stream",
591 | "text": [
592 | "['These', 'words', 'are', 'separated', 'by', 'spaces']\n"
593 | ]
594 | }
595 | ],
596 | "source": [
597 | "# converting a string into a list of words\n",
598 | "\n",
599 | "s = 'These words are separated by spaces'\n",
600 | "\n",
601 | "print(s.split(' '))"
602 | ]
603 | },
604 | {
605 | "cell_type": "markdown",
606 | "metadata": {},
607 | "source": [
608 | "# Friday Project: Printing Receipts"
609 | ]
610 | },
611 | {
612 | "cell_type": "code",
613 | "execution_count": 68,
614 | "metadata": {
615 | "scrolled": true
616 | },
617 | "outputs": [
618 | {
619 | "name": "stdout",
620 | "output_type": "stream",
621 | "text": [
622 | "**************************************************\n",
623 | "\t\tCoding Temple, Inc.\n",
624 | "\t\t283 Franklin St.\n",
625 | "\t\tBoston, Ma\n",
626 | "==================================================\n",
627 | "\tProduct Name\tProduct Price\n",
628 | "\tBooks\t\t$49.95\n",
629 | "\tComputer\t$579.99\n",
630 | "\tMonitor\t\t$124.89\n",
631 | "==================================================\n",
632 | "\t\t\tTotal\n",
633 | "\t\t\t$754.83\n",
634 | "==================================================\n",
635 | "\n",
636 | "\tThanks for shopping with us today!\n",
637 | "\n",
638 | "**************************************************\n"
639 | ]
640 | }
641 | ],
642 | "source": [
643 | "# create a product and price for three items\n",
644 | "p1_name, p1_price = 'Books', 49.95\n",
645 | "p2_name, p2_price = 'Computer', 579.99\n",
646 | "p3_name, p3_price = 'Monitor', 124.89\n",
647 | "\n",
648 | "# create a company name and information\n",
649 | "company_name = 'coding temple, inc.'\n",
650 | "company_address = '283 Franklin St.'\n",
651 | "company_city = 'Boston, MA'\n",
652 | "\n",
653 | "# declare ending message\n",
654 | "message = 'Thanks for shopping with us today!'\n",
655 | "\n",
656 | "# create a top border\n",
657 | "print('*' * 50)\n",
658 | "\n",
659 | "# print company information first using format\n",
660 | "print('\\t\\t{}'.format(company_name.title()))\n",
661 | "print('\\t\\t{}'.format(company_address.title()))\n",
662 | "print('\\t\\t{}'.format(company_city.title()))\n",
663 | "\n",
664 | "# print a line between sections\n",
665 | "print('=' * 50)\n",
666 | "\n",
667 | "# print out header for section of items\n",
668 | "print('\\tProduct Name\\tProduct Price')\n",
669 | "\n",
670 | "# create a print statement for each item\n",
671 | "print('\\t{}\\t\\t${}'.format(p1_name.title(), p1_price))\n",
672 | "print('\\t{}\\t${}'.format(p2_name.title(), p2_price))\n",
673 | "print('\\t{}\\t\\t${}'.format(p3_name.title(), p3_price))\n",
674 | "\n",
675 | "# print a line between sections\n",
676 | "print('=' * 50)\n",
677 | "\n",
678 | "# print out header for section of total\n",
679 | "print('\\t\\t\\tTotal')\n",
680 | "\n",
681 | "# calculate total price and print out\n",
682 | "total = p1_price + p2_price + p3_price\n",
683 | "print('\\t\\t\\t${}'.format(total))\n",
684 | "\n",
685 | "# print a line between sections\n",
686 | "print('=' * 50)\n",
687 | "\n",
688 | "# output thank you message\n",
689 | "print('\\n\\t{}\\n'.format(message))\n",
690 | "\n",
691 | "# create a bottom border\n",
692 | "print('*' * 50)"
693 | ]
694 | },
695 | {
696 | "cell_type": "markdown",
697 | "metadata": {},
698 | "source": [
699 | "# Monday Exercises - Answers"
700 | ]
701 | },
702 | {
703 | "cell_type": "markdown",
704 | "metadata": {},
705 | "source": [
706 | "\n",
707 | "1. Output: Print our your name.\n",
708 | "
"
709 | ]
710 | },
711 | {
712 | "cell_type": "code",
713 | "execution_count": 1,
714 | "metadata": {},
715 | "outputs": [
716 | {
717 | "name": "stdout",
718 | "output_type": "stream",
719 | "text": [
720 | "Connor P. Milliken\n"
721 | ]
722 | }
723 | ],
724 | "source": [
725 | "print(\"Connor P. Milliken\")"
726 | ]
727 | },
728 | {
729 | "cell_type": "markdown",
730 | "metadata": {},
731 | "source": [
732 | "\n",
733 | "2. Type Checking: Try checking the type of a value by using the type() method. This will always print out what kind of data type you're checking. This is useful to check data types when you're unsure. As an example:\n",
734 | "
\n",
735 | "\n",
736 | "\n",
737 | ">>> type(int) # will output \n",
738 | "
"
739 | ]
740 | },
741 | {
742 | "cell_type": "code",
743 | "execution_count": 4,
744 | "metadata": {},
745 | "outputs": [
746 | {
747 | "data": {
748 | "text/plain": [
749 | "int"
750 | ]
751 | },
752 | "execution_count": 4,
753 | "metadata": {},
754 | "output_type": "execute_result"
755 | }
756 | ],
757 | "source": [
758 | "type(5)"
759 | ]
760 | },
761 | {
762 | "cell_type": "markdown",
763 | "metadata": {},
764 | "source": [
765 | "# Tuesday Exercises - Answers"
766 | ]
767 | },
768 | {
769 | "cell_type": "markdown",
770 | "metadata": {},
771 | "source": [
772 | "\n",
773 | "1. Variable Output: Store the value 3 in a variable called \"x\" and the value 10 in a variable called \"y\". Save the result of x * y into a separate variabled called \"result\". Finally, output the information so it shows like the following:\n",
774 | "
\n",
775 | "\n",
776 | "\n",
777 | ">>> 3 + 10 = 13\n",
778 | "
"
779 | ]
780 | },
781 | {
782 | "cell_type": "code",
783 | "execution_count": 7,
784 | "metadata": {},
785 | "outputs": [
786 | {
787 | "name": "stdout",
788 | "output_type": "stream",
789 | "text": [
790 | "3 * 10 = 30\n"
791 | ]
792 | }
793 | ],
794 | "source": [
795 | "x = 3\n",
796 | "y = 10\n",
797 | "\n",
798 | "result = x * y\n",
799 | "\n",
800 | "print(x, '*', y, '=', result)"
801 | ]
802 | },
803 | {
804 | "cell_type": "markdown",
805 | "metadata": {},
806 | "source": [
807 | "\n",
808 | "2. Area Calculation: Calculate the area of a 245.54\" x 13.66\" rectangle. Print out the result. HINT: Area is width multiplied by height.\n",
809 | "
"
810 | ]
811 | },
812 | {
813 | "cell_type": "code",
814 | "execution_count": 9,
815 | "metadata": {},
816 | "outputs": [
817 | {
818 | "name": "stdout",
819 | "output_type": "stream",
820 | "text": [
821 | "The area is 3354.0764\n"
822 | ]
823 | }
824 | ],
825 | "source": [
826 | "width = 245.54\n",
827 | "height = 13.66\n",
828 | "\n",
829 | "area = width * height\n",
830 | "\n",
831 | "print('The area is', area)"
832 | ]
833 | },
834 | {
835 | "cell_type": "markdown",
836 | "metadata": {},
837 | "source": [
838 | "# Wednesday Exercises - Answers"
839 | ]
840 | },
841 | {
842 | "cell_type": "markdown",
843 | "metadata": {},
844 | "source": [
845 | "\n",
846 | "1. Variable Injection: Create a print statement that injects an integer, float, boolean, and string all into one line. The output should look like \"23 4.5 False John\".\n",
847 | "
"
848 | ]
849 | },
850 | {
851 | "cell_type": "code",
852 | "execution_count": 10,
853 | "metadata": {},
854 | "outputs": [
855 | {
856 | "name": "stdout",
857 | "output_type": "stream",
858 | "text": [
859 | "23 4.5 False John\n"
860 | ]
861 | }
862 | ],
863 | "source": [
864 | "print(\"{} {} {} {}\".format(23, 4.5, False, \"John\"))"
865 | ]
866 | },
867 | {
868 | "cell_type": "markdown",
869 | "metadata": {},
870 | "source": [
871 | "\n",
872 | "2. Fill in the Blanks: Using the format method, fill in the blanks below by assinging your name and favorite activities into variables:\n",
873 | "
\n",
874 | "\n",
875 | "\n",
876 | "\"{}'s favorite sport is {}.\"\n",
877 | "
\n",
878 | "\n",
879 | "\n",
880 | "\"{} is working on {} programming!\"\n",
881 | "
"
882 | ]
883 | },
884 | {
885 | "cell_type": "code",
886 | "execution_count": 11,
887 | "metadata": {},
888 | "outputs": [
889 | {
890 | "name": "stdout",
891 | "output_type": "stream",
892 | "text": [
893 | "Connor's favorite sport is Baseball\n",
894 | "Connor is working on Python programming!\n"
895 | ]
896 | }
897 | ],
898 | "source": [
899 | "print(\"{}'s favorite sport is {}\".format(\"Connor\", \"Baseball\"))\n",
900 | "print(\"{} is working on {} programming!\".format(\"Connor\", \"Python\"))"
901 | ]
902 | },
903 | {
904 | "cell_type": "markdown",
905 | "metadata": {},
906 | "source": [
907 | "# Thursday Exercises - Answers"
908 | ]
909 | },
910 | {
911 | "cell_type": "markdown",
912 | "metadata": {},
913 | "source": [
914 | "\n",
915 | "1. Uppercasing: Try manipulating the string 'uppercase' so it prints out as all uppercase letters. You'll need to lookup a new method.\n",
916 | "
"
917 | ]
918 | },
919 | {
920 | "cell_type": "code",
921 | "execution_count": 13,
922 | "metadata": {},
923 | "outputs": [
924 | {
925 | "name": "stdout",
926 | "output_type": "stream",
927 | "text": [
928 | "UPPERCASE\n"
929 | ]
930 | }
931 | ],
932 | "source": [
933 | "s = 'uppercase'\n",
934 | "\n",
935 | "print(s.upper())"
936 | ]
937 | },
938 | {
939 | "cell_type": "markdown",
940 | "metadata": {},
941 | "source": [
942 | "\n",
943 | "2. Strip Symbols: Strip all dollar signs from the left side of this string \"$$John Smith\". Try it with .lstrip() and .strip(). To see a description of how to use the strip method further, try using the help function by typing the following:\n",
944 | "
\n",
945 | "\n",
946 | "\n",
947 | ">>> help(\"\".strip)\n",
948 | "
"
949 | ]
950 | },
951 | {
952 | "cell_type": "code",
953 | "execution_count": 15,
954 | "metadata": {},
955 | "outputs": [
956 | {
957 | "name": "stdout",
958 | "output_type": "stream",
959 | "text": [
960 | "John Smith\n"
961 | ]
962 | }
963 | ],
964 | "source": [
965 | "# help(\"\".strip)\n",
966 | "\n",
967 | "s = \"$$John Smith\"\n",
968 | "\n",
969 | "print(s.lstrip('$'))"
970 | ]
971 | },
972 | {
973 | "cell_type": "markdown",
974 | "metadata": {},
975 | "source": [
976 | "# End of Week Exercises - Answers"
977 | ]
978 | },
979 | {
980 | "cell_type": "markdown",
981 | "metadata": {},
982 | "source": [
983 | "\n",
984 | "1. Side Borders: In the Friday project, we ended up creating borders above and below the information printed out. Try adding a star border on the sides as well now.\n",
985 | "
"
986 | ]
987 | },
988 | {
989 | "cell_type": "code",
990 | "execution_count": 35,
991 | "metadata": {},
992 | "outputs": [
993 | {
994 | "name": "stdout",
995 | "output_type": "stream",
996 | "text": [
997 | "**************************************************\n",
998 | "*\t\tCoding Temple, Inc.\t\t *\n",
999 | "*\t\t283 Franklin St.\t\t *\n",
1000 | "*\t\tBoston, Ma\t\t\t *\n",
1001 | "*================================================*\n",
1002 | "*\tProduct Name\tProduct Price\t\t *\n",
1003 | "*\tBooks\t\t$49.95\t\t\t *\n",
1004 | "*\tComputer\t$579.99\t\t\t *\n",
1005 | "*\tMonitor\t\t$124.89\t\t\t *\n",
1006 | "*================================================*\n",
1007 | "*\t\t\tTotal\t\t\t *\n",
1008 | "*\t\t\t$754.83\t\t\t *\n",
1009 | "*================================================*\n",
1010 | "*\t\t\t\t\t\t *\n",
1011 | "*\tThanks for shopping with us today!\t *\n",
1012 | "*\t\t\t\t\t\t *\n",
1013 | "**************************************************\n"
1014 | ]
1015 | }
1016 | ],
1017 | "source": [
1018 | "# create a product and price for three items\n",
1019 | "p1_name, p1_price = 'Books', 49.95\n",
1020 | "p2_name, p2_price = 'Computer', 579.99\n",
1021 | "p3_name, p3_price = 'Monitor', 124.89\n",
1022 | "\n",
1023 | "# create a company name and information\n",
1024 | "company_name = 'coding temple, inc.'\n",
1025 | "company_address = '283 Franklin St.'\n",
1026 | "company_city = 'Boston, MA'\n",
1027 | "\n",
1028 | "# declare ending message\n",
1029 | "message = 'Thanks for shopping with us today!'\n",
1030 | "\n",
1031 | "# create a top border\n",
1032 | "print('*' * 50)\n",
1033 | "\n",
1034 | "# print company information first using format\n",
1035 | "print('*\\t\\t{}\\t\\t *'.format(company_name.title()))\n",
1036 | "print('*\\t\\t{}\\t\\t *'.format(company_address.title()))\n",
1037 | "print('*\\t\\t{}\\t\\t\\t *'.format(company_city.title()))\n",
1038 | "\n",
1039 | "# print a line between sections\n",
1040 | "print('*' + '=' * 48 + '*')\n",
1041 | "\n",
1042 | "# print out header for section of items\n",
1043 | "print('*\\tProduct Name\\tProduct Price\\t\\t *')\n",
1044 | "\n",
1045 | "# create a print statement for each item\n",
1046 | "print('*\\t{}\\t\\t${}\\t\\t\\t *'.format(p1_name.title(), p1_price))\n",
1047 | "print('*\\t{}\\t${}\\t\\t\\t *'.format(p2_name.title(), p2_price))\n",
1048 | "print('*\\t{}\\t\\t${}\\t\\t\\t *'.format(p3_name.title(), p3_price))\n",
1049 | "\n",
1050 | "# print a line between sections\n",
1051 | "print('*' + '=' * 48 + '*')\n",
1052 | "\n",
1053 | "# print out header for section of total\n",
1054 | "print('*\\t\\t\\tTotal\\t\\t\\t *')\n",
1055 | "\n",
1056 | "# calculate total price and print out\n",
1057 | "total = p1_price + p2_price + p3_price\n",
1058 | "print('*\\t\\t\\t${}\\t\\t\\t *'.format(total))\n",
1059 | "\n",
1060 | "# print a line between sections\n",
1061 | "print('*' + '=' * 48 + '*')\n",
1062 | "\n",
1063 | "# output thank you message\n",
1064 | "print('*\\t\\t\\t\\t\\t\\t *\\n*\\t{}\\t *\\n*\\t\\t\\t\\t\\t\\t *'.format(message))\n",
1065 | "\n",
1066 | "# create a bottom border\n",
1067 | "print('*' * 50)"
1068 | ]
1069 | },
1070 | {
1071 | "cell_type": "markdown",
1072 | "metadata": {},
1073 | "source": [
1074 | "\n",
1075 | "2. Researching Methods: We've gone over a few of the string manipulation methods that are widely used; however, there's many more, try looking up some and implementing them.\n",
1076 | "
"
1077 | ]
1078 | },
1079 | {
1080 | "cell_type": "markdown",
1081 | "metadata": {},
1082 | "source": [
1083 | "\n",
1084 | "3. Reverse: Declare a variable equal to \"Hello\". Reverse the string using slicing. Try looking it up if you struggle. Tip: You can define a start, stop, and step when slicing.\n",
1085 | "
"
1086 | ]
1087 | },
1088 | {
1089 | "cell_type": "code",
1090 | "execution_count": 16,
1091 | "metadata": {},
1092 | "outputs": [
1093 | {
1094 | "name": "stdout",
1095 | "output_type": "stream",
1096 | "text": [
1097 | "olleH\n"
1098 | ]
1099 | }
1100 | ],
1101 | "source": [
1102 | "s = \"Hello\"\n",
1103 | "\n",
1104 | "print(s[::-1])"
1105 | ]
1106 | }
1107 | ],
1108 | "metadata": {
1109 | "kernelspec": {
1110 | "display_name": "Python 3",
1111 | "language": "python",
1112 | "name": "python3"
1113 | },
1114 | "language_info": {
1115 | "codemirror_mode": {
1116 | "name": "ipython",
1117 | "version": 3
1118 | },
1119 | "file_extension": ".py",
1120 | "mimetype": "text/x-python",
1121 | "name": "python",
1122 | "nbconvert_exporter": "python",
1123 | "pygments_lexer": "ipython3",
1124 | "version": "3.6.5"
1125 | }
1126 | },
1127 | "nbformat": 4,
1128 | "nbformat_minor": 2
1129 | }
1130 |
--------------------------------------------------------------------------------
/Week_04.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Lists"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [
15 | {
16 | "name": "stdout",
17 | "output_type": "stream",
18 | "text": [
19 | "[5, 10, 15.2, 20]\n"
20 | ]
21 | }
22 | ],
23 | "source": [
24 | "# declaring a list of numbers\n",
25 | "\n",
26 | "nums = [5, 10, 15.2, 20]\n",
27 | "\n",
28 | "print(nums)"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 3,
34 | "metadata": {},
35 | "outputs": [
36 | {
37 | "name": "stdout",
38 | "output_type": "stream",
39 | "text": [
40 | "10\n",
41 | "15.2\n"
42 | ]
43 | }
44 | ],
45 | "source": [
46 | "# accessing elements within a list\n",
47 | "\n",
48 | "print(nums[1]) # will output the value at index 1 = 10\n",
49 | "\n",
50 | "num = nums[2] # saves index value 2 into num\n",
51 | "\n",
52 | "print(num) # prints value assigned to num"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 5,
58 | "metadata": {},
59 | "outputs": [
60 | {
61 | "name": "stdout",
62 | "output_type": "stream",
63 | "text": [
64 | "[4.3, 'word', True]\n"
65 | ]
66 | }
67 | ],
68 | "source": [
69 | "# declaring a list of mixed data types\n",
70 | "\n",
71 | "num = 4.3\n",
72 | "\n",
73 | "data = [num, 'word', True] # the power of data collection\n",
74 | "\n",
75 | "print(data)"
76 | ]
77 | },
78 | {
79 | "cell_type": "code",
80 | "execution_count": 6,
81 | "metadata": {},
82 | "outputs": [
83 | {
84 | "name": "stdout",
85 | "output_type": "stream",
86 | "text": [
87 | "[5, 'book', [34, 'hello'], True]\n",
88 | "[34, 'hello']\n"
89 | ]
90 | }
91 | ],
92 | "source": [
93 | "# understanding lists within lists\n",
94 | "\n",
95 | "data = [5, 'book', [34, 'hello'], True] # lists can hold any type\n",
96 | "\n",
97 | "print(data)\n",
98 | "\n",
99 | "print(data[2])"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 7,
105 | "metadata": {},
106 | "outputs": [
107 | {
108 | "name": "stdout",
109 | "output_type": "stream",
110 | "text": [
111 | "34\n",
112 | "hello\n"
113 | ]
114 | }
115 | ],
116 | "source": [
117 | "# using double bracket notation to access lists within lists\n",
118 | "\n",
119 | "print(data[2][0]) # will output 34\n",
120 | "\n",
121 | "inner_list = data[2] # inner list will equal [34, 'hello']\n",
122 | "\n",
123 | "print(inner_list[1]) # will output 'hello'"
124 | ]
125 | },
126 | {
127 | "cell_type": "code",
128 | "execution_count": 8,
129 | "metadata": {},
130 | "outputs": [
131 | {
132 | "name": "stdout",
133 | "output_type": "stream",
134 | "text": [
135 | "[5, 10, 15, 20]\n",
136 | "[100, 10, 15, 20]\n"
137 | ]
138 | }
139 | ],
140 | "source": [
141 | "# changing values in a list through index\n",
142 | "\n",
143 | "data = [5, 10, 15, 20]\n",
144 | "\n",
145 | "print(data)\n",
146 | "\n",
147 | "data[0] = 100 # change the value at index 0 - (5 to 100)\n",
148 | "\n",
149 | "print(data)"
150 | ]
151 | },
152 | {
153 | "cell_type": "code",
154 | "execution_count": 9,
155 | "metadata": {},
156 | "outputs": [
157 | {
158 | "name": "stdout",
159 | "output_type": "stream",
160 | "text": [
161 | "a: [5, 10]\t b: [5, 10]\n",
162 | "Location a[0]: 1977642176\t Location b[0]: 1977642176\n",
163 | "a: [20, 10]\t b: [20, 10]\n"
164 | ]
165 | }
166 | ],
167 | "source": [
168 | "# understanding how lists are stored\n",
169 | "\n",
170 | "a = [5, 10]\n",
171 | "\n",
172 | "b = a\n",
173 | "\n",
174 | "print('a: {}\\t b: {}'.format(a, b))\n",
175 | "\n",
176 | "print('Location a[0]: {}\\t Location b[0]: {}'.format(id(a[0]), id(b[0])))\n",
177 | "\n",
178 | "a[0] = 20 # re-declaring the value of a[0] also changes b[0]\n",
179 | "\n",
180 | "print('a: {}\\t b: {}'.format(a, b))"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": 10,
186 | "metadata": {},
187 | "outputs": [
188 | {
189 | "name": "stdout",
190 | "output_type": "stream",
191 | "text": [
192 | "data: [50, 10, 15, 20]\t data_copy: [5, 10, 15, 20]\n"
193 | ]
194 | }
195 | ],
196 | "source": [
197 | "# using [:] to copy a list\n",
198 | "\n",
199 | "data = [5, 10, 15, 20]\n",
200 | "\n",
201 | "data_copy = data[:] # a single colon copies the list\n",
202 | "\n",
203 | "data[0] = 50\n",
204 | "\n",
205 | "print('data: {}\\t data_copy: {}'.format(data, data_copy))"
206 | ]
207 | },
208 | {
209 | "cell_type": "markdown",
210 | "metadata": {},
211 | "source": [
212 | "# For Loops"
213 | ]
214 | },
215 | {
216 | "cell_type": "code",
217 | "execution_count": 1,
218 | "metadata": {},
219 | "outputs": [
220 | {
221 | "name": "stdout",
222 | "output_type": "stream",
223 | "text": [
224 | "Value: 0\n",
225 | "Value: 1\n",
226 | "Value: 2\n",
227 | "Value: 3\n",
228 | "Value: 4\n"
229 | ]
230 | }
231 | ],
232 | "source": [
233 | "# writing your first for loop using range\n",
234 | "\n",
235 | "for num in range(5):\n",
236 | " print('Value: {}'.format(num))"
237 | ]
238 | },
239 | {
240 | "cell_type": "code",
241 | "execution_count": 2,
242 | "metadata": {},
243 | "outputs": [
244 | {
245 | "name": "stdout",
246 | "output_type": "stream",
247 | "text": [
248 | "Value: 2\n",
249 | "Value: 4\n",
250 | "Value: 6\n",
251 | "Value: 8\n"
252 | ]
253 | }
254 | ],
255 | "source": [
256 | "# providing the start, stop, and step for the range function\n",
257 | "\n",
258 | "for num in range(2, 10, 2):\n",
259 | " print('Value: {}'.format(num)) # will print all evens between 2 and 10"
260 | ]
261 | },
262 | {
263 | "cell_type": "code",
264 | "execution_count": 3,
265 | "metadata": {},
266 | "outputs": [
267 | {
268 | "name": "stdout",
269 | "output_type": "stream",
270 | "text": [
271 | "Value: J\n",
272 | "Value: o\n",
273 | "Value: h\n",
274 | "Value: n\n",
275 | "Value: \n",
276 | "Value: S\n",
277 | "Value: m\n",
278 | "Value: i\n",
279 | "Value: t\n",
280 | "Value: h\n"
281 | ]
282 | }
283 | ],
284 | "source": [
285 | "# printing all characters in a name using the 'in' keyword\n",
286 | "\n",
287 | "name = 'John Smith'\n",
288 | "\n",
289 | "for letter in name:\n",
290 | " print('Value: {}'.format(letter))"
291 | ]
292 | },
293 | {
294 | "cell_type": "code",
295 | "execution_count": 4,
296 | "metadata": {},
297 | "outputs": [
298 | {
299 | "name": "stdout",
300 | "output_type": "stream",
301 | "text": [
302 | "0\n",
303 | "1\n",
304 | "2\n",
305 | "4\n"
306 | ]
307 | }
308 | ],
309 | "source": [
310 | "# using the continue statement within a for loop\n",
311 | "\n",
312 | "for num in range(5):\n",
313 | " if num == 3:\n",
314 | " continue\n",
315 | " print(num)"
316 | ]
317 | },
318 | {
319 | "cell_type": "code",
320 | "execution_count": 5,
321 | "metadata": {},
322 | "outputs": [
323 | {
324 | "name": "stdout",
325 | "output_type": "stream",
326 | "text": [
327 | "0\n",
328 | "1\n",
329 | "2\n"
330 | ]
331 | }
332 | ],
333 | "source": [
334 | "# breaking out of a loop using the 'break' keyword\n",
335 | "\n",
336 | "for num in range(5):\n",
337 | " if num == 3:\n",
338 | " break\n",
339 | " print(num)"
340 | ]
341 | },
342 | {
343 | "cell_type": "code",
344 | "execution_count": 6,
345 | "metadata": {
346 | "collapsed": true
347 | },
348 | "outputs": [],
349 | "source": [
350 | "# setting a placeholder using the 'pass' keyword\n",
351 | "\n",
352 | "for i in range(5):\n",
353 | " # TODO: add code to print number\n",
354 | " pass"
355 | ]
356 | },
357 | {
358 | "cell_type": "markdown",
359 | "metadata": {},
360 | "source": [
361 | "# While Loops"
362 | ]
363 | },
364 | {
365 | "cell_type": "code",
366 | "execution_count": 7,
367 | "metadata": {},
368 | "outputs": [
369 | {
370 | "name": "stdout",
371 | "output_type": "stream",
372 | "text": [
373 | "10\n",
374 | "9\n",
375 | "8\n",
376 | "7\n",
377 | "6\n",
378 | "5\n",
379 | "4\n",
380 | "3\n",
381 | "2\n",
382 | "1\n"
383 | ]
384 | }
385 | ],
386 | "source": [
387 | "# writing your first while loop\n",
388 | "\n",
389 | "health = 10\n",
390 | "\n",
391 | "while health > 0:\n",
392 | " print(health)\n",
393 | " health -= 1 # forgetting this line will result in infinite loop"
394 | ]
395 | },
396 | {
397 | "cell_type": "code",
398 | "execution_count": 8,
399 | "metadata": {},
400 | "outputs": [
401 | {
402 | "name": "stdout",
403 | "output_type": "stream",
404 | "text": [
405 | "0 0\n",
406 | "0 1\n",
407 | "0 2\n",
408 | "1 0\n",
409 | "1 1\n",
410 | "1 2\n"
411 | ]
412 | }
413 | ],
414 | "source": [
415 | "# using two or more loops together is called a nested loop\n",
416 | "\n",
417 | "for i in range(2): # outside loop\n",
418 | " for j in range(3): # inside loop\n",
419 | " print(i, j)"
420 | ]
421 | },
422 | {
423 | "cell_type": "markdown",
424 | "metadata": {},
425 | "source": [
426 | "# Working with Lists"
427 | ]
428 | },
429 | {
430 | "cell_type": "code",
431 | "execution_count": 9,
432 | "metadata": {},
433 | "outputs": [
434 | {
435 | "name": "stdout",
436 | "output_type": "stream",
437 | "text": [
438 | "3\n"
439 | ]
440 | }
441 | ],
442 | "source": [
443 | "# checking the number of items within a list\n",
444 | "\n",
445 | "nums = [5, 10, 15]\n",
446 | "\n",
447 | "length = len(nums) # len() returns an integer\n",
448 | "\n",
449 | "print(length)"
450 | ]
451 | },
452 | {
453 | "cell_type": "code",
454 | "execution_count": 11,
455 | "metadata": {},
456 | "outputs": [
457 | {
458 | "name": "stdout",
459 | "output_type": "stream",
460 | "text": [
461 | "[10, 15]\n",
462 | "[5, 10]\n",
463 | "[5, 15]\n",
464 | "[10, 15]\n"
465 | ]
466 | }
467 | ],
468 | "source": [
469 | "# accessing specific items of a list with slices\n",
470 | "\n",
471 | "print(nums[1:3]) # will output items in index 1 and 2\n",
472 | "\n",
473 | "print(nums[:2]) # will output items in index 0 and 1\n",
474 | "\n",
475 | "print(nums[::2]) # will print every other index - 0, 2, 4, etc.\n",
476 | "\n",
477 | "print(nums[-2:]) # will output the last two items in list"
478 | ]
479 | },
480 | {
481 | "cell_type": "code",
482 | "execution_count": 12,
483 | "metadata": {},
484 | "outputs": [
485 | {
486 | "name": "stdout",
487 | "output_type": "stream",
488 | "text": [
489 | "[10, 20, 5]\n"
490 | ]
491 | }
492 | ],
493 | "source": [
494 | "# adding an item to the back of a list using append\n",
495 | "\n",
496 | "nums = [10, 20]\n",
497 | "\n",
498 | "nums.append(5)\n",
499 | "\n",
500 | "print(nums) # outputs [10, 20, 5]"
501 | ]
502 | },
503 | {
504 | "cell_type": "code",
505 | "execution_count": 13,
506 | "metadata": {
507 | "collapsed": true
508 | },
509 | "outputs": [],
510 | "source": [
511 | "# adding a value to the beginning of the list\n",
512 | "\n",
513 | "words = ['ball', 'base']\n",
514 | "\n",
515 | "nums.insert(0, 'glove') # first number is the index, second is the value"
516 | ]
517 | },
518 | {
519 | "cell_type": "code",
520 | "execution_count": 14,
521 | "metadata": {},
522 | "outputs": [
523 | {
524 | "name": "stdout",
525 | "output_type": "stream",
526 | "text": [
527 | "5 \n",
528 | " ['ball']\n"
529 | ]
530 | }
531 | ],
532 | "source": [
533 | "# using pop to remove items and saving to a variable to use later\n",
534 | "\n",
535 | "items = [5, 'ball', True]\n",
536 | "\n",
537 | "items.pop() # by default removes the last item\n",
538 | "\n",
539 | "removed_item = items.pop(0) # removes 5 and saves it into the variable\n",
540 | "\n",
541 | "print(removed_item, '\\n', items)"
542 | ]
543 | },
544 | {
545 | "cell_type": "code",
546 | "execution_count": 15,
547 | "metadata": {},
548 | "outputs": [
549 | {
550 | "name": "stdout",
551 | "output_type": "stream",
552 | "text": [
553 | "['baseball', 'football', 'hockey']\n"
554 | ]
555 | }
556 | ],
557 | "source": [
558 | "# using the remove method with a try and except\n",
559 | "\n",
560 | "sports = ['baseball', 'soccer', 'football', 'hockey']\n",
561 | "\n",
562 | "try:\n",
563 | " sports.remove('soccer')\n",
564 | "except:\n",
565 | " print('That item does not exist in the list.')\n",
566 | " \n",
567 | "print(sports)"
568 | ]
569 | },
570 | {
571 | "cell_type": "code",
572 | "execution_count": 16,
573 | "metadata": {},
574 | "outputs": [
575 | {
576 | "name": "stdout",
577 | "output_type": "stream",
578 | "text": [
579 | "3\n",
580 | "9\n",
581 | "17\n"
582 | ]
583 | }
584 | ],
585 | "source": [
586 | "# using min, max, and sum\n",
587 | "\n",
588 | "nums = [5, 3, 9]\n",
589 | "\n",
590 | "print(min(nums)) # will find the lowest number in the list\n",
591 | "print(max(nums)) # will find the highest number in the list\n",
592 | "print(sum(nums)) # will add all numbers in the list and return the sum"
593 | ]
594 | },
595 | {
596 | "cell_type": "code",
597 | "execution_count": 17,
598 | "metadata": {},
599 | "outputs": [
600 | {
601 | "name": "stdout",
602 | "output_type": "stream",
603 | "text": [
604 | "[5, 8, 0, 2] [0, 2, 5, 8]\n"
605 | ]
606 | }
607 | ],
608 | "source": [
609 | "# using sorted on lists for numerical and alphabetical data\n",
610 | "\n",
611 | "nums = [5, 8, 0, 2]\n",
612 | "\n",
613 | "sorted_nums = sorted(nums) # save to a new variable to use later\n",
614 | "\n",
615 | "print(nums, sorted_nums) # the original list is in tact"
616 | ]
617 | },
618 | {
619 | "cell_type": "code",
620 | "execution_count": 18,
621 | "metadata": {},
622 | "outputs": [
623 | {
624 | "name": "stdout",
625 | "output_type": "stream",
626 | "text": [
627 | "[0, 3, 5, 8]\n"
628 | ]
629 | }
630 | ],
631 | "source": [
632 | "# sorting a list with .sort() in-place\n",
633 | "\n",
634 | "nums = [5, 0, 8, 3]\n",
635 | "\n",
636 | "nums.sort() # alters the original variable directly\n",
637 | "\n",
638 | "print(nums)"
639 | ]
640 | },
641 | {
642 | "cell_type": "code",
643 | "execution_count": 19,
644 | "metadata": {},
645 | "outputs": [
646 | {
647 | "name": "stdout",
648 | "output_type": "stream",
649 | "text": [
650 | "found\n",
651 | "not found\n"
652 | ]
653 | }
654 | ],
655 | "source": [
656 | "# using conditional statements on a list\n",
657 | "\n",
658 | "names = ['Jack', 'Robert', 'Mary']\n",
659 | "\n",
660 | "if 'Mary' in names:\n",
661 | " print('found') # will run since Mary is in the list\n",
662 | " \n",
663 | "if 'Jimmy' not in names:\n",
664 | " print('not found') # will run since Jimmy is not in the list"
665 | ]
666 | },
667 | {
668 | "cell_type": "code",
669 | "execution_count": 20,
670 | "metadata": {},
671 | "outputs": [
672 | {
673 | "name": "stdout",
674 | "output_type": "stream",
675 | "text": [
676 | "empty\n"
677 | ]
678 | }
679 | ],
680 | "source": [
681 | "# using conditionals to see if a list is empty\n",
682 | "\n",
683 | "nums = []\n",
684 | "\n",
685 | "if not nums: # could also say 'if nums == []'\n",
686 | " print('empty')"
687 | ]
688 | },
689 | {
690 | "cell_type": "code",
691 | "execution_count": 21,
692 | "metadata": {},
693 | "outputs": [
694 | {
695 | "name": "stdout",
696 | "output_type": "stream",
697 | "text": [
698 | "Baseball\n",
699 | "Hockey\n",
700 | "Football\n",
701 | "Basketball\n"
702 | ]
703 | }
704 | ],
705 | "source": [
706 | "# using a for loop to print all items in a list\n",
707 | "\n",
708 | "sports = ['Baseball', 'Hockey', 'Football', 'Basketball']\n",
709 | "\n",
710 | "for sport in sports:\n",
711 | " print(sport)"
712 | ]
713 | },
714 | {
715 | "cell_type": "code",
716 | "execution_count": 22,
717 | "metadata": {},
718 | "outputs": [
719 | {
720 | "name": "stdout",
721 | "output_type": "stream",
722 | "text": [
723 | "['Jack', 'Rob', 'Robert']\n"
724 | ]
725 | }
726 | ],
727 | "source": [
728 | "# using the while loop to remove a certain value\n",
729 | "\n",
730 | "names = ['Bob', 'Jack', 'Rob', 'Bob', 'Robert']\n",
731 | "\n",
732 | "while 'Bob' in names:\n",
733 | " names.remove('Bob') # removes all instances of 'Bob'\n",
734 | " \n",
735 | "print(names)"
736 | ]
737 | },
738 | {
739 | "cell_type": "markdown",
740 | "metadata": {},
741 | "source": [
742 | "# Friday Project: Creating Hangman"
743 | ]
744 | },
745 | {
746 | "cell_type": "code",
747 | "execution_count": 25,
748 | "metadata": {},
749 | "outputs": [
750 | {
751 | "name": "stdout",
752 | "output_type": "stream",
753 | "text": [
754 | "Thanks for playing.\n"
755 | ]
756 | }
757 | ],
758 | "source": [
759 | "# import additional functions\n",
760 | "from random import choice\n",
761 | "from IPython.display import clear_output\n",
762 | "\n",
763 | "# declare game variables\n",
764 | "words = ['tree', 'basket', 'chair', 'paper', 'python']\n",
765 | "word = choice(words)\n",
766 | "guessed, lives, game_over = [], 7, False\n",
767 | "\n",
768 | "# create a list of underscores to the length of the word\n",
769 | "guesses = ['_ '] * len(word)\n",
770 | "\n",
771 | "# create main game loop\n",
772 | "while not game_over:\n",
773 | " # output game information\n",
774 | " hidden_word = ''.join(guesses)\n",
775 | " print('Your guessed letters: {}'.format(guessed))\n",
776 | " print('Word to guess: {}'.format(hidden_word))\n",
777 | " print('Lives: {}'.format(lives))\n",
778 | " \n",
779 | " ans = input('Type quit or guess a letter: ').lower()\n",
780 | " \n",
781 | " clear_output() # clear all previous output\n",
782 | " \n",
783 | " if ans == 'quit':\n",
784 | " print('Thanks for playing.')\n",
785 | " game_over = True\n",
786 | " elif ans in word and ans not in guessed:\n",
787 | " print('You guessed correctly!')\n",
788 | " \n",
789 | " # create a loop to change underscore to proper letter\n",
790 | " for i in range(len(word)):\n",
791 | " if word[i] == ans:\n",
792 | " guesses[i] = ans\n",
793 | " elif ans in guessed:\n",
794 | " print('You already guessed that. Try again.')\n",
795 | " else: # otherwise lose life\n",
796 | " lives -= 1\n",
797 | " print('Incorrect, you lost a life.')\n",
798 | " \n",
799 | " if ans not in guessed:\n",
800 | " guessed.append(ans) # add guess to guessed list\n",
801 | " \n",
802 | " if lives <= 0:\n",
803 | " print('You lost all your lives, you lost!')\n",
804 | " game_over = True\n",
805 | " elif word == ''.join(guesses):\n",
806 | " print('Congratulations, you guessed it correctly!')\n",
807 | " game_over = True"
808 | ]
809 | },
810 | {
811 | "cell_type": "markdown",
812 | "metadata": {},
813 | "source": [
814 | "# Monday Exercises - Answers"
815 | ]
816 | },
817 | {
818 | "cell_type": "markdown",
819 | "metadata": {},
820 | "source": [
821 | "\n",
822 | "1. Sports: Define a list of strings, where each string is a sport. Then output each sport with the following line \"I like to play {}\"...\n",
823 | "
"
824 | ]
825 | },
826 | {
827 | "cell_type": "code",
828 | "execution_count": 1,
829 | "metadata": {},
830 | "outputs": [
831 | {
832 | "name": "stdout",
833 | "output_type": "stream",
834 | "text": [
835 | "I like to play baseball.\n",
836 | "I like to play soccer.\n",
837 | "I like to play football.\n"
838 | ]
839 | }
840 | ],
841 | "source": [
842 | "sports = [ \"baseball\", 'soccer', 'football']\n",
843 | "\n",
844 | "print(\"I like to play {}.\".format(sports[0]))\n",
845 | "print(\"I like to play {}.\".format(sports[1]))\n",
846 | "print(\"I like to play {}.\".format(sports[2]))"
847 | ]
848 | },
849 | {
850 | "cell_type": "markdown",
851 | "metadata": {},
852 | "source": [
853 | "\n",
854 | "2. First Character: For the following list, print out each item's first letter. (output should be 'J', 'A', 'S', 'K')\n",
855 | "
\n",
856 | " names = ['John', 'Abraham', 'Sam', 'Kelly']
"
857 | ]
858 | },
859 | {
860 | "cell_type": "code",
861 | "execution_count": 4,
862 | "metadata": {},
863 | "outputs": [
864 | {
865 | "name": "stdout",
866 | "output_type": "stream",
867 | "text": [
868 | "J\n",
869 | "A\n",
870 | "S\n",
871 | "K\n"
872 | ]
873 | }
874 | ],
875 | "source": [
876 | "names = [ 'John', 'Abraham', 'Sam', 'Kelly' ]\n",
877 | "\n",
878 | "print(names[0][0])\n",
879 | "print(names[1][0])\n",
880 | "print(names[2][0])\n",
881 | "print(names[3][0])"
882 | ]
883 | },
884 | {
885 | "cell_type": "markdown",
886 | "metadata": {},
887 | "source": [
888 | "# Tuesday Exercises - Answers"
889 | ]
890 | },
891 | {
892 | "cell_type": "markdown",
893 | "metadata": {},
894 | "source": [
895 | "\n",
896 | "1. Divisble by Three: Write a for loop that prints out all numbers from 1 to 100 that are divisble by three.\n",
897 | "
"
898 | ]
899 | },
900 | {
901 | "cell_type": "code",
902 | "execution_count": 6,
903 | "metadata": {},
904 | "outputs": [
905 | {
906 | "name": "stdout",
907 | "output_type": "stream",
908 | "text": [
909 | "3\n",
910 | "6\n",
911 | "9\n",
912 | "12\n",
913 | "15\n",
914 | "18\n",
915 | "21\n",
916 | "24\n",
917 | "27\n",
918 | "30\n",
919 | "33\n",
920 | "36\n",
921 | "39\n",
922 | "42\n",
923 | "45\n",
924 | "48\n",
925 | "51\n",
926 | "54\n",
927 | "57\n",
928 | "60\n",
929 | "63\n",
930 | "66\n",
931 | "69\n",
932 | "72\n",
933 | "75\n",
934 | "78\n",
935 | "81\n",
936 | "84\n",
937 | "87\n",
938 | "90\n",
939 | "93\n",
940 | "96\n",
941 | "99\n"
942 | ]
943 | }
944 | ],
945 | "source": [
946 | "for i in range(1, 100):\n",
947 | " if i % 3 == 0:\n",
948 | " print(i)"
949 | ]
950 | },
951 | {
952 | "cell_type": "markdown",
953 | "metadata": {},
954 | "source": [
955 | "\n",
956 | "2. Only Vowels: Ask for user input and write a for loop that will output all the vowels within it. For example:\n",
957 | "
\n",
958 | "\n",
959 | "\n",
960 | ">>> \"Hello\" --> \"eo\"\n",
961 | "
"
962 | ]
963 | },
964 | {
965 | "cell_type": "code",
966 | "execution_count": 7,
967 | "metadata": {},
968 | "outputs": [
969 | {
970 | "name": "stdout",
971 | "output_type": "stream",
972 | "text": [
973 | "Enter a word: Hello\n",
974 | "e\n",
975 | "o\n"
976 | ]
977 | }
978 | ],
979 | "source": [
980 | "ans = input(\"Enter a word: \")\n",
981 | "\n",
982 | "for letter in ans:\n",
983 | " if letter in ['a', 'e', 'i', 'o', 'u']:\n",
984 | " print(letter)"
985 | ]
986 | },
987 | {
988 | "cell_type": "markdown",
989 | "metadata": {},
990 | "source": [
991 | "# Wednesday Exercises - Answers"
992 | ]
993 | },
994 | {
995 | "cell_type": "markdown",
996 | "metadata": {},
997 | "source": [
998 | "\n",
999 | "1. User Input: Write a while loop that continues to ask for user input and runs until they type 'quit'.\n",
1000 | "
"
1001 | ]
1002 | },
1003 | {
1004 | "cell_type": "code",
1005 | "execution_count": 9,
1006 | "metadata": {},
1007 | "outputs": [
1008 | {
1009 | "name": "stdout",
1010 | "output_type": "stream",
1011 | "text": [
1012 | "Enter something: asd\n",
1013 | "Enter something: asdf\n",
1014 | "Enter something: adsf\n",
1015 | "Enter something: quit\n"
1016 | ]
1017 | }
1018 | ],
1019 | "source": [
1020 | "while input('Enter something: ').lower() != 'quit':\n",
1021 | " pass"
1022 | ]
1023 | },
1024 | {
1025 | "cell_type": "markdown",
1026 | "metadata": {},
1027 | "source": [
1028 | "\n",
1029 | "2. Double Loop: Write a for loop within a while loop that will count from 0 to 5, but when it reaches 3 it sets a game_over variable to True and breaks out of the loop. The while loop should continue to loop until game_over is True. The output should only be 0, 1, 2.\n",
1030 | "
"
1031 | ]
1032 | },
1033 | {
1034 | "cell_type": "code",
1035 | "execution_count": 10,
1036 | "metadata": {},
1037 | "outputs": [
1038 | {
1039 | "name": "stdout",
1040 | "output_type": "stream",
1041 | "text": [
1042 | "0\n",
1043 | "1\n",
1044 | "2\n"
1045 | ]
1046 | }
1047 | ],
1048 | "source": [
1049 | "game_over = False\n",
1050 | "\n",
1051 | "while game_over == False:\n",
1052 | " for i in range(5):\n",
1053 | " if i == 3:\n",
1054 | " game_over = True\n",
1055 | " break\n",
1056 | " else:\n",
1057 | " print(i)"
1058 | ]
1059 | },
1060 | {
1061 | "cell_type": "markdown",
1062 | "metadata": {},
1063 | "source": [
1064 | "# Thursday Exercises - Answers"
1065 | ]
1066 | },
1067 | {
1068 | "cell_type": "markdown",
1069 | "metadata": {},
1070 | "source": [
1071 | "\n",
1072 | "1. Remove Duplicates: Remove all duplicates from the list below. Hint: Use the .count() method. The output should be [\"Bob\", \"Kenny\", \"Amanda\"]\n",
1073 | "
\n",
1074 | "\n",
1075 | "\n",
1076 | ">>> names = [\"Bob\", \"Kenny\", \"Amanda\", \"Bob\", \"Kenny\"]\n",
1077 | "
"
1078 | ]
1079 | },
1080 | {
1081 | "cell_type": "code",
1082 | "execution_count": 12,
1083 | "metadata": {},
1084 | "outputs": [
1085 | {
1086 | "name": "stdout",
1087 | "output_type": "stream",
1088 | "text": [
1089 | "['Amanda', 'Bob', 'Kenny']\n"
1090 | ]
1091 | }
1092 | ],
1093 | "source": [
1094 | "names = [\"Bob\", \"Kenny\", \"Amanda\", \"Bob\", \"Kenny\"]\n",
1095 | "\n",
1096 | "for name in names:\n",
1097 | " while names.count(name) > 1:\n",
1098 | " names.remove(name)\n",
1099 | " \n",
1100 | "print(names)"
1101 | ]
1102 | },
1103 | {
1104 | "cell_type": "markdown",
1105 | "metadata": {},
1106 | "source": [
1107 | "\n",
1108 | "2. User Input: Use a while loop to continually ask the user to input a word, until they type 'quit'. Once they type a word in, add it to a list. Once they quit the loop, use a for loop to output all items within the list.\n",
1109 | "
"
1110 | ]
1111 | },
1112 | {
1113 | "cell_type": "code",
1114 | "execution_count": 13,
1115 | "metadata": {},
1116 | "outputs": [
1117 | {
1118 | "name": "stdout",
1119 | "output_type": "stream",
1120 | "text": [
1121 | "Enter a word: hello\n",
1122 | "Enter a word: yes\n",
1123 | "Enter a word: quit\n",
1124 | "hello\n",
1125 | "yes\n"
1126 | ]
1127 | }
1128 | ],
1129 | "source": [
1130 | "done = False\n",
1131 | "words = []\n",
1132 | "\n",
1133 | "while done == False:\n",
1134 | " ans = input('Enter a word: ')\n",
1135 | " \n",
1136 | " if ans.lower() == 'quit':\n",
1137 | " done = True\n",
1138 | " else:\n",
1139 | " words.append(ans)\n",
1140 | " \n",
1141 | "for word in words:\n",
1142 | " print(word)"
1143 | ]
1144 | },
1145 | {
1146 | "cell_type": "markdown",
1147 | "metadata": {},
1148 | "source": [
1149 | "# End of Week Exercises - Answers"
1150 | ]
1151 | },
1152 | {
1153 | "cell_type": "markdown",
1154 | "metadata": {},
1155 | "source": [
1156 | "\n",
1157 | "1. Pyramids: Use a for loop to build a pyramid of x's. It should be modular so that if you loop to 5 or 50, it still creates evenly spaced rows. Hint: Multiply the string 'x' by the row. For example, if you loop to the range of 4, it should produce the following result:\n",
1158 | "
\n",
1159 | "\n",
1160 | "\n",
1161 | ">>> x
\n",
1162 | ">>> x x
\n",
1163 | ">>> x x x\n",
1164 | "
"
1165 | ]
1166 | },
1167 | {
1168 | "cell_type": "code",
1169 | "execution_count": 15,
1170 | "metadata": {},
1171 | "outputs": [
1172 | {
1173 | "name": "stdout",
1174 | "output_type": "stream",
1175 | "text": [
1176 | " \n",
1177 | " x\n",
1178 | " x x\n",
1179 | " x x x\n",
1180 | " x x x x\n"
1181 | ]
1182 | }
1183 | ],
1184 | "source": [
1185 | "row = 5\n",
1186 | "\n",
1187 | "for i in range(row):\n",
1188 | " print(' ' * (row - i) + ' x' * i)"
1189 | ]
1190 | },
1191 | {
1192 | "cell_type": "markdown",
1193 | "metadata": {},
1194 | "source": [
1195 | "\n",
1196 | "2. Output Names: Write a loop that will iterate over a list of items and only output items which have letters inside of a string. Take the following list for example, only \"John\" and \"Amanda\" should be output:\n",
1197 | "
\n",
1198 | "\n",
1199 | "\n",
1200 | ">>> names = [ \"John\", \" \", \"Amanda\", 5]\n",
1201 | "
"
1202 | ]
1203 | },
1204 | {
1205 | "cell_type": "code",
1206 | "execution_count": 18,
1207 | "metadata": {},
1208 | "outputs": [
1209 | {
1210 | "name": "stdout",
1211 | "output_type": "stream",
1212 | "text": [
1213 | "John\n",
1214 | "Amanda\n"
1215 | ]
1216 | }
1217 | ],
1218 | "source": [
1219 | "names = [ \"John\", \" \", \"Amanda\", 5]\n",
1220 | "\n",
1221 | "for name in names:\n",
1222 | " if type(name) == str:\n",
1223 | " if name.strip() != '':\n",
1224 | " print(name)"
1225 | ]
1226 | },
1227 | {
1228 | "cell_type": "markdown",
1229 | "metadata": {},
1230 | "source": [
1231 | "\n",
1232 | "3. Convert Celsius: Given a list of temperatures that are in Celsius, write a loop that iterates over the list and outputs the temperature converted into Fahrenheit. Hint: The conversion is \"F = (9/5) * C + 32\".\n",
1233 | "
\n",
1234 | "\n",
1235 | "\n",
1236 | ">>> temps = [32, 12, 44, 29]
\n",
1237 | "Output would be [89.6, 53.6, 111.2, 84.2]\n",
1238 | "
"
1239 | ]
1240 | },
1241 | {
1242 | "cell_type": "code",
1243 | "execution_count": 19,
1244 | "metadata": {},
1245 | "outputs": [
1246 | {
1247 | "name": "stdout",
1248 | "output_type": "stream",
1249 | "text": [
1250 | "[89.6, 53.6, 111.2, 84.2]\n"
1251 | ]
1252 | }
1253 | ],
1254 | "source": [
1255 | "temps = [32, 12, 44, 29]\n",
1256 | "\n",
1257 | "for i in range(len(temps)):\n",
1258 | " temps[i] = (9/5) * temps[i] + 32\n",
1259 | " \n",
1260 | "print(temps)"
1261 | ]
1262 | }
1263 | ],
1264 | "metadata": {
1265 | "kernelspec": {
1266 | "display_name": "Python 3",
1267 | "language": "python",
1268 | "name": "python3"
1269 | },
1270 | "language_info": {
1271 | "codemirror_mode": {
1272 | "name": "ipython",
1273 | "version": 3
1274 | },
1275 | "file_extension": ".py",
1276 | "mimetype": "text/x-python",
1277 | "name": "python",
1278 | "nbconvert_exporter": "python",
1279 | "pygments_lexer": "ipython3",
1280 | "version": "3.6.5"
1281 | }
1282 | },
1283 | "nbformat": 4,
1284 | "nbformat_minor": 2
1285 | }
1286 |
--------------------------------------------------------------------------------
/Week_08.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# List Comprehension"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 3,
13 | "metadata": {},
14 | "outputs": [
15 | {
16 | "name": "stdout",
17 | "output_type": "stream",
18 | "text": [
19 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n"
20 | ]
21 | }
22 | ],
23 | "source": [
24 | "# create a list of ten numbers using list comprehension\n",
25 | "\n",
26 | "nums = [ x for x in range(100) ] # generates a list from 0 up to 100\n",
27 | "\n",
28 | "print(nums)"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 4,
34 | "metadata": {},
35 | "outputs": [
36 | {
37 | "name": "stdout",
38 | "output_type": "stream",
39 | "text": [
40 | "[0, 2, 4, 6, 8]\n"
41 | ]
42 | }
43 | ],
44 | "source": [
45 | "# using if statements within list comprehension\n",
46 | "\n",
47 | "nums = [ x for x in range(10) if x % 2 == 0 ] # generates a list of even numbers up to 10\n",
48 | "\n",
49 | "print(nums)"
50 | ]
51 | },
52 | {
53 | "cell_type": "code",
54 | "execution_count": 5,
55 | "metadata": {},
56 | "outputs": [
57 | {
58 | "name": "stdout",
59 | "output_type": "stream",
60 | "text": [
61 | "['Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd']\n"
62 | ]
63 | }
64 | ],
65 | "source": [
66 | "# using if/else statements within list comprehension\n",
67 | "\n",
68 | "nums = [ \"Even\" if x % 2 == 0 else \"Odd\" for x in range(10) ] # generates a list of even/odd strings\n",
69 | "\n",
70 | "print(nums)"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": 6,
76 | "metadata": {},
77 | "outputs": [
78 | {
79 | "name": "stdout",
80 | "output_type": "stream",
81 | "text": [
82 | "[2, 4, 6, 8]\n"
83 | ]
84 | }
85 | ],
86 | "source": [
87 | "# creating a list of squared numbers from another list of numbers using list comprehension\n",
88 | "\n",
89 | "nums = [2, 4, 6, 8]\n",
90 | "\n",
91 | "squared_nums = [ num**2 for num in nums ] # creates a new list of squared numbers based on nums\n",
92 | "\n",
93 | "print(nums)"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": 7,
99 | "metadata": {},
100 | "outputs": [
101 | {
102 | "name": "stdout",
103 | "output_type": "stream",
104 | "text": [
105 | "{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}\n"
106 | ]
107 | }
108 | ],
109 | "source": [
110 | "# creating a dictionary of even numbers and square values using comprehension\n",
111 | "\n",
112 | "numbers = [ x for x in range(10) ]\n",
113 | "\n",
114 | "squares = { num : num**2 for num in numbers if num % 2 == 0 }\n",
115 | "\n",
116 | "print(squares)"
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "metadata": {},
122 | "source": [
123 | "# Lambda Functions"
124 | ]
125 | },
126 | {
127 | "cell_type": "code",
128 | "execution_count": 9,
129 | "metadata": {},
130 | "outputs": [
131 | {
132 | "data": {
133 | "text/plain": [
134 | "16"
135 | ]
136 | },
137 | "execution_count": 9,
138 | "metadata": {},
139 | "output_type": "execute_result"
140 | }
141 | ],
142 | "source": [
143 | "# using a lambda to square a number\n",
144 | "\n",
145 | "( lambda x: x**2 )(4) # takes in 4 and returns the number squared"
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": 10,
151 | "metadata": {},
152 | "outputs": [
153 | {
154 | "data": {
155 | "text/plain": [
156 | "50"
157 | ]
158 | },
159 | "execution_count": 10,
160 | "metadata": {},
161 | "output_type": "execute_result"
162 | }
163 | ],
164 | "source": [
165 | "# passing multiple arguments into a lambda\n",
166 | "\n",
167 | "( lambda x, y: x * y)(10, 5) # x = 10, y = 5 and returns the result of 5 * 10"
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": 11,
173 | "metadata": {},
174 | "outputs": [
175 | {
176 | "name": "stdout",
177 | "output_type": "stream",
178 | "text": [
179 | " at 0x0000022934CA9400>\n",
180 | "50\n"
181 | ]
182 | }
183 | ],
184 | "source": [
185 | "# saving a lambda function into a variable\n",
186 | "\n",
187 | "square = lambda x, y: x * y\n",
188 | "\n",
189 | "print(square)\n",
190 | "\n",
191 | "result = square(10, 5) # calls the lambda function stored in the square variable and returns 5 * 10\n",
192 | "\n",
193 | "print(result)"
194 | ]
195 | },
196 | {
197 | "cell_type": "code",
198 | "execution_count": 12,
199 | "metadata": {},
200 | "outputs": [
201 | {
202 | "name": "stdout",
203 | "output_type": "stream",
204 | "text": [
205 | "10\n"
206 | ]
207 | }
208 | ],
209 | "source": [
210 | "# using if/else statements within a lambda to return the greater number\n",
211 | "\n",
212 | "greater = lambda x, y: x if x > y else y\n",
213 | "\n",
214 | "result = greater(5, 10)\n",
215 | "\n",
216 | "print(result)"
217 | ]
218 | },
219 | {
220 | "cell_type": "code",
221 | "execution_count": 13,
222 | "metadata": {},
223 | "outputs": [
224 | {
225 | "name": "stdout",
226 | "output_type": "stream",
227 | "text": [
228 | "10\n",
229 | "15\n"
230 | ]
231 | }
232 | ],
233 | "source": [
234 | "# returning a lambda function from another function\n",
235 | "\n",
236 | "def my_func(n):\n",
237 | " return lambda x: x * n\n",
238 | "\n",
239 | "doubler = my_func(2) # returns equivalent of lambda x: x * 2\n",
240 | "\n",
241 | "print( doubler(5) ) # will output 10\n",
242 | "\n",
243 | "tripler = my_func(3) # returns equivalent of lambda x: x * 3\n",
244 | "\n",
245 | "print(tripler(5)) # will output 15"
246 | ]
247 | },
248 | {
249 | "cell_type": "markdown",
250 | "metadata": {},
251 | "source": [
252 | "# Map, Reduce, and Filter"
253 | ]
254 | },
255 | {
256 | "cell_type": "code",
257 | "execution_count": 15,
258 | "metadata": {},
259 | "outputs": [
260 | {
261 | "name": "stdout",
262 | "output_type": "stream",
263 | "text": [
264 | "