{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## In-class demo (Module 3)\n", "\n", "Some in class excercises to test the programming skills you've gained from Module 4. This is ungraded, and 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": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.2e-12" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.2e-12" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(2) Which of the following are valid ways to specify the string literal **foo'bar** in Python:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "stuff = \"foo'bar\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(3) What type of object type is the following:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "listOne = [20, 40, 60, 80]\n", "listTwo = {20, 40, 60, 80}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(4) How would we go about confirming the data type of the objects above?" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n" ] } ], "source": [ "print(type(listOne))\n", "print(type(listTwo))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(5) How would someone pull out the 5th element from the following list?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "my_list = [100,\"atmos_5340\",\"weather\",1.5e7,True,False,True,\"grape\"]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(6) How would compute the length of the list provided above,*without counting*?" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(my_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(7) How would we pull out the last element? Is there a way we can do this *without counting?*" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'grape'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[-1]\n", "my_list[len(my_list)-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(8) How would we add the string *\"environment\"* to the list above?" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[100, 'atmos_5340', 'weather', 15000000.0, True, False, True, 'grape', 'environment']\n" ] } ], "source": [ "my_list.append(\"environment\")\n", "print(my_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(9) How could you use python to sort the folloing list of numbers?\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[51, 69, 91, 54, 79, 29, 23, 78, 80, 62, 35, 2, 94, 90, 0, 27, 83, 22, 41, 5]\n" ] } ], "source": [ "import random\n", "randomlist = random.sample(range(0, 100), 20)\n", "print(randomlist)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 2, 5, 22, 23, 27, 29, 35, 41, 51, 54, 62, 69, 78, 79, 80, 83, 90, 91, 94]\n" ] } ], "source": [ "randomlist.sort()\n", "print(randomlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(10) Create a list of numbers that goes from 100 to 0" ] }, { "cell_type": "code", "execution_count": 22, "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, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n" ] } ], "source": [ "print(list(range(100,0,-1)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }