{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2023
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4.14 `import`: A Deeper Look" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We can import a module such as `math` from the Standard Python Library. Then we can call any of its functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "math.ceil(10.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "math.floor(10.7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Importing Multiple Identifiers from a Module" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We can explicitly import functions from a module. Then we don't have to specify the module name when we call the functions we've imported." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from math import ceil, floor" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ceil(10.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "floor(10.7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Caution: Avoid Wildcard Imports " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### It is dangerous to use a wildcard to import all the names from a module. Here's a subtle error that can result from a wildcard import." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pi = 'apple'\n", "\n", "pi" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from math import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The `math` module defines a variable `pi` (an important mathematical constant. After the import, our variable `pi` was silently overridden." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Binding Names for Modules and Module Identifiers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We can import a module and give it an alias if that will make our code easier to read." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import statistics as stats" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grades = [85, 93, 45, 87, 93]\n", "\n", "stats.mean(grades)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXERCISE** Import the `decimal` module with the shorthand name `dec`, then create a `Decimal` object with the value `2.5` and square its value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2023 by Ronald Mak" ] } ], "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.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }