diff --git a/f24/Louis_Lecture_Notes/03_Operators/Lec_03_Operators_Template_Oliphant.ipynb b/f24/Louis_Lecture_Notes/03_Operators/Lec_03_Operators_Template_Oliphant.ipynb index d880a9e7fa7edf7e12eb4721a6e755702cea95e2..52af0e689879bbaa407ee505ffcfac0df028e931 100644 --- a/f24/Louis_Lecture_Notes/03_Operators/Lec_03_Operators_Template_Oliphant.ipynb +++ b/f24/Louis_Lecture_Notes/03_Operators/Lec_03_Operators_Template_Oliphant.ipynb @@ -4,14 +4,15 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Operators\n", + "# Values, Data Types, and Expressions\n", "\n", "## Readings:\n", - "- Chapter 1 of Think Python, \n", - "- Chapter 2 of Python for Everybody\n", + "- [Chapter 1 of *Think Python*](https://greenteapress.com/thinkpython2/html/thinkpython2003.html)\n", + "- [Chapter 2 of *Python for Everybody*](https://runestone.academy/ns/books/published//py4e-int/variables/toctree.html)\n", "\n", "## Additional readings: \n", - "- Computer terminology" + "- [Computer terminology](https://cs220.cs.wisc.edu/f24/materials/lecture_ppts/Software_hardware.pdf)\n", + "- [Jupyter Lab User Guide](https://jupyterlab.readthedocs.io/en/latest/user/index.html)" ] }, { @@ -20,22 +21,225 @@ "source": [ "## Learning Objectives\n", "After this lecture you will be able to...\n", - "- Run Python code using:\n", + "\n", + "- Understand and Use in Python:\n", + " - Values\n", + " - Data Types\n", + " - Expressions and operators\n", + " - Comments\n", + "- Use [**Jupyter Lab**](https://jupyterlab.readthedocs.io/en/latest/user/index.html) Features:\n", + " - Left-edge side bar\n", + " - Notebook menubar\n", + " - Contextual Help\n", + " - Tab completion within a coding cell\n", + "- **Run Python** code using:\n", " - Python console\n", " - Other terminal\n", - " - jupyter notebook\n", - "- Understand the Jupyter interface, including:\n", - " - Left-edge side panel\n", - " - Notebook menu\n", - " - Contextual Help\n", - " - Tab completer within a coding cell\n", - "- Recognize examples of different Python data types: `int`, `float`, `str`, `bool`\n", - "- Explain the different behavior of the `/`, `//`, and `%` operators\n", - "- Evaluate numeric expressions containing mathematical operators (eg: `+`, `-`, `*`, `/`, `**`, etc.,), using correct precedence\n", - "- Determine the correct order of operations in Python\n", - "- Evaluate string expressions containing string operators and escape characters\n", - "- Evaluate expressions containing comparison operators (eg: `<`, `>`, `<=`, `==`, etc.,)\n", - "- Evaluate boolean expressions that contain the operators `and`, `or`, `not`" + " - jupyter notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Values and Data Types\n", + "\n", + "A **value** is a piece of information that can be used by a program. All values in Python have an associated **data type**.\n", + "\n", + "In the coding cell below, try typing each of the following one at a time and then running your code by typing `shift-enter`:\n", + "\n", + "```python\n", + "7\n", + "\n", + "3.14\n", + "\n", + "True\n", + "\n", + "\"Hello\"\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# TODO -- Enter your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The `type()` function\n", + "In Jupyter, when the code in a cell runs, the value of the last line in the cell is output.\n", + "\n", + "To see the type of a value, you must send the value into the `type()` function. In the cell below, type and run each of the following lines of code:\n", + "\n", + "```python\n", + "type(7)\n", + "\n", + "type(3.14)\n", + "\n", + "type(True)\n", + "\n", + "type(\"Hello\")\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "# TODO -- Enter your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Basic Data Types\n", + "\n", + "The code above represents 4 basic data types:\n", + "- **int** (e.g. 7, -12, 5232326) -- numbers without a decimal point\n", + "- **float** (e.g. 1.0, 3.14159, 2.71828) -- numbers with a decimal point\n", + "- **str** (e.g. \"Filibuster\", 'Whatchamacallit', \"27\") -- anything surounded by quotation marks (single or double but they must match)\n", + "- **bool** (True, False) -- just these two values\n", + "\n", + "Knowing the type of a value is very important because it effects how the value will be treated. **Note that `7` and `7.0` and `\"7\"` are all different data types** and will be treated very differently by Python.\n", + "\n", + "### Escape Characters\n", + "\n", + "A string can contain special escape characters that start with \\\n", + " - `\\n` is for a newline character\n", + " - `\\t` is for a tab character\n", + " - `\\'` is for a literal single quotation mark\n", + " - `\\\"` is for a literal double quotation mark\n", + " - `\\\\` is for a literal slash\n", + "\n", + "Try using the escape characters in a string in the cell below. Here are some examples you can try. You do need to send the string to the `print()` function for the escape characters to be interpreted and printed out properly.\n", + "\n", + "```python\n", + "\n", + "\"Mary said, \\\"Hi.\\\"\"\n", + "\n", + "print(\"Mary said, \\\"Hi.\\\"\")\n", + "\n", + "\"What does\\tthis do\\nif I type it in.\"\n", + "\n", + "print(\"What does\\tthis do\\nif I type it in.\")\n", + "\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "#TODO -- Enter your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Can you write a **single line of code** that would print out exactly:\n", + "\n", + "```\n", + "Mary said, 'Hello John. How are you?'\n", + "John said, \"I am doing fine. Thank you for asking.\"\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [], + "source": [ + "#TODO -- Enter your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Expressions\n", + "\n", + "An **expression** combines values together using **operators** to create a new value. Each of the following lines of code are examples of expressions. Try typing them in the cell below and see what new value is created.\n", + "\n", + "```python\n", + "5-2*3.0\n", + "\n", + "\"Hello\"+\"goodbye\"\n", + "\n", + "not True\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "#TODO -- Enter your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It is important that you know the various operators and how they are used with different data types\n", + "\n", + "### Mathematical Operators `+` `-` `*` `/` `//` `**` `%`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### String Operators `+` `*`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Comparison Operators `<` `>` `<=` `>=` `!=` `==`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Boolean Operators `and` `or` `not`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Order of Operations" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ***********OLD STUFF ******************************************" ] }, { @@ -804,6 +1008,30 @@ "### Other References\n", "You may find [this resource](https://www.w3schools.com/python/python_operators.asp) to be helpful. It has more than you need to know." ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "- Use several features of the [**Jupyter Lab** interface](https://jupyterlab.readthedocs.io/en/latest/user/index.html), including:\n", + "\n", + "- Write and Evaluate **Python expressions**, understanding the different operators and data types, including:\n", + " - Data Types: `int`, `float`, `str`, and `bool`\n", + " - Mathematical operators: `+`, `-`, `*`, `/`, `**`, **`//`**, and **`%`**\n", + " - String operators: `+` (concatination), `*` (repetition)\n", + " - Comparison operators: `<`, `>`, `<=`, `>=`, `==`, `!=`\n", + " - Boolean operators: `and`, `or`, and `not`\n", + "- Write Python comments and use String escape characters\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -822,7 +1050,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.10.12" } }, "nbformat": 4,