{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "lVM5e7MBGDY6" }, "source": [ "# CS5481 - Tutorial 1\n", "## Introduction to JupyterHub and Python\n", "\n", "Welcome to CS5481 tutorial. In this tutorial, you will get familiar with our exprimental environment, and also practice with some basic Python operations.\n", "\n", "## 1. Setup\n", "\n", "### 1.1 JupyterHub\n", "a. You can use the [JupyterHub](https://jupyter.org/install) on your local environment.\n", "\n", "b. You are also recommended to use [Colab](https://colab.research.google.com/).\n", "\n", "c. You can also insatll jupyter notebook on your IDE, such as Pycharm.\n", "\n", "etc...\n", "\n", "### 1.2 Anaconda\n", "We recommend to use [Anaconda](https://www.anaconda.com/) to manage the different Python versions.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "SKh5ZzZtGDY8" }, "source": [ "## 2. Quick start - Python concepts and operations\n", "\n", "In this section, we introcue some basic concepts and operations of Python." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "hIT-LrOwGDY8", "outputId": "965b4a81-5aa7-4f73-98d6-0fc8a1365148" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hello World!\n" ] } ], "source": [ "print(\"Hello World!\")" ] }, { "cell_type": "markdown", "metadata": { "id": "4ng596ALGDY9" }, "source": [ "\n", "\n", "### 2.1 Data Type in Python\n", "\n", "- Number\n", "- String\n", "- List\n", "- Tuple\n", "- Set\n", "- Dictionary\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "75848Z_rGDY9", "outputId": "e4cb9b2a-887f-4c87-f95b-5b0117598441" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The name is: Jack\n", "The height is: 1.6 and the age is 15\n" ] } ], "source": [ "# we can define a people with some attributes\n", "name = \"Jack\" # string\n", "height = 1.6 # float number\n", "age = 15 # integar number\n", "print(\"The name is: \", name)\n", "print(\"The height is: {0} and the age is {1}\".format(height, age))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "fQqvQjTxGDY9", "outputId": "4b2290e8-4b5c-4c86-fcd9-b07d51909c37" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The attributes values are : ['Jack', 1.6, 15]\n" ] } ], "source": [ "# we can put these values into a list\n", "attributes = [name, height, age] # list\n", "print(\"The attributes values are :\", attributes)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IOSniZKAGDY9", "outputId": "cd9d6733-a643-46cc-a5aa-062a769f5626" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The name in attributes is Jack\n", "The height and age in attributes are [1.6, 15]\n" ] } ], "source": [ "# we can get values in list via index\n", "print(\"The name in attributes is {}\".format(attributes[0]))\n", "print(\"The height and age in attributes are \", attributes[1:3])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "q6hzpqPHGDY9", "outputId": "20532b8b-cf3d-4a5b-f4b9-c7ba85ae3928" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The name in attribute dictionary is : Jack\n", "The age in attribute dictionary is : 15\n", "The height in attribute dictionary is : 1.6\n" ] } ], "source": [ "# However, if we do not know the attribute's index in list, we can not obtain it\n", "# So we could use dictionary to store his attributes\n", "# dictionary consists of key: value pairs and we can obtain the value via key\n", "# here is an example\n", "attributes_dict = {\"name\": \"Jack\", \"height\": 1.6, \"age\": 15}\n", "print(\"The name in attribute dictionary is :\", attributes_dict[\"name\"])\n", "print(\"The age in attribute dictionary is :\", attributes_dict[\"age\"])\n", "print(\"The height in attribute dictionary is :\", attributes_dict[\"height\"])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YmOxfCTdGDY-", "outputId": "60b34793-d5f8-4041-c660-a2f186eb9503" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The keys of attribute dictionary is dict_keys(['name', 'height', 'age'])\n", "The values of attribute dictionary is dict_values(['Jack', 1.6, 15])\n", "The list format of keys of attribute dictionary is ['name', 'height', 'age']\n", "The list format of values of attribute dictionary is ['Jack', 1.6, 15]\n" ] } ], "source": [ "# We can use dictionary.keys() and dictionary.values() to obtain keys and values of a dictionary\n", "keys = attributes_dict.keys()\n", "values = attributes_dict.values()\n", "print(\"The keys of attribute dictionary is\", keys)\n", "print(\"The values of attribute dictionary is\", values)\n", "\n", "# we can convert them to list\n", "print(\"The list format of keys of attribute dictionary is\", list(keys))\n", "print(\"The list format of values of attribute dictionary is\", list(values))\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kDPA6psVGDY-", "outputId": "9d1b0d47-ccc2-4877-bd4f-25bf7a863dc1" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The attributes' type is \n", "The age' type is \n", "The height' type is \n" ] } ], "source": [ "# we can check the type of a variable via type() function\n", "print(\"The attributes' type is \", type(attributes))\n", "print(\"The age' type is \", type(age))\n", "print(\"The height' type is \", type(name))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Yj1uCxCZGDY-", "outputId": "1c78c025-5101-4bea-c806-aa29762d2098" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The name in attribute dictionary is : Jack\n", "The age in attribute dictionary is : 17\n", "The height in attribute dictionary is : 178\n" ] } ], "source": [ "# we assume Jack is a student and he has a student id\n", "stu_id = '12345' # string\n", "attributes_dict['id'] = stu_id\n", "\n", "# age and height will change but the name and stu_id will not change\n", "# we assume jack is 17 now and his height is 178. we can change them in attributes dictionary\n", "attributes_dict[\"age\"] = 17\n", "attributes_dict[\"height\"] = 178\n", "print(\"The name in attribute dictionary is :\", attributes_dict[\"name\"])\n", "print(\"The age in attribute dictionary is :\", attributes_dict[\"age\"])\n", "print(\"The height in attribute dictionary is :\", attributes_dict[\"height\"])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NbFHG9F4GDY-", "outputId": "d5de1060-fe4d-4599-fb1b-e62734f64d50" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Jack\n", "12345\n" ] } ], "source": [ "# tuple could store variables that need not to be changed\n", "attributes_tuple = (name, stu_id)\n", "print(attributes_tuple[0])\n", "print(attributes_tuple[1])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HR9Is-oeGDY-", "outputId": "ee6a93fc-5e44-4035-8be1-0c449ecb9302" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The student list is: ['Jack', 'Tom', 'Jack']\n", "The student set is: {'Jack', 'Tom'}\n" ] } ], "source": [ "# this operation will lead to an error\n", "#attributes_tuple[0] = \"Tom\"\n", "\n", "# set could store elements that each element is different from other elements\n", "# we assume that there are three students, Jack, Tom and another Jack\n", "student_list = [\"Jack\", \"Tom\", \"Jack\"]\n", "print(\"The student list is: \", student_list)\n", "student_set = set(student_list)\n", "print(\"The student set is: \", student_set)" ] }, { "cell_type": "markdown", "metadata": { "id": "8BI38mWgGDY-" }, "source": [ "### 2.2 Operations on numerical values\n", "- Basic operations like ``+``, ``-``, ``*``, ``/`` on the numerical values.\n", "- Advanced operations on complex data type like list, string." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "tCANbZjgGDY-", "outputId": "51b4f581-ec5c-4801-d071-3ac2f6b4f7e9" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1\n", "2\n", "6\n", "3.0\n", "1.0\n", "9.0\n" ] } ], "source": [ "a = 1\n", "print(a)\n", "# +\n", "b = a + 1\n", "print(b)\n", "# *\n", "c = b * 3\n", "print(c)\n", "# / will produce float results\n", "d = c / 2\n", "print(d)\n", "# -\n", "e = d - 2\n", "print(e)\n", "\n", "# ** equals pow()\n", "f = d**2\n", "print(f)" ] }, { "cell_type": "markdown", "metadata": { "id": "XK4RRPCMGDY_" }, "source": [ "### 2.3 Operations on List" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "yMcLlRBXGDY_", "outputId": "9644f5c7-ff68-40ed-86f8-6dc7fbe6a16c" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[1, 2, 3]\n", "[1, 2, 3, 4]\n", "[1, 2, 3, 4, 1, 2, 3, 4]\n", "[1, 2, 3, 1, 2, 3, 4]\n", "a: [1, 2, 3, 4]\n", "b: [1, 2, 3, 1, 2, 3, 4]\n", "c = a+b: [1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4]\n", "The lenght of list b is 7\n" ] } ], "source": [ "# we can add element to list via append() function\n", "a = [1, 2, 3]\n", "print(a)\n", "a.append(4)\n", "print(a)\n", "\n", "# we can extend a list via * operation\n", "b = a * 2\n", "print(b)\n", "\n", "# we can remove an elemnt via remove() function (if there are multiple same elements, the first one will be removed)\n", "b.remove(4)\n", "print(b)\n", "\n", "# we can add two list to produce a new list\n", "c = a + b\n", "print(\"a: \", a)\n", "print(\"b: \", b)\n", "print(\"c = a+b: \", c)\n", "\n", "# we can obtain the length of the list via len() function\n", "len_b = len(b)\n", "print(\"The lenght of list b is \", len_b)\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "NxqwC6KhGDY_" }, "source": [ "### 2.4 Operations on String" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "57kZXw8NGDY_", "outputId": "58f0a024-a93d-4250-805c-e9ba1b4acd39" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Tom\n", "TomTom\n", "The lenght of string 1 is 3\n" ] } ], "source": [ "# we regard string as a list of char\n", "\n", "str1 = \"Tom\"\n", "print(str1)\n", "str2 = str1 * 2\n", "print(str2)\n", "len_str1 = len(str1)\n", "print(\"The lenght of string 1 is \", len(str1))" ] }, { "cell_type": "markdown", "metadata": { "id": "yJWz0LWRGDY_" }, "source": [ "## 3. Advanced Functions\n", "- max()\n", "- min()\n", "- sorted()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VdrMEVHaGDY_", "outputId": "27dd2fd3-934d-45d6-f694-35afca1f05fe" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "3\n", "1\n", "[1, 2, 3, 4, 5]\n", "Unsorted attributes dictionary: {'name': 'Jack', 'height': 178, 'age': 17, 'id': '12345'}\n", "Sorted attributes dictionary: [('age', 17), ('height', 178), ('id', '12345'), ('name', 'Jack')]\n", "dict_items([('name', 'Jack'), ('height', 178), ('age', 17), ('id', '12345')])\n" ] } ], "source": [ "# max()\n", "max_a = max(a)\n", "print(max_a)\n", "\n", "# min()\n", "min_a = min(a)\n", "print(min_a)\n", "\n", "# sorted()\n", "sorted_c = sorted(c)\n", "print(sorted_c)\n", "\n", "\n", "# sorted() on dictionary in alphabetical order of keys\n", "sorted_attributes_dict = sorted(attributes_dict.items(), reverse=False) # reverse order\n", "print(\"Unsorted attributes dictionary: \", attributes_dict)\n", "print(\"Sorted attributes dictionary: \", sorted_attributes_dict)" ] }, { "cell_type": "markdown", "metadata": { "id": "swrHpITUGDY_" }, "source": [ "## 4. Logical Operations in Python\n", "- For\n", "- If\n", "- While" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "se9naJ3BGDY_", "outputId": "0b849afe-800f-4019-daa9-d967de5b928f" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "For on list\n", "1\n", "2\n", "3\n", "4\n", "\n", "For on set\n", "Jack\n", "Tom\n", "\n", "For on tuple\n", "Jack\n", "12345\n", "\n", "For on dictionary\n", "name Jack\n", "height 178\n", "age 17\n", "id 12345\n" ] } ], "source": [ "# we can get all elements of a list/set/tuple/dictionary via for logic\n", "print(\"For on list\")\n", "for item in a:\n", " print(item)\n", "\n", "print()\n", "print(\"For on set\")\n", "for item in student_set:\n", " print(item)\n", "\n", "print()\n", "print(\"For on tuple\")\n", "for item in attributes_tuple:\n", " print(item)\n", "\n", "print()\n", "print(\"For on dictionary\")\n", "for k, v in attributes_dict.items():\n", " print(k, v)\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lBmaSQYeGDY_", "outputId": "1e35c039-6da9-4c63-bceb-b8210bbad910" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4]\n", "3\n", "4\n", "3\n", "3\n", "4\n", "[3, 4, 3, 3, 4]\n" ] } ], "source": [ "# If\n", "# we want to print values larger than 2 in list c\n", "print(c)\n", "for item in c:\n", " if item > 2:\n", " print(item)\n", "\n", "# we can create new list via list comprehension\n", "c_larger_2 = [item for item in c if item > 2]\n", "print(c_larger_2)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9Dii9mUcGDY_", "outputId": "213ede6b-3d8a-421c-dc4c-271de1cd7f46" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "10\n" ] } ], "source": [ "# while\n", "# we add 1 to sum = 0 util the sum equals 10\n", "sum = 0\n", "while sum != 10:\n", " sum += 1\n", "print(sum)" ] }, { "cell_type": "markdown", "metadata": { "id": "BQN0D21QGDY_" }, "source": [ "## Practice" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "id": "KrqfjmAwGDZA" }, "outputs": [], "source": [ "a = [1, 2, 3]\n", "# obtain the length, min_value, max_value of list a" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0ti9PFWtGDZA", "outputId": "abe39c15-68b5-4b28-de34-cc2f7ef841ac" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[('Tom', 12), ('Jerry', 20), ('Jack', 17)]\n" ] } ], "source": [ "b = {\"Tom\": 12, \"Jack\": 17, \"Jerry\": 20}\n", "# sort dictionary b according to value, from larger one to small one\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-ttOEh9GGDZA", "outputId": "c2d03477-b30d-4163-cd3a-63148436565b" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[1, 4, 9]\n" ] } ], "source": [ "c = [1, 2, 3, 4, 5]\n", "# obtain list [1, 4, 9] via list comprehension (one line code)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ujbEJJUgGDZA" }, "outputs": [], "source": [] } ], "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.4" }, "colab": { "provenance": [] } }, "nbformat": 4, "nbformat_minor": 0 }