{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW04 - InClass - Repetition and Programming - TEMPLATE TEACHER \n", "\n", "This file is intended for interactive revision in the first hour of class. You write directly in the lines of code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create and output a list of numbers \n", "Manually create a list of at least 5 integers. Print them one by one to the console.\n", "Extend your program so that the index of the element is also output before the element in the list.\n", "\n", "Option f-Formatted output of string\n", "Option 2: Keep float data types in the list and output them with precision 3 after the decimal point." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "200\n", "3\n", "303\n", "42\n", "0. 3.123\n", "1. 4.556\n", "2. 123.010\n", "3. 33.844\n", "4. 0.001\n" ] } ], "source": [ "int_list = [2, 200, 3, 303, 42]\n", "\n", "for nbr in int_list:\n", " print(nbr)\n", " # print(f\"Element: {nbr}\")\n", "\n", "float_list = [3.12343, 4.5555, 123.01, 33.8436745, 1.2e-3]\n", "for i, nbr in enumerate(float_list):\n", " print(f\"{i}. {nbr:.3f}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## split() function and list \n", "Sentence: \"the cat sat on the mat with a hat\"\n", "Create a list of the sentence and output the words one by one" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['the', 'cat', 'sat', 'on', 'the', 'mat', 'with', 'a', 'hat']\n", "the\n", "cat\n", "sat\n", "on\n", "the\n", "mat\n", "with\n", "a\n", "hat\n" ] } ], "source": [ "sentence = \"the cat sat on the mat with a hat\"\n", "\n", "s_list = sentence.split()\n", "print(s_list)\n", "for word in s_list:\n", " print(word)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List of even numbers from 13 to 20 " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[14, 16, 18, 20]\n" ] } ], "source": [ "even_list = []\n", "for i in range(13, 21):\n", " if i % 2 == 0:\n", " even_list.append(i)\n", "\n", "print(even_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summing up all odd numbers from 20 up to and including 44 \n", "total is 384" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the sum is: 384\n" ] } ], "source": [ "total = 0\n", "for i in range(20, 45):\n", " if i % 2 == 1:\n", " total += i\n", "print(f\"the sum is: {total}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List of Fahrenheit values: \n", "Code from previous order" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[26.4, 27.0, 27.6, 28.1, 28.7, 29.2, 29.8, 30.3, 30.9, 31.4, 32.0, 32.6, 33.1, 33.7, 34.2, 34.8, 35.3, 35.9, 36.4, 37.0, 37.6, 38.1, 38.7, 39.2, 39.8, 40.3, 40.9, 41.4, 42.0, 42.6, 43.1, 43.7, 44.2, 44.8, 45.3, 45.9, 46.4, 47.0, 47.6, 48.1]\n", "26.4 °F\n", "27.0 °F\n", "27.6 °F\n", "28.1 °F\n", "28.7 °F\n", "29.2 °F\n", "29.8 °F\n", "30.3 °F\n", "30.9 °F\n", "31.4 °F\n", "32.0 °F\n", "32.6 °F\n", "33.1 °F\n", "33.7 °F\n", "34.2 °F\n", "34.8 °F\n", "35.3 °F\n", "35.9 °F\n", "36.4 °F\n", "37.0 °F\n", "37.6 °F\n", "38.1 °F\n", "38.7 °F\n", "39.2 °F\n", "39.8 °F\n", "40.3 °F\n", "40.9 °F\n", "41.4 °F\n", "42.0 °F\n", "42.6 °F\n", "43.1 °F\n", "43.7 °F\n", "44.2 °F\n", "44.8 °F\n", "45.3 °F\n", "45.9 °F\n", "46.4 °F\n", "47.0 °F\n", "47.6 °F\n", "48.1 °F\n" ] } ], "source": [ "fahrh_list = []\n", "for c in range(-10, 30, 1):\n", " fahrh_list.append(round(c*5/9+32, 1))\n", "\n", "print(fahrh_list)\n", "\n", "for f in fahrh_list:\n", " print(f, \"°F\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List with a list \n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\n", "D\n", "L\n", "B\n", "12\n", "43\n", "88\n", "12\n" ] } ], "source": [ "list_x = [\"A\", \"D\", \"L\", \"B\"]\n", "list_y = [12, 43, 88, 12]\n", "\n", "li_list = [list_x, list_y]\n", "# li_list = [[\"A\", \"D\", \"L\", \"B\"], [12, 43, 88, 12]]\n", "\n", "for li in li_list:\n", " for i in li:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### half Christmas tree with a bow \n", "for loop, use \"*\" * 5" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "*\n", "**\n", "***\n", "****\n", "*****\n" ] } ], "source": [ "for i in range(1, 6):\n", " print(\"*\" * i)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Optional: Twitter Generator \n", "Introducing the random module and choice function" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from random import choice\n", "\n", "part1 = (\"Putin\", \"Biden\", \"Kamala\", \"Fake News\", \"The Democrats\", \"Ukraine\")\n", "#part1b = (\"Taylor Swift\", \"Jonny Depp\", \"Arnold\", \"Lady Gaga\", \"Mike Tyson\")\n", "part2 = (\"no talent\", \"on the way down\", \"really poor number\", \"nasty tone\", \"looking like a fool\", \"bad homber\")\n", "part3 = (\"got destroyed by my ratings\", \"rigged my elections\", \"had a smaller crowd\", \"will pay for the wall\")\n", "part4 = (\"so sad\", \"apologize\", \"so true\", \"Media won't report\", \"Big trouble\", \"Fantastic Job\", \"Stay tuned\")\n", "part5 = (\"!\", \"!!!\", \"?\")\n", "\n", "bs_list = [part1, part2, part3, part4, part5]\n", "\n", "tw_text = \"\"\n", "for indx, part in enumerate(bs_list):\n", " tw_text += choice(part)\n", " if indx < len(bs_list)-2:\n", " tw_text += \", \"\n", "print(tw_text)\n", "\n", "#tw_text = choice(part1) + \", \" + choice(part2) + \", \" + choice(part3) + \", \" + choice(part4) + \"!\"\n", "#print(tw_text)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 4 }