{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "irgOWNuvFG6q" }, "source": [ "\n", "\n", "# Basic of Python Programming\n", "\n", "This is a practical introduction to Python Programming Language. Python is an interpreted, high-level, and general purpose programming language that was designed for efficiency, readability, and simplicity. \n", "\n", "Python design philosophy emphasizes simplicity and code readability. There are about 19 Python design guiding principles, the top 8 being:\n", "\n", "* Beautiful is better than ugly. \n", "* Explicit is better than implicit\n", "* Simple is better than complex\n", "* Complex is better than complicated\n", "* Readability counts\n", "* Now is better than ever\n", "* If the implementation is hard to explain, it's a bad idea.\n", "* If the implementation is easy to explain, it may be a good idea.\n", "\n", "More design rules can be found in the [Zen of Python](https://en.wikipedia.org/wiki/Zen_of_Python). You can also display them by importing this(`import this`) in any Python interpreter. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n", "Flat is better than nested.\n", "Sparse is better than dense.\n", "Readability counts.\n", "Special cases aren't special enough to break the rules.\n", "Although practicality beats purity.\n", "Errors should never pass silently.\n", "Unless explicitly silenced.\n", "In the face of ambiguity, refuse the temptation to guess.\n", "There should be one-- and preferably only one --obvious way to do it.\n", "Although that way may not be obvious at first unless you're Dutch.\n", "Now is better than never.\n", "Although never is often better than *right* now.\n", "If the implementation is hard to explain, it's a bad idea.\n", "If the implementation is easy to explain, it may be a good idea.\n", "Namespaces are one honking great idea -- let's do more of those!\n" ] } ], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python is a popular and go-to programming language in different tech communities, most notable in machine learning and data science. It is sometimes referred to as [“batteries included”](https://docs.python.org/3/tutorial/stdlib.html) due to its rich standard library. Below are more elaborated advantages of Python:\n", "\n", "* It is simple to read and write: Python syntaxes are very easy to write and easy to recall as well. \n", "* It has a beautiful design and built-in data types. \n", "* It has thousands of great libraries in many disciplines.\n", "* Supportive communities: Good documentation, courses, tutorials, social groups.\n", "* Easy to learn and use due to its simple syntaxes which feel like a natural language. \n", "\n", "This introduction will cover the following: \n", "\n", "\n", "* [1. Variables, Numbers and Strings](#1)\n", " * [1.1 Variables](#1-1)\n", " * [1.2 Numbers](#1-2)\n", " * [1.3 Strings](#1-3)\n", "* [2. Data Structures](#2)\n", " * [2.1 Lists](#2-1)\n", " * [2.2 Dictionaries](#2-2)\n", " * [2.3 Tuples](#2-3)\n", " * [2.4 Sets](#2-4)\n", "* [3. Comparison and Logic Operators](#3)\n", "* [4. Control Flow](#4)\n", " * [4.1 If Condition](#4-1)\n", " * [4.2 For Loop](#4-2)\n", " * [4.3 While Loop](#4-3)\n", "* [5. Functions](#5)\n", "* [6. Lambda Functions](#6)\n", "* [7. Built in functions](#7)\n", " * [7.1 Map Function](#7-1)\n", " * [7.2 Filter Function](#7-2)\n", "\n", "* [8. More Useful Python Stuff](#8)\n", " * [8.1 List Comprehension](#8-1)\n", " * [8.2 Enumerate Function](#8-2)\n", " * [8.3 Zip Function](#8-3)" ] }, { "cell_type": "markdown", "metadata": { "id": "LQkPE-1-f4_w" }, "source": [ "\n", "\n", "## 1. Variables, Numbers, and Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "### 1.1 Variables" ] }, { "cell_type": "markdown", "metadata": { "id": "Ay7vWsxNgOvQ" }, "source": [ "Below are quick notes about Python variables and other most important things to know before writing actual Python code:\n", "\n", "* `A Variable` is a location in memory where we store the data. \n", "* `A variable` in Python can either be of 3 data types: `integer`, `float`, or a `string`. Data type specifies the category of the variables.\n", "* We can use `type(variable_name)` to find the type of given `variable_name`.\n", "* In Python, we use `#` to add `comments`. Comments do not change anything and are not compiled.\n", "* If your comment is longer than one line, you can use triple quotes. The lines inside triple quotes are ignore during runtime.\n", "\n", "```\n", "\"\"\"\n", "In Python, there is no official way to write long comments, but you can use triple quotes.\n", "The sentence between triple quote are ignored at runtime. You can also use single quote('''....'''). Python treats single quote as double quotes in many scanerios such as strings representation.\n", "\n", "Guido also agreed it works: https://twitter.com/gvanrossum/status/112670605505077248 \n", "\"\"\"\n", "```\n", "\n", "* We also use `=` to assign a value to the name of variable. Example: `var_name = 1`. Note that it's different to comparison operator of equal to (`==`).\n", "* We can use `print()` to display the value of variable or the results of any expression.\n", "* Each line of the code start on the new line.\n", "* Be aware of indentations. Python is serious about them." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "rpDCNUAigQ7T" }, "outputs": [], "source": [ "# EXAMPLE OF CREATING A VARIABLE\n", "\n", "# We use # to add comment, it won’t run or affect the code\n", "# You use = to assign a value to the name of the variable. \n", "# Each code starts on the new line. No semicolon or {}\n", "# Python is awesome. You do not need to provide the data type of variable when creating it.\n", "\n", "int_var = 1\n", "str_var = 'Hundred'" ] }, { "cell_type": "markdown", "metadata": { "id": "QKqOitvHiJGq" }, "source": [ "\n", "\n", "### 1.2 Numbers\n", "\n", "Numbers in Python can either be integers `int` or floats `float`. Integer are real, finite, natural or whole numbers. Take an example: `1`,`2`,`3`,`4` are integers. Floats are numbers that have decimal points such as`4.6`, `6.0`, `7.7`. Note that `4.0` is considered as a float data type too. Recently, Karpathy, AI Director at Tesla posted that [floats are not real](https://twitter.com/karpathy/status/1475317897660612617).\n", "\n", "We can perform operations on numbers. The operations that we can perform include addition, multiplication, division, modular, etc..." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "D4k_6MQvDGyw", "outputId": "22956e00-8d13-47a9-e8ec-92abcd562ea7" }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int_var = 10\n", "float_var = 12.8\n", "\n", "type(int_var)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-Xux_M0IDSNd", "outputId": "de6cf2b9-e855-4376-be5e-e36a98ac95b3" }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(float_var)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ylRf_AakiOX5", "outputId": "af61036a-51e4-471a-fc9f-6bbb5ed2bb7e" }, "outputs": [ { "data": { "text/plain": [ "101" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Numeric Operations \n", "\n", "# Addition\n", "\n", "1 + 100" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "z8RlLLQRiclC", "outputId": "329c4f62-fb29-4cdb-f2a8-11bda7294f48" }, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Multiplication\n", "\n", "1 * 100" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "j3Z7GFSjiuJ9", "outputId": "4b0715e2-c877-4927-87b0-16da07b7fe99" }, "outputs": [ { "data": { "text/plain": [ "0.01" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Division\n", "\n", "1 / 100" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Floor division\n", "\n", "7 // 2" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2TC5FUgKizLU", "outputId": "cca0eb84-b6f0-4db1-e3b7-bba58d03ea66" }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Modular (%)\n", "# This is the remainder or a value remaining after dividing two numbers\n", "# 100 / 1 = 100, remainder is 0\n", "\n", "100 % 1" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0Z0LsbTZA2_x", "outputId": "984b21a1-d43e-4af9-c823-f459ce35c320" }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 % 100" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "FtIoIic0Bvuf", "outputId": "622ed914-b4c6-40a8-d72b-979f96ea41f0" }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 % 2" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LEXWCtDTAjxN", "outputId": "082a19fc-38ec-41dc-c6b1-a3085747fd52" }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Powers\n", "# 1 power any number is 1 always\n", "\n", "1 ** 100" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aX6B6U7vBs0T", "outputId": "4cdc396a-50eb-4712-96f8-2212aa801da5" }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** 2" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UfhAyildkFFn", "outputId": "65333e6c-fba8-4811-8279-f97a4182a8df" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "101\n" ] } ], "source": [ "# We use print() to display the results of the operations or a variable\n", "\n", "print(1 + 100)" ] }, { "cell_type": "markdown", "metadata": { "id": "eHvcuGQoBCTU" }, "source": [ "\n", "\n", "### 1.3 Strings\n", "\n", "Python supports strings. String is a sequence of characters. \n", "\n", "Strings are one of the commonly used and important data types. Most problems involve working with strings. Thus, knowing how to work with strings is an incredible thing.\n", "\n", "Strings are expressed in either `\"...\"` or `'...'`.\n", "\n", "```\n", "\"text inside here will be a string\"\n", "'text inside here will also be a string'\n", "```\n", "\n", "We can manipulate strings in many ways. A simple example is to concat the strings. " ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "id": "VNjcQxVRj3qu" }, "outputs": [], "source": [ "str_var = 'One'\n", "str_var2 = 'Hundred'" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "Vu-n4cy1F9uj", "outputId": "4b9720ce-6212-4f15-d3bb-eb39bba8d05f" }, "outputs": [ { "data": { "text/plain": [ "'OneHundred'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_var + str_var2" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "177djVcAj8oL", "outputId": "8534b840-a7e2-4a2c-bf26-d57ed25b8d42" }, "outputs": [ { "data": { "text/plain": [ "'One and Hundred.'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_var + ' ' + 'and' + ' '+ str_var2 + '.'" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jemHahb6GCyx", "outputId": "24754a7d-018d-4e6d-fa9d-16a0ef67f444" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " This is a string\n" ] } ], "source": [ "# We can use print() to display a string\n", "\n", "print(\" This is a string\")" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can also compare strings to check whether they are similar. \n", "# If they are similar, case by case, comparison operator returns true. Else false\n", "\n", "\"A string\" == \"a string\"" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"A string\" == \"A string\"" ] }, { "cell_type": "markdown", "metadata": { "id": "-8qJK4h8n8_P" }, "source": [ "#### Strings Methods\n", "\n", "Python provides many built-in methods for manipulating strings. As a programmer, knowing typical string methods and how to use them will give you a real leverage when working with strings.\n", "\n", "There are many string methods. You can find them [here](https://docs.python.org/2.5/lib/string-methods.html). Let's see some common methods." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "id": "xF6Kk0Vco2Uh" }, "outputs": [], "source": [ "sentence = 'This IS A String'" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "xgYf8qO2o2Uj", "outputId": "b28989dc-b99e-4c6b-c5eb-9861902a314f" }, "outputs": [ { "data": { "text/plain": [ "'This is a string'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Case capitalization \n", "# It return the string with first letter capitalized and the rest being lower cases. \n", "\n", "sentence.capitalize()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "OqbKJIqjpFUv", "outputId": "6affdb88-344d-41f2-952f-55154a8f883c" }, "outputs": [ { "data": { "text/plain": [ "'This Is A String To Be Titled'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Given a string, convert it into title (each word is capitalized)\n", "\n", "sentence_2 = 'this is a string to be titled'\n", "sentence_2.title()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "vUvd51eGpFUw", "outputId": "1820345d-e13e-4539-e2c4-edde8a62105e" }, "outputs": [ { "data": { "text/plain": [ "'THIS IS A STRING'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Converting the string to upper case\n", "\n", "sentence.upper()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "msoUtVcfpFUx", "outputId": "c4ba7416-6f2a-4d96-f428-d66495b17449" }, "outputs": [ { "data": { "text/plain": [ "'this is a string'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Converting the string to upper case\n", "\n", "sentence.lower()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lddBz3mFpFUx", "outputId": "d0a16ebc-5260-417f-ff97-00bfb902a4d9" }, "outputs": [ { "data": { "text/plain": [ "['This', 'IS', 'A', 'String']" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Splitting the string\n", "\n", "sentence.split()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lastly, we can use `replace()` method to replace some characters in string with another characters. Replace method takes two inputs: characters to be replaced, and new characters to be inserted in string, `replace('characters to be replaced', 'new characters')`. \n", "\n", "Example, given the string \"This movie was awesome\", replace the world `movie` with `project`. " ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This project was awesome'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stri = \"This movie was awesome\"\n", "stri.replace('movie', 'project')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'The%20future%20is%20great'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# In the following string, replace all spaces with `%20'\n", "\n", "stri_2 = \"The future is great\"\n", "stri_2.replace(' ', '%20')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, strings methods are powerful and can save you time. Remember one of the Python philosophies that we saw in the beginning: `Simple is better than complex`. " ] }, { "cell_type": "markdown", "metadata": { "id": "lG9ZQje1k92N" }, "source": [ "\n", "## 2. Data Structures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Data structures are used to organize and store the data. Algorithms supports operations on data.\n", "\n", "Python has 4 main data structures: `Lists`, `Dictionaries`, `Tuples` and `Sets`. " ] }, { "cell_type": "markdown", "metadata": { "id": "DH8pNlbTic4m" }, "source": [ "\n", "\n", "### 2.1 List" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A list is a set of ordered values. Each value in a list is called an `element` or `item` and can be identified by an index. A list supports different data types, we can have a list of integers, strings, and floats. \n", "\n", "What we will see with Python lists:\n", "\n", "- Creating a list\n", "- Accessing elements in a list\n", "- Slicing a list\n", "- Changing elements in a list\n", "- Traversing a list\n", "- Operations on list\n", "- Nested lists\n", "- List methods\n", "- List and strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Creating a List\n", "\n", "A python list can be created by enclosing elements of similar or different data type in square brackets `[...]`, or with `range()` function." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "id": "swPhe55olYIy" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Mon', 'Tue', 'Wed', 'Thur', 'Fri']\n" ] } ], "source": [ "# Creating a list\n", "\n", "week_days = ['Mon', 'Tue', 'Wed', 'Thur','Fri']\n", "even_numbers = [2, 4, 6, 8, 10]\n", "mixed_list = ['Mon', 1, 'Tue', 2, 'Wed', 3]\n", "\n", "# Displaying elements of a list\n", "print(week_days)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "# Creating a list with range()\n", "\n", "nums = range(5)\n", "\n", "for i in nums:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Accessing the elements of the list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can access the a given element of the list by providing the index of the element in a bracket. The index starts at 0 in Python." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "fn8cVIUKlkLc", "outputId": "76d88488-058d-4681-f12b-0446fc19bb1d" }, "outputs": [ { "data": { "text/plain": [ "'Mon'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Accessing the first elements of the list\n", "\n", "week_days[0]" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-RBb9JkBl818", "outputId": "33dc5ade-a128-4412-fc65-a052ba5613f7" }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "even_numbers[2]" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "D4ZyzdIqmJhp", "outputId": "bf0e2e13-9aa7-4dc8-b587-db9b7f9ad27a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "# Getting the last element of the list\n", "\n", "print(even_numbers[-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Slicing a list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given a list, we can slice it to get any parts or combination of its elements forming another list." ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6vgpF3uumLSh", "outputId": "94668bd8-cc67-4662-8005-ad85d70c0415" }, "outputs": [ { "data": { "text/plain": [ "['Mon', 'Tue']" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the elements from index 0 to 2. Index 2 is not included.\n", "\n", "week_days = ['Mon', 'Tue', 'Wed', 'Thur','Fri']\n", "week_days[0:2]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rbz02O80mV1K", "outputId": "2ba533db-6467-4121-b964-ba9c1bf40a3b" }, "outputs": [ { "data": { "text/plain": [ "['Tue', 'Wed', 'Thur', 'Fri']" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get elements from the last fourth elements to the first\n", "# -1 starts at the last element 'Fri', -2 second last element `Thur'..... -4 to 'Tue'\n", "\n", "week_days[-4:]" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Mon', 'Tue', 'Wed', 'Thur']" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get all elements up to the fourth index\n", "\n", "week_days[:4]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Wed', 'Thur', 'Fri']" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get all elements from the second to the last index\n", "\n", "week_days[2:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use `[:]` to copy the entire list. " ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Mon', 'Tue', 'Wed', 'Thur', 'Fri']" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "week_days[:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Changing elements in a list\n", "\n", "Python lists are mutable. We can delete or change the elements of the list." ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['James', 'Jean', 'Sebastian', 'Prit']" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "names = ['James', 'Jean', 'Sebastian', 'Prit']\n", "names" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['James', 'Nyandwi', 'Ras', 'Prit']" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change 'Jean' to 'Nyandwi' and 'Sebastian' to 'Ras'\n", "\n", "names[1:3] = ['Nyandwi', 'Ras']\n", "names" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['James', 'Nyandwi', 'Ras', 'Sun']" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change 'Prit' to Sun\n", "\n", "names[-1] = 'Sun'\n", "names" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Francois', 'Nyandwi', 'Ras', 'Sun']" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change `James` to ``Francois`\n", "\n", "names[0] = 'Francois'\n", "names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to delete a given element in a list, we can empty slice it but the better way to delete element is to use `del` keyword." ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Francois', 'Ras', 'Sun']" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Delete Nyandwi in names list\n", "\n", "del names[1]\n", "names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you know the index of the element you want to remove, you can use `pop()`. If you don't provide the index in pop(), the last element will be deleted." ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['James', 'Jean', 'Prit']" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "names = ['James', 'Jean', 'Sebastian', 'Prit']\n", "names.pop(2)\n", "names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Also, we can use `remove()` to delete the element provided inside the remove() method." ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Jean', 'Sebastian', 'Prit']" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "names = ['James', 'Jean', 'Sebastian', 'Prit']\n", "names.remove('James')\n", "names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use `append()` to add element to the list." ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4IQ2eSWKnUUe", "outputId": "b9a4bb0a-7e58-4c65-e2f6-0e6bc7e26cc1" }, "outputs": [ { "data": { "text/plain": [ "['James', 'Jean', 'Sebastian', 'Prit', 'Jac', 'Jess']" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Adding the new elements in list\n", "\n", "names = ['James', 'Jean', 'Sebastian', 'Prit']\n", "names.append('Jac')\n", "names.append('Jess')\n", "names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Traversing a list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are times that we may need to go over the list to read the elements of the list or perform iterative operations. We can use `for loop` to traverse through the list." ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "James\n", "Jean\n", "Sebastian\n", "Prit\n" ] } ], "source": [ "# Given a list names, use for loop to display its elements\n", "\n", "names = ['James', 'Jean', 'Sebastian', 'Prit']\n", "\n", "for name in names:\n", " print(name)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 4, 6, 10]" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Given a list nums, add 1 to the first element, 2 to the second, 3 to 3rd element, 4 to 4th element\n", "# Example: nums = [1,2,3,6] will be nums_new = [2,4,6,10]\n", "\n", "nums = [1, 2, 3, 6]\n", "nums_new = []\n", "\n", "for i in range(len(nums)): #len(nums) gives the length of the list\n", " num = nums[i] + i + 1\n", " nums_new.append(num)\n", " \n", "nums_new" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Operations on list" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "eAFvBTBwnwE6", "outputId": "b38e412a-9839-454c-b8b2-5594174225e2" }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Concatenating two lists\n", "\n", "a = [1,2,3]\n", "b = [4,5,6]\n", "\n", "c = a + b\n", "\n", "c" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[None, None, None, None, None]" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can also use * operator to repeat a list a number of times\n", "\n", "[None] * 5" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[True, True, True, True]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[True] * 4" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 4, 5, 1, 2, 4, 5]" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,4,5] * 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Nested lists" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YfuV7azQoAfP", "outputId": "8d5fe027-3b49-482b-af56-4eaf0713169b" }, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Creating a list in other list\n", "\n", "nested_list = [1,2,3, ['a', 'b', 'c']]\n", "\n", "\n", "# Get the ['a', 'b', 'c'] from the nested_list\n", "\n", "nested_list[3]" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Indexing and slicing a nested list is quite similar to normal list\n", "\n", "nested_list[1]" ] }, { "cell_type": "markdown", "metadata": { "id": "-06aEzRYpQuv" }, "source": [ "#### List Methods\n", "\n", "Python also offers methods which make it easy to work with lists. We already have been using some list methods such as `pop()` and `append()` but let's review more other methods." ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "J9mdh2H7pQuw", "outputId": "5ae746d6-e318-438d-cfef-88548b052299" }, "outputs": [ { "data": { "text/plain": [ "[2, 8, 10, 12, 14, 16, 20]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sorting a list with sort()\n", "\n", "even_numbers = [2,14,16,12,20,8,10]\n", "\n", "even_numbers.sort()\n", "\n", "even_numbers" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rIkRCqJFpQuw", "outputId": "7a1c9c79-4c4c-4c7e-b740-83b78636d4dc" }, "outputs": [ { "data": { "text/plain": [ "[20, 16, 14, 12, 10, 8, 2]" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Reversing a string with reverse()\n", "\n", "even_numbers.reverse()\n", "even_numbers" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XkPIhdgspQux", "outputId": "dbc9490d-f065-4ae0-a760-2411d620f499" }, "outputs": [ { "data": { "text/plain": [ "[2, 14, 16, 12, 20, 8, 10, 40]" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Adding other elements to a list with append()\n", "\n", "even_numbers = [2,14,16,12,20,8,10]\n", "\n", "even_numbers.append(40)\n", "even_numbers" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ar0cycDrpQux", "outputId": "fa83b714-1d63-4b14-f4d6-a5d0ac6cd3dc" }, "outputs": [ { "data": { "text/plain": [ "[14, 16, 12, 20, 8, 10, 40]" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Removing the first element of a list\n", "\n", "even_numbers.remove(2)\n", "even_numbers" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cEaIcsmepQux", "outputId": "626ea699-cdf2-454b-958e-8fba665495dd" }, "outputs": [ { "data": { "text/plain": [ "14" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Return the element of the list at index x\n", "\n", "even_numbers = [2,14,16,12,20,8,10]\n", "\n", "## Return the item at the 1st index\n", "\n", "even_numbers.pop(1)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "F1VVBhZOpQuy", "outputId": "f77f0562-e653-4d83-eefb-b8346470b9d9" }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pop() without index specified will return the last element of the list\n", "\n", "even_numbers = [2,14,16,12,20,8,10]\n", "even_numbers.pop()" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "X19sfgRHpQuy", "outputId": "2334f253-bc52-4314-9dc6-26914a3d7d51" }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Count a number of times an element appear in a list\n", "\n", "even_numbers = [2,2,4,6,8]\n", "even_numbers.count(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List and strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We previously have learned about strings. Strings are sequence of characters. List is a sequence of values. " ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['A', 'p', 'p', 'l', 'e']" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can convert a string into list\n", "\n", "stri = 'Apple'\n", "\n", "list(stri)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['List', 'and', 'Strings']" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Splitting a string produces a list of individual words\n", "\n", "stri_2 = \"List and Strings\"\n", "stri_2.split()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The split() string method allows to specify the character to use a a boundary while splitting the string. It's called delimiter." ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['state', 'of', 'the', 'art']" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stri_3 = \"state-of-the-art\"\n", "\n", "stri_3.split('-')" ] }, { "cell_type": "markdown", "metadata": { "id": "qXcv4KtLq2RJ" }, "source": [ "\n", "\n", "### 2.2 Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionaries are powerful Python builtin data structure that are used to store data of key and values. A dictionary is like a list but rather than using integers as indices, indices in dictionary can be any data type. Also, unlike lists, dictionary are unordered. Dictionaries dont guarrantee keeping the order of the data.\n", "\n", "Generally, a dictionary is a collection of key and values. A dictionary stores a mapping of keys and values. A key is what we can refer to index.\n", "\n", "What we will see:\n", "\n", "- Creating a dictionary\n", "- Accessing values and keys in dictionary\n", "- Solving counting problems with dictionary\n", "- Traversing a dictionary\n", "- The setdefault() method" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Creating a dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can create with a `dict()` function and add items later, or insert keys and values right away in the curly brackets { }. Let's start with dict() function to create an empty dictionary. " ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n" ] } ], "source": [ "# Creating a dictionary\n", "\n", "countries_code = dict()\n", "print(countries_code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can verify it's a dictionary by passing it through `type()`." ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(countries_code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's add items to the empty dictionary that we just created." ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "id": "ehjyFwirq7A0" }, "outputs": [], "source": [ "# Adding items to the empty dictionary.\n", "\n", "countries_code[\"United States\"] = 1" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'United States': 1}" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "countries_code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create a dictionary with {}. It's the common way to create a dictionary." ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "R1miLn1BuN9J", "outputId": "3eda6a2d-d452-4272-80af-2f1d45dd525b" }, "outputs": [ { "data": { "text/plain": [ "{'United States': 1, 'China': 86, 'Rwanda': 250, 'Germany': 49, 'India': 91}" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "countries_code = { \n", " \"United States\": 1,\n", " \"China\": 86,\n", " \"Rwanda\":250,\n", " \"Germany\": 49,\n", " \"India\": 91,\n", "}\n", "\n", "countries_code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To add key and values to a dictionary, we just add the new key to [ ] and set its new value. See below for example..." ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'United States': 1,\n", " 'China': 86,\n", " 'Rwanda': 250,\n", " 'Germany': 49,\n", " 'India': 91,\n", " 'Australia': 61}" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "countries_code['Australia'] = 61\n", "countries_code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Accessing the values and keys in a dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just like how we get values in a list by using their indices, in dictionary, we can use a key to get its corresponding value." ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "250" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Getting the code of Rwanda\n", "\n", "countries_code[\"Rwanda\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also check if a key exists in a dictionary by using a classic `in` operator." ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"India\" in countries_code " ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Should be False\n", "\n", "\"Singapore\" in countries_code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get all the keys, value, and items of the dictionary, we can respectively use `keys()`, `values()`, and `items()` methods." ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Keys: dict_keys(['United States', 'China', 'Rwanda', 'Germany', 'India', 'Australia'])\n", " Values:dict_values([1, 86, 250, 49, 91, 61])\n", " Items:dict_items([('United States', 1), ('China', 86), ('Rwanda', 250), ('Germany', 49), ('India', 91), ('Australia', 61)])\n" ] } ], "source": [ "# Getting the keys and the values and items of the dictionary\n", "\n", "dict_keys = countries_code.keys()\n", "dict_values = countries_code.values()\n", "dict_items = countries_code.items()\n", "\n", "print(f\"Keys: {dict_keys}\\n Values:{dict_values}\\n Items:{dict_items}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lastly, we can use `get()` method to return the value of a specified key. Get method allows to also provide a value that will be returned in case the key doesn't exists. This is a cool feature!!" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "61" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the value of the Australia\n", "\n", "countries_code.get('Australia')" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "41" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# In case a provided key is absent....\n", "\n", "countries_code.get('UK', 41)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Solving counting problems with dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When solving real world problems, or perhaps doing interviews, most problems involve counting certain elements. Let's take a simple example: Given a string, write an algorithm that can count the occurence of each character. \n", "\n", "**Solution:**\n", "\n", "The first approach would be to create a list of 128 elements given that the standard size of characters in ASCII is 128, convert each character to a number with `ord()` method(`char()` convert from number to string), use the number as the index of the string, and then increment it as you see a recurring character. The code would like like this...." ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 3,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 3,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 3,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 3,\n", " 0,\n", " 0,\n", " 5,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 3,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 3,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0]" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stri = 'aaaaajjj222@@@sss^^^888'\n", "\n", "counts = [0] * 128 # Create a list of 128 elements, initially each character count is 0. \n", "\n", "for c in stri:\n", " c_num = ord(c)\n", " \n", " counts[c_num] = counts[c_num] + 1\n", " \n", "counts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using dictionary, we would not have to worry about the size of the string or the nmber of characters to keep beforehand. We would just create a dictionary whose keys are characters and values are counts of corresponding characters. See character for the first time, add it to the dictionary with value of 1 since it's the first count, and then increment it if the character exists in dictionary." ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'j': 10, 'd': 7}\n" ] } ], "source": [ "stri = \"jdjjdjdjdjjdjdjdj\"\n", "\n", "counts_dict = {}\n", "\n", "for c in stri:\n", " if c not in counts_dict:\n", " counts_dict[c] = 1\n", " \n", " else:\n", " counts_dict[c] += 1\n", " \n", "print(counts_dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Also, we can use `get()` method that we saw above to simplify our solution. To make it a bit fancier, let's also assume that the string will be provided by the user at the beginning of the program." ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ABBABABSHHSSH\n", "{'A': 3, 'B': 4, 'S': 3, 'H': 3}\n" ] } ], "source": [ "stri = input(str) #input must be a string\n", "\n", "char_count = {}\n", "\n", "for c in stri:\n", " char_count[c] = char_count.get(c,0) + 1\n", " \n", "print(char_count)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Traversing a dictionary\n", "\n", "We previously used for loop in dictionary to iterate through the values. Let's review it again." ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'United States': 1,\n", " 'China': 86,\n", " 'Rwanda': 250,\n", " 'Germany': 49,\n", " 'India': 91,\n", " 'Australia': 61}" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "countries_code" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "United States\n", "China\n", "Rwanda\n", "Germany\n", "India\n", "Australia\n" ] } ], "source": [ "for country in countries_code:\n", " print(country)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also iterate through the items(key, values) of the dictionary." ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "United States 1\n", "China 86\n", "Rwanda 250\n", "Germany 49\n", "India 91\n", "Australia 61\n" ] } ], "source": [ "for country, code in countries_code.items():\n", " print(country, code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The setdefault() Method\n", "\n", "The setdefault() method allows you to set a value of a given key that does not already have a key. This can be helpful when you want to update the dictionary with a new value in case the key you are looking for does not exist." ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "41" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "countries_code.setdefault(\"UK\", 41)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'United States': 1,\n", " 'China': 86,\n", " 'Rwanda': 250,\n", " 'Germany': 49,\n", " 'India': 91,\n", " 'Australia': 61,\n", " 'UK': 41}" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "countries_code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cool! The UK value is added to the dictionary because it was not in the dictionary before. The setdefault() method and get() method are different.\n", "\n", "We can also use the setdefault() in the count program we wrote above." ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AAAHHSHSHS\n", "{'A': 3, 'H': 4, 'S': 3}\n" ] } ], "source": [ "stri = input(str) #input must be a string\n", "\n", "char_count = {}\n", "\n", "for c in stri:\n", " char_count.setdefault(c,0) #If character doesn't exist in char_count, add it and set it to 0\n", " char_count[c] += 1\n", " \n", "print(char_count)" ] }, { "cell_type": "markdown", "metadata": { "id": "Lep0FmTjvTct" }, "source": [ "***Summarizing dictionary***\n", "\n", "* Dictionaries are not ordered and they can not be sorted - list are ordered (and unordered) and can be sorted.\n", "\n", "* Dictionary can store data of different types: floats, integers and strings and can also store lists. " ] }, { "cell_type": "markdown", "metadata": { "id": "0LH13FnDwfJq" }, "source": [ "\n", "\n", "### 2.3 Tuples\n", "\n", "Tuple is similar to list but the difference is that you can't change the values once it is defined (termed as `immutability`). Due to this property it can be used to keep things that you do not want to change in your program. Example can be a country codes, zipcodes, etc...\n", "\n" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "id": "mXy3mtYWxeNC" }, "outputs": [], "source": [ "tup = (1,4,5,6,7,8)" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WDn6FBozxpn8", "outputId": "3bffe88b-ce26-4eea-ae57-b9b9d15481de" }, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Indexing\n", "\n", "tup[4]" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "id": "n4i9sYcVxtTu" }, "outputs": [], "source": [ "## Tuples are not changeable. Running below code will cause an error\n", "\n", "# tup[2] = 10" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "id": "DiuL_4xrx8t-" }, "outputs": [], "source": [ "# You can not also add other values to the tuple. This will be error\n", "#tup.append(12)" ] }, { "cell_type": "markdown", "metadata": { "id": "IELzAECOycaI" }, "source": [ "\n", "\n", "### 2.4 Sets" ] }, { "cell_type": "markdown", "metadata": { "id": "Ul5m3MjMzTih" }, "source": [ "Sets are used to store the unique elements. They are not ordered like list. " ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aJUhhYUBzdXy", "outputId": "51daa7c3-4157-4f00-acba-7e2752e054ec" }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5, 6, 7, 8}" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set_1 = {1,2,3,4,5,6,7,8}\n", "\n", "set_1" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "s5vrQMXvzrrn", "outputId": "3666c04f-d689-4987-fa24-048a06d0400b" }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5, 7, 8}" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set_2 = {1,1,2,3,5,3,2,2,4,5,7,8,8,5}\n", "set_2" ] }, { "cell_type": "markdown", "metadata": { "id": "1n9h4Q_Vz_Li" }, "source": [ "As you can see, set only keep unique values. There can't be a repetition of values. " ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lsKLPvIp0Mc3", "outputId": "5291d7fa-103e-48bb-aece-e9210cc0099c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "List:[1, 1, 3, 7, 9, 3, 5, 7, 9, 9]\n", "********\n", "Set:{1, 3, 5, 7, 9}\n" ] } ], "source": [ "# List Vs Set\n", "\n", "odd_numbers = [1,1,3,7,9,3,5,7,9,9]\n", "\n", "print(\"List:{}\".format(odd_numbers))\n", "\n", "print(\"********\")\n", "\n", "set_odd_numbers = {1,1,3,7,9,3,5,7,9,9}\n", "\n", "print(\"Set:{}\".format(set_odd_numbers))" ] }, { "cell_type": "markdown", "metadata": { "id": "TSqms6Vk2OKk" }, "source": [ "\n", "\n", "## 3. Comparison and Logic operators\n", "\n", "`Comparison` operators are used to compare values. It will either return true or false. " ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "E0byfFXV2eNO", "outputId": "692ed6e7-1e26-404d-c47f-9ed3fafeb780" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Greater than \n", "100 > 1" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "fzUdnfqh2gnd", "outputId": "f11ecacc-652b-437d-ac2d-eaa96d7007c8" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Equal to\n", "\n", "100 == 1" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "sW39wpYQ2lBN", "outputId": "ce77eb3f-04b1-4223-ca2a-a56736baa944" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Less than\n", "\n", "100 < 1" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "pmj9bZsy2qH4", "outputId": "25968aca-2a82-46fd-f89d-9f6a44c23b90" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Greater or equal to\n", "\n", "100 >= 1" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2GQMkYjn3DpW", "outputId": "06843fd0-bbc8-42ce-a18e-51ecf1361dfd" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Less or equal to\n", "\n", "100 <= 1" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "utLU3_7y3KEJ", "outputId": "65271608-58ac-4e9a-c110-4a3776ce4c76" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Intro to Python' == 'intro to python'" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "68QTmaFd3YpI", "outputId": "8983e42c-c607-4770-ae95-63d36dbc90a0" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Intro to Python' == 'Intro to Python'" ] }, { "cell_type": "markdown", "metadata": { "id": "I00qTqfM3d5R" }, "source": [ "`Logic` operators are used to compare two expressions made by comparison operators. \n", "\n", "* Logic `and` returns true only when both expressions are true, otherwise false. \n", "\n", "* Logic `or` returns true when either any of both expressions is true. Only false if both expressions are false.\n", "\n", "* Logic `not` as you can guess, it will return false when given expression is true, vice versa. " ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dlNW3zaW3tHj", "outputId": "7ae4a248-3ac9-48a6-827e-ccd5fe532879" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100 == 100 and 100 == 100" ] }, { "cell_type": "code", "execution_count": 101, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "sJ77YW6D5zpq", "outputId": "7037d1a0-1641-4294-c01b-f6f21120dc7e" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100 <= 10 and 100 == 100" ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "W51xeGzr552j", "outputId": "493b4a66-30e4-47b5-cf15-4ed46ca62931" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100 == 10 or 100 == 100" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "SAN6F8xj59iG", "outputId": "10e31953-d7f8-49f4-bfef-95b4ba118ce4" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100 == 10 or 100 == 10" ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9MOShqdV6DrH", "outputId": "f99699af-e8ec-49c7-d331-ffb6f2c79d9b" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 1 == 2" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3YYMcUeA6H3c", "outputId": "f68b4aed-e0a0-486a-b192-50a829315b63" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 1 == 1" ] }, { "cell_type": "markdown", "metadata": { "id": "3LFYoIiX_IdG" }, "source": [ "\n", "## 4. Control Flow\n", "As an engineer, you will need to make decisions depending on the particular situation. You will also need to control the flow of the program and this is where `Control Flow` comes in. \n", "\n", "We will cover:\n", "\n", "* If statement\n", "* For loop\n", "* While loop\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "dECd-PX0i4L0" }, "source": [ "\n", "\n", "### 4.1 If, Elif, Else" ] }, { "cell_type": "markdown", "metadata": { "id": "PNm5yBHvFFZs" }, "source": [ "\n", "\n", "Structure of If condition:\n", "\n", "```\n", "if condition:\n", " do something\n", "\n", "else:\n", " do this\n", "```\n" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "id": "kliJhGlkA-dM" }, "outputs": [], "source": [ "if 100 < 2:\n", "\n", " print(\"As expected, no thing will be displayed\")" ] }, { "cell_type": "code", "execution_count": 107, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DA6VkMFABLOE", "outputId": "12823390-a9ec-4aaa-9af2-e215c4700f87" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "As expected, no thing will be displayed\n" ] } ], "source": [ "if 100 > 2:\n", "\n", " print(\"As expected, no thing will be displayed\")" ] }, { "cell_type": "code", "execution_count": 108, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "tB42MrxeBP9e", "outputId": "f13618dc-53c7-4754-b695-a2761c2f49a3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Printed\n" ] } ], "source": [ "if 100 < 2:\n", "\n", " print(\"As expected, no thing will be displayed\")\n", "\n", "else:\n", " print('Printed')" ] }, { "cell_type": "code", "execution_count": 109, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "pJ8gAIF9B147", "outputId": "72970118-476a-4412-f72a-a9b38a652a35" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "John is older than Luck\n" ] } ], "source": [ "# Let's assign a number to a variable name 'jean_age' and 'yannick_age'\n", "\n", "john_age = 30\n", "luck_age = 20\n", "\n", "if john_age > luck_age:\n", " print(\"John is older than Luck\")\n", "\n", "else:\n", " print(\" John is younger than Luck\")" ] }, { "cell_type": "code", "execution_count": 110, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Zl-eiwRICAZg", "outputId": "fdbf8cf0-5aa6-4416-9006-6309df3fc3ef" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "John's age is same as Yan\n" ] } ], "source": [ "# Let's use multiple conditions \n", "\n", "john_age = 30\n", "luck_age = 20\n", "yan_age = 30\n", "\n", "if john_age < luck_age:\n", " print(\"John is older than Luck\")\n", "\n", "elif yan_age == luck_age:\n", " print(\" Yan's Age is same as Luck\")\n", "\n", "elif luck_age > john_age:\n", " print(\"Luck is older than John\")\n", "\n", "else:\n", " print(\"John's age is same as Yan\")" ] }, { "cell_type": "markdown", "metadata": { "id": "D_mbbtQMRjcA" }, "source": [ "We can also put if condition into one line of code. This can be useful when you want to make a quick decision between few choices. \n", "\n", "Here is the structure:\n", "\n", "`'value_to_return_if_true' if condition else 'value_to_return_if_false'`\n", "\n", "Let's take some examples..." ] }, { "cell_type": "code", "execution_count": 111, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "Yca_thlBSTp1", "outputId": "fe1ddd46-ebf6-4ebf-c088-da387fcaa46b" }, "outputs": [ { "data": { "text/plain": [ "'Odd'" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example 1: Return 'Even' if below num is 'Even' and `Odd` if not. \n", "\n", "num = 45\n", "\n", "'Even' if num % 2 == 0 else 'Odd'" ] }, { "cell_type": "code", "execution_count": 112, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aDxiANsSS-sd", "outputId": "48cd7c6d-a835-4ce5-c907-fcc22e2736b5" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example 2: Return True if a given element is in a list and False if not\n", "\n", "nums = [1,2,3,4,5,6]\n", "\n", "True if 3 in nums else False" ] }, { "cell_type": "markdown", "metadata": { "id": "DR82CZXPi97L" }, "source": [ "\n", "### 4.2 For Loop" ] }, { "cell_type": "markdown", "metadata": { "id": "4nNM1F9zEPIU" }, "source": [ "For loop is used to iterate over list, string, tuples, or dictionary. \n", "\n", "Structure of for loop:\n", "\n", "```\n", "for item in items:\n", " do something\n", "```\n", "\n" ] }, { "cell_type": "code", "execution_count": 113, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lv8sBhK5EStD", "outputId": "20f7abc8-b48b-4adc-d21c-06c36ec5a31f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "4\n", "6\n", "8\n", "10\n" ] } ], "source": [ "even_nums = [2,4,6,8,10]\n", "\n", "for num in even_nums:\n", " print(num)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "BhUn214hEbul", "outputId": "e5ef21eb-2b58-4d0d-a26e-8cf3e66e42c8" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mon\n", "Tue\n", "Wed\n", "Thur\n", "Fri\n" ] } ], "source": [ "week_days = ['Mon', 'Tue', 'Wed', 'Thur','Fri']\n", "\n", "for day in week_days:\n", " print(day)" ] }, { "cell_type": "code", "execution_count": 115, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IDhmxmTGF3ui", "outputId": "bfe3c5c6-a913-4d75-e5cd-983de528e8e6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I\n", "t\n", "'\n", "s\n", " \n", "b\n", "e\n", "e\n", "n\n", " \n", "a\n", " \n", "l\n", "o\n", "n\n", "g\n", " \n", "t\n", "i\n", "m\n", "e\n", " \n", "l\n", "e\n", "a\n", "r\n", "n\n", "i\n", "n\n", "g\n", " \n", "P\n", "y\n", "t\n", "h\n", "o\n", "n\n", ".\n", " \n", "I\n", " \n", "a\n", "m\n", " \n", "r\n", "e\n", "v\n", "i\n", "s\n", "i\n", "t\n", "i\n", "n\n", "g\n", " \n", "t\n", "h\n", "e\n", " \n", "b\n", "a\n", "s\n", "i\n", "c\n", "s\n", "!\n", "!\n" ] } ], "source": [ "sentence = \"It's been a long time learning Python. I am revisiting the basics!!\"\n", "\n", "for letter in sentence:\n", " print(letter)" ] }, { "cell_type": "code", "execution_count": 116, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WJvsmovUGLMf", "outputId": "25a9c7cf-1644-41e6-808f-bfa4ab7daf1d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It's\n", "been\n", "a\n", "long\n", "time\n", "learning\n", "Python.\n", "I\n", "am\n", "revisiting\n", "the\n", "basics!!\n" ] } ], "source": [ "sentence = \"It's been a long time learning Python. I am revisiting the basics!!\"\n", "\n", "# split is a string method to split the words making the string\n", "\n", "for letter in sentence.split():\n", " print(letter)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "iPMw20rMGfiP", "outputId": "735e5a51-1013-40ec-ab2a-83cd1f6aeee7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "United States\n", "India\n", "Germany\n", "China\n", "Rwanda\n" ] } ], "source": [ "# For loop in dictionary \n", "\n", "countries_code = { \"United States\": 1,\n", " \"India\": 91,\n", " \"Germany\": 49,\n", " \"China\": 86,\n", " \"Rwanda\":250\n", " }\n", "\n", "for country in countries_code:\n", " print(country)" ] }, { "cell_type": "code", "execution_count": 118, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wsxejcLhGuhR", "outputId": "f9e78e4d-4a1f-489d-d92f-95de770f18bd" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "91\n", "49\n", "86\n", "250\n" ] } ], "source": [ "for code in countries_code.values():\n", " print(code)" ] }, { "cell_type": "markdown", "metadata": { "id": "BlKSXtqtJsCn" }, "source": [ "For can also be used to iterate over an sequence of numbers. `Range` is used to generate the sequence of numbers. \n", "\n", "\n", "\n", "```\n", "for number in range: \n", " do something\n", "```\n", "\n" ] }, { "cell_type": "code", "execution_count": 119, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9JtM6IVQKEOr", "outputId": "4d995ca0-dda8-470a-aeaa-2b5964e33cb0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for number in range(10):\n", " print(number)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Rrd4cXk5KYCq", "outputId": "4ca9aed3-2e87-4d30-f574-a4ddf2abe87c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "11\n", "12\n", "13\n", "14\n", "15\n", "16\n", "17\n", "18\n", "19\n" ] } ], "source": [ "for number in range(10, 20):\n", " print(number)" ] }, { "cell_type": "markdown", "metadata": { "id": "N9liuYozLIKp" }, "source": [ "One last thing about `for loop` is that we can use it to make a list. This is called list comprehension." ] }, { "cell_type": "code", "execution_count": 121, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4-N327iaLZQP", "outputId": "584dfca6-d844-45c8-c556-553e1587e33d" }, "outputs": [ { "data": { "text/plain": [ "['M', 'a', 'c', 'h', 'i', 'n', 'e', 'L', 'e', 'a', 'r', 'n', 'i', 'n', 'g']" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letters = []\n", "\n", "for letter in 'MachineLearning':\n", " letters.append(letter)\n", "\n", "letters" ] }, { "cell_type": "markdown", "metadata": { "id": "VkOTDujzL7re" }, "source": [ "The above code can be simplified to the following code:" ] }, { "cell_type": "code", "execution_count": 122, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XKPCOnUJMBlu", "outputId": "420eac32-2e40-4893-a9bc-160c29139059" }, "outputs": [ { "data": { "text/plain": [ "['M', 'a', 'c', 'h', 'i', 'n', 'e', 'L', 'e', 'a', 'r', 'n', 'i', 'n', 'g']" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letters = [letter for letter in 'MachineLearning']\n", "\n", "letters" ] }, { "cell_type": "markdown", "metadata": { "id": "UNgH0qhbHWEm" }, "source": [ "\n", "\n", "### 4.3 While loop\n", "\n", "While loop will executes the statement(s) as long as the condition is true.\n", "\n", "Structure of while loop\n", "\n", "\n", "\n", "```\n", "while condition:\n", " statement(s)\n", "```\n", "\n" ] }, { "cell_type": "code", "execution_count": 123, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "STL6xiWTHp3w", "outputId": "c29cccc6-3e0a-4c2b-e625-ffd4d4a1b9a7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a is: 10\n", "a is: 11\n", "a is: 12\n", "a is: 13\n", "a is: 14\n", "a is: 15\n", "a is: 16\n", "a is: 17\n", "a is: 18\n", "a is: 19\n" ] } ], "source": [ "a = 10\n", "while a < 20:\n", " print('a is: {}'.format(a))\n", " a = a + 1" ] }, { "cell_type": "markdown", "metadata": { "id": "i_kah8CoLNf1" }, "source": [ "\n", "\n", "## 5. Functions\n", "\n", "Functions are used to write codes or statements that can be used multiple times with different parameters. \n", "\n", "One fundamental rule in programming is \"DRY\" or Do not Repeat Yourself. Functions will help to not repeat yourself. \n", "\n", "This is how you define a function in Python:\n", "\n", "```\n", "def function_name(parameters):\n", "\n", " \"\"\"\n", " This is Doc String\n", " You can use it to notes about the functions\n", " \"\"\"\n", " statements \n", "\n", " return results\n", "```\n", "* `function_name` is the name of the function. It must not be similar to any built in functions. We will see built in functions later. \n", "* `parameters` are the values that are passed to the function.\n", "* `Doc String` is used to add notes about the function. It is not a must to use it but it is a `good practice`. \n", "\n", "* `return` specify something or value that you want to return everytime you call or run your function. \n", "\n" ] }, { "cell_type": "code", "execution_count": 124, "metadata": { "id": "PaLZl1rAMKFP" }, "outputs": [], "source": [ "# Function to add two numbers and return a sum\n", "\n", "def add_nums(a,b):\n", "\n", " \"\"\"\n", " Function to add two numbers given as inputs\n", " It will return a sum of these two numbers\n", " \"\"\"\n", "\n", " sum = a+b\n", " \n", " return sum" ] }, { "cell_type": "code", "execution_count": 125, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2ZC1shNkMXiW", "outputId": "21911973-ef53-4c0f-d9b6-d7c5ae93d270" }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add_nums(2,4)" ] }, { "cell_type": "code", "execution_count": 126, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7b5NRnJknUoM", "outputId": "43375893-6316-45cb-9e3c-23119adc159f" }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add_nums(4,5)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "q6ZlApF1uOQa", "outputId": "aa353c28-19b5-4327-c845-2e187452ee83" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " Function to add two numbers given as inputs\n", " It will return a sum of these two numbers\n", " \n" ] } ], "source": [ "# Displaying the doc string noted early\n", "\n", "print(add_nums.__doc__)" ] }, { "cell_type": "code", "execution_count": 128, "metadata": { "id": "cS9vtHboMjDM" }, "outputs": [], "source": [ "def activity(name_1, name_2):\n", "\n", " print(\"{} and {} are playing basketball!\".format(name_1, name_2))" ] }, { "cell_type": "code", "execution_count": 129, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "PwWx1JKInAy9", "outputId": "9f01176f-57ad-4cac-d39a-6e46528c8a98" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Chris and Francois are playing basketball!\n" ] } ], "source": [ "activity(\"Chris\", \"Francois\")" ] }, { "cell_type": "code", "execution_count": 130, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LzIRq3C_nPsk", "outputId": "1365d31e-0603-4747-d17d-2935ea9aaead" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kennedy and Kleber are playing basketball!\n" ] } ], "source": [ "activity(\"Kennedy\", \"Kleber\")" ] }, { "cell_type": "markdown", "metadata": { "id": "ljGxEw6onphY" }, "source": [ "As you can see, functions do not need to always have `return`. When you only want to display the customized message (not involving expression), `print()` will be enough. " ] }, { "cell_type": "markdown", "metadata": { "id": "UdBoHK66wJIj" }, "source": [ "\n", "\n", "## 6. Lamdba Functions\n", "\n", "There are times that you want to create anonymous functions. These types of functions will only need to have one expressions. \n" ] }, { "cell_type": "code", "execution_count": 131, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "M7QssT3XyBUa", "outputId": "f3b4bf11-ed3a-4aef-9df2-966a869a0639" }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## Sum of two numbers \n", "\n", "def add_nums(a,b):\n", "\n", " sum = a+b\n", " \n", " return sum\n", "\n", "add_nums(1,3)" ] }, { "cell_type": "markdown", "metadata": { "id": "QZEDAQ91yN2V" }, "source": [ "We can use lambda to make the same function in just one line of code! Let's do it!!" ] }, { "cell_type": "code", "execution_count": 132, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3KuUmPLRyVMv", "outputId": "18f70243-45d5-42ef-eef3-8d08b8c0bbf3" }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum_of_two_nums = lambda c,d: c + d\n", "\n", "sum_of_two_nums(4,5)" ] }, { "cell_type": "markdown", "metadata": { "id": "uyY1LsFnoZdF" }, "source": [ "## \n", "\n", "## 7. Built in Functions\n", "\n", "Python being a high level programming language, it has bunch of built in functions which make it easy to get quick computations done. \n", "\n", "An example of built in functions we used is `len()` which gives the length of the string or the list givhttps://github.com/Nyandwi/python_basics/blob/main/n as the input to it. \n", "\n", "Here is a full preview on all [Python Built in functions](https://docs.python.org/3/library/functions.html).\n", "\n", "![py_functions](https://github.com/Nyandwi/python_basics/blob/main/py-built-func.jpg)" ] }, { "cell_type": "code", "execution_count": 133, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "z9jRgZSSoiNv", "outputId": "73f486fe-5917-4b5e-b31b-59db44852609" }, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# using len() to count the length of the string\n", "\n", "message = 'Do not give up!'\n", "len(message)" ] }, { "cell_type": "code", "execution_count": 134, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DgJ5lG1Vp84a", "outputId": "54354bcf-80a1-4cbd-a59e-59790c4b210c" }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "odd_numbers = [1,3,5,7]\n", "len(odd_numbers)" ] }, { "cell_type": "code", "execution_count": 135, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ak5Xm5vhqGAL", "outputId": "eb679a70-e8f3-40e0-daab-ac03f1722e97" }, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Using max() to find the maximum number in a list\n", "\n", "odd_numbers = [1,3,5,7]\n", "max(odd_numbers)" ] }, { "cell_type": "code", "execution_count": 136, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4_nIijESsGYj", "outputId": "8d176183-a12f-4897-ef41-59834acd237e" }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Using min() to find the minimum number in a list\n", "\n", "min(odd_numbers)" ] }, { "cell_type": "code", "execution_count": 137, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rayIhzYzsNe_", "outputId": "c0780c20-7d6e-48ee-9f23-bf1052a1ac4a" }, "outputs": [ { "data": { "text/plain": [ "[1, 3, 5, 7, 9, 11, 13, 15]" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sorting the list with sorted()\n", "\n", "odd_numbers = [9,7,3,5,11,13,15,1]\n", "\n", "sorted(odd_numbers)" ] }, { "cell_type": "markdown", "metadata": { "id": "WYX_BY1Tu7PJ" }, "source": [ "Let's learn two more useful built functions: they are `map` and `filter`. You can try to explore or use more built in functions on your own. " ] }, { "cell_type": "markdown", "metadata": { "id": "8lOs84gi2ZMW" }, "source": [ "\n", "\n", "### 7.1 Map function \n", "\n", "Map gives you the ability to apply a function to an iterable structures such as list. When used with a list for example, you can apply the function to every element of the list. \n", "\n", "Let's see how it works. \n" ] }, { "cell_type": "code", "execution_count": 138, "metadata": { "id": "W0vcZBUD27ah" }, "outputs": [], "source": [ "def cubic(number):\n", " return number ** 3" ] }, { "cell_type": "code", "execution_count": 139, "metadata": { "id": "bsf6dgts3O2Y" }, "outputs": [], "source": [ "num_list = [0,1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 140, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "alUGYCrp3eh9", "outputId": "a46328b2-f18b-4a3c-a794-c8e298fc290b" }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 8, 27, 64]" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Applying `map` to the num_list to just return the list where each element is cubed...(xxx3)\n", "\n", "list(map(cubic, num_list))" ] }, { "cell_type": "markdown", "metadata": { "id": "Lwoq1ore4gX8" }, "source": [ "\n", "\n", "### 7.2 Filter function\n", "\n", "Let's say that you have a list of numbers and you want to filter the list and remain with odd numbers. Odd number is any number which can not be divided by 2. \n", "\n", "You can develop a function to calculate it but you would have to always pass single value or values but not entire list. \n", "\n", "Using `filter`, you can return `true` for every element of list evaluated by the function. " ] }, { "cell_type": "code", "execution_count": 141, "metadata": { "id": "WYPtPI8H5jt8" }, "outputs": [], "source": [ "def odd_check(number):\n", " \n", " return number % 2 != 0 \n", "\n", "# != is not equal to operation" ] }, { "cell_type": "code", "execution_count": 142, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Yz06GwMG57tp", "outputId": "aa8eeebd-21cd-4a7d-b379-42b21cc0e7eb" }, "outputs": [ { "data": { "text/plain": [ "[1, 5, 7, 9, 11]" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num_list = [1,2,4,5,6,7,8,9,10,11]\n", "\n", "list(filter(odd_check, num_list))" ] }, { "cell_type": "markdown", "metadata": { "id": "Svoes7X-TzZ2" }, "source": [ "\n", "\n", "## 8. More Useful Python Stuff" ] }, { "cell_type": "markdown", "metadata": { "id": "KVoGtQLbUBtw" }, "source": [ "Python is an awesome programming language that has a lot of useful functions.\n", "\n", "Let's see more useful things you may need beyond what's we just saw already." ] }, { "cell_type": "markdown", "metadata": { "id": "mL3B-qcKUT0V" }, "source": [ "### 8.1 List Comprehension" ] }, { "cell_type": "markdown", "metadata": { "id": "6Fj2keTkUaLs" }, "source": [ "List comprehension makes it easy to make a new list from an existing list based on a given conditions. It's very concise and readable.\n", "\n", "Take an example for the following cases. " ] }, { "cell_type": "code", "execution_count": 143, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "koXKO-YBXg7h", "outputId": "df4e9239-6182-4528-9801-f9b07197e50d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4, 6, 8, 10, 12, 14, 16, 18]\n" ] } ], "source": [ "# Given a list of numbers, can you make a new list of even numbers from the list nums?\n", "# Even numbers are numbers divisible by 2, and they give the remainder of 0\n", "\n", "nums = range(1,20)\n", "even_nums = []\n", "\n", "\n", "# A traditional way to do it is:\n", "\n", "for num in nums: \n", " if num % 2 == 0: \n", " even_nums.append(num)\n", "\n", "print(even_nums)" ] }, { "cell_type": "markdown", "metadata": { "id": "On4y8_jRY1pT" }, "source": [ "A more concise and easy way to do that is to use list comprehension. " ] }, { "cell_type": "code", "execution_count": 144, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Qlmc7atXY_ZB", "outputId": "4eeda4bc-6a8c-4e4e-d88e-78863593e347" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4, 6, 8, 10, 12, 14, 16, 18]\n" ] } ], "source": [ "even_nums = [num for num in nums if num % 2 == 0]\n", "print(even_nums)" ] }, { "cell_type": "markdown", "metadata": { "id": "usia15jeZTeu" }, "source": [ "You can see it's pretty simple. And more readable than the former. Let's take another example." ] }, { "cell_type": "code", "execution_count": 145, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "MOLI8nVrZYoW", "outputId": "80f606b8-f33c-41c9-aeea-efef6111cdd9" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Tuesday', 'Thursday']\n" ] } ], "source": [ "days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']\n", "day_T = []\n", "\n", "# Make a list of days that start with `T`\n", "\n", "for day in days: \n", " if day[0] == 'T':\n", " day_T.append(day)\n", "\n", "print(day_T)" ] }, { "cell_type": "markdown", "metadata": { "id": "fwm2skAJa5gn" }, "source": [ "A more concise way to do it would be: " ] }, { "cell_type": "code", "execution_count": 146, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-m-9Aqjka9qU", "outputId": "19b9087d-f9ab-4e91-a762-1fdb8229d84b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Tuesday', 'Thursday']\n" ] } ], "source": [ "day_T = [day for day in days if day[0] == 'T']\n", "print(day_T)" ] }, { "cell_type": "markdown", "metadata": { "id": "iU8ylnxKWmMf" }, "source": [ "### 8.2 Enumerate Function" ] }, { "cell_type": "markdown", "metadata": { "id": "iSej80ASb_eb" }, "source": [ "Enumerate function convert iterable objects into enumerate object. It basically returns a tuple that also contain a counter. \n", "\n", "That's sounds hard, but with examples, you can see how powerful this function is... " ] }, { "cell_type": "code", "execution_count": 147, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WibF3Munca0U", "outputId": "057678f1-9ae9-42a8-d61a-e17e96e6afe8" }, "outputs": [ { "data": { "text/plain": [ "[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "seasons = ['Spring', 'Summer', 'Fall', 'Winter']\n", "\n", "list(enumerate(seasons))" ] }, { "cell_type": "markdown", "metadata": { "id": "a2ISSpE-c9YE" }, "source": [ "As you can see, each element came with index counter automatically. The counter initially start at 0, but we can change it. " ] }, { "cell_type": "code", "execution_count": 148, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VTFXbLr_dc1i", "outputId": "572ef0a5-9807-49b7-cc0b-5e4b1c162b56" }, "outputs": [ { "data": { "text/plain": [ "[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(enumerate(seasons, start=1))" ] }, { "cell_type": "markdown", "metadata": { "id": "T8Z522_3eIPX" }, "source": [ "Here is another example: " ] }, { "cell_type": "code", "execution_count": 149, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "fqeWYORBc8in", "outputId": "14dd00ee-4855-4c60-b497-7617d2fd1440" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 - Rock\n", "1 - Paper\n", "2 - Scissor\n" ] } ], "source": [ "class_names = ['Rock', 'Paper', 'Scissor']\n", "\n", "for index, class_name in enumerate(class_names, start=0):\n", " print(index,'-',class_name)" ] }, { "cell_type": "markdown", "metadata": { "id": "4yCl-dG1eO2C" }, "source": [ "### 8.3 Zip Function" ] }, { "cell_type": "markdown", "metadata": { "id": "uXM_FqareiWw" }, "source": [ "Zip is an incredible function that takes two iterators and returns a pair of corresponsing elements as a tuple. " ] }, { "cell_type": "code", "execution_count": 150, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VPUuYcJJe5Eu", "outputId": "d690b1c1-aa17-4d2f-9c73-4a16b3156832" }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = ['Jessy', 'Joe', 'Jeannette']\n", "role = ['ML Engineer', 'Web Developer', 'Data Engineer']\n", "\n", "zipped_name_role = zip(name, role)\n", "zipped_name_role" ] }, { "cell_type": "markdown", "metadata": { "id": "FKvYS_glfjrs" }, "source": [ "The zip object return nothing. In order to show the zipped elements, we can use a list. It's also same thing for enumerate you saw above." ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "R2r_01K9fylb", "outputId": "79ce3437-2b07-4496-e5df-a225b54f113d" }, "outputs": [ { "data": { "text/plain": [ "[('Jessy', 'ML Engineer'),\n", " ('Joe', 'Web Developer'),\n", " ('Jeannette', 'Data Engineer')]" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(zipped_name_role)" ] }, { "cell_type": "markdown", "metadata": { "id": "ipgT7Bcxp8Bm" }, "source": [ "*This is the end of the lab. The basics of the programming are always good to have, and hopefully this single notebook provided all the necessary things to know about Python.*" ] }, { "cell_type": "markdown", "metadata": { "id": "9kfdbcevQQ6T" }, "source": [ "## [BACK TO TOP](#0)" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "0. Intro to Python for Machine Learning.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 1 }