{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## In-class Assignment (Module 2)\n", "\n", "Some in class excercises to review the programming skills you've gained from Chapter 2. This should be completed in class." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(1) How would you express the constant floating-point value 3.2 × 10^-12 in Python?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.2e-12\n" ] } ], "source": [ "b = 3.2e-12\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2) Which of the following are valid ways to assign a string \"s\" as **foo'bar** in Python:\n", "* s1 = \"foo'bar\"\n", "* s2 = foo'bar\n", "* s3 = foo\\'bar\n", "* s4 = foo\\\\'bar\n", "* s5 = 'foo\\'bar'" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "unterminated string literal (detected at line 1) (3504514792.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m Input \u001b[0;32mIn [6]\u001b[0;36m\u001b[0m\n\u001b[0;31m s1 = 'foo'bar'\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unterminated string literal (detected at line 1)\n" ] } ], "source": [ "s1 = \"foo'bar\"\n", "print(s1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(3) What object types are the following and how do you determine that?" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'cat': 20, 'dog': 40, 40: 'dog'}\n" ] } ], "source": [ "listOne = [20, 40, 60, 80]\n", "listTwo = {20, 40, 60, 80}\n", "listThree = (20, 40, 60, 80)\n", "listFour = {'cat':20,'dog':40,40:'dog'}\n", "print(listFour)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['hi', 40, 60, 80]\n" ] } ], "source": [ "type(listThree)\n", "listOne[0] = 'hi'\n", "print(listOne)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{40, 80, 20, 60}\n" ] } ], "source": [ "listFive = {20,40,40,60,60,80}\n", "print(listFive)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(4) How would you pull out the 5th element from the following list?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(5) How would you compute the length of the list provided above,*without counting*?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(6) How would we pull out the last element? Is there a way we can do this *without counting?*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(7) How would we add the string *\"environment\"* to the list above?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(8) How could you use python to sort the following list of numbers?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[743, 726, 35, 552, 309, 954, 957, 315, 529, 648, 129, 296, 453, 66, 550, 944, 209, 374, 769, 405, 39, 165, 278, 727, 955, 807, 84, 60, 294, 340, 457, 580, 755, 982, 752, 647, 599, 642, 57, 895, 753, 662, 362, 325, 850, 546, 695, 938, 589, 935]\n" ] } ], "source": [ "import random\n", "randomlist = random.sample(range(0, 1000), 50)\n", "print(randomlist)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "print(randomlist[4])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "50\n", "50\n", "938\n" ] } ], "source": [ "print(len(randomlist))\n", "a = len(randomlist)\n", "print(a)\n", "print(randomlist[a-3])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "935\n" ] } ], "source": [ "print(randomlist[-1])" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[743, 726, 35, 552, 309, 954, 957, 315, 529, 648, 129, 296, 453, 66, 550, 944, 209, 374, 769, 405, 39, 165, 278, 727, 955, 807, 84, 60, 294, 340, 457, 580, 755, 982, 752, 647, 599, 642, 57, 895, 753, 662, 362, 325, 850, 546, 695, 938, 589, 935, 'environment']\n" ] } ], "source": [ "randomlist.append('environment')\n", "print(randomlist)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[35, 39, 57, 60, 66, 84, 129, 165, 209, 278, 294, 296, 309, 315, 325, 340, 362, 374, 405, 453, 457, 529, 546, 550, 552, 580, 589, 599, 642, 647, 648, 662, 695, 726, 727, 743, 752, 753, 755, 769, 807, 850, 895, 935, 938, 944, 954, 955, 957]\n", "None\n" ] } ], "source": [ "del randomlist[-1]\n", "print(randomlist)\n", "print(randomlist.sort())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(9) Create a list, a, of numbers that goes from 100 to 10 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(10) Now reverse the list, a, to a new list, b, which is increasing only for multiples of 3" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10]\n" ] } ], "source": [ "a = list(range(100,9,-1))\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]\n" ] } ], "source": [ "b = a[-3::-3]\n", "print(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.10.5" } }, "nbformat": 4, "nbformat_minor": 4 }