{"cells": [{"cell_type": "markdown", "id": "1", "metadata": {}, "source": ["\n", "\n", "\n", "## ![](https://s3.amazonaws.com/python-ga/images/GA_Cog_Medium_White_RGB.png) {.separator}\n", "\n", "

Unit 3 Lab: Intermediate Variables

\n", "\n", "\n", "\n", "\n", "\n", "---\n", "\n", "## Lesson Objectives\n", "*After this lesson, you will be able to...*\n", "\n", "* Create and floor floats.\n", "* Use special string characters.\n", "* Format strings.\n", "\n", "---\n", "\n", "## Introducing: Floats\n", "\n", "Did you notice that until now, we've only used whole numbers? Whole numbers are integers or, in programming terms, `int`.\n", "\n", "Where are all the decimal points?\n", "\n", "3.3, 1.1, and 2.2 are all **floats**.\n", "\n", "- Short for \"floating point value\"\n", "- A number with a decimal point. Even 2.0 is a float - it has the decimal!\n", "- Just another numerical variable!\n", "\n"]}, {"cell_type": "code", "id": "2", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "3", "metadata": {}, "source": ["\n", "\n", "\n", "\n", "---\n", "\n", "## Int / Int == Float ?!\n", "\n", "A quotient is not necessarily a whole number!\n", "* `5 / 2 == 2.5`\n", "* `1 / 3 == 1.333...`\n", "\n", "Therefore, quotients are always floats - even when they look like ints. Python doesn't distinguish!\n", "\n", "* `6 / 2 == 3.0`\n", "* `8 / 4 == 2.0`\n", "\n", "**Protip:** This is called **implicit type conversion** - Python changed our numbers from ints to floats automatically.\n", "\n", "\n", "\n", "---\n", "\n", "## Explicit Type Conversion\n", "\n", "`6 / 2 == 3.0`: A float. What if you just want the int `3`? (Pretty soon, having the right type will be important!). We need **explicit type conversion.**\n", "\n", "- `int()` converts something to an integer.\n", "- `float()` converts to a float.\n", "- `str()` converts to a string\n", "\n", "\n", "\n", "\n", "\n", "\n", "---\n", "\n", "\n", "## We Do: Let's Practice\n", "\n", "Let's try:\n", "\n", "* Declare two variables, `x` and `y`, and assign each an `int` value.\n", "* Declare a variable `z` and assign a `float` value.\n", "* Declare a variable `result`, which stores `x + y`. What type is `result`? Let's convert it to other types.\n", "* Is this behavior the same for other operators `-`, `*`, `/`, or `**`? What about using `x` and `z`?\n", "\n", "\n", "\n", "\n", "\n", "\n", "---\n", "\n", "## Quick Review: Floats\n", "\n", "In programming:\n", "\n", "- An *int* is a whole number: `1`, `0`, `-5`.\n", "- A *float* is a number with a decimal point: `1.6`, `-28.2`, `0.0`.\n", "- Doing any math with a float results in a float: `6 + 3.0 = 9.0`.\n", "- Dividing integers results in a float: `4 / 2 = 2.0`\n", "\n", "You can use *explicit type conversion* to turn one variable type into another:\n", "\n", "- `int()` converts to an integer: `int(6.0) # 6`\n", "- `float()` converts to a float: `float(6) # 6.0`\n", "- `str()` converts to a string: `str(6) # \"6\"`\n", "\n", "**Up next:** Floor Division.\n", "\n", "\n", "\n", "---\n", "\n", "## Finding the Midpoint\n", "\n", "One intermediate variable down! Let's move on past floats.\n", "\n", "What if we want to find the middle index of a list?\n", "\n"]}, {"cell_type": "code", "id": "6", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "7", "metadata": {}, "source": ["\n", "We want `2`. Any ideas? This is a very common use case - there must be a way!\n", "\n", "**Protip:** Remember, indexes start at 0!\n", "\n", "\n", "\n", "\n", "---\n", "\n", "## Introducing Floor Division\n", "\n", "Python has a shortcut.\n", "\n", "**Floor division** (a.k.a. integer division):\n", "\n", "* We use `//` instead of just `/`.\n", "* Does normal division, then drops the decimal and returns an int.\n", "- Think of the floor - it's beneath you. We floor by rounding **down**. The decimal is chopped! `2.8` will become `2`, not `3`.\n", "\n"]}, {"cell_type": "code", "id": "8", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "9", "metadata": {}, "source": ["\n", "\n", "\n", "\n", "\n", "---\n", "\n", "## You Do: Using Floor Division\n", "\n", "Correct the code by using floor division:\n", "\n", "\n", "\n", "\n", "\n", "\n", "---\n", "\n", "## Quick Review:\n", "\n", "Floor division:\n", "\n", "- Drops the decimal point - always rounds down.\n", "- Performed using `//` instead of just `/`.\n", "- Returns an int instead of a float.\n", "\n"]}, {"cell_type": "code", "id": "12", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "13", "metadata": {}, "source": ["\n", "**Next up:** Specialty Strings!\n", "\n", "\n", "\n", "---\n", "\n", "## Switching Gears: Strings\n", "\n", "Our intermediate variables checklist:\n", "- Floats\n", "- Floor division\n", "\n", "What about strings? We might want:\n", "\n", "- Printing special characters: A newline, a tab, or a quote inside of a string.\n", "- Formatting\n", " - A string.\n", " - The way an integer or float prints out.\n", "\n", "**Discussion**: How would you go about printing a new line between strings, like below?\n", "\n"]}, {"cell_type": "code", "id": "14", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "15", "metadata": {}, "source": ["\n", "\n", "\n", "\n", "---\n", "\n", "## Special String Characters\n", "\n", "\n", "| Name | Escape Character | Notes |\n", "|-----------|-----|-----------------------------------------|\n", "| Newline | \\n | Whitespace: Inserts another line |\n", "| Tab | \\t | Whitespace: Inserts a tab |\n", "| Quote | \\\" | Print a double quote, don't end the string |\n", "| Backslash | \\\\ | Prints \\ |\n", "\n"]}, {"cell_type": "code", "id": "16", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "17", "metadata": {}, "source": ["\n", "This prints, *including* the quotation marks:\n", "\n"]}, {"cell_type": "code", "id": "18", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "19", "metadata": {}, "source": ["\n", "\n", "\n", "\n", "---\n", "\n", "\n", "## String Format\n", "\n", "What else with strings?\n", "\n", "String formatting uses index numbers, in `{}`, as placeholders for strings we later specify in `format`.\n", "\n", "Indexes inside the braces refer to the arguments, in order!\n", "\n"]}, {"cell_type": "code", "id": "20", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "21", "metadata": {}, "source": ["\n", "\n", "\n", "\n", "\n", "---\n", "\n", "## Escaping and Format\n", "\n", "Check it out:\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "---\n", "\n", "## Quick Review\n", "\n", "Special strings:\n", "\n", "- A backslash `\\` escapes special characters: `\\\"` will print a quote and `\\\\` prints a `\\`.\n", "- `\\n` creates a New line; `\\t` creates a Tab.\n", "\n", "String formatting:\n", "\n", "- Can be used when printing or creating new strings.\n", "- Use `{x}`; `x` corresponds to the number of the argument.\n", "\n", "\n"]}, {"cell_type": "code", "id": "24", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "25", "metadata": {}, "source": ["\n", "\n", "\n", "---\n", "\n", "\n", "## Number Format\n", "\n", "What about number formatting?\n", "\n", "* Specify a float's precision (how many decimal points are shown).\n", "* Add commas to an integer (so it's more readable!).\n", "\n"]}, {"cell_type": "code", "id": "26", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "27", "metadata": {}, "source": ["\n"]}, {"cell_type": "code", "id": "28", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "29", "metadata": {}, "source": ["\n", "**Note: Number formatting creates strings!**\n", "\n", "\n", "\n", "---\n", "\n", "## You Do: Bring It All Together!\n", "\n", "- Open a new file and name it \"solution.py\".\n", "\n", "- Make a dictionary called \"sports\" with at least 4 key / value pairs.\n", "\n", " * Keys are the names (e.g., tennis, soccer, volleyball).\n", " * Values are the the number of people that play in a game.\n", "\n", "- Use a loop to print out all the keys and values.\n", "\n", " * Output:\n"]}, {"cell_type": "code", "id": "30", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "31", "metadata": {}, "source": [" * Note the new line and quotes, and use `format` to print out your string!\n", "\n", "- BONUS: Every other sport, indent by another tab.\n", "\n", " * 0 tabs: Tennis.\n", " * 1 tab level: Soccer.\n", " * 2 tab levels: Volleyball.\n", "\n", "**HINT**: Use floor division for the bonus! `number_of_tabs = loop_counter // 2`\n", "\n", "\n", "\n", "---\n", "\n", "## Summary and Q&A\n", "\n", "- Floats (`2.52`)\n", "- Floor (`int_index = 5 // 2`) - creates an int.\n", "- Escape characters (`\\\\`, `\\n`, `\\r`, `\\t`, `\\\"`)\n", "- Formatting:\n", "\n"]}, {"cell_type": "code", "id": "32", "metadata": {}, "outputs": [], "source": []}, {"cell_type": "markdown", "id": "33", "metadata": {}, "source": ["\n", "- Type conversion:\n", " * `int()`\n", " * `float()`\n", " * `str()`\n", "\n", "\n", "\n", "---\n", "\n", "## Additional Resources\n", "\n", "* [Floating Point (Docs)](https://docs.python.org/3/tutorial/floatingpoint.html)\n", "* [Decimal Module](https://docs.python.org/3/library/decimal.html)\n", "* [Floor Division](http://python-reference.readthedocs.io/en/latest/docs/operators/floor_division.html)\n", "* [List of Escape Characters](https://linuxconfig.org/list-of-python-escape-sequence-characters-with-examples)\n", "* [List of Unicode Characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters)\n", "* [Obscure Unicode Characters](http://jrgraphix.net/r/Unicode)\n", "- [Unicode Database](https://en.wikipedia.org/wiki/List_of_Unicode_characters)\n"]}], "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.6"}}, "nbformat": 4, "nbformat_minor": 5}