<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Functions\n",
    "\n",
    "* Define the function using **def** keyword.\n",
    "* to return a value using **return** keyword\n",
    "* even no **return** inside the function, function still return None => no method like C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "def square(x):\n",
    "    return x * x\n",
    "\n",
    "print square(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Default arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1024\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "def exponential(x, base=2):\n",
    "    value = 1\n",
    "    for i in range(x):\n",
    "        value = value * base\n",
    "\n",
    "    return value\n",
    "\n",
    "print exponential(10)\n",
    "print exponential(2, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Keyword arguments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Arbitrary argument lists"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lambda expression"
   ]
  },
  {
   "cell_type": "code",<commit_msg>update function<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Functions\n",
    "\n",
    "* Define the function using **<code>def</code>** keyword.\n",
    "* to return a value using **<code>return</code>** keyword\n",
    "* even no **<code>return</code>** inside the function, function still return None => no procedure like C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "def square(x):\n",
    "    return x * x\n",
    "\n",
    "print square(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* function name is a variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<function square at 0x102aa4230>\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "print square\n",
    "power2 = square\n",
    "print power2(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Default arguments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* specify a default value for one or more arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1024\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "def exponential(x, base=2):\n",
    "    value = 1\n",
    "    for i in range(x):\n",
    "        value = value * base\n",
    "\n",
    "    return value\n",
    "\n",
    "print exponential(10)\n",
    "print exponential(2, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* non-default argument cannot follow after default argument"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "non-default argument follows default argument (<ipython-input-15-c36fe27b5bfa>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-15-c36fe27b5bfa>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    def exp(base=2, x):\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n"
     ]
    }
   ],
   "source": [
    "def exp(base=2, x):\n",
    "    return exponential(x, base)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **Important**: the default value is evaluated only once, which means the default value is shared between subsequent calls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n",
      "[1, 2]\n"
     ]
    }
   ],
   "source": [
    "def append2list(value, array=[]):\n",
    "    array.append(value)\n",
    "    return array\n",
    "\n",
    "print append2list(1)\n",
    "print append2list(2) # get [1, 2] instead of [1] because the default value is shared between first call"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Keyword arguments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* function can be called using keyword arguments of the form key_word_argument=value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):\n",
    "    print \"-- This parrot wouldn't\", action,\n",
    "    print \"if you put\", voltage, \"volts through it.\"\n",
    "    print \"-- Lovely plumage, the\", type\n",
    "    print \"-- It's\", state, \"!\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* the following call is valid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-- This parrot wouldn't voom if you put 1000 volts through it.\n",
      "-- Lovely plumage, the Norwegian Blue\n",
      "-- It's a stiff !\n",
      "-- This parrot wouldn't voom if you put 1000 volts through it.\n",
      "-- Lovely plumage, the Norwegian Blue\n",
      "-- It's a stiff !\n",
      "-- This parrot wouldn't VOOOOOM if you put 1000000 volts through it.\n",
      "-- Lovely plumage, the Norwegian Blue\n",
      "-- It's a stiff !\n",
      "-- This parrot wouldn't VOOOOOM if you put 1000000 volts through it.\n",
      "-- Lovely plumage, the Norwegian Blue\n",
      "-- It's a stiff !\n",
      "-- This parrot wouldn't jump if you put a million volts through it.\n",
      "-- Lovely plumage, the Norwegian Blue\n",
      "-- It's bereft of life !\n",
      "-- This parrot wouldn't voom if you put a thousand volts through it.\n",
      "-- Lovely plumage, the Norwegian Blue\n",
      "-- It's pushing up the daisies !\n"
     ]
    }
   ],
   "source": [
    "parrot(1000)                                          # 1 positional argument\n",
    "parrot(voltage=1000)                                  # 1 keyword argument\n",
    "parrot(voltage=1000000, action='VOOOOOM')             # 2 keyword arguments\n",
    "parrot(action='VOOOOOM', voltage=1000000)             # 2 keyword arguments\n",
    "parrot('a million', 'bereft of life', 'jump')         # 3 positional arguments\n",
    "parrot('a thousand', state='pushing up the daisies')  # 1 positional, 1 keyword"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* but the following call is invalid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "non-keyword arg after keyword arg (<ipython-input-21-b68b97b43e2a>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-21-b68b97b43e2a>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m    parrot(voltage=5.0, 'dead')  # non-keyword argument after a keyword argument\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-keyword arg after keyword arg\n"
     ]
    }
   ],
   "source": [
    "parrot()                     # required argument missing\n",
    "parrot(voltage=5.0, 'dead')  # non-keyword argument after a keyword argument\n",
    "parrot(110, voltage=220)     # duplicate value for the same argument\n",
    "parrot(actor='<NAME>')  # unknown keyword argument"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Arbitrary argument lists"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* we can also specify that a function can be called with an arbitrary number of arguments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def sum_exp(base, *numbers):\n",
    "    total = 0\n",
    "    for no in numbers:\n",
    "        total = total + exp(no, base)\n",
    "    return total"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* unpacking arguments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lambda expression"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* define small anonymous function using lambda keyword"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "prod = lambda a, b: a * b\n",
    "\n",
    "print prod(5, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
{'fraction_non_alphanumeric': 0.19742371642609172, 'fraction_numerical': 0.027224556915768316, 'mean_word_length': 2.2301563883151374, 'pattern_counts': {'":': 210, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7514377', 'n_tokens_mistral': 4248, 'n_tokens_neox': 3996, 'n_words': 871}