{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW04 - InClass - Repetition and Programming \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": 121, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index 0: 54\n", "Index 1: 74\n", "Index 2: 6\n", "Index 3: 235\n", "Index 4: 75\n" ] } ], "source": [ "a = [54,74,6,235,75]\n", "\n", "for i in a:\n", " print(f\"Index {a.index(i)}: {i}\")\n" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "animals = ['cat', 'dog', 'rabbit', 'horse']\n", "\n", "# get the index of 'dog'\n", "index = animals.index('dog')\n", "\n", "print(index)" ] }, { "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": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List of even numbers from 13 to 20 " ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[14, 16, 18, 20]\n" ] } ], "source": [ "b = list()\n", "\n", "for a in range(13,21):\n", " if a % 2 == 0:\n", " b.append(a)\n", "\n", "print(b)" ] }, { "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": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "384\n" ] } ], "source": [ "b = list()\n", "\n", "for a in range(20,45):\n", " if a % 2 != 0:\n", " b.append(a)\n", "\n", "print(sum(b))\n", " " ] }, { "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": [] }, { "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": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### half Christmas tree with a bow \n", "for loop, use \"*\" * 5" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "*\n", "**\n", "***\n", "****\n", "*****\n" ] } ], "source": [ "i = \"\"\n", "for j in enumerate(\"12345\"):\n", " i += f\"*\"\n", "\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Additional optional exercises TwitterGenerator \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" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12.6" } }, "nbformat": 4, "nbformat_minor": 2 }