{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "s1", "content", "l1" ] }, "source": [ "# Intro to Python\n", "\n", "Python is a simple, easy-to-learn, pseudo-code resembling programming language. It is rich with all the features of any object oriented language. Scientists and Mathematicians have been using python since its inception and hence is popular for analytical tasks. Python is also known for its brevity. \n", "\n", "## Why is Python a Favorite of Data Scientists?\n", "\n", "Python is interactive and has a lot of flexibility such as not necessitating declaration of complex types of variables. This helps Data Scientists easily translate their ideas into experiments. Python offers extensive libraries such as pandas and scikit-learn. Any area of Data Science will most likely have libraries in python available. We shall therefore be using Python throughout the course.  Let us refresh some of the concepts in python starting with variables. The Machine Learning/AI Engineers use the object-oriented programming for writing production code. It was developed by Guido van Rossum.\n", "\n", "## Data types\n", "\n", "\n", "\n", "### Variables\n", "\n", "Python variables do not need declaration or specification of data type. For example,\n", "\n", "```\n", "var = 5\n", "var = 5.0\n", "var = \"string\"\n", "```\n", "\n", "are all correct assignments.\n", "\n", "The same variable (value) can be assigned to multiple variables in a sequence as shown below unlike many other languages which allow single variable assignment.\n", "\n", "```\n", "var_a = var_b = var_c = 5\n", "```\n", "\n", "\n", "Let us begin coding with our first exercise. Through out this course pay close attention to the variable names. To pass the exercises, you must use the same variable names provided in the exercise.\n", "\n", "\n", "### Exercise\n", "\n", "Initialize and print \n", "* a) an integer variable (x) with value 4\n", "* b) a floating point variable (y) with value 6.78\n", "* c) a string variable (z) with value 'Hello'\n", "* d) re-assign all three variables with a string 'I love python' and print all three variables.\n", "\n", "The objective of this task is to show the ease of re-assigning a variable in python. The variable does not need to be typecasted and python automatically assigns the data type to the variable based on the value it is assigned. (i.e., If x='Hello', then 'x' is automatically identified by python as a string variable. If 'x' is re-assigned a value of 12.6, say x=12.6, then 'x' is automatically identified as a float)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true, "tags": [ "s1", "ce", "l1" ] }, "outputs": [], "source": [ "# Initialize the variables" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "s1", "l1", "hint" ] }, "outputs": [], "source": [ "# Please make sure you use the variables x,y,z only to do your exercise and pass the exercise. If you use other variables, you might not get right results." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "s1", "l1", "ans" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4 6.78 Hello\n", "I love python I love python I love python\n" ] } ], "source": [ "x = 4\n", "y = 6.78\n", "z = 'Hello'\n", "\n", "print (x, y, z)\n", "\n", "# Re-assigning all three variables\n", "x = y = z = 'I love python'\n", "print (x, y, z)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "s1", "hid", "l1" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "continue\n" ] } ], "source": [ "ref_tmp_var = False\n", "\n", "# ref_assert_var\n", "\n", "ref_assert_var = False\n", "try:\n", " if (x == y) and (x == z):\n", " ref_assert_var = True\n", " ref_tmp_var = True\n", " else:\n", " ref_assert_var = False\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "except Exception:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "\n", "assert ref_tmp_var" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "l2", "content", "s2" ] }, "source": [ "## Python Lists\n", "\n", "Python lists are equivalent to arrays. For example:\n", "\n", "fruits = [\"Mango\", \"Banana\", \"Apple\", \"Orange\"]\n", "\n", "Python lists can have multiple types of objects. For example:\n", "\n", "mixed_list = [\"Apple\", 3, 25.68, \"This is a mixed list\"]\n", "\n", "### Exercise\n", "\n", "Create a list vegetables with Tomato, Spinach, Asparagus, Lettuce and Jalapenos as the elements and print the list out" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": true, "tags": [ "l2", "ce", "s2" ] }, "outputs": [], "source": [ "# Create a list of vegetables\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "l2", "s2", "hint" ] }, "outputs": [], "source": [ "# Use 'vegetables' as the variable name of the list." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "tags": [ "l2", "s2", "ans" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Tomato', 'Spinach', 'Asparagus', 'Lettuce', 'Jalapenos']\n" ] } ], "source": [ "vegetables = [\"Tomato\", \"Spinach\", \"Asparagus\", \"Lettuce\", \"Jalapenos\"]\n", "print (vegetables)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "tags": [ "l2", "hid", "s2" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please follow the instructions given and use the same variables provided in the instructions.\n" ] } ], "source": [ "ref_tmp_var = False\n", "\n", "# ref_assert_var\n", "\n", "try:\n", " import numpy as np\n", "\n", " if np.all(vegetables == [\"Tomato\", \"Spinach\", \"Asparagus\", \"Lettuce\", \"Jalapenos\"]):\n", " ref_assert_var = True\n", " ref_tmp_var = True\n", " else:\n", " ref_assert_var = False\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "except Exception: \n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "assert ref_tmp_var" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "l3", "s3", "content" ] }, "source": [ "## Indexing in a list\n", "\n", "You can access the objects with the indices:\n", "\n", "To access the first object:\n", "\n", "```\n", ">> fruits[0]\n", "'Mango'\n", "```\n", "\n", "To access the last object, esp when the length of the list is large, a useful index method is to use negative index that refers to objects from the end of the list:\n", "\n", "```\n", ">> fruits[-1]\n", "'Orange'\n", "```\n", "\n", "The flexibility of python is that it can contain various objects of different types unlike many other languages:\n", "\n", "```\n", "collection = [\"cost\", 100, \"Apples\", 5.0]\n", "```\n", "\n", "Ref: https://docs.python.org/3/tutorial/datastructures.html\n", "\n", "\n", "\n", "### Exercise\n", "\n", "* Retrieve the third element in the vegetables list created before and assign it to variable, veg_2\n", "* Retrieve the last element using the negative index and assign it to the varible veg_1\n", "* Print out veg_1 and veg_2 variables" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": true, "tags": [ "l3", "s3", "ce" ] }, "outputs": [], "source": [ "#" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "l3", "s3", "hint" ] }, "outputs": [], "source": [ "# Use the indexing to retrieve the variables You don't have to re-initialize the variable 'vegetables' here, as it is already initialized by you in the previous exercise and available in memory for you to reuse through out this lesson.\n", "# Index of a list starts with 0" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [ "l3", "s3", "ans" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Asparagus Jalapenos\n" ] } ], "source": [ "veg_2 = vegetables[2]\n", "veg_1 = vegetables[-1]\n", "print (veg_2, veg_1)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [ "l3", "s3", "hid" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "ref_tmp_var = False\n", "\n", "# ref_assert_var\n", "\n", "try:\n", " if (veg_2 == \"Asparagus\") and (veg_1 == \"Jalapenos\"):\n", " ref_assert_var = True\n", " ref_tmp_var = True\n", " else:\n", " ref_assert_var = False\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "except Exception:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "assert ref_tmp_var" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "l4", "s4", "content" ] }, "source": [ "## List As Stack\n", "\n", "\n", "A list in python can be used as a stack and is flexible to support stack operations such as push and pop.\n", "\n", "#### Push\n", "\n", "Use the .append() command to push objects to a list.\n", "\n", "#### Pop\n", "\n", "Use the .pop() command to pop objects from a list.\n", "\n", "### Exercise\n", "\n", "* Use Stack operations on the vegetables list to first pop Jalapenos off the list, and then add Celery. Print the new list out.\n", "\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true, "tags": [ "l4", "s4", "ce" ] }, "outputs": [], "source": [ "vegetables = [\"Tomato\", \"Spinach\", \"Asparagus\", \"Lettuce\", \"Jalapenos\"]\n", "\n", "# pop jalapenos, push celery and print" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "s4", "l4", "hint" ] }, "outputs": [], "source": [ "# We recommend re-initializing variable 'vegetables' with Tomano, Spinach, Asparagus, Lettuce and Jalapenos at the beginning of this exercise. When you run the code push and pop operations change the data in the vegetables list. If you do not initialize the variable vegetables, when you execute the same code multiple times, you might get different results as the new code gets executed on changed list.\n", "# Re-initialize the vegetables list by going back to the previous exercise and running it again or create a new vegetables list with in this exercise.\n", "# Do this re-inializing step first, before each time you run the pop operation." ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "tags": [ "s4", "l4", "ans" ] }, "outputs": [ { "data": { "text/plain": [ "['Tomato', 'Spinach', 'Asparagus', 'Lettuce', 'Celery']" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vegetables.pop(-1)\n", "vegetables.append(\"Celery\")\n", "vegetables" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "tags": [ "s4", "hid", "l4" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please follow the instructions given and use the same variables provided in the instructions.\n" ] } ], "source": [ "ref_tmp_var = False\n", "\n", "try:\n", " import numpy as np\n", "\n", " if np.all(vegetables == ['Tomato', 'Spinach', 'Asparagus', 'Lettuce', 'Celery']):\n", " ref_assert_var = True\n", " ref_tmp_var = True\n", " else:\n", " ref_assert_var = False\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "except Exception:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "assert ref_tmp_var" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "l5", "content", "s5" ] }, "source": [ "## List As Queue\n", "\n", "A queue is a data structure similar to real-life queue systems. A queue follows FIFO process, i.e. First-in-First-out, for insertion and deletion of elements. For example, lets assume one end of a queue to be front and the other to be rear end. When the first element is inserted in this queue, because it is the only element present, it faces both front and rear end. But when the second element is inserted, this element faces the front end and the first element takes the rear end. From then on, every element that gets added faces the front end and the first element remains at the rear end, till its deleted. After its deletion, the second element takes rear end.\n", "\n", "Python lists can be used as queues as well. The collections module contains a function called 'deque'. It converts any list variable into a 'double-ended queue'. Once the variable is converted into a double ended queue, elements can be added using the 'append' operation and deleted using the 'pop' operation in the FIFO (First-In-First-Out) order. The insertion of an element or the enqueue(append) operation is illustrated in the diagram below:\n", "\n", "\n", "\n", "Below given example code shows 'pop' operation on a list.\n", "\n", "from collections import deque\n", "\n", "fruit_queue = deque([\"Apples\", \"Oranges\", \"Mango\"])\n", "fruit_queue.append(\"Banana\")        \n", "fruit_queue.popleft()  \n", "\n", "The output of the block of code given above is:\n", "'Apples'. The 'popleft' operation works by removing the first inserted element first (FIFO - First In, First Out) as shown below;\n", "\n", "\n", "\n", "### Exercise\n", "\n", "Convert the vegetables list into a queue with the name 'vegetables_queue' and pop the leftmost element. Print the remaining queue." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true, "tags": [ "l5", "ce", "s5" ] }, "outputs": [], "source": [ "from collections import deque\n", "vegetables = [\"Tomato\", \"Spinach\", \"Asparagus\", \"Lettuce\", \"Jalapenos\"]\n", "vegetables.pop(-1)\n", "vegetables.append(\"Celery\")\n", "\n", "#enter your code here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "l5", "s5", "hint" ] }, "outputs": [], "source": [ "# Read the code examples given in 'List as queue' lesson and apply the same" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "tags": [ "l5", "s5", "ans" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "deque(['Spinach', 'Asparagus', 'Lettuce', 'Celery'])\n" ] } ], "source": [ "vegetables_queue = deque(vegetables)\n", "vegetables_queue.popleft()\n", "print(vegetables_queue)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "tags": [ "hid", "l5", "s5" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "continue\n" ] } ], "source": [ "ref_tmp_var = False\n", "\n", "# ref_assert_var\n", "\n", "try:\n", " import numpy as np\n", "\n", " if np.all(vegetables_queue == deque(['Spinach', 'Asparagus', 'Lettuce', 'Celery'])):\n", " ref_assert_var = True\n", " ref_tmp_var = True\n", " else:\n", " ref_assert_var = False\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "except Exception:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "assert ref_tmp_var" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "content", "l6", "s6" ] }, "source": [ "## Strings\n", "\n", "Strings in python are easy to work with. Here are the commonly used string operations:\n", "\n", "### Concatenation:\n", "\n", "To concatenate two strings, \"chair\" and \"table\", you can use:\n", "\n", "furniture = \"chair\" + \", table\"\n", "\n", "### Other operations:\n", "\n", ".lower(), .upper() to lower & upper case the strings\n", "\n", "Ref: https://docs.python.org/3/library/string.html\n", "\n", "### Regular Expressions:\n", "\n", "You can use 're' package to work with regular expressions. For example the following code can be used to find the position of the string\n", "\n", "```\n", "import re -- Importing re library\n", "mangoes = re.search(\"Mango\", \"This is a Mango\") -- Searching for the first occurance of string Mango\n", "mangoes.group(0)\n", "print (mangoes)\n", "<_sre.SRE_Match object; span=(10, 15), match='Mango'> -- Printing the search results with 10 as the starting index of Mango and 15 as the ending index of the mango in the input string\n", "```\n", "\n", "will contain the matches.\n", "\n", "\n", "Ref: https://docs.python.org/3/library/re.html\n", "\n", "### Exercise\n", "\n", "* a) Given two strings a = \"hello! How are you?\" and b = \"how can I help you? \", concatenate them into a new string c. Print c\n", "* b) Use regular expressions to search for 'hel' in string c and assign it to variable hel_strings" ] }, { "cell_type": "code", "execution_count": 111, "metadata": { "tags": [ "ce", "l6", "s6" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello! How are you? how can I help you?\n" ] } ], "source": [ "a = \"hello! How are you? \"\n", "b = \"how can I help you?\"\n", "c = a + b\n", "print (c)\n", "\n", "# regular expression" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "tags": [ "l6", "s6", "hint" ] }, "outputs": [], "source": [ "# After successful execution of this exercise, we encourage you to try to find the index of second 'hel' for fun and practice." ] }, { "cell_type": "code", "execution_count": 112, "metadata": { "tags": [ "l6", "s6", "ans" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hel\n" ] } ], "source": [ "import re\n", "hel_strings = re.search(\"hel\", c)\n", "print(hel_strings.group(0))" ] }, { "cell_type": "code", "execution_count": 113, "metadata": { "tags": [ "hid", "s6", "l6" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "ref_tmp_var = False\n", "\n", "# ref_assert_var\n", "\n", "try:\n", " if(hel_strings.group(0) == \"hel\"):\n", " ref_assert_var = True\n", " ref_tmp_var = True\n", " else:\n", " ref_assert_var = False\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "except Exception:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "assert ref_tmp_var" ] } ], "metadata": { "anaconda-cloud": {}, "executed_sections": [], "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.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }