{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# In Class Activity - Python - For loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 0: For-loop over a dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is a dictionary `colleges` that lists the city and zip (as values) for several schools in NYC (as keys)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "colleges = {}\n", "colleges['NYU'] = 'New York, NY 10012'\n", "colleges['Columbia'] = 'New York, NY 10027'\n", "colleges['Fordham'] = 'Bronx, NY 10458'\n", "colleges['Brooklyn college'] = 'Brooklyn, NY 11210'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a for-loop to iterate over the keys and return the values, printing in this format:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NYU : New York, NY 10012\n", "Columbia : New York, NY 10027\n", "Fordham : Bronx, NY 10458\n", "Brooklyn college : Brooklyn, NY 11210\n" ] } ], "source": [ "# Your answer goes here\n", "for k in colleges.keys():\n", " print(k, ':', colleges[k])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1: Multiplication table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function `mult_table(n)` that produces the multiplication table below. Note that for each cell $c_{ij}$, its value is equal to the product of the row number $i$ and column number $j$, assuming they are numbered $1,\\dots,n$.\n", "\n", "Note, it's not necessary to get the spacing exactly as in the example, but kudos if you do!\n", "\n", "Here is what the output should look like when you run `mult_table(10)`\n", "" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1 2 3 4 5 6 7 8 9 10 \n", " 2 4 6 8 10 12 14 16 18 20 \n", " 3 6 9 12 15 18 21 24 27 30 \n", " 4 8 12 16 20 24 28 32 36 40 \n", " 5 10 15 20 25 30 35 40 45 50 \n", " 6 12 18 24 30 36 42 48 54 60 \n", " 7 14 21 28 35 42 49 56 63 70 \n", " 8 16 24 32 40 48 56 64 72 80 \n", " 9 18 27 36 45 54 63 72 81 90 \n", " 10 20 30 40 50 60 70 80 90 100 \n" ] } ], "source": [ "# Your answer goes here\n", "def pad_num(x):\n", " x = str(x)\n", " npad = 3-len(x)\n", " x = npad*' '+x\n", " return x\n", " \n", "def mult_table(n):\n", " for i in range(1,n+1):\n", " for j in range(1,n+1):\n", " print(pad_num(i*j), end=' ')\n", " print(\"\")\n", " \n", "mult_table(10)" ] } ], "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.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }