{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to Data Science (CS4661). Cal State LA, CS Dept.\n",
    "#### Instructor: Dr. Mo Porhomayoun\n",
    "---------------------------------------------------------------------------------------------------\n",
    "---------------------------------------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Jupyter Notebook (ipython Notebook)\n",
    "\n",
    "The Jupyter Notebook (previously known as ipython Notebook) is a web-based interactive development environment, in which you can combine code execution, text and notations (markdown), mathematics, and plots. It will be run under your brrowser. The Jupyter Notebook supports a number of programming languages including Python, Julia, Octave, and R.\n",
    "\n",
    "The Jupyter Notebook include cells. Each cell can be run independently.\n",
    "You can add/remove cells, or arrange the order of them. A piece of code, or several lines of the code that we want to run together can be written in a single cell. We strongly recommend using Jupyter Notebook as a framework\n",
    "\n",
    "---------------------------------------------------------------------------------------------------\n",
    "---------------------------------------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Python Programming\n",
    "\n",
    "#### This is a quick review of some python syntax. Please refer to the suggested resources for more information.\n",
    "\n",
    "---------------------------------------------------------------------------------------------------\n",
    "---------------------------------------------------------------------------------------------------\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Let's start with some numeric operations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# we can use '#' to comment out a line!\n",
    "\n",
    "# sum:\n",
    "2+3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# multiply:\n",
    "2*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# Integer and floating-point division:\n",
    "print(5/2) # int/int returns int in python ver2.x! But, in python ver3.x in returns float.\n",
    "\n",
    "print(5/2.0)\n",
    "print(5.0/2)\n",
    "print(5/float(2))\n",
    "\n",
    "print(5.0//2) # floor (truncate)\n",
    "\n",
    "print(10%3)  # remainder\n",
    "\n",
    "print(10**3) # exponent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Variables:\n",
    "a = 2\n",
    "b = 3\n",
    "c = a+b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a = 2\n",
    "a += 1   # a = a + 1\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Variables:\n",
    "a = 5\n",
    "b = 5/2\n",
    "print(type(a), type(b))\n",
    "print(a,\"\\n\",b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## String:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# String:\n",
    "s = 'hello world!'\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "s = 'hello world!'\n",
    "print(s)\n",
    "\n",
    "print(s[0])\n",
    "\n",
    "print(s[0:2]) # 0 is included, 2 is NOT included\n",
    "\n",
    "print(s[2:10]) # 10 is NOT included\n",
    "\n",
    "print(s[2:])\n",
    "\n",
    "print(s[:5])\n",
    "\n",
    "print(s[-1]) # last element\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "s = 'hello world!'\n",
    "print(s)\n",
    "\n",
    "print(len(s)) # length\n",
    "\n",
    "print('hello' + ' ' + 'world' + '!' + '\\n')  # concat with +\n",
    "\n",
    "d = s + ' I love CS4661.'\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# converting to int:\n",
    "a = 2016\n",
    "s = str(a)\n",
    "print(a)\n",
    "print(s)\n",
    "print(s[0])\n",
    "\n",
    "b = int(s)\n",
    "b += 1\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "s = 'hello world! I love CS4661!'\n",
    "print(s)\n",
    "\n",
    "i = s.find('love')      # returns 15 \n",
    "j = s.find('hate')      # returns -1 when not found\n",
    "print(i,'\\n',j)\n",
    "\n",
    "t = s.replace('love','like')    # replacing 'love' by 'like'\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# List also known as array or vector: \n",
    "# a list of elements such as numbers or char or strings separated by comma\n",
    "\n",
    "list1 = [1,2,3,7,-12,0,157]\n",
    "\n",
    "print(list1)\n",
    "print(len(list1))  # lenght of a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "empt = [] # empty list\n",
    "print(empt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# indexing for a list:\n",
    "\n",
    "list1 = [1,2,3,7,-12,0,157]\n",
    "\n",
    "print(list1[0])   # indexing starts from 0\n",
    "print(list1[1])\n",
    "print(list1[6])\n",
    "print(list1[-1])  # -i points to the last element\n",
    "print(list1[-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Slicing:\n",
    "\n",
    "l1 = [1,2,3,7,-12,0,157]\n",
    "\n",
    "print(l1[1:5])     # does not include index 5\n",
    "print(l1[1:]) \n",
    "print(l1[:5])\n",
    "print(l1[0:7:2])   # [start:end:step]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "list2 = ['hello', 231, 2.0, '2.0', 'CS4661']\n",
    "\n",
    "print(list2)\n",
    "print(list2[0])\n",
    "print(list2[0][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# '+' for concatenating the lists\n",
    "v1 = [1,2,3]\n",
    "v2 = [4,5,6]\n",
    "u = v1 + v2\n",
    "\n",
    "print(u)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# some useful opperations/methods on Lists:\n",
    "\n",
    "l = [23,5,4,5,-19]\n",
    "\n",
    "l.append(6)    # ==> [23, 5, 4, 5, -19, 6]\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# some useful opperations/methods on Lists:\n",
    "\n",
    "l = [23,5,4,5,-19]\n",
    "\n",
    "l.append(6)    # ==> [23, 5, 4, 5, -19, 6]\n",
    "print(l)\n",
    "\n",
    "l.pop()        # ==> returns 6, and l=[23, 5, 4, 5, -19]\n",
    "print(l)\n",
    "\n",
    "l.sort()       # ==> [-19, 5, 5, 7, 23]\n",
    "print(l)\n",
    "\n",
    "print(l.count(5))   # counts the number of 5 repetitions\n",
    "\n",
    "print(l.index(5))   # returns index of first 5\n",
    "\n",
    "l_s = sorted(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# copying a list:\n",
    "l1 = [1,2,3,7,-12,0,157]\n",
    "l2 = l1 # notice: l2 is just another reference to the same list!!!!\n",
    "\n",
    "l1[0]=8 # since they both refer to the same list, changing an element of l1 will change the same elemnet of l2 too!\n",
    "\n",
    "print(l1)\n",
    "print(l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# copying a list as a new list:\n",
    "l1 = [1,2,3,7,-12,0,157]\n",
    "l2 = l1[:] # notice: l2 is a new list!!!!\n",
    "\n",
    "l1[0]=8\n",
    "\n",
    "print(l1)\n",
    "print(l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# List of lists (Matrix):\n",
    "X = [[1,2],[5,6],[0,-3]]\n",
    "print(X)\n",
    "print(X[1][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Converting to string:\n",
    "\n",
    "\n",
    "# converting a list into a string using a delimiter\n",
    "string_list = ['I','love','CS4661!']\n",
    "\n",
    "s = ' '.join(string_list)   # 'I love CS4661!'\n",
    "print(s)\n",
    "\n",
    "\n",
    "# converting a string into a list separated by a delimiter\n",
    "l = s.split(' ')        # returns ['I','love','CS4661!']\n",
    "print(l)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dictionary:\n",
    "Super helpful! It is very important for you to be able to work comfortably with dictionaries in python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# key-value pairs\n",
    "# Defining a dictionary:          d = {'key0':'value0','key1':'value1',...} \n",
    "# Defining a blank dictionary:    d = {} \n",
    "# Note1: Dictionary is unorderred! (ordering in meaningless for dictionaries\n",
    "# Note2: keys can be strings or numbers, values can be any type\n",
    "# Note3: keys must be unique, but there is no limitation on value \n",
    "\n",
    "\n",
    "age_dict = {'Tom':27, 'Joe':34, 'Ryan':19, 'Sarah':28}\n",
    "\n",
    "print(age_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# accessing the pairs\n",
    "age_dict = {'Tom':27, 'Joe':34, 'Ryan':19, 'Sarah':28}\n",
    "\n",
    "print(age_dict['Tom'])\n",
    "print(age_dict['Joe'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# changing/assigning/adding the pairs\n",
    "age_dict = {'Tom':27, 'Joe':34, 'Ryan':19, 'Sarah':28}  # Name:Age\n",
    "\n",
    "print(age_dict['Joe'])\n",
    "\n",
    "age_dict['Joe'] = 51   # changing a value\n",
    "age_dict['Rose'] = 63  # adding new key-value\n",
    "\n",
    "print(age_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# value can be a list\n",
    "\n",
    "# Name:[Gender,Age,Weight]\n",
    "age_dict = {'Tom':['m',27,155], 'Joe':['m',34,175], 'Ryan':['m',19,183], 'Sarah':['f',28,150]}\n",
    "\n",
    "print(age_dict)\n",
    "print(age_dict['Joe'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# accessing all key,value\n",
    "age_dict = {'Tom':27, 'Joe':34, 'Ryan':19, 'Sarah':28}  # Name:Age\n",
    "\n",
    "print(len(age_dict))     # returns 4\n",
    "\n",
    "print(age_dict.keys())       # returns a list: ['Tom', 'Joe', 'Ryan', 'Sarah']\n",
    "print(age_dict.values())     # returns a list: [27, 34, 19, 28]\n",
    "print(age_dict.items())     # returns a list of tuples: [('Tom',27),('Joe',34),('Ryan',19),('Sarah',28)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# checking if a *key* is already available:\n",
    "\n",
    "'Ryan' in  age_dict    # returns True\n",
    "'Annie' in  age_dict # returns False\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conditional Statements:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# comparisons:\n",
    "print(7 > 5)\n",
    "print(7 >= 5)\n",
    "print(7 != 5)\n",
    "print(7 == 7)\n",
    "\n",
    "# boolean operations:\n",
    "print ((3 > -3) and (1 > 0))\n",
    "print ((11 > 21) or (9 < 11))\n",
    "print ((11 > 21) or not(9 < 11))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# if statement:\n",
    "n = 2\n",
    "if n > 0:\n",
    "    print(n ,'is positive')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "####  Note: Four Spaces for indentation level is the Python standard. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# if/else statement:\n",
    "n = -2\n",
    "if n > 0:\n",
    "    print(n ,'is positive')\n",
    "else:\n",
    "    print(n ,'is not positive')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# if/elif/else statement\n",
    "n = 2\n",
    "if n > 0:\n",
    "    print(n ,'is positive')\n",
    "elif n == 0:\n",
    "    print(n ,'is zero')\n",
    "else:\n",
    "    print(n ,'is negative')\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  Note: Four Spaces for indentation level is the Python standard. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Example:\n",
    "m = 3\n",
    "n = 2\n",
    "\n",
    "if m>n:\n",
    "    print('m is greater than n')\n",
    "    print('I will replace them')\n",
    "    m,n = n,m\n",
    "print('succes!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# using dictionary in if statement:\n",
    "\n",
    "age_dict = {'Tom':27, 'Joe':34, 'Ryan':19, 'Sarah':28}\n",
    "\n",
    "if 'Tom' in age_dict: \n",
    "    print('Tom is already in the dictionary')\n",
    "else:    \n",
    "    age_dict['Tom'] = 27\n",
    "    print('Tom is added')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loops:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# For Loop:\n",
    "\n",
    "foods = ['chicken','pizza','sandwitch']\n",
    "\n",
    "for f in foods:\n",
    "    print(f)       # inside the loop\n",
    "    print(len(f))  # inside the loop\n",
    "\n",
    "print(foods)       # outside the loop\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  Note: Four Spaces for indentation level is the Python standard. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# range:\n",
    "# range returns a list of numbers\n",
    "\n",
    "r1 = range(5)     # r1 = [0, 1, 2, 3, 4]\n",
    "\n",
    "r2 = range(3,5)     # r2 = [3, 4]\n",
    "\n",
    "r3 = range(0,5,2)     # r3 = [0, 2, 4]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# for loop with range:\n",
    "\n",
    "for x in range(5):\n",
    "    print(x)\n",
    "\n",
    "print('\\n')    \n",
    "\n",
    "for y in range(0,20,5):\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# for loop over a dictionary items:\n",
    "\n",
    "major_dict = {'Tom':'CS', 'Joe':'CS', 'Ryan':'EE', 'Sarah':'CE'}\n",
    "\n",
    "for k in major_dict.keys():\n",
    "    print(k,'is studying in', major_dict[k])\n",
    "\n",
    "print('\\n')\n",
    "\n",
    "# equivalent approach:\n",
    "for k,v in major_dict.items():\n",
    "    print(k,'is studying in',v)\n",
    "\n",
    "# Again remember that dictionary items have no definite order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# break and continue:\n",
    "\n",
    "magic_number = 11\n",
    "for i in range(101):\n",
    "    if i == magic_number:\n",
    "        print(i,\" is the magic number\")\n",
    "        break # Jump out of the current \"For Loop\" to the line: print(\"done!\\n\")\n",
    "    else:\n",
    "        print(i)\n",
    "print(\"done!\\n\")\n",
    "\n",
    "\n",
    "MyList = [1, 5 , 7, 9]\n",
    "for j in range(0,10):\n",
    "    if j in MyList:\n",
    "        continue # stop the iteration here and continue with the next j\n",
    "    print(j)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# for loop in line:\n",
    "\n",
    "# Example1:\n",
    "mylist=[1,2,3,4,5]\n",
    "mylist_sqr = [i*i for i in mylist]\n",
    "print(mylist_sqr)    # returns [1, 4, 9, 16, 25]\n",
    "\n",
    "\n",
    "# Example2:\n",
    "x = [i for i in range(10)]\n",
    "print(x)    # returns [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "\n",
    "\n",
    "# Example3:\n",
    "x = [j**2 for j in range(10) if j>4]\n",
    "print(x)    # returns [25, 36, 49, 64, 81]\n",
    "\n",
    "\n",
    "# Example4: Matrix\n",
    "Matrix = [[0 for x in range(5)] for x in range(3)]\n",
    "print(Matrix)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# While Loop: \n",
    "\n",
    "n = 0\n",
    "while n<5:\n",
    "    print(n)\n",
    "    n += 1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# simplest case: defining a function with no input argument and no return results:\n",
    "\n",
    "def myPrint(): \n",
    "    print('This is a test!')\n",
    "\n",
    "\n",
    "# calling this function:\n",
    "myPrint()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# defining a function to get two input arguments and return the sum:\n",
    "# Note: we usually add function/input/output description in the first line after def. We use \"\"\"text\"\"\" to do this documentation.\n",
    "# Anything between 3 quotation-marks will be considered as comment\n",
    "\n",
    "def mySum(a = 0, b = 0): # default values are optional\n",
    "    \"\"\"This is a function for summation\n",
    "       inputs: a,b\n",
    "       output: it returns a+b as output\"\"\"\n",
    "    c = a + b\n",
    "    return c\n",
    "\n",
    "\n",
    "# calling this function:\n",
    "s = mySum(7,3)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# defining a function to get two input arguments and return the sum and sub:\n",
    "\n",
    "def mySum(a = 0, b = 0): # default values are optional\n",
    "    \"\"\"This is a function for summation and subtraction\n",
    "       inputs: a,b\n",
    "       output: it returns a+b and a-b as outputs\"\"\"\n",
    "    c = a + b\n",
    "    d = a - b\n",
    "    return c,d\n",
    "\n",
    "\n",
    "# calling this function:\n",
    "sm,sb = mySum(7,3)\n",
    "print(sm,sb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lambda:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Lambda: in-line functions!\n",
    "\n",
    "# Example1 (one input, one output):\n",
    "# defining a function that gets an input, and calculate and return squared value using lambda\n",
    "mySqr = lambda x: x**2\n",
    "\n",
    "# calling:\n",
    "y = mySqr(2)\n",
    "print(y)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Lambda: in-line functions!\n",
    "\n",
    "# Example2 (more than one input, one output):\n",
    "# defining a function that gets two input, and calculate and return the summation of squared values using lambda\n",
    "mySqr = lambda x1,x2: x1**2 + x2**2 \n",
    "\n",
    "# calling:\n",
    "y = mySqr(2,4)\n",
    "print(y)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Lambda: in-line functions!\n",
    "\n",
    "# Example3 (more than one input, more than one output):\n",
    "\n",
    "mySqr = lambda x1,x2: (x1**2 + x2**2 , x1**2 - x2**2)\n",
    "\n",
    "# calling:\n",
    "y,z = mySqr(2,4)\n",
    "print(y,z)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exceptions:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a = 0\n",
    "b = 1\n",
    "try:\n",
    "    c = b/a\n",
    "except:\n",
    "    print(\"devided by zero\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
