{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab 2: Table operations, Data Types, and Arrays (oh my)\n", "\n", "Welcome to Lab 2! This week, we'll learn how to import a...

Need my Statistics/Coding Homework Done


{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab 2: Table operations, Data Types, and Arrays (oh my)\n", "\n", "Welcome to Lab 2! This week, we'll learn how to import a module and practice table operations! You'll then see how to represent and manipulate another fundamental type of data: text. A piece of text is called a *string* in Python. You'll also see how to work with *arrays* of data, such as all the numbers between 0 and 100 or all the words in the chapter of a book. Lastly, you'll create tables and practice analyzing them with your knowledge of table operations. This lab is a bit looooong, so make sure you begin early!\n", "\n", "Recommended Reading:\n", " * [Introduction to tables](https://www.inferentialthinking.com/chapters/03/4/Introduction_to_Tables)\n", "\n", "First run the cell below." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Just run this cell\n", "\n", "import numpy as np\n", "from datascience import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Importing code\n", "\n", "![imports](https://external-preview.redd.it/ZVPjiFo_Ubl4JeiU63SaTjdIoq5zveSnNZimKpgn2I8.png?auto=webp&s=bf32c94b630befa121075c1ae99b2599af6dedc5) \n", "\n", "[source](https://www.reddit.com/r/ProgrammerHumor/comments/cgtk7s/theres_no_need_to_reinvent_the_wheel_oc/)\n", "\n", "Most programming involves work that is very similar to work that has been done before. Since writing code is time-consuming, it's good to rely on others' published code when you can. Rather than copy-pasting, Python allows us to **import modules**. A module is a file with Python code that has defined variables and functions. By importing a module, we are able to use its code in our own notebook.\n", "\n", "Python includes many useful modules that are just an `import` away. We'll look at the `math` module as a first example. The `math` module is extremely useful in computing mathematical expressions in Python. \n", "\n", "Suppose we want to very accurately compute the area of a circle with a radius of 5 meters. For that, we need the constant $\\pi$, which is roughly 3.14. Conveniently, the `math` module has `pi` defined for us:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "78.53981633974483" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "radius = 5\n", "area_of_circle = radius**2 * math.pi\n", "area_of_circle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the code above, the line `import math` imports the math module. This statement creates a module and then assigns the name `math` to that module. We are now able to access any variables or functions defined within `math` by typing the name of the module followed by a dot, then followed by the name of the variable or function we want.\n", "\n", "." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 1.1.** The module `math` also provides the name `e` for the base of the natural logarithm, which is roughly 2.71. Compute $(e^{\\pi}-\\pi)^2$, giving it the name `near_400`.\n", "\n", "*Remember: You can access `pi` from the `math` module as well!*\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "near_400 = ...\n", "near_400" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1. Accessing functions\n", "\n", "In the question above, you accessed variables within the `math` module. \n", "\n", "**Modules** also define **functions**. For example, `math` provides the name `log` for the natural log function. Having imported `math` already, we can write `math.log(3)` to compute the natural log of 3. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For your reference, below are some more examples of functions from the `math` module.\n", "\n", "Notice how different functions take in different numbers of arguments. Often, the [documentation](https://docs.python.org/3/library/math.html) of the module will provide information on how many arguments are required for each function.\n", "\n", "*Hint: If you press `shift+tab` while next to the function call, the documentation for that function will appear*" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Calculating logarithms (the logarithm of 8 in base 2).\n", "# The result is 3 because 2 to the power of 3 is 8.\n", "math.log(8, 2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.23606797749979" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Calculating square roots.\n", "math.sqrt(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are various ways to import and access code from outside sources. The method we used above — `import ` — imports the entire module and requires that we use `.` to access its code. \n", "\n", "We can also import a specific constant or function instead of the entire module. Notice that you don't have to use the module name beforehand to reference that particular value. However, you do have to be careful about reassigning the names of the constants or functions to other values!" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1.0\n" ] }, { "data": { "text/plain": [ "1.1447298858494002" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Importing just cos and pi from math.\n", "# We don't have to use `math.` in front of cos or pi\n", "from math import cos, pi\n", "print(cos(pi))\n", "\n", "# We do have to use it in front of other functions from math, though\n", "math.log(pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or we can import every function and value from the entire module." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.1447298858494002" ]
Sep 11, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here