{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab 01: Functions, RGB Colors" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact\n", "from IPython.core.display import display, HTML" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Actvity 1: Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions\n", "\n", "Use the [modulo] (`%`) arithmetic operator and [less-than-or-equal-to] (`<=`) comparison operator to implement the following functions:\n", "\n", "[modulo]: https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations\n", "[less-than-or-equal-to]: https://docs.python.org/3/reference/expressions.html#comparisons" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def is_odd(x):\n", " ''' Returns whether or not x is odd '''\n", " # Write boolean expression" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def is_even(x):\n", " ''' Returns whether or not x is even '''\n", " # Write boolean expression" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def is_multiple(x, y):\n", " ''' Returns whether or not x is a multiple of y '''\n", " # Write boolean expression" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def is_between(x, y, z):\n", " ''' Returns whether or not y is between x and z (inclusive) '''\n", " # Write boolean expression" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Interaction\n", "\n", "Once you have implemented and tested the functions above, write a `test_numbers` function that uses the functions you wrote to display the results of passing inputs `x`, `y`, `z` to the respective functions:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def test_numbers(x, y, z):\n", " ''' Print the results of passing x, y, z to is_odd, is_even, is_multiple, is_between '''\n", " # Write print statement for each function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output of `test_numbers(5, 5, 5)` should look something like this:\n", "\n", "```\n", "is_odd(5) -> True\n", "is_even(5) -> False\n", "is_multiple(5, 5) -> True\n", "is_between(5, 5, 5) -> True\n", "```\n", "\n", "Once you have a working `test_numbers` function, use the [interact] function to call the `text_numbers` function repeated based on interactive inputs:\n", "\n", "[interact]: https://ipywidgets.readthedocs.io/en/stable/examples/Using%20Interact.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interact(test_numbers, x=(0, 10), y=(0, 10), z=(0, 10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Expressions\n", "\n", "Use your functions above to write **boolean expressions** that evaluate the following:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Whether or not a number is both odd and a multiple of `7`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# boolean expression..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Whether or not a number is even but not a multiple of `6`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# boolean expression..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Whether or not a number is odd or a multiple of `8`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# boolean expression..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Whether or not a number is between `0` and `100` and is even?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# boolean expression..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Whether or not a number is odd but not between `20` and `50`, or a multiple of `3`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# boolean expression..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For each **boolean expression** provide an example of the expression evaluating to `True` and another where it evaluates to `False`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Activity 2: RGB Colors\n", "\n", "Implement the function `display_color`, which given `r`, `g`, and `b` color components, it generates a table that shows the following:\n", "\n", "
Components of:
\n", "
\n", "
 
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ComponentDecimalHexadecimalBinary
Red1277f01111111
Green1277f01111111
Blue1277f01111111
\n", "\n", "That is, for each color component show its decimal, hexadecimal, and binary representation. Likewise, display a color block that shows the color the three components combine to create." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### HTML Table\n", "\n", "To display a [HTML] table in a Jupyter notebook, we can simply embed the [HTML] code into a string that we translate and convert using the `display` and `HTML` function. For instance, to show some bolded text, we can do the following:\n", "\n", "```\n", "text = 'Hello, World'\n", "display(HTML(text))\n", "```\n", "\n", "Note, the [HTML] text is simply a Python string and thus you can do all the things you can normally do on a string such as [format].\n", "\n", "[HTML]: https://developer.mozilla.org/en-US/docs/Web/HTML\n", "[format]: https://docs.python.org/3/library/functions.html#format\n", "\n", "Since we haven't covered [HTML] yet (we will later in the semester), we have provided with the necessary [HTML] template as a string below. You simply need to format it with the appropriate values to make display the results." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "TABLE = '''\n", "
Components of:
\n", "
\n", "
 
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ComponentDecimalHexadecimalBinary
Red{}{}{}
Green{}{}{}
Blue{}{}{}
\n", "'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display Color Function\n", "\n", "Write a `display_color` function that given the `r`, `g`, and `b` values of a color as integers, it outputs an [HTML] table as described above:\n", "\n", "[HTML]: https://developer.mozilla.org/en-US/docs/Web/HTML" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def display_color(r, g, b):\n", " ''' Display a HTML table that shows the color of the r, g, b \n", " components and their decimal, hexadecimal, and binary representations.\n", " '''\n", " display(HTML(TABLE))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Color Interaction" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interact(display_color, r=(0, 255), g=(0, 255), b=(0, 255))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exploration\n", "\n", "Use your `display_color` widget to fill in the following table of colors:\n", "\n", "| Color | Red | Green | Blue | Hexadecimal |\n", "|----------|------|-------|--------|-------------|\n", "| Black | | | | |\n", "| Red | | | | |\n", "| Green | | | | |\n", "| Yellow | | | | |\n", "| Blue | | | | |\n", "| Magenta | | | | |\n", "| Cyan | | | | |\n", "| Gray | | | | |\n", "| White | 255 | 255 | 255 | ffffff |\n", "\n", "Of course, there are many different shades of each color, so use your `display_color` widget to find your favorite shade and enter in the **RGB** values of each color as decimals, along with the hexadecimal representation of each color." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python3", "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }