{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Level 1: Defining variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Version 1.1. Prepared by [Makzan](https://makzan.net). Updated at 2021 February." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Welcome to level 1 of Python. In this notebook, we will take a look at storing results in variables." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are 3 examples in the following code snippets, they are 3 keys πŸ”‘ `key_result_1`, πŸ”‘ `key_result_2`, πŸ”‘ `key_result_3`. Please try to complete the notebooks and use the `key_results` in the πŸ€– [boss level](#πŸ€–-Boss-level)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## πŸ”‘ Key 1: Storing data in variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variables are used to store calculated result and temporary result." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "key_result_1 = \"Welcome to CM540\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can print the variable out" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [], "source": [ "print(key_result_1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are several common types in Python. They are `int`, `float`, `str`, `boolean`, `None`, `list`, `dict`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "variable = 123" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "type(variable)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "variable = 12.3" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "type(variable)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "variable = \"Hello\"" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(variable)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "variable = True" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(variable)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "variable = None" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NoneType" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(variable)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "variable = [1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(variable)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "variable = {'key': 'value'}" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(variable)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## πŸ”‘ Key 2: Calculated Result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is another example of storing the calculated result of `a+b` into `c`." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# Defining variables\n", "\n", "a = 1\n", "\n", "b = 2\n", "c = a + b" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Given `a=1900` and `b=90`. What is `a+b`?\n", "\n", "Please replace `None` in the following code snippet into `a+b`." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "a=1900\n", "b=90\n", "\n", "key_result_2 = None\n", "\n", "print(key_result_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Expected result|\n", "|---|\n", "|1990|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " Click to reveal solution\n", "
key_result_2 = a+b
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## πŸ”‘ Key 3: Concatenate string and numberic variables together" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "current_version = 3.9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "πŸ‘Ύ You will get an error of **can only concatenate str not float to str** in the followling line. No worry, we will fix it." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate str (not \"float\") to str", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# DON'T COPY. Not good code example.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Hello\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\"Python\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcurrent_version\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"float\") to str" ] } ], "source": [ "# DON'T COPY. Not good code example.\n", "\n", "print(\"Hello\" + \"Python\" + current_version)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HelloPython3.9\n" ] } ], "source": [ "# DON'T COPY. Not good code example.\n", "\n", "print(\"Hello\" + \"Python\" + str(current_version))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wait. Where are the spaces?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Formatted String" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Python 3.9\n" ] } ], "source": [ "print(f\"Hello Python {current_version}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now please store the \"Hello Python 3.9\" into `key_result_3`." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "key_result_3 = None\n", "\n", "print(key_result_3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Expected result|\n", "|---|\n", "|Hello Python 3.9|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " Click to reveal solution\n", "
key_result_3 = f\"Hello Python {current_version}\"
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## πŸ€– Boss level" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From the examples above, you should already computed the key results. Let’s use the key results here." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to CM540\n" ] } ], "source": [ "print(key_result_1)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(key_result_2)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(key_result_3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Optional: We can store the 3 results into a list:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "results = [key_result_1, key_result_2, key_result_3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can inspect the list values by typing the variable name in a cell. That works because Jupyter notebook display the result of last line of each cell if it is not a variable assignment." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "['Welcome to CM540', None, None]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Final Result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once you get the key results correct, you will be able to run the following `final_result` and get the expected result." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to CM540! Python was first released in None. None\n" ] } ], "source": [ "final_result = f\"{key_result_1}! Python was first released in {key_result_2}. {key_result_3}\"\n", "print(final_result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "|Expected Result|\n", "|---|\n", "|Welcome to CM540! Python was first released in 1990. Hello Python 3.9|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "In this notebook, we leanred to store result in **variable**. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let’s move to the next level: Collection." ] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }