{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "eudDL43_A155",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Astronomical data analysis using Python\n",
    "====================================\n",
    "\n",
    "Lecture 3\n",
    "---------------------\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Our first program - introduced in the previous lecture\n",
    "------------------------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n",
      "Sum, Difference =  8 -2\n",
      "Quotient and Remainder =  0.6 3\n"
     ]
    }
   ],
   "source": [
    "a = 3\n",
    "b = 5\n",
    "c = a+b\n",
    "d = a-b\n",
    "q, r = a/b, a%b # Yes, this is allowed!\n",
    "\n",
    "# Now, let's print!\n",
    "print (\"Hello World!\") # just for fun\n",
    "print (\"Sum, Difference = \", c, d)\n",
    "print (\"Quotient and Remainder = \", q, r)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ai0osL1GA15-",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Our First Program - Rewritten!\n",
    "------------------------------\n",
    "\n",
    "Let us introduce the following modifications to the program.\n",
    "\n",
    "* We use floats instead of ints.\n",
    "* We accept the numbers from the user instead of \"hard coding\" them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 280
    },
    "id": "X6ea5lr4A15_",
    "outputId": "b82ec197-ce4a-4fc0-922b-15f0c3cdb235",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter number 1: 3\n",
      "Please enter number 2: 5\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'str' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-65-8d9a5e1f3c2a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Please enter number 2: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0md\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m//\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'str' and 'str'"
     ]
    }
   ],
   "source": [
    "# Modified first program.\n",
    "a = input(\"Please enter number 1: \")\n",
    "b = input(\"Please enter number 2: \")\n",
    "\n",
    "c, d = a+b, a-b\n",
    "q, i = a/b, a//b\n",
    "\n",
    "print (c,d,q,i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7V-T0CGuA16C",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "What happened?\n",
    "--------------\n",
    "\n",
    "* Anything input through the keyboard using input() is ... a \"string\".\n",
    "* Strings support addition (concatenation) but nothing else.\n",
    "\n",
    "So what should we do?\n",
    "---------------\n",
    "\n",
    "* \"3.0\" is a string. 3.0 is a float!\n",
    "* To convert \"3.0\" into a float, we use a simple function - float(\"3.0\")\n",
    "\n",
    "So, let's rewrite our program!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "iv63FWotA16D",
    "outputId": "f7142fbd-7c29-4477-89d2-f22c01f5f8ae",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter Number 1: 3\n",
      "Enter Number 2: 5\n",
      "Addition = 8.000000, Difference = -2.000000 \n",
      "Quotient = 0.600000, Floor division quotient  = 0.000000\n"
     ]
    }
   ],
   "source": [
    "a = float( input(\"Enter Number 1: \") ) # We are nesting functions here.\n",
    "b = float( input(\"Enter Number 2: \") )\n",
    "\n",
    "c,d = a+b, a-b\n",
    "q,i = a/b, a//b # a//b is the floor division operator\n",
    "\n",
    "print (\"Addition = %f, Difference = %f \" % (c,d))\n",
    "print (\"Quotient = %f, Floor division quotient  = %f\" % (q,i))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ZTPYxTGaA16D",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "The output looks ugly. Wish I could control the number of decimal places..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "l0N2n5WyA16E",
    "outputId": "459217ec-0fb4-4112-dc28-c8491b19147c",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter Number 1: 3\n",
      "Enter Number 2: 5\n",
      "Addition = 8.00, Difference = -2.00 \n",
      "Quotient = 0.60, Floor division quotient = 0.00\n"
     ]
    }
   ],
   "source": [
    "a = float( input(\"Enter Number 1: \") )\n",
    "b = float( input(\"Enter Number 2: \") )\n",
    "\n",
    "c,d = a+b, a-b\n",
    "q,i = a/b, a//b\n",
    "\n",
    "print(\"Addition = %.2f, Difference = %.2f \" % (c,d))\n",
    "print(\"Quotient = %.2f, Floor division quotient = %.2f\" % (q,i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "qnp2OgUAA16F",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Ah! now, that's much better."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EvA0c5HyA16G",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "String Formatting\n",
    "----------\n",
    "\n",
    "We have seen a powerful of constructing strings in the previous example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "0d_Oh5S7A16G",
    "outputId": "488d72a7-5625-487b-a238-b77acca8f9d6",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Addition = 8.00, Difference = -2.00 \n"
     ]
    }
   ],
   "source": [
    "print (\"Addition = %.2f, Difference = %.2f \" % (c,d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "m_q7pzhTA16H",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "C / FORTRAN users amongst you will immediately understand this method of string construction.\n",
    "\n",
    "Python supports this and its own way of string formatting.\n",
    "-------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "id": "0VRpFQNsA16I",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "gal_name = \"NGC 7709\"; int_bmagnitude = 13.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "id": "jc3HmdpBA16I",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "statement1 = \"The galaxy %s has an integrated \\\n",
    "B-band magnitude of %.2f\" % (gal_name, int_bmagnitude)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "id": "MhFErU9_A16J",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "statement2 = \"The galaxy {0:s} has an integrated \\\n",
    "B-band magnitude of {1:.2f}\".format(gal_name, int_bmagnitude)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "id": "ngucooc0A16J",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "statement3 = \"The galaxy {name:s} has an integrated \\\n",
    "B-band magnitude of {mag:.2f}\".format(name=gal_name, mag=int_bmagnitude)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "gREaBvSEA16K",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "All the above statements are equivalent!\n",
    "------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "VcYaDMb7A16K",
    "outputId": "f3853b5d-d161-46a7-ad7d-75a8ff2dbe6d",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The galaxy NGC 7709 has an integrated B-band magnitude of 13.60 \n",
      " The galaxy NGC 7709 has an integrated B-band magnitude of 13.60 \n",
      " The galaxy NGC 7709 has an integrated B-band magnitude of 13.60 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "print (statement1,\"\\n\", statement2, \"\\n\", statement3, \"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "LrX7Xq4ZA16L",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "You can choose whichever method you like!\n",
    "\n",
    "As a former C/C++ user, I tend to use the first method.\n",
    "\n",
    "But ... second and third methods are more \"Pythonic\". If you don't have previous experience with the C type formatting use one of the more Pythonic ways."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "r85Y2kNyA16L",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Raw Strings\n",
    "------------\n",
    "\n",
    "* We have seen the three methods of string declaration. \n",
    "* We have also seen string formatting.\n",
    "* String formatting taught us that symbols like { or % have special meanings in Python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "zxHTPu4jA16L",
    "outputId": "7a9bcd07-6d38-4fa5-85eb-7cbe2f3666cd",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Here is a percentage sign % and a brace }\n"
     ]
    }
   ],
   "source": [
    "# There is a special way of declaring strings where\n",
    "# we can ask Python to ignore all these symbols.\n",
    "raw_string = r\"Here is a percentage sign % and a brace }\"\n",
    "print (raw_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "gfduv2gAA16M",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Usefulness of Raw Strings - Example\n",
    "-------\n",
    "\n",
    "* Typically, when we make plots and set labels, we would like to invoke a LaTeX parser.\n",
    "* This would involve a lot \\ $ and {}. \n",
    "\n",
    "In such cases, it's best to use raw strings.\n",
    "\n",
    "    plt.xlabel(r\" \\log \\rm{F}_v\")\n",
    "    \n",
    "Other examples with special characters include writing Windows file paths, XML code, HTML code, etc."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "G8L9YdzRA16M",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Conditionals\n",
    "------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "5XKcjDUsA16N",
    "outputId": "20ad21bf-bd9e-4949-ecd3-17931d9627a1",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter number: 3\n",
      "3 is odd!\n"
     ]
    }
   ],
   "source": [
    "num = int(input(\"Enter number: \") )\n",
    "if num %2 == 0:\n",
    "    print (\"%d is even!\" % num)\n",
    "else:\n",
    "    print (\"%d is odd!\" % num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "JlK5XhVlA16N",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "You will use conditionals a lot in your data analysis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 262
    },
    "id": "uWVdHH-PA16O",
    "outputId": "acf83481-692e-46e9-8758-ee2c2fe0fcd7",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter choice [1 or 2]: 2\n",
      "Model 2 fitted.\n"
     ]
    }
   ],
   "source": [
    "model_choice = int(input( \"Enter choice [1 or 2]: \") )\n",
    "spectrum = 3 # In a realistic case, this will be some complicated object.\n",
    "\n",
    "if model_choice == 1:\n",
    "    #model1(spectrum)\n",
    "    print (\"Model 1 fitted.\")\n",
    "elif model_choice == 2:\n",
    "    #model2(spectrum)\n",
    "    print (\"Model 2 fitted.\")\n",
    "else:\n",
    "    print (\"Invalid model entered.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "h7kKIwnPA16P",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "What do you notice apart from the syntax in the above example?\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "OuBxYPTfA16P",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Indentation - A Vital Part of the Pythonic Way\n",
    "--------------\n",
    "\n",
    "Be it the if-block illustrated above or the loops or the functions (to be introduced soon), indentation is at the heart of the Python's way of delimiting blocks!\n",
    "\n",
    "Function definitions, loops, if-blocks - nothing has your typical boundaries like { } as in C/C++/Java.\n",
    "\n",
    "The \"level of the indentation\" in the only way to define the scope of a \"block\"."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "YEr6KNyGA16P",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "In support of indentation\n",
    "------\n",
    "\n",
    "Look at the following C-like code.\n",
    "\n",
    "    if (x>0)\n",
    "        if (y>0)\n",
    "            print \"Woohoo!\"\n",
    "    else\n",
    "            print \"Booboo!\"\n",
    "            \n",
    "Which \"if\" does the \"else\" belong to?\n",
    "\n",
    "In C and C-like languages, the braces {}s do the marking, the indentation is purely optional. In Python, indentation levels determine scopes. In Python the \"the else\" belongs to \"if (x>0)\". \n",
    "\n",
    "Python forces you to write clean code! (Obfuscation lovers, go take a jump!)\n",
    "\n",
    "Use either spaces or tabs (don't ever ever mix them) and use them consistently. I strongly recommend using 4 spaces for each level of indentation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "CasnbgxNA16Q",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Wrapping up if-elif-else\n",
    "-------\n",
    "\n",
    "The general syntax:\n",
    "\n",
    "    if <condition>:\n",
    "        do this\n",
    "        and this\n",
    "    elif <condition>:\n",
    "        this\n",
    "        and this\n",
    "    ...\n",
    "    else:\n",
    "        do this \n",
    "        and this\n",
    "     "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "uPLqV1FWA16Q",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Conditions are anything that return True or False.\n",
    "------\n",
    "\n",
    "* == (equal to)\n",
    "* !=\n",
    "* \\>\n",
    "* \\>=\n",
    "* < \n",
    "* <= \n",
    "\n",
    "You can combine conditionals using \"logical operators\"\n",
    "\n",
    "* and\n",
    "* or\n",
    "* not"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "yiVYyZawA16Q",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "The Boolean Data Type\n",
    "--------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "N9KUiHFsA16R",
    "outputId": "27fb4f6f-5c3f-4cde-f22a-eb0c62caf614",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This comes on screen.\n",
      "This also comes on screen.\n"
     ]
    }
   ],
   "source": [
    "a = True\n",
    "b = False\n",
    "\n",
    "if a:\n",
    "    print (\"This comes on screen.\")\n",
    "\n",
    "if b:\n",
    "    print (\"This won't come on screen.\")\n",
    "    \n",
    "if a or b:\n",
    "    print (\"This also comes on screen.\")\n",
    "    \n",
    "if a and b:\n",
    "    print (\"This won't come on screen either.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "zAPBbQtSA16R",
    "outputId": "efae56c3-2700-4afc-da86-b4df75f15f8c",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a) # To check type of object."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "hT5VIRS2A16R",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Almost all other types have a Boolean Equivalent\n",
    "------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "6sIEwLnLA16S",
    "outputId": "63736e36-8737-43d9-f275-d48d228aa0eb",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 1\n",
    "b = 0\n",
    "\n",
    "if a:\n",
    "    print (\"Hello!\")\n",
    "if b:\n",
    "    print (\"Oh No!\")\n",
    "    \n",
    "type(a)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "axffo2llA16S",
    "outputId": "e30ccc9a-f6db-45ee-93c3-29efe6c1b911",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Will be printed.\n"
     ]
    }
   ],
   "source": [
    "s1 = \"\"; s2 = \"Hello\" # s1 is an empty string\n",
    "\n",
    "if s1:\n",
    "    print (\"Won't be printed.\")\n",
    "if s2:\n",
    "    print (\"Will be printed.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "This is bad Python style because remember that explicit is better than implicit. Use an expression that evaluates to a boolean instead. Keep your programs readable."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "aj75rPnGA16S",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Conditional Expression\n",
    "-------\n",
    "\n",
    "Consider..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "id": "aFLx3oosA16T",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "if 5 > 6:\n",
    "    x = 2\n",
    "else:\n",
    "    x = 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "id": "St9kX66EA16T",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "y = 2 if 5 > 6 else 3 # if else block in one line is allowed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "l_urXUCVA16T",
    "outputId": "ba44f7a5-e65c-4ec4-c940-f28184cee5b7",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 3\n"
     ]
    }
   ],
   "source": [
    "print (x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "NnF2zI1JA16U",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "A Second Plunge into the Data Types\n",
    "===========\n",
    "\n",
    "The two other data types we need to know:\n",
    "\n",
    "* Lists \n",
    "* Dictionaries\n",
    "\n",
    "Data Types I will not cover (formally):\n",
    "\n",
    "* Tuples (immutable lists!)\n",
    "* Sets (key-less dictionaries!)\n",
    "* Complex Numbers\n",
    "* Fractions\n",
    "* Decimals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "zwFFCw7gA16U",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Lists\n",
    "-----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "id": "mfGima1-A16U",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "a = [1,2,3,4] # simple ordered collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "id": "ueky_qt3A16U",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "b = [\"Hello\", 45, 7.64, True] # can be heterogeneous"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "csRX423tA16U",
    "outputId": "f3bb19c6-5637-454f-a583-cb54fe9975bf",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 4, [2, 3])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0], a[-1], a[1:3] # All \"sequence\" operations supported."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "id": "2qAiDsDeA16V",
    "outputId": "745a547e-5f0a-48f4-dbb2-6aeafe73bb04",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e'"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0][1] # 2nd member of the 1st member"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "id": "PZQz0TqvA16V",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "a = [ [1,2,3] , [4,5,6] , [7,8,9] ] # list of lists allowed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "GfoDTS76A16V",
    "outputId": "b789b0a6-6c5c-4ff5-9b88-e37ae6e1c5a0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2][1] # Accessing elements in nested structures."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "zerKPIDzA16W",
    "outputId": "285fadda-6e2c-43ff-cdab-6738f5550093",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1,3,4] + [5,6,7] # Support concatenation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "1lFG0rfZA16W",
    "outputId": "4231cca0-fe47-49fe-a249-caa7e4338ae5",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 6, 8, 1, 6, 8, 1, 6, 8]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1,6,8] * 3 # Repetition (like strings)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8hZkuRJYA16W",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Lists are Mutable! (Strings are not!)\n",
    "----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "id": "4C5frXMzA16W",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "a = [1,4,5,7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "mJqUc-pRA16X",
    "outputId": "676ab222-7d18-42c1-c398-b0a6d7aeb6cd",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 5, 7]\n"
     ]
    }
   ],
   "source": [
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "id": "yH8loIolA16b",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "a[2] = 777 # set third element to 777"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "f-fmrb4EA16c",
    "outputId": "c1077cc2-415b-46d5-a29f-822a837e61c1",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 777, 7]\n"
     ]
    }
   ],
   "source": [
    "print (a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "MSOG_286A16c",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "List Methods\n",
    "----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "s3H6022FA16c",
    "outputId": "801da5ac-c9e1-415d-fc7b-328376b395db",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5]\n"
     ]
    }
   ],
   "source": [
    "a = [1,3,5]\n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "uSoSel5kA16d",
    "outputId": "bae4fffe-f29d-41f0-be76-b74a45e58309",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7]\n"
     ]
    }
   ],
   "source": [
    "a.append(7) # adds an element to the end\n",
    "print (a) # the list has changed (unlike string methods!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "id": "4KxTDi30A16d",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9, 11, 13]\n"
     ]
    }
   ],
   "source": [
    "a.extend([9,11,13]) # concatenates a list at the end\n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "id": "8f80xd3SA16d",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9, 11]\n"
     ]
    }
   ],
   "source": [
    "a.pop() # Removes one element at the end.\n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "id": "ilZCq8USA16e",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 7, 9, 11]\n"
     ]
    }
   ],
   "source": [
    "a.pop(2) # Removes 3rd element. \n",
    "print (a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "056OlUDEA16e",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Don't Forget!!!\n",
    "-----\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "4YvwXXovA16e",
    "outputId": "0d6fb6c0-3c40-4c70-d469-8a560e890f36",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']\n"
     ]
    }
   ],
   "source": [
    "print (dir(a)) # list of methods for a list \"a\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "a6Q97rYsA16e",
    "outputId": "6c295bac-1807-438c-8338-d6841415ca89",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function sort:\n",
      "\n",
      "sort(*, key=None, reverse=False) method of builtins.list instance\n",
      "    Sort the list in ascending order and return None.\n",
      "    \n",
      "    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      "    order of two equal elements is maintained).\n",
      "    \n",
      "    If a key function is given, apply it once to each list item and sort them,\n",
      "    ascending or descending, according to their function values.\n",
      "    \n",
      "    The reverse flag can be set to sort in descending order.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.sort)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "wN1t_euxA16f",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Implications of Mutability\n",
    "----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "9DAlqj_pA16f",
    "outputId": "5a2fd795-634b-4696-f0e6-7b53b844b8dc",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "l = [1,2,3,4]\n",
    "m = l\n",
    "\n",
    "l.append(5)\n",
    "print (l)\n",
    "print (m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "auD5Nr7lA16f",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "l and m point to the same object. When the object mutates, whether you refer to it using l or m, you get the same mutated object."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TUtRr_bDA16f",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "How do I make a copy then?\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "3rigVF6nA16f",
    "outputId": "69cdaea1-f260-4fca-bbd2-061f6c03a610",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "l = [1,2,3,4]\n",
    "m = l[:] \n",
    "\n",
    "l.append(5)\n",
    "print (l)\n",
    "print (m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "6GBBXIZ7A16g",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Python has a module called \"copy\" available for making copies. Refer to the standard library documentation for details."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "dmoKFrsTA16g",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Dictionaries\n",
    "----\n",
    "\n",
    "* Imagine a list as a collection of objects obj0, obj1, obj2 ... \n",
    "* First object has a location 0, second 1 ... \n",
    "* Now, imagine renaming location 0 as \"something\", location 1 as \"somethingelse\" ...\n",
    "* Earlier, you accessed objects at numbered locations a[0].\n",
    "* Now, you access objects by specifying location labels a[\"something\"]\n",
    "\n",
    "Let's see this at work."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Amcw98N2A16g",
    "outputId": "7953d6cc-f7bb-48bb-ad4d-87b6c5970413",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "d1 = { \"a\" : 3, \"b\" : 5}\n",
    "print (d1[\"a\"])\n",
    "print (d1[\"b\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "W2AXXMttA16g",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "\"a\", \"b\" are called keys and 3,5 are called values. So formally, a dictionary is a collection of key-value pairs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "id": "RZOjVbQBA16g",
    "outputId": "44077c4c-d31a-4071-a3ee-417ac6f67232",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 5, 'c': 7}\n"
     ]
    }
   ],
   "source": [
    "d1[\"c\"] = 7 # Since \"c\" does not exist, a new key-value pair is made.\n",
    "d1[\"a\"] = 1 # Since \"a\" exists already, its value is modified.\n",
    "print (d1) # the ordering of key-value pairs in the dictionary is not guaranteed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "oEJoF8tgA16h",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Dictionary Methods\n",
    "----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "id": "TyoqqQYgA16h",
    "outputId": "aa1f3fb3-6c51-4eeb-b570-e49a29f45f26",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['a', 'b', 'c'])\n"
     ]
    }
   ],
   "source": [
    "keys = d1.keys() # Returns a list of all keys which is stored in \"keys\".\n",
    "print (keys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "id": "0suA_6Y7A16h",
    "outputId": "801a5377-7c36-4b57-c862-ec73e82992ff",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values([1, 5, 7])\n"
     ]
    }
   ],
   "source": [
    "values = d1.values() # Returns a list of values.\n",
    "print (values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "6t4EkkQsA16h",
    "outputId": "a41c1ab8-bdef-4b84-b4e0-12f59be68aaf",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('a', 1), ('b', 5), ('c', 7)])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1.items() # List of Tuples of key-value pairs."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "bpt8fQKiA16i",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Defining Dictionaries - ways to do this\n",
    "-----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "id": "neW8cfwrA16i",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "d1 = {\"a\":3, \"b\":5, \"c\":7} # we've seen this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "id": "85cPTIzvA16i",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "keys =  [\"a\", \"b\", \"c\"]\n",
    "values = [3,5,7]\n",
    "d2 = dict( zip(keys,values) ) # creates dictionary similar to d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "id": "-Fj8jH_VA16i",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "d3 = dict( a=3, b=5, c=7) # again, same as d1,d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "id": "U6DbMUChA16i",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "d4 = dict( [ (\"a\",3), (\"b\",5), (\"c\",7) ] ) # same as d1,d2,d3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Dictionaries in data analysis\n",
    "===============\n",
    "\n",
    "z['M31']\n",
    "\n",
    "rmag['M1']\n",
    "\n",
    "lumdist['3C 273']\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Q4vo-kiZA16j",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "The while loop\n",
    "-------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "y-ufrjSoA16j",
    "outputId": "cc1d664b-e855-489f-a408-ec143e7a4797",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "x = 0\n",
    "while x<5:\n",
    "    print (x)  \n",
    "    x += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "34NonpoXA16j",
    "outputId": "9cc04a7b-1f89-4ca9-bf9a-57cf04730137",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = 1\n",
      "Do you want to continue? y\n",
      "x = 2\n",
      "Do you want to continue? y\n",
      "x = 3\n",
      "Do you want to continue? n\n"
     ]
    }
   ],
   "source": [
    "x = 1\n",
    "while True: # Without the break statement this loop is infinite\n",
    "    print (\"x = %d\" % x)\n",
    "    choice = input(\"Do you want to continue? \") \n",
    "    if choice != \"y\":\n",
    "        break # This statement breaks the loop.\n",
    "    else:\n",
    "        x += 1\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Tbda_2CdA16j",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "The \"for\" loop - Pay Attention!\n",
    "-----\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "5rjetTEOA16j",
    "outputId": "1366fd90-4a2e-4dcb-91da-628a337f98c3",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "x = [5,6,7,8,9,0] # a simple list\n",
    "for i in x:\n",
    "    print (i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "tFGXNnAJA16k",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "In \" for i in x\", x can be many different things, any iterable object is allowed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "pu2stLx-A16k",
    "outputId": "e181f757-6101-42a3-9f63-9ce7e6fdd8f1",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n",
      "!\n"
     ]
    }
   ],
   "source": [
    "s = \"Hello!\"\n",
    "\n",
    "for c in s:\n",
    "    print (c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "1ZgXFvY3A16k",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "No No No! I want my good old for-loop back which generates numbers x to y in steps of z!!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "NyJf9gD1A16k",
    "outputId": "e8f97a0f-669d-44db-b0e2-9ec3b1d65c9b",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "5\n",
      "8\n",
      "11\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "# OKAY!!! Let's try something here.\n",
    "\n",
    "for i in range(2,15,3):\n",
    "    print (i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "G4ldbddEA16l",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Let us see some wicked for-loops."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "-OWbKGo6A16l",
    "outputId": "282447e5-b068-4ae1-ef8c-c32e919d2611",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'zip'>\n",
      "1 H\n",
      "2 e\n",
      "3 l\n",
      "4 l\n",
      "5 o\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "b = \"Hello\"\n",
    "c = zip(a,b)\n",
    "print(type(c))\n",
    "\n",
    "for i,j in c:\n",
    "    print (i, j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Z3e4q16PA16l",
    "outputId": "f61e2a59-f3bc-42c4-8d9a-894d93fc901c",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Character no. 1 is H\n",
      "Character no. 2 is e\n",
      "Character no. 3 is l\n",
      "Character no. 4 is l\n",
      "Character no. 5 is o\n",
      "Character no. 6 is !\n",
      "\n",
      "Help on class enumerate in module builtins:\n",
      "\n",
      "class enumerate(object)\n",
      " |  enumerate(iterable, start=0)\n",
      " |  \n",
      " |  Return an enumerate object.\n",
      " |  \n",
      " |    iterable\n",
      " |      an object supporting iteration\n",
      " |  \n",
      " |  The enumerate object yields pairs containing a count (from start, which\n",
      " |  defaults to zero) and a value yielded by the iterable argument.\n",
      " |  \n",
      " |  enumerate is useful for obtaining an indexed list:\n",
      " |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __next__(self, /)\n",
      " |      Implement next(self).\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Return state information for pickling.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello!\"\n",
    "\n",
    "for i, c in enumerate(a):\n",
    "    print (\"Character no. %d is %s\" % (i+1, c)) # i+1 is used because Python is 0-indexed.\n",
    "\n",
    "print()\n",
    "help(enumerate)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Y4DC1xNZA16m",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "You can break and continue for-loops too!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "NadDIWZsA16m",
    "outputId": "c9042bb6-0b72-4fb5-c58a-e67ca6f5fa5c",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 is Even\n",
      "1 is Odd\n",
      "2 is Even\n",
      "3 is Odd\n",
      "4 is Even\n",
      "5 is Odd\n",
      "6 is Even\n",
      "7 is Odd\n"
     ]
    }
   ],
   "source": [
    "for i in range(10000):\n",
    "    if i%2 == 0: # Even\n",
    "        print (i,\"is Even\")\n",
    "        continue\n",
    "    print (i, \"is Odd\")\n",
    "    \n",
    "    if i == 7: # What if I had said \"i==8 or i==10\" ??????\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "64dSUMYYA16m",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Traversing Dictionaries using for-loops\n",
    "------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "nrQyE8fRA16m",
    "outputId": "4cd67f4c-d92e-4fb8-8e77-42b07c83a8d9",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a --> 1\n",
      "b --> 2\n",
      "c --> 3\n",
      "d --> 4\n"
     ]
    }
   ],
   "source": [
    "d = dict( a = 1, b = 2, c = 3, d = 4)\n",
    "\n",
    "for key,value in d.items():\n",
    "    print (key, \"-->\", value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "HI5xfzKmA16n",
    "outputId": "066d30d2-6ff1-42a4-dc96-5746e286a2d3",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a --> 1\n",
      "b --> 2\n",
      "c --> 3\n",
      "d --> 4\n"
     ]
    }
   ],
   "source": [
    "for key in d.keys():\n",
    "    print (key, \"-->\", d[key])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Style Guide for Python Code\n",
    "----------\n",
    "\n",
    "The PEP 8 provides excellent guidance on what to do and what to avoid while writing Python code. I strongly urge you to study PEP 8 carefully and implement its recommendations strictly.\n",
    "\n",
    " \n",
    "https://www.python.org/dev/peps/pep-0008/\n",
    "\n",
    "The PEP 8 is very terse. For a more expanded explanation see:\n",
    "\n",
    "\n",
    "https://realpython.com/python-pep8/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Python is fully Unicode UTF-8 standard compliant\n",
    "====="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What is your name?\n",
      "আপনার নাম কি?\n",
      "உங்கள் பெயர் என்ன?\n",
      "तुझं नाव काय आहे?\n",
      "तुम्हारा नाम क्या हे?\n"
     ]
    }
   ],
   "source": [
    "print(\"What is your name?\")\n",
    "print(\"আপনার নাম কি?\")\n",
    "print(\"உங்கள் பெயர் என்ன?\")\n",
    "print(\"तुझं नाव काय आहे?\")\n",
    "print(\"तुम्हारा नाम क्या हे?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Google provides the Cloud Translation API client library for Python \n",
    "\n",
    "https://cloud.google.com/translate/docs/reference/libraries/v2/python"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "colab": {
   "name": "basicpython2.ipynb",
   "provenance": []
  },
  "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}