{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "mq_oYvpIUlBZ",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Astronomical data analysis using Python\n",
    "\n",
    "\n",
    "## Lecture 2, second lecture of the course\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "equivalent-football",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Assumptions!!!\n",
    "================\n",
    "\n",
    " \n",
    "\n",
    "You are new to Python! You may or may not have programming experience in another language.\n",
    "-----------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "direct-chile",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Our First Program!\n",
    "==================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "computational-backup",
    "outputId": "bf3f926b-230c-42d6-8d6e-bd8178c9ff0b",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n",
      "Sum, Difference =  7 -3\n",
      "Quotient and Remainder =  0.4 2\n"
     ]
    }
   ],
   "source": [
    "a = 2\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)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "silent-pollution",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "What can we learn from this simple program?\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "coordinated-relationship",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Dynamic Typing\n",
    "--------------\n",
    "\n",
    "* We do not declare variables and types in advance. (dynamic typing)\n",
    "* Variables created when first assigned values.\n",
    "* Variables do not exist if not assigned. (strong typing)\n",
    "\n",
    "Commenting\n",
    "----------\n",
    "\n",
    "Everything after # is a comment and is ignored. Comment freely. A comment can be on its own line or following a line of code.\n",
    "\n",
    "\"print\" statement\n",
    "------------------\n",
    "\n",
    "Used to print output of any kind. We will use this built-in function of Python often.\n",
    "\n",
    "Tuple unpacking assignments\n",
    "----------------------------\n",
    " a,b = 5,6\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "premium-encoding",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Other Things\n",
    "==================\n",
    "\n",
    "* Behavior of / and % operators with integer types. (changed in Python 3)\n",
    "* No termination symbols at end of Python statements.\n",
    "* Exception to the above...\n",
    "\n",
    "    a = 3; b = 5\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "convenient-hanging",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Under the Hood\n",
    "===================\n",
    "\n",
    "* No explicit compiling/linking step. Just run...\n",
    "    $ python First.py\n",
    "* Internally, program translated into bytecode (.pyc files)\n",
    "* The \"translation + execution\" happens line-by-line\n",
    "\n",
    "Implications of \"line-by-line\" style\n",
    "-------------------------------------\n",
    "\n",
    "* N lines will be executed before error on N+1th line halts program!\n",
    "* An interactive shell.\n",
    "* Interpreted language codes are generally slower than equivalent code in compiled languages."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "romance-genius",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "The First Tour of the Data Types\n",
    "================================\n",
    "\n",
    "* Numbers - Integers\n",
    "* Numbers - Floats\n",
    "\n",
    "Exploration of math module\n",
    "\n",
    "* Strings \n",
    "\n",
    "Methods of Declaring Strings\n",
    "\n",
    "Concept of Sequences\n",
    "\n",
    "Concept of Slicing\n",
    "\n",
    "Concept of Mutability\n",
    "\n",
    "Introduction of Object.Method concepts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "adapted-heading",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Integers\n",
    "---------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "enhanced-carolina",
    "outputId": "069a3067-0d70-4229-e9b1-a0f4fbd63d33",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "8 ** 2 # Exponentiation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "id": "separated-cambridge",
    "outputId": "0946c0a4-0600-41ca-8a4a-e9ab8cf28136",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "221598697564115095916538315188172875314354600282592890206517191909967025172536308830343071583454849289814240677195547664161196197773103139821259127019202606635932853150774379161903661721108884741902313128449334671098765711668174784729026178087482963822180304753020435752001"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "23**200 # Auto-upgrade to \"LONG INT\" Notice the L!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "id": "difficult-oracle",
    "outputId": "e738c28b-df0f-48b5-c2be-b4a59c8e88a6",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2.5, 1)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 / 2, 5%2 # Quotient-Remainder Revisited."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Htqx6g6HUlBl",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Notice that Python is an effective scientific calculator!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "written-indication",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Floats\n",
    "-------\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "id": "complimentary-residence",
    "outputId": "62da7455-3174-4316-d849-38637b1fdedb",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10.0, 10.0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5.0 * 2, 5*2.0 # Values upgraded to \"higher data type\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "id": "dense-father",
    "outputId": "c13fa659-ec97-4908-e119-d04e1971ffae",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.23606797749979"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5**0.5 # Yes, it works! Square-root."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "id": "patent-nelson",
    "outputId": "da98eb77-de57-4e2d-df46-ac597a4b9a46",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.25"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 / 4.0 # No longer a quotient."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "id": "appropriate-whale",
    "outputId": "be6f0c6a-23b1-4a76-a2c7-7049f60b93bf",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1.0, 0.9000000000000004)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 % 4.0, 5 % 4.1 # Remainder, yes!!!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "verified-young",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Math Module\n",
    "------------\n",
    "\n",
    "* A module can be thought of as a collection of related functions.\n",
    "* To use a module, \n",
    "\n",
    "    import ModuleName\n",
    "    \n",
    "* To use a function inside a module, simply say\n",
    "\n",
    "    ModuleName.Function(inputs)\n",
    "    \n",
    "Let's see the math module in action!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "id": "elect-queue",
    "outputId": "d0285a98-6101-4b79-e87f-28a72177bb22",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8660254037844386"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "x = 60*math.pi/180.0\n",
    "math.sin(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "id": "worse-better",
    "outputId": "598569eb-e21e-4a51-f56d-ef9512c2fbb9",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8660254037844386"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.sin( math.radians(60) ) # nested functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "medical-injury",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "There are about 42 functions inside Math library! So, where can one get a quick reference of what these functions are, what they do and how to use them!?!?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "id": "alternate-baghdad",
    "outputId": "d93a3a3c-b1f1-4ad9-f421-23e42618a51a",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']\n"
     ]
    }
   ],
   "source": [
    "print (dir(math)) # Prints all functions associated with Math module."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Help on specific functions in a module\n",
    "----------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "id": "monetary-guide",
    "outputId": "34929e45-a53c-4201-cae5-332bbd497c2a",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function hypot in module math:\n",
      "\n",
      "hypot(...)\n",
      "    hypot(*coordinates) -> value\n",
      "    \n",
      "    Multidimensional Euclidean distance from the origin to a point.\n",
      "    \n",
      "    Roughly equivalent to:\n",
      "        sqrt(sum(x**2 for x in coordinates))\n",
      "    \n",
      "    For a two dimensional point (x, y), gives the hypotenuse\n",
      "    using the Pythagorean theorem:  sqrt(x*x + y*y).\n",
      "    \n",
      "    For example, the hypotenuse of a 3/4/5 right triangle is:\n",
      "    \n",
      "        >>> hypot(3.0, 4.0)\n",
      "        5.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(math.hypot)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "cutting-intersection",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Strings\n",
    "--------\n",
    "There are three methods of defining strings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "id": "matched-sample",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "a = \"John's Computer\" # notice the '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "id": "steady-batch",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "b = 'John said, \"This is my computer.\"' # notice the \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "id": "independent-database",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "a_alt = 'John\\'s Computer' # now you need the escape sequence \\"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "id": "twelve-arizona",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "b_alt = \"John said, \\\"This is my computer.\\\"\" # again escape sequence."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "id": "under-resistance",
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "long_string = \"\"\"Hello World! \n",
    "\n",
    "I once said to people, \"Learn Python!\" \n",
    "\n",
    "And then they said, \"Organize a workshop!\" \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "id": "interior-bidding",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "long_string_traditional = 'Hello World! \\n\\nI once said to people, \"Learn Python!\" \\\n",
    "\\n\\nAnd then they said, \"Organize an online course!\" '"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "honest-feeling",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "* Can be used to dynamically build scripts, both Python-based and other \"languages\".\n",
    "* Used for documenting functions/modules. (To come later!)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "smoking-percentage",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "String Arithmetic\n",
    "----------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "id": "warming-cherry",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "s1 = \"Hello\" ; s2 = \"World!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "id": "republican-observer",
    "outputId": "ffe5dd58-e4b4-4250-87a7-8c8ab8bc426b",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HelloWorld!\n"
     ]
    }
   ],
   "source": [
    "string_sum = s1 + s2\n",
    "print (string_sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "id": "excellent-resistance",
    "outputId": "ad02f1b8-8729-445d-94d4-90f9f759c046",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HelloHelloHello\n"
     ]
    }
   ],
   "source": [
    "string_product = s1*3\n",
    "print (string_product)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "id": "backed-darkness",
    "outputId": "3cb9e980-b06b-48b8-b562-0cb815625051",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HelloHelloHelloWorld!\n"
     ]
    }
   ],
   "source": [
    "print (s1*3+s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "dynamic-taiwan",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "String is a sequence!\n",
    "---------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "id": "objective-beach",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "a = \"Python rocks!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "id": "bearing-external",
    "outputId": "5b298599-9c73-49cf-dad7-bd163fc4688c",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('P', 'y', 't')"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0], a[1], a[2] # Positions begin from 0 onwards."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "id": "yellow-young",
    "outputId": "fa918f95-c753-4ebe-f53e-86de09a3f117",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('!', 's', 'k')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[-1], a[-2], a[-3] # Negative indices - count backwards!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "id": "regular-judges",
    "outputId": "fd499d4c-da72-443b-a5e4-b509f7aa8da7",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a) # Measures length of both sequence/unordered collections!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "filled-hobby",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Sequences can be sliced!\n",
    "----------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "id": "close-embassy",
    "outputId": "76426bd8-21fb-4c69-d1a2-51ab0086ad41",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'thon'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2:6] # elements with indices 2,3,4,5 but not 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "id": "cellular-makeup",
    "outputId": "9592aebb-d333-4403-856b-5c09c77a6cbb",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ock'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[8:-2] # indices 8,9 ... upto 2nd last but not including it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "id": "temporal-panic",
    "outputId": "6a07757e-87af-472b-c3aa-7cfe06536fa3",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Pytho'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:5] # Missing first index, 0 assumed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "id": "referenced-venezuela",
    "outputId": "550a8654-ab38-4531-ba70-e657222a21aa",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'n rocks!'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[5:] # Missing last index, len(a) assumed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "mechanical-forty",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Crazier Slicing\n",
    "---------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "id": "differential-weekly",
    "outputId": "96e413a7-0d72-4f71-8ff7-202e543fd8e7",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('yhn', 'y', 'h', 'n')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:6:2],a[1],a[3],a[5] # Indices 1, 3, 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "id": "hourly-ridge",
    "outputId": "d5db87df-7394-4733-c02c-d6f32528ecd3",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Pto ok!'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::2] # beginning to end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "id": "sixth-thesis",
    "outputId": "2e06cdd3-5c3a-444e-ce08-a5ce569e6016",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'!skcor nohtyP'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-1] # Reverse slicing!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "id": "abroad-madison",
    "outputId": "8b9d3557-0994-4cfa-d6b9-4e7f3e5c98ef",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:6:-1] # In a[i:j:-1], changes meaning of i and j"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "opened-house",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Objects and Methods - An oversimplified Introduction\n",
    "=====\n",
    "\n",
    "An object can be thought of a construct in the memory.\n",
    "\n",
    "It has a well defined behavior with respect to other objects. (2\\*3 is allowed, \"a\"\\*\"b\" is not!)\n",
    "\n",
    "The properties of the object, the operations that can be performed all are pre-defined.\n",
    "\n",
    "A method is a function bound to an object that can perform specific operations that the object supports.\n",
    "\n",
    "    ObjectName.MethodName(arguments)\n",
    "    \n",
    "OK, let's see some string methods in action!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "driven-actress",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "String Methods\n",
    "--------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "id": "protective-checkout",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "a = \"   I am a string, I am an object, I am immutable!   \""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "id": "ruled-valve",
    "outputId": "df34c530-a1d0-4d30-f722-a9d805984671",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'   I Am A String, I Am An Object, I Am Immutable!   '"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.title()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "id": "korean-canal",
    "outputId": "5333e7a0-d1a4-4c87-d945-967c490915a9",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['   I am a string', ' I am an object', ' I am immutable!   ']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.split(\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "id": "naval-recruitment",
    "outputId": "690a5be7-f686-4384-f86c-99b34419f017",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I am a string, I am an object, I am immutable!'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.strip() # Remove trailing and leading whitespaces."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "cathedral-failing",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Strings are Immutable!\n",
    "-----------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "id": "earned-citizen",
    "outputId": "798c9590-7c31-48e3-f2fb-a6c1ba5ddab7",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   I am a string, I am an object, I am immutable!   \n"
     ]
    }
   ],
   "source": [
    "print (a) # Check the value!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "id": "impaired-winning",
    "outputId": "86b9ee68-822b-4f6c-e2df-1767616bd160",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'   I Am A String, I Am An Object, I Am Immutable!   '"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.title() # Transform string to title case ... really?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "id": "recovered-briefing",
    "outputId": "f3d94d41-4e57-4695-96fd-44f7267d9b5d",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   I am a string, I am an object, I am immutable!   \n"
     ]
    }
   ],
   "source": [
    "print (a) # Nothing changed! Strings are immutabe."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "id": "entitled-scholarship"
   },
   "outputs": [],
   "source": [
    "b = a.title() # String methods return strings instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "id": "persistent-checkout",
    "outputId": "9e5692e1-95df-4497-8999-cbb6dde225b3",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   I Am A String, I Am An Object, I Am Immutable!   \n"
     ]
    }
   ],
   "source": [
    "print (b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "id": "allied-barrel",
    "outputId": "48458083-ec56-4cf2-abb9-77afbf9ef2e5",
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'str' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-47-1c6b97054996>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"x\"\u001b[0m \u001b[0;31m# Immutability implies no in-place changes.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "a[3] = \"x\" # Immutability implies no in-place changes."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "attended-fitting",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Getting Help\n",
    "-------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "id": "pressed-horror",
    "outputId": "c8859695-b654-45fc-a4b1-20a51efaa451",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\n"
     ]
    }
   ],
   "source": [
    "print (dir(a)) # a is a string object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "id": "genetic-telephone",
    "outputId": "6f69f078-2665-473b-dd74-07ea57f6528c",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function find:\n",
      "\n",
      "find(...) method of builtins.str instance\n",
      "    S.find(sub[, start[, end]]) -> int\n",
      "    \n",
      "    Return the lowest index in S where substring sub is found,\n",
      "    such that sub is contained within S[start:end].  Optional\n",
      "    arguments start and end are interpreted as in slice notation.\n",
      "    \n",
      "    Return -1 on failure.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(a.find)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "colab": {
   "name": "basicpython1.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
}