{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 3 Tutorial\n",
    "\n",
    "Author: Raquel Aoki\n",
    "\n",
    "## Summary\n",
    "\n",
    " 1. Print\n",
    " 2. Simple Math \n",
    " * If, else and elif Statements\n",
    " * Lists \n",
    " * Loops \n",
    " * Dictionaries \n",
    " * Sets \n",
    " * Functions\n",
    " * Modules\n",
    " \n",
    " \n",
    " ## Requirements \n",
    " Jupyter Notebook \n",
    " \n",
    " ## References: \n",
    "* https://github.com/mGalarnyk/Python_Tutorials\n",
    "* https://www.tutorialspoint.com/python/index.htm\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1\n",
    "#To execute the code in a cell type shift+enter \n",
    "print(\"Hello World\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#2\n",
    "#Exploring other alternatives\n",
    "firstVariable = 'HeLLo WoRLD'\n",
    "\n",
    "#You can press tab to open a list with functions and variables names\n",
    "print(firstVariable)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Simple Math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#3\n",
    "#Addition (+), Subtraction (-), product (*), division (/)\n",
    "3*3-(2+2)/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#4\n",
    "#Exponentiation \n",
    "3**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#5\n",
    "#Modulo \n",
    "print(\"Interger division of 7 by 3 is\",7//3,\"and the remainder of the division is\",7%3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. If, else and elif Statements\n",
    "\n",
    "Comparison Operators: <, <=, >, >=, ==, != \n",
    "Logical Operators: and, or, not"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#6\n",
    "#if: check if the statement. If it is True, do it; if it is False, don't do it.\n",
    "NumberTree = 3\n",
    "if NumberTree == 3:\n",
    "    print(NumberTree == 3)\n",
    "    print(NumberTree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#7\n",
    "#Nothing will be produced because it is a False Statement\n",
    "if NumberTree == 5:\n",
    "    print(NumberTree == 5)\n",
    "    print(NumberTree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#8\n",
    "#We can make it print something by additing an else statement\n",
    "if NumberTree == 5:\n",
    "    print(\"if output:\",NumberTree == 5)\n",
    "    print(\"if output:\",NumberTree)\n",
    "else: \n",
    "    print(\"else output:\",NumberTree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#9\n",
    "#Logical Operators: allows us to have if statements with more than one condition\n",
    "\n",
    "#This if statement...  \n",
    "if NumberTree>2:\n",
    "    if NumberTree<4:\n",
    "        print(\"Number 3 is between 2 and 4\")\n",
    "    \n",
    "#...is equal to\n",
    "if NumberTree>2 and NumberTree<4:\n",
    "    print(\"Number 3 is between 2 and 4\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#10\n",
    "if NumberTree == 3 or NumberTree == 5: \n",
    "    print('The variable NumberTree is equal 3 OR 5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#11\n",
    "#elif: check multiple expressions for a True statement. It MUST be after an if statement. \n",
    "if NumberTree==1:\n",
    "    print(\"NumberTree is equal to 1\")\n",
    "elif NumberTree==2:\n",
    "    print(\"NumberTree is equal to 2\")\n",
    "elif NumberTree==3:\n",
    "    print(\"NumberTree is equal to 3\")\n",
    "elif NumberTree==4:\n",
    "    print(\"NumberTree is equal to 4\")\n",
    "else:\n",
    "    print(\"NumberTree is bigger than 4\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#12\n",
    "#Creating a list: the elements needs to be inside [] and separated by ','\n",
    "mylist = [4,9,15,25]\n",
    "print('mylist:',mylist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#13\n",
    "#Use the command len() to check the size of the list\n",
    "print('Size of mylist:',len(mylist))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#14\n",
    "#The index of lists in Python starts from 0\n",
    "print('First element:',mylist[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#15\n",
    "#You can also use negative index, the last position starts on -1\n",
    "print('Last element:',mylist[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#16\n",
    "#Use index_start:index_end to slice the list\n",
    "#mylist[0:2] -> elements on my list starting at position 0 and ending on position before 2 (aka position 1)\n",
    "print(mylist[0:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#17\n",
    "#another common slices options\n",
    "print(mylist[:2])\n",
    "print(mylist[2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#18\n",
    "#Updating values\n",
    "print('Old Values:', mylist)\n",
    "mylist[2] = 16\n",
    "print('New Values:', mylist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#19\n",
    "#Counting how my times an element appears on a list\n",
    "print(\"Count of elements 4 in mylist:\",mylist.count(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#20\n",
    "#We can also use the method sort() to sort the list\n",
    "mylist = [16,9,25,4]\n",
    "print(\"random:\", mylist)\n",
    "mylist.sort()\n",
    "print(\"sorted:\",mylist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#21\n",
    "#You can mix different datatypes in a list\n",
    "mylist[3] = \"5*5\"\n",
    "print('Mix datatypes:',mylist)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#22\n",
    "#Remove an item from a list -> .remove(element)\n",
    "mylist.remove('5*5')\n",
    "print(\"Removing the 5*5 element:\", mylist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#23\n",
    "#Adding an item in a list -> .append(new_element)\n",
    "mylist.append(5*5)\n",
    "print(\"Adding the 5*5 element:\",mylist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#24\n",
    "#Removing an item using index -> .pop(index)\n",
    "mylist.pop(2)\n",
    "print('Removing element on index 2:', mylist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#25\n",
    "#Adding an element using index -> .insert(index,element)\n",
    "mylist.insert(2,16)\n",
    "print('Adding element 16 on index 2:', mylist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Loops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#26\n",
    "#Loops in lists: \n",
    "print('Option 1')\n",
    "for item in mylist:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#27\n",
    "print('Option 2')\n",
    "#Loops using index:\n",
    "print(\"My range is:\",range(len(mylist)))\n",
    "for item in range(len(mylist)):\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#28\n",
    "#Mixing loops and if statements \n",
    "for item in mylist:\n",
    "    if item%2==0:\n",
    "        print(item, \"is even\")\n",
    "    else: \n",
    "        print(item, \"is odd\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#29\n",
    "#while method repeatedly executes the statements as long as the condition is True\n",
    "#1) Important define the start values of variables used in conditions \n",
    "#2) Remember to check if your condition can become False. Otherwise, you can have an infinity loop \n",
    "\n",
    "variable = 1\n",
    "while variable < 5 : \n",
    "    print(\"Count:\",variable)\n",
    "    variable = variable+1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Dictionaries\n",
    "\n",
    "Dictionaries are used to make keys into values (lists, functions, strings)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#30\n",
    "#Defining a dictionaty name = {key: value}\n",
    "mydictionary = {'yellow':'sun', \n",
    "                'blue': 'sea', \n",
    "                'green': 'leaf', \n",
    "                'red': 'fire'}\n",
    "print(mydictionary)\n",
    "\n",
    "#Accessing values using key\n",
    "print('key = Yellow: ',mydictionary['yellow'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#31\n",
    "#Updating a key's value\n",
    "print(\"original key = blue: \", mydictionary['blue'])\n",
    "mydictionary['blue'] = 'ocean'\n",
    "print(\"New key = blue: \", mydictionary['blue'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#32\n",
    "#Adding more keys \n",
    "print('First dictionary: ', mydictionary)\n",
    "mydictionary.update({'white':'snow'})\n",
    "print('New dictionary: ', mydictionary)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#33\n",
    "#Removing a key\n",
    "del mydictionary['white']\n",
    "print('Removing a key: ', mydictionary)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Sets\n",
    "\n",
    "Sets are mutable like lists, but unlike lists and tuples, sets don't have multiple occurrences of the same element. \n",
    "The elements inside a set are always sorted. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#34\n",
    "myset = set([1,2,2,3,3,3,4,4,4,4])\n",
    "print('Creating a set from a list: ',myset)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#35\n",
    "#Creating a set {} //// Remember: Creating a list [] and creating a tuple ()\n",
    "myset = {2,4,3,1}\n",
    "print(type(myset))\n",
    "print(myset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#36\n",
    "#adding elements \n",
    "myset.add(5)\n",
    "print('adding elements', myset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#37\n",
    "#removing elements \n",
    "myset.remove(1)\n",
    "print('removing elements', myset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#38\n",
    "#Sets: Union, Intersection, Difference, Symmetric Difference\n",
    "set1 = {1,3,5,7,9}\n",
    "set2 = {2,4,6,8,10}\n",
    "\n",
    "#Union between 2 sets\n",
    "print(\"Union\", set1.union(set2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#39\n",
    "#Intersection between 2 sets\n",
    "print('Intersection',set1.intersection(set2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#40\n",
    "#Difference set1 - set2\n",
    "print('Difference', set1.difference(set2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#41\n",
    "#Symmetric Difference: elements present in only one of the sets\n",
    "print('Symmetric Difference', set1.symmetric_difference(set2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9. Functions \n",
    "\n",
    "Block of reasable code. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#42\n",
    "#Defining a function\n",
    "def myfunction():\n",
    "    print(\"My first function\")\n",
    "\n",
    "#Calling a function \n",
    "myfunction()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#43\n",
    "#We can also add parameters \n",
    "def coffeefunction(parameter1, parameter2):\n",
    "    print(\"Hello, Can I get a\", parameter1, parameter2, \",please?\")\n",
    "\n",
    "coffeefunction(\"large\", \"coffee\")    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#44\n",
    "#The function can also return values\n",
    "def coffeefunction(size, item): #size: small or large; item = coffee, tea or hot chocolate\n",
    "    price = 0\n",
    "    if size == \"small\":\n",
    "        if item == \"coffee\":\n",
    "            price = 2.50\n",
    "        elif item == \"tea\":\n",
    "            price = 2.00\n",
    "        else: \n",
    "            price = 2.75\n",
    "    else: \n",
    "        if item == \"coffee\":\n",
    "            price = 3.00\n",
    "        elif item == 'tea':\n",
    "            price = 2.25\n",
    "        else: price = 3.25\n",
    "    return price\n",
    "\n",
    "print('Small coffee price:',coffeefunction('small', 'coffee'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#45\n",
    "print('Large tea price',coffeefunction('large', 'tea'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#46\n",
    "size = 'large'\n",
    "item = 'hot chocolate'\n",
    "print('Large hot chocolate price',coffeefunction(size,item))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10. Modules\n",
    "\n",
    "Modules are a collection of functions and codes that we can import to use its functionalities. \n",
    "Modules are also know as libraries.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#47\n",
    "#importing a module called pandas as pd \n",
    "#pandas is Python Data Analysis Library https://pandas.pydata.org/\n",
    "import pandas as pd \n",
    "\n",
    "data = pd.DataFrame({'col1': [1,1,2,3,5,8,13,21],\n",
    "                      'col2': ['a','b','c','d','e','f','g','h']})\n",
    "print('Size(rows x columns):',data.shape)\n",
    "print('First 5 lines\\n',data.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises \n",
    "\n",
    "If, else and elif Statements\n",
    "Lists\n",
    "Loops\n",
    "Dictionaries\n",
    "Sets\n",
    "Functions\n",
    "Modules\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider the following flights prices departing from Vancouver: \n",
    "\n",
    "| Destination   | Price            | Continents   |\n",
    "| ------------- |:-------------:   |------------- |\n",
    "| London        | $\\$659   $       |Europe        |  \n",
    "| Paris         |$\\$968    $       |Europe        | \n",
    "| New York      | $\\$417  $        |America       | \n",
    "| Tokyo         |$\\$1010  $        |Asia          |\n",
    "| Melbourne     | $\\$1383 $        |Oceania       |\n",
    "|Rio de Janeiro | $\\$796 $         |America       |\n",
    "| Montreal      | $\\$533$          |America       | \n",
    "\n",
    "1) Create 3 lists ('destination', 'price' and 'continents') and add the values shown on the table above. \n",
    "\n",
    "2) Update the price of the New York flight from $\\$417$ to $\\$525$.\n",
    "\n",
    "3) We are interest only in international flights, so you should remove the Montreal flight and its price from the 3 lists.\n",
    "\n",
    "4) Using a loop, print all destinations under $\\$1000$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5) Using a loop and ONE if statement print all destinations with flights between $\\$500$  and  $\\$700$.\n",
    "\n",
    "6) Print the set of continents (using the set()).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7) Make a dictionary using the continents as key and destinations as values. Print the dictionary.\n",
    "\n",
    "8) Create a function that receive as parameter 3 lists and print row by row in the format 'Destination-Continent: Price'. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9) Working with the pandas library, create a pandas dataframe with the destination, price and continents lists. Print the dataframe. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
