diff --git a/.ipynb_checkpoints/00-checkpoint.ipynb b/.ipynb_checkpoints/00-checkpoint.ipynb index 4c95d8d..757067d 100644 --- a/.ipynb_checkpoints/00-checkpoint.ipynb +++ b/.ipynb_checkpoints/00-checkpoint.ipynb @@ -4,21 +4,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "# Python Overview" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Python-Lectures" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Introduction" + "## Introduction" ] }, { @@ -32,7 +25,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Installation" + "## Installation" ] }, { @@ -52,7 +45,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Installation from unofficial distributions" + "### Installation from unofficial distributions" ] }, { @@ -66,7 +59,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "####Anaconda" + "#### Anaconda" ] }, { @@ -82,7 +75,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "####Canopy" + "#### Canopy" ] }, { @@ -98,7 +91,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Launching IPython Notebook" + "## Launching IPython Notebook" ] }, { @@ -116,7 +109,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##How to learn from this resource?" + "## How to learn from this resource?" ] }, { @@ -136,7 +129,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##License" + "## Get the notebooks\n", + "\n", + "The notebooks are available [on github](https://github.com/abingham/Python-Lectures). They are based on the [original work by Rajath Kumar M. P.](https://github.com/rajathkmp/Python-Lectures)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## License" ] }, { @@ -149,23 +151,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/.ipynb_checkpoints/01-checkpoint.ipynb b/.ipynb_checkpoints/01-checkpoint.ipynb index a9668bc..49a6d63 100644 --- a/.ipynb_checkpoints/01-checkpoint.ipynb +++ b/.ipynb_checkpoints/01-checkpoint.ipynb @@ -4,22 +4,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#The Zen Of Python" + "# The Zen Of Python" ] }, { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -64,15 +55,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "A name that is used to denote something or a value is called a variable. In python, variables can be declared and values can be assigned to it as follows," + "A name that is used to refer to an object is called a variable. In Python, variables can be declared and values can be assigned to it as follows:" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ "x = 2\n", @@ -82,21 +71,22 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 46, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "7 Hey\n" - ] + "data": { + "text/plain": [ + "(2, 'Hey')" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print x+y, xy" + "x + y, xy" ] }, { @@ -108,10 +98,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, + "execution_count": 5, + "metadata": {}, "outputs": [], "source": [ "x = y = 1" @@ -119,21 +107,22 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 47, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 1\n" - ] + "data": { + "text/plain": [ + "(1, 1)" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print x,y" + "x, y" ] }, { @@ -147,7 +136,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Arithmetic Operators" + "## Arithmetic Operators" ] }, { @@ -167,10 +156,8 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "data": { @@ -178,7 +165,7 @@ "3" ] }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -189,10 +176,8 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 8, + "metadata": {}, "outputs": [ { "data": { @@ -200,7 +185,7 @@ "1" ] }, - "execution_count": 7, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -211,10 +196,8 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 9, + "metadata": {}, "outputs": [ { "data": { @@ -222,7 +205,7 @@ "2" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -233,18 +216,16 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 10, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "0" + "0.5" ] }, - "execution_count": 9, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -253,19 +234,10 @@ "1/2" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "0? This is because both the numerator and denominator are integers but the result is a float value hence an integer value is returned. By changing either the numerator or the denominator to float, correct answer can be obtained." - ] - }, { "cell_type": "code", "execution_count": 10, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -285,9 +257,7 @@ { "cell_type": "code", "execution_count": 11, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -308,36 +278,34 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Floor division is nothing but converting the result so obtained to the nearest integer." + "Floor division with `//` gives you the floor of the division result:" ] }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1.0" + "1" ] }, - "execution_count": 12, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "2.8//2.0" + "3 // 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Relational Operators" + "## Relational Operators" ] }, { @@ -356,10 +324,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": true - }, + "execution_count": 15, + "metadata": {}, "outputs": [], "source": [ "z = 1" @@ -367,10 +333,8 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { "data": { @@ -378,7 +342,7 @@ "True" ] }, - "execution_count": 14, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -389,10 +353,8 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { "data": { @@ -400,7 +362,7 @@ "False" ] }, - "execution_count": 15, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -413,7 +375,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Bitwise Operators" + "## Bitwise Operators" ] }, { @@ -432,43 +394,58 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": true - }, + "execution_count": 24, + "metadata": {}, "outputs": [], "source": [ - "a = 2 #10\n", - "b = 3 #11" + "a = 0b10 # decimal 2\n", + "b = 0b11 # decimal 3" ] }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 48, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "0b10\n" - ] + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print a & b\n", - "print bin(a&b)" + "a & b" ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'0b10'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bin(a&b)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, "outputs": [ { "data": { @@ -476,7 +453,7 @@ "2" ] }, - "execution_count": 18, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -489,19 +466,17 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "0000 0101 -> 5 \n", + "`0000 0101` -> 5 \n", "\n", "Shifting the digits by 1 to the right and zero padding\n", "\n", - "0000 0010 -> 2" + "`0000 0010` -> 2" ] }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, + "execution_count": 51, + "metadata": {}, "outputs": [ { "data": { @@ -509,7 +484,7 @@ "10" ] }, - "execution_count": 19, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -522,18 +497,18 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "0000 0101 -> 5 \n", + "`0000 0101` -> 5 \n", "\n", "Shifting the digits by 1 to the left and zero padding\n", "\n", - "0000 1010 -> 10" + "`0000 1010` -> 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Built-in Functions" + "# Built-in Functions" ] }, { @@ -547,22 +522,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Conversion from one system to another" + "## Conversion from one system to another" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Conversion from hexadecimal to decimal is done by adding prefix **0x** to the hexadecimal value or vice versa by using built in **hex( )**, Octal to decimal by adding prefix **0** to the octal value or vice versa by using built in function **oct( )**." + "Conversion from hexadecimal to decimal is done by adding prefix `0x` to the hexadecimal value or vice versa by using built in `hex( )`, Octal to decimal by adding prefix `0` to the octal value or vice versa by using built in function `oct( )`." ] }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 52, + "metadata": {}, "outputs": [ { "data": { @@ -570,7 +543,7 @@ "'0xaa'" ] }, - "execution_count": 20, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } @@ -581,10 +554,8 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, + "execution_count": 53, + "metadata": {}, "outputs": [ { "data": { @@ -592,7 +563,7 @@ "170" ] }, - "execution_count": 21, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } @@ -603,18 +574,16 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, + "execution_count": 54, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'010'" + "'0o10'" ] }, - "execution_count": 22, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -625,97 +594,89 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, + "execution_count": 55, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "8" + "2" ] }, - "execution_count": 23, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "010" + "0b10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**int( )** accepts two values when used for conversion, one is the value in a different number system and the other is its base. Note that input number in the different number system should be of string type." + "`int( )` accepts two values when used for conversion, one is the value in a different number system and the other is its base. Note that input number in the different number system should be of string type." ] }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, + "execution_count": 56, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "8\n", - "170\n", - "10\n" - ] + "data": { + "text/plain": [ + "(8, 170, 10)" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print int('010',8)\n", - "print int('0xaa',16)\n", - "print int('1010',2)" + "int('010',8), int('0xaa',16), int('1010',2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**int( )** can also be used to get only the integer value of a float number or can be used to convert a number which is of type string to integer format. Similarly, the function **str( )** can be used to convert the integer back to string format" + "`int( )` can also be used to get only the integer value of a float number or can be used to convert a number which is of type string to integer format. Similarly, the function `str( )` can be used to convert the integer back to string format" ] }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, + "execution_count": 57, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "7\n" - ] + "data": { + "text/plain": [ + "(7, 7)" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print int(7.7)\n", - "print int('7')" + "int(7.7), int('7')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Also note that function **bin( )** is used for binary and **float( )** for decimal/float values. **chr( )** is used for converting ASCII to its alphabet equivalent, **ord( )** is used for the other way round." + "Also note that function `bin( )` is used for binary and `float( )` for decimal/float values. `chr( )` is used for converting ASCII to its alphabet equivalent, `ord( )` is used for the other way round." ] }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, + "execution_count": 58, + "metadata": {}, "outputs": [ { "data": { @@ -723,7 +684,7 @@ "'b'" ] }, - "execution_count": 26, + "execution_count": 58, "metadata": {}, "output_type": "execute_result" } @@ -734,10 +695,8 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, + "execution_count": 59, + "metadata": {}, "outputs": [ { "data": { @@ -745,7 +704,7 @@ "98" ] }, - "execution_count": 27, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } @@ -758,78 +717,77 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Simplifying Arithmetic Operations" + "## Simplifying Arithmetic Operations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**round( )** function rounds the input value to a specified number of places or to the nearest integer. " + "`round( )` function rounds the input value to a specified number of places or to the nearest integer. " ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 60, "metadata": { - "collapsed": false, "scrolled": false }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "6.0\n", - "4.56\n" - ] + "data": { + "text/plain": [ + "(6, 4.56)" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print round(5.6231) \n", - "print round(4.55892, 2)" + "round(5.6231), round(4.55892, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**complex( )** is used to define a complex number and **abs( )** outputs the absolute value of the same." + "`complex( )` is used to define a complex number and `abs( )` outputs the absolute value of the same." ] }, { "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, + "execution_count": 61, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "5.38516480713\n" - ] + "data": { + "text/plain": [ + "5.385164807134504" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "c =complex('5+2j')\n", - "print abs(c)" + "c = complex('5+2j')\n", + "abs(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**divmod(x,y)** outputs the quotient and the remainder in a tuple(you will be learning about it in the further chapters) in the format (quotient, remainder). " + "`divmod(x,y)` outputs the quotient and the remainder in a tuple(you will be learning about it in the further chapters) in the format (quotient, remainder). " ] }, { "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, + "execution_count": 39, + "metadata": {}, "outputs": [ { "data": { @@ -837,7 +795,7 @@ "(4, 1)" ] }, - "execution_count": 30, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -850,256 +808,216 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**isinstance( )** returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once." + "`isinstance( )` returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once." ] }, { "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, + "execution_count": 62, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n", - "True\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print isinstance(1, int)\n", - "print isinstance(1.0,int)\n", - "print isinstance(1.0,(int,float))" + "isinstance(1, int)" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 63, "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "**cmp(x,y)**\n", - "\n", - "|x ? y|Output|\n", - "|---|---|\n", - "| x < y | -1 |\n", - "| x == y | 0 |\n", - "| x > y | 1 |" + "isinstance(1.0,int)" ] }, { "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, + "execution_count": 64, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "-1\n", - "1\n", - "0\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print cmp(1,2)\n", - "print cmp(2,1)\n", - "print cmp(2,2)" + "isinstance(1.0,(int,float))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**pow(x,y,z)** can be used to find the power $x^y$ also the mod of the resulting value with the third specified number can be found i.e. : ($x^y$ % z)." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "27\n", - "2\n" - ] - } - ], - "source": [ - "print pow(3,3)\n", - "print pow(3,3,5)" + "`cmp(x,y)`\n", + "\n", + "|x ? y|Output|\n", + "|---|---|\n", + "| x < y | -1 |\n", + "| x == y | 0 |\n", + "| x > y | 1 |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**range( )** function outputs the integers of the specified range. It can also be used to generate a series by specifying the difference between the two numbers within a particular range. The elements are returned in a list (will be discussing in detail later.)" + "`pow(x,y,z)` can be used to find the power $x^y$ also the mod of the resulting value with the third specified number can be found i.e. : ($x^y$ % z)." ] }, { "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, + "execution_count": 65, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 2]\n", - "[2, 3, 4, 5, 6, 7, 8]\n", - "[2, 10, 18, 26]\n" - ] + "data": { + "text/plain": [ + "(27, 2)" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print range(3)\n", - "print range(2,9)\n", - "print range(2,27,8)" + "pow(3,3), pow(3,3,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Accepting User Inputs" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**raw_input( )** accepts input and stores it as a string. Hence, if the user inputs a integer, the code should convert the string to an integer and then proceed." + "`range( )` generates the sequence of integers of the specified range. It can also be used to generate a series by specifying the difference between the two numbers within a particular range. The elements are lazily generated, and will only be generated on demand (e.g. by passing them to the `list` constructor):" ] }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, + "execution_count": 66, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Type something here and it will be stored in variable abc \tHey\n" - ] + "data": { + "text/plain": [ + "range(0, 3)" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "abc = raw_input(\"Type something here and it will be stored in variable abc \\t\")" + "range(5)" ] }, { "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, + "execution_count": 67, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "str" + "[0, 1, 2]" ] }, - "execution_count": 36, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "type(abc)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**input( )**, this is used only for accepting only integer inputs." + "list(range(5))" ] }, { "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, + "execution_count": 68, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Only integer can be stored in in variable abc \t275\n" - ] + "data": { + "text/plain": [ + "[2, 3, 4, 5, 6, 7, 8]" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "abc1 = input(\"Only integer can be stored in in variable abc \\t\")" + "list(range(2, 9))" ] }, { "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, + "execution_count": 69, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "int" + "[2, 10, 18, 26]" ] }, - "execution_count": 38, + "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "type(abc1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note that **type( )** returns the format or the type of a variable or a number" + "list(range(2,27,8))" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/.ipynb_checkpoints/02-checkpoint.ipynb b/.ipynb_checkpoints/02-checkpoint.ipynb index c4cb722..587bde7 100644 --- a/.ipynb_checkpoints/02-checkpoint.ipynb +++ b/.ipynb_checkpoints/02-checkpoint.ipynb @@ -4,34 +4,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "# `print`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Print Statement" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The **print** statement can be used in the following different ways :\n", - "\n", - " - print \"Hello World\"\n", - " - print \"Hello\", \n", - " - print \"Hello\" + \n", - " - print \"Hello %s\" % " + "The `print` function is used to print one or more objects to the screen:" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, + "execution_count": 45, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -42,568 +28,392 @@ } ], "source": [ - "print \"Hello World\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In Python, single, double and triple quotes are used to denote a string.\n", - "Most use single quotes when declaring a single character. \n", - "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines." + "print(\"Hello World\")" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 56, "metadata": { - "collapsed": false + "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Hey\n" + "Hello world !\n" ] } ], "source": [ - "print 'Hey'" + "print(\"Hello\", \"world\", \"!\")" ] }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 57, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "My name is Rajath Kumar M.P.\n", - "\n", - "I love Python.\n" + "1 2.3 [4, 5, 6]\n" ] } ], "source": [ - "print \"\"\"My name is Rajath Kumar M.P.\n", - "\n", - "I love Python.\"\"\"" + "print(1, 2.3, [4, 5, 6])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement." + "# Strings\n", + "\n", + "In Python, single, double and triple quotes are used to denote a string.\n", + "Most use single quotes when declaring a single character. \n", + "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines." ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false, - "scrolled": true - }, + "execution_count": 46, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n", - "Hello World !\n" - ] + "data": { + "text/plain": [ + "'Hey'" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "string1 = 'World'\n", - "print 'Hello', string1\n", - "\n", - "string2 = '!'\n", - "print 'Hello', string1, string2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings." + "'Hey'" ] }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 47, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "HelloWorld!\n" - ] + "data": { + "text/plain": [ + "'My name is Rajath Kumar M.P.\\n\\nI love Python.'" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print 'Hello' + string1 + string2" + "\"\"\"My name is Rajath Kumar M.P.\n", + "\n", + "I love Python.\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**%s** is used to refer to a variable which contains a string." + "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement." ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 48, "metadata": { - "collapsed": false + "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Hello World\n" + "Hello World\n", + "Hello World !\n" ] } ], "source": [ - "print \"Hello %s\" % string1" + "string1 = 'World'\n", + "print('Hello', string1)\n", + "\n", + "string2 = '!'\n", + "print('Hello', string1, string2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Similarly, when using other data types\n", - "\n", - " - %s -> string\n", - " - %d -> Integer\n", - " - %f -> Float\n", - " - %o -> Octal\n", - " - %x -> Hexadecimal\n", - " - %e -> exponential\n", - " \n", - "This can be used for conversions inside the print statement itself." + "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings." ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 49, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Actual Number = 18\n", - "Float of the number = 18.000000\n", - "Octal equivalent of the number = 22\n", - "Hexadecimal equivalent of the number = 12\n", - "Exponential equivalent of the number = 1.800000e+01\n" - ] + "data": { + "text/plain": [ + "'HelloWorld!'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print \"Actual Number = %d\" %18\n", - "print \"Float of the number = %f\" %18\n", - "print \"Octal equivalent of the number = %o\" %18\n", - "print \"Hexadecimal equivalent of the number = %x\" %18\n", - "print \"Exponential equivalent of the number = %e\" %18" + "'Hello' + string1 + string2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "When referring to multiple variables parenthesis is used." + "The `format()` method on strings allows you to interpolate objects into templates. While string formatting can get quite sophisticated, in general you just use `{}` where you want your arguments to go:" ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 50, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World !\n" - ] + "data": { + "text/plain": [ + "'Hello World'" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print \"Hello %s %s\" %(string1,string2)" + "\"Hello {}\".format(string1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Other Examples" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The following are other different ways the print statement can be put to use." + "This works for all kinds of objects, not just string interpolants:" ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I want %d to be printed here\n" - ] - } - ], - "source": [ - "print \"I want %%d to be printed %s\" %'here'" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "_A_A_A_A_A_A_A_A_A_A\n" - ] - } - ], - "source": [ - "print '_A'*10" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Jan\n", - "Feb\n", - "Mar\n", - "Apr\n", - "May\n", - "Jun\n", - "Jul\n", - "Aug\n" - ] - } - ], - "source": [ - "print \"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\"" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 51, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "I want \\n to be printed.\n" - ] + "data": { + "text/plain": [ + "'integers: 18'" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print \"I want \\\\n to be printed.\"" + "\"integers: {}\".format(18)" ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Routine:\n", - "\t- Eat\n", - "\t- Sleep\n", - "\t- Repeat\n", - "\n" - ] - } - ], - "source": [ - "print \"\"\"\n", - "Routine:\n", - "\\t- Eat\n", - "\\t- Sleep\\n\\t- Repeat\n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#PrecisionWidth and FieldWidth" - ] - }, - { - "cell_type": "markdown", + "execution_count": 52, "metadata": {}, - "source": [ - "Fieldwidth is the width of the entire number and precision is the width towards the right. One can alter these widths based on the requirements.\n", - "\n", - "The default Precision Width is set to 6." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, "outputs": [ { "data": { "text/plain": [ - "'3.121312'" + "'floats: 4.2'" ] }, - "execution_count": 14, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"%f\" % 3.121312312312" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Notice upto 6 decimal points are returned. To specify the number of decimal points, '%(fieldwidth).(precisionwidth)f' is used." + "\"floats: {}\".format(4.2)" ] }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, + "execution_count": 53, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'3.12131'" + "'lists: [1, 2, 3]'" ] }, - "execution_count": 15, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"%.5f\" % 3.121312312312" + "\"lists: {}\".format([1,2,3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "If the field width is set more than the necessary than the data right aligns itself to adjust to the specified values." + "You can pass multiple arguments to `format()` as well:" ] }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 54, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "' 3.12131'" + "'Hello World!'" ] }, - "execution_count": 16, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"%9.5f\" % 3.121312312312" + "\"Hello {}{}\".format(string1, string2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Zero padding is done by adding a 0 at the start of fieldwidth." + "# Precision and padding" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "By default `format()` will print the full precision of a `float`:" ] }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 55, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'00000000000003.12131'" + "'3.121312312312'" ] }, - "execution_count": 17, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"%020.5f\" % 3.121312312312" + "\"{}\".format(3.121312312312)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "For proper alignment, a space can be left blank in the field width so that when a negative number is used, proper alignment is maintained." + "You can limit this by using `{:.}` in the format specifier: " ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " 3.121312\n", - "-3.121312\n" - ] - } - ], - "source": [ - "print \"% 9f\" % 3.121312312312\n", - "print \"% 9f\" % -3.121312312312" - ] - }, - { - "cell_type": "markdown", + "execution_count": 43, "metadata": {}, - "source": [ - "'+' sign can be returned at the beginning of a positive number by adding a + sign at the beginning of the field width." - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "+3.121312\n", - "-3.121312\n" - ] + "data": { + "text/plain": [ + "'3.12'" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print \"%+9f\" % 3.121312312312\n", - "print \"% 9f\" % -3.121312312312" + "\"{:.3}\".format(3.121312312312)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "As mentioned above, the data right aligns itself when the field width mentioned is larger than the actualy field width. But left alignment can be done by specifying a negative symbol in the field width." + "Similarly, you can specify padding with `{:.}`:" ] }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 44, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'3.121 '" + "' 3.12'" ] }, - "execution_count": 20, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"%-9.3f\" % 3.121312312312" + "'{:6.3}'.format(3.121312312312)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This just scratches the surface of Python string formatting. Read all about it in [PEP 3101](https://www.python.org/dev/peps/pep-3101/)." ] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/.ipynb_checkpoints/03-checkpoint.ipynb b/.ipynb_checkpoints/03-checkpoint.ipynb index 7a9de7f..823f5b0 100644 --- a/.ipynb_checkpoints/03-checkpoint.ipynb +++ b/.ipynb_checkpoints/03-checkpoint.ipynb @@ -4,28 +4,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "# Data Structures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Data Structures" + "In simple terms, a data structure is a collection or group of data in a particular structure." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "In simple terms, It is the the collection or group of data in a particular structure." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Lists" + "## Lists" ] }, { @@ -34,55 +27,36 @@ "source": [ "Lists are the most commonly used data structure. Think of it as a sequence of data that is enclosed in square brackets and data are separated by a comma. Each of these data can be accessed by calling it's index value.\n", "\n", - "Lists are declared by just equating a variable to '[ ]' or list." + "Lists can be declared in literal form with `[ ]`:" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [], "source": [ - "a = []" + "x = ['apple', 'pear', 'orange']" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print type(a)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "One can directly assign the sequence of data to a list x as shown." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "x = ['apple', 'orange']" + "type(x)" ] }, { @@ -96,15 +70,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In python, Indexing starts from 0. Thus now the list x, which has two elements will have apple at 0 index and orange at 1 index." + "In python, Indexing starts from 0. Thus now the list `x`, which has two elements will have apple at 0 index and orange at 1 index." ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "data": { @@ -112,7 +84,7 @@ "'apple'" ] }, - "execution_count": 4, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -130,10 +102,8 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 15, + "metadata": {}, "outputs": [ { "data": { @@ -141,7 +111,7 @@ "'orange'" ] }, - "execution_count": 5, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -154,15 +124,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "As you might have already guessed, x[0] = x[-2], x[1] = x[-1]. This concept can be extended towards lists with more many elements." + "As you might have already guessed, `x[0] = x[-2], x[1] = x[-1]`. This concept can be extended towards lists with more many elements." ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [], "source": [ "y = ['carrot','potato']" @@ -172,27 +140,28 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Here we have declared two lists x and y each containing its own data. Now, these two lists can again be put into another list say z which will have it's data as two lists. This list inside a list is called as nested lists and is how an array would be declared which we will see later." + "Here we have declared two lists `x` and `y` each containing its own data. Now, these two lists can again be put into another list say `z` which will have it's data as two lists. This list inside a list is called as nested lists and is how an array would be declared which we will see later." ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[['apple', 'orange'], ['carrot', 'potato']]\n" - ] + "data": { + "text/plain": [ + "[['apple', 'pear', 'orange'], ['carrot', 'potato']]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "z = [x,y]\n", - "print z" + "z = [x,y]\n", + "z" ] }, { @@ -202,42 +171,41 @@ "Indexing in nested lists can be quite confusing if you do not understand how indexing works in python. So let us break it down and then arrive at a conclusion.\n", "\n", "Let us access the data 'apple' in the above nested list.\n", - "First, at index 0 there is a list ['apple','orange'] and at index 1 there is another list ['carrot','potato']. Hence z[0] should give us the first list which contains 'apple'." + "First, at index 0 there is a list ['apple','orange'] and at index 1 there is another list ['carrot','potato']. Hence `z[0]` should give us the first list which contains 'apple'." ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'orange']\n" - ] + "data": { + "text/plain": [ + "['apple', 'pear', 'orange']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "z1 = z[0]\n", - "print z1" + "z1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Now observe that z1 is not at all a nested list thus to access 'apple', z1 should be indexed at 0." + "Now observe that `z1` is not at all a nested list thus to access 'apple', `z1` should be indexed at 0." ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "data": { @@ -245,7 +213,7 @@ "'apple'" ] }, - "execution_count": 9, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -258,15 +226,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Instead of doing the above, In python, you can access 'apple' by just writing the index values each time side by side." + "Instead of doing the above, In Python, you can access 'apple' by just writing the index values each time side by side." ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, "outputs": [ { "data": { @@ -274,7 +240,7 @@ "'apple'" ] }, - "execution_count": 10, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -287,7 +253,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If there was a list inside a list inside a list then you can access the innermost value by executing z[ ][ ][ ]." + "If there was a list inside a list inside a list then you can access the innermost value by executing `z[ ][ ][ ]`." ] }, { @@ -301,41 +267,50 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Indexing was only limited to accessing a single element, Slicing on the other hand is accessing a sequence of data inside the list. In other words \"slicing\" the list.\n", + "Indexing was only limited to accessing a single element. Slicing on the other hand is accessing a sequence of data inside the list. In other words \"slicing\" the list.\n", "\n", - "Slicing is done by defining the index values of the first element and the last element from the parent list that is required in the sliced list. It is written as parentlist[ a : b ] where a,b are the index values from the parent list. If a or b is not defined then the index value is considered to be the first value for a if a is not defined and the last value for b when b is not defined." + "Slicing is done by defining the index values of the first element and the last element from the parent list that is required in the sliced list. It is written as parentlist `[ a : b ]` where `a,b` are the index values from the parent list. If a or b is not defined then the index value is considered to be the first value for a if a is not defined and the last value for b when b is not defined." ] }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "num = [0,1,2,3,4,5,6,7,8,9]" + "num = [0,1,2,3,4,5,6,7,8,9]\n", + "num[0:4]" ] }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 23, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 2, 3]\n", - "[4, 5, 6, 7, 8, 9]\n" - ] + "data": { + "text/plain": [ + "[4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print num[0:4]\n", - "print num[4:]" + "num[4:]" ] }, { @@ -347,10 +322,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 25, + "metadata": {}, "outputs": [ { "data": { @@ -358,7 +331,7 @@ "[0, 3, 6]" ] }, - "execution_count": 13, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } @@ -371,22 +344,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Built in List Functions" + "### Built in List Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "To find the length of the list or the number of elements in a list, **len( )** is used." + "To find the length of the list or the number of elements in a list, `len( )` is used." ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 27, + "metadata": {}, "outputs": [ { "data": { @@ -394,7 +365,7 @@ "10" ] }, - "execution_count": 14, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -407,15 +378,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If the list consists of all integer elements then **min( )** and **max( )** gives the minimum and maximum value in the list." + "If the list consists of all integer elements then `min( )` and `max( )` gives the minimum and maximum value in the list." ] }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, + "execution_count": 28, + "metadata": {}, "outputs": [ { "data": { @@ -423,7 +392,7 @@ "0" ] }, - "execution_count": 15, + "execution_count": 28, "metadata": {}, "output_type": "execute_result" } @@ -434,10 +403,8 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 29, + "metadata": {}, "outputs": [ { "data": { @@ -445,7 +412,7 @@ "9" ] }, - "execution_count": 16, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } @@ -463,10 +430,8 @@ }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 30, + "metadata": {}, "outputs": [ { "data": { @@ -474,7 +439,7 @@ "[1, 2, 3, 5, 4, 7]" ] }, - "execution_count": 17, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -492,10 +457,8 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, + "execution_count": 31, + "metadata": {}, "outputs": [], "source": [ "names = ['Earth','Air','Fire','Water']" @@ -510,10 +473,8 @@ }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, + "execution_count": 32, + "metadata": {}, "outputs": [ { "data": { @@ -521,7 +482,7 @@ "True" ] }, - "execution_count": 19, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -532,10 +493,8 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 33, + "metadata": {}, "outputs": [ { "data": { @@ -543,7 +502,7 @@ "False" ] }, - "execution_count": 20, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -556,15 +515,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In a list with elements as string, **max( )** and **min( )** is applicable. **max( )** would return a string element whose ASCII value is the highest and the lowest when **min( )** is used. Note that only the first index of each element is considered each time and if they value is the same then second index considered so on and so forth." + "In a list with elements as string, `max( )` and `min( )` is applicable. `max( )` would return a string element whose ASCII value is the highest and the lowest when `min( )` is used. Note that only the first index of each element is considered each time and if they value is the same then second index considered so on and so forth." ] }, { "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": true - }, + "execution_count": 34, + "metadata": {}, "outputs": [], "source": [ "mlist = ['bzaa','ds','nc','az','z','klm']" @@ -572,23 +529,22 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, + "execution_count": 35, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "z\n", - "az\n" - ] + "data": { + "text/plain": [ + "('z', 'az')" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print max(mlist)\n", - "print min(mlist)" + "max(mlist), min(mlist)" ] }, { @@ -600,10 +556,8 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": true - }, + "execution_count": 36, + "metadata": {}, "outputs": [], "source": [ "nlist = ['1','94','93','1000']" @@ -611,77 +565,73 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, + "execution_count": 38, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "94\n", - "1\n" - ] + "data": { + "text/plain": [ + "('94', '1')" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print max(nlist)\n", - "print min(nlist)" + "max(nlist), min(nlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Even if the numbers are declared in a string the first index of each element is considered and the maximum and minimum values are returned accordingly." + "Here, the strings are still sorted as string (that is, lexically) even though they \"look like\" numbers." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "But if you want to find the **max( )** string element based on the length of the string then another parameter 'key=len' is declared inside the **max( )** and **min( )** function." + "But if you want to find the `max( )` string element based on the length of the string then another parameter 'key=len' is declared inside the `max( )` and `min( )` function." ] }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, + "execution_count": 39, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Earth\n", - "Air\n" - ] + "data": { + "text/plain": [ + "('Earth', 'Air')" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print max(names, key=len)\n", - "print min(names, key=len)" + "max(names, key=len), min(names, key=len)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "But even 'Water' has length 5. **max()** or **min()** function returns the first element when there are two or more elements with the same length.\n", + "But even 'Water' has length 5. `max()` or `min()` function returns the first element when there are two or more elements with the same length.\n", "\n", - "Any other built in function can be used or lambda function (will be discussed later) in place of len.\n", + "Any other built in function can be used or lambda function (will be discussed later) in place of `len`.\n", "\n", - "A string can be converted into a list by using the **list()** function." + "A string can be converted into a list by using the `list()` function." ] }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, + "execution_count": 40, + "metadata": {}, "outputs": [ { "data": { @@ -689,7 +639,7 @@ "['h', 'e', 'l', 'l', 'o']" ] }, - "execution_count": 26, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -702,15 +652,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**append( )** is used to add a element at the end of the list." + "`append( )` is used to add a element at the end of the list." ] }, { "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, + "execution_count": 41, + "metadata": {}, "outputs": [], "source": [ "lst = [1,1,4,8,7]" @@ -718,37 +666,36 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, + "execution_count": 42, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1]\n" - ] + "data": { + "text/plain": [ + "[1, 1, 4, 8, 7, 1]" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.append(1)\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**count( )** is used to count the number of a particular element that is present in the list. " + "`count( )` is used to count the number of a particular element that is present in the list. " ] }, { "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, + "execution_count": 43, + "metadata": {}, "outputs": [ { "data": { @@ -756,7 +703,7 @@ "3" ] }, - "execution_count": 29, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } @@ -769,15 +716,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**append( )** function can also be used to add a entire list at the end. Observe that the resultant list becomes a nested list." + "`append( )` function can also be used to add a entire list at the end. Observe that the resultant list becomes a nested list." ] }, { "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": true - }, + "execution_count": 44, + "metadata": {}, "outputs": [], "source": [ "lst1 = [5,4,2,8]" @@ -785,66 +730,64 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, + "execution_count": 45, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8]]\n" - ] + "data": { + "text/plain": [ + "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8]]" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.append(lst1)\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ - "But if nested list is not what is desired then **extend( )** function can be used." + "But if nested list is not what is desired then `extend( )` function can be used." ] }, { "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, + "execution_count": 46, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] - } - ], - "source": [ - "lst.extend(lst1)\n", - "print lst" - ] + "data": { + "text/plain": [ + "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2, 8]" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst.extend(lst1)\n", + "lst" + ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**index( )** is used to find the index value of a particular element. Note that if there are multiple elements of the same value then the first index value of that element is returned." + "`index( )` is used to find the index value of a particular element. Note that if there are multiple elements of the same value then the first index value of that element is returned." ] }, { "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, + "execution_count": 47, + "metadata": {}, "outputs": [ { "data": { @@ -852,7 +795,7 @@ "0" ] }, - "execution_count": 33, + "execution_count": 47, "metadata": {}, "output_type": "execute_result" } @@ -865,69 +808,69 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**insert(x,y)** is used to insert a element y at a specified index value x. **append( )** function made it only possible to insert at the end. " + "`insert(x,y)` is used to insert a element y at a specified index value x. `append( )` function made it only possible to insert at the end. " ] }, { "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, + "execution_count": 48, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] + "data": { + "text/plain": [ + "[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4, 2, 8]" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.insert(5, 'name')\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**insert(x,y)** inserts but does not replace element. If you want to replace the element with another element you simply assign the value to that particular index." + "`insert(x,y)` inserts but does not replace element. If you want to replace the element with another element you simply assign the value to that particular index." ] }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, + "execution_count": 49, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] + "data": { + "text/plain": [ + "[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2, 8]" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst[5] = 'Python'\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**pop( )** function return the last element in the list. This is similar to the operation of a stack. Hence it wouldn't be wrong to tell that lists can be used as a stack." + "`pop( )` function return the last element in the list. This is similar to the operation of a stack. Hence it wouldn't be wrong to tell that lists can be used as a stack." ] }, { "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, + "execution_count": 50, + "metadata": {}, "outputs": [ { "data": { @@ -935,7 +878,7 @@ "8" ] }, - "execution_count": 36, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -953,10 +896,8 @@ }, { "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, + "execution_count": 51, + "metadata": {}, "outputs": [ { "data": { @@ -964,7 +905,7 @@ "1" ] }, - "execution_count": 37, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -977,83 +918,84 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**pop( )** is used to remove element based on it's index value which can be assigned to a variable. One can also remove element by specifying the element itself using the **remove( )** function." + "`pop( )` is used to remove element based on it's index value which can be assigned to a variable. One can also remove element by specifying the element itself using the `remove( )` function." ] }, { "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, + "execution_count": 52, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n" - ] + "data": { + "text/plain": [ + "[1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.remove('Python')\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Alternative to **remove** function but with using index value is **del**" + "Alternative to `remove` function but with using index value is `del`" ] }, { "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, + "execution_count": 54, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n" - ] + "data": { + "text/plain": [ + "[1, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "del lst[1]\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ - "The entire elements present in the list can be reversed by using the **reverse()** function." + "The entire elements present in the list can be reversed by using the `reverse()` function." ] }, { "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, + "execution_count": 55, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 4, 5, [5, 4, 2, 8], 1, 7, 8, 1]\n" - ] + "data": { + "text/plain": [ + "[2, 4, 5, [5, 4, 2, 8], 1, 7, 8, 1]" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.reverse()\n", - "print lst" + "lst" ] }, { @@ -1062,27 +1004,29 @@ "source": [ "Note that the nested list [5,4,2,8] is treated as a single element of the parent list lst. Thus the elements inside the nested list is not reversed.\n", "\n", - "Python offers built in operation **sort( )** to arrange the elements in ascending order." + "Python offers built in operation `sort( )` to arrange the elements in ascending order." ] }, { "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false - }, + "execution_count": 57, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 2, 4, 5, 7, 8, [5, 4, 2, 8]]\n" - ] + "data": { + "text/plain": [ + "[1, 2, 3, 6, 7, 9, 42]" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ + "lst = [9, 6, 3, 7, 2, 1, 42]\n", "lst.sort()\n", - "print lst" + "lst" ] }, { @@ -1094,52 +1038,72 @@ }, { "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false - }, + "execution_count": 59, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[5, 4, 2, 8], 8, 7, 5, 4, 2, 1, 1]\n" - ] + "data": { + "text/plain": [ + "[42, 9, 7, 6, 3, 2, 1]" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.sort(reverse=True)\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Similarly for lists containing string elements, **sort( )** would sort the elements based on it's ASCII value in ascending and by specifying reverse=True in descending." + "Similarly for lists containing string elements, `sort( )` would sort the elements based on it's ASCII value in ascending and by specifying `reverse=True` in descending." ] }, { "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, + "execution_count": 61, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Air', 'Earth', 'Fire', 'Water']\n", - "['Water', 'Fire', 'Earth', 'Air']\n" - ] + "data": { + "text/plain": [ + "['Air', 'Earth', 'Fire', 'Water']" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "names.sort()\n", - "print names\n", + "names" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Water', 'Fire', 'Earth', 'Air']" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ "names.sort(reverse=True)\n", - "print names" + "names" ] }, { @@ -1151,25 +1115,44 @@ }, { "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, + "execution_count": 63, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Air', 'Fire', 'Water', 'Earth']\n", - "['Water', 'Earth', 'Fire', 'Air']\n" - ] + "data": { + "text/plain": [ + "['Air', 'Fire', 'Water', 'Earth']" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "names.sort(key=len)\n", - "print names\n", + "names" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Water', 'Earth', 'Fire', 'Air']" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ "names.sort(key=len,reverse=True)\n", - "print names" + "names" ] }, { @@ -1183,212 +1166,240 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Most of the new python programmers commit this mistake. Consider the following," + "To copy a list, it's not sufficient to simply assign a new name to it. Consider the following," ] }, { "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, + "execution_count": 66, + "metadata": {}, "outputs": [], "source": [ - "lista= [2,1,4,3]" + "lista = [2,1,4,3]" ] }, { "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, + "execution_count": 67, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] + "data": { + "text/plain": [ + "[2, 1, 4, 3]" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "listb = lista\n", - "print listb" + "listb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Here, We have declared a list, lista = [2,1,4,3]. This list is copied to listb by assigning it's value and it get's copied as seen. Now we perform some random operations on lista." + "Here, We have declared a list, `lista = [2,1,4,3]`. This list is assigned to `listb`. While we might expect `lista` and `listb` to be different lists, we can see that changes to `lista` are reflected in `listb`:" ] }, { "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, + "execution_count": 68, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4]\n", - "[2, 1, 4, 9]\n" - ] + "data": { + "text/plain": [ + "[2, 1, 4, 9]" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lista.pop()\n", - "print lista\n", "lista.append(9)\n", - "print lista" + "lista" ] }, { "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false - }, + "execution_count": 70, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 9]\n" - ] + "data": { + "text/plain": [ + "[2, 1, 4, 9]" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print listb" + "listb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "listb has also changed though no operation has been performed on it. This is because you have assigned the same memory space of lista to listb. So how do fix this?\n", + "`listb` has also changed though no operation has been performed on it. This is because `lista` and `listb` refer to the same object. So how do fix this?\n", "\n", - "If you recall, in slicing we had seen that parentlist[a:b] returns a list from parent list with start index a and end index b and if a and b is not mentioned then by default it considers the first and last element. We use the same concept here. By doing so, we are assigning the data of lista to listb as a variable." + "One way to copy a list is to construct a new list around it:" ] }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([1, 2, 3, 4], [2, 1, 4, 3])" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista = [2,1,4,3]\n", + "listb = list(lista)\n", + "\n", + "lista.sort()\n", + "\n", + "lista, listb" + ] + }, + { + "cell_type": "markdown", "metadata": { "collapsed": true }, - "outputs": [], "source": [ - "lista = [2,1,4,3]" + "Another approach is to use slicing. Since slicing creates copies, a full slice of a list will effectively create a copy of it:" ] }, { "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, + "execution_count": 74, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] + "data": { + "text/plain": [ + "([1, 2, 3, 4], [1, 2, 3, 4])" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "listb = lista[:]\n", - "print listb" + "lista, listb" ] }, { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4]\n", - "[2, 1, 4, 9]\n" - ] - } - ], + "cell_type": "markdown", + "metadata": {}, "source": [ - "lista.pop()\n", - "print lista\n", - "lista.append(9)\n", - "print lista" + "These lists are different objects but have identical contents. We can see this by modifying one of them:" ] }, { "cell_type": "code", - "execution_count": 52, - "metadata": { - "collapsed": false - }, + "execution_count": 75, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] + "data": { + "text/plain": [ + "([1, 2, 3], [1, 2, 3, 4])" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print listb" + "lista.pop()\n", + "lista, listb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Tuples" + "## Tuples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Tuples are similar to lists but only big difference is the elements inside a list can be changed but in tuple it cannot be changed. Think of tuples as something which has to be True for a particular something and cannot be True for no other values. For better understanding, Recall **divmod()** function." + "Tuples are similar to lists but only big difference is the elements inside a list can be changed but in tuple it cannot be changed. We can create tuples literals similar to how we create lists but using `()` instead:" ] }, { "cell_type": "code", - "execution_count": 53, - "metadata": { - "collapsed": false - }, + "execution_count": 76, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "(3, 1)\n", - "\n" - ] + "data": { + "text/plain": [ + "(3, 8, 5)" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "xyz = divmod(10,3)\n", - "print xyz\n", - "print type(xyz)" + "xyz = (3, 8, 5)\n", + "xyz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Here the quotient has to be 3 and the remainder has to be 1. These values cannot be changed whatsoever when 10 is divided by 3. Hence divmod returns these values in a tuple." + "We can see that this is a tuple with the `type()` function:" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(xyz)" ] }, { @@ -1400,10 +1411,8 @@ }, { "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": false - }, + "execution_count": 79, + "metadata": {}, "outputs": [], "source": [ "tup = ()\n", @@ -1414,15 +1423,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If you want to directly declare a tuple it can be done by using a comma at the end of the data." + "One thing to keep in mind is that if you want to declare a tuple with a single element, you need a trailing comma:" ] }, { "cell_type": "code", - "execution_count": 55, - "metadata": { - "collapsed": false - }, + "execution_count": 81, + "metadata": {}, "outputs": [ { "data": { @@ -1430,42 +1437,40 @@ "(27,)" ] }, - "execution_count": 55, + "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "27," + "(27,)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "27 when multiplied by 2 yields 54, But when multiplied with a tuple the data is repeated twice." + "You can multiply a tuple by an integer to repeat it that many times:" ] }, { "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": false - }, + "execution_count": 83, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "(27, 27)" + "('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c')" ] }, - "execution_count": 56, + "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "2*(27,)" + "3 * ('a', 'b', 'c')" ] }, { @@ -1477,112 +1482,124 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 84, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1, 2, 3)\n", - "('H', 'e', 'l', 'l', 'o')\n" - ] + "data": { + "text/plain": [ + "(1, 2, 3)" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "tup3 = tuple([1,2,3])\n", - "print tup3\n", - "tup4 = tuple('Hello')\n", - "print tup4" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "It follows the same indexing and slicing as Lists." + "tup3" ] }, { "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": false - }, + "execution_count": 85, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "('H', 'e', 'l')\n" - ] + "data": { + "text/plain": [ + "('H', 'e', 'l', 'l', 'o')" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print tup3[1]\n", - "tup5 = tup4[:3]\n", - "print tup5" + "tup4 = tuple('Hello')\n", + "tup4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Mapping one tuple to another" + "It follows the same indexing and slicing as Lists." ] }, { "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "(a,b,c)= ('alpha','beta','gamma')" + "tup3[1]" ] }, { "cell_type": "code", - "execution_count": 60, - "metadata": { - "collapsed": false - }, + "execution_count": 87, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "alpha beta gamma\n" - ] + "data": { + "text/plain": [ + "('H', 'e', 'l')" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print a,b,c" + "tup5 = tup4[:3]\n", + "tup5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tuple unpacking\n", + "\n", + "You can assign the elements of a tuple to variables by separating the variable names by commans:" ] }, { "cell_type": "code", - "execution_count": 61, - "metadata": { - "collapsed": false - }, + "execution_count": 89, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "('R', 'a', 'j', 'a', 't', 'h', 'K', 'u', 'm', 'a', 'r', 'M', 'P')\n" - ] + "data": { + "text/plain": [ + "'The elements are alpha, beta, and gamma'" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "d = tuple('RajathKumarMP')\n", - "print d" + "a,b,c = ('alpha','beta','gamma')\n", + "\"The elements are {}, {}, and {}\".format(a, b, c)" ] }, { @@ -1596,15 +1613,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**count()** function counts the number of specified element that is present in the tuple." + "`count()` function counts the number of specified element that is present in the tuple." ] }, { "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, + "execution_count": 93, + "metadata": {}, "outputs": [ { "data": { @@ -1612,12 +1627,13 @@ "3" ] }, - "execution_count": 62, + "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "d = tuple('alphabetical')\n", "d.count('a')" ] }, @@ -1625,23 +1641,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**index()** function returns the index of the specified element. If the elements are more than one then the index of the first element of that specified element is returned" + "`index()` function returns the index of the specified element. If the elements are more than one then the index of the first element of that specified element is returned" ] }, { "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": false - }, + "execution_count": 94, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1" + "0" ] }, - "execution_count": 63, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } @@ -1654,56 +1668,58 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Sets" + "## Sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Sets are mainly used to eliminate repeated numbers in a sequence/list. It is also used to perform some standard set operations.\n", + "Sets are collections of unique, immutable objects. It is also used to perform some standard set operations.\n", "\n", - "Sets are declared as set() which will initialize a empty set. Also set([sequence]) can be executed to declare a set with elements" + "Sets are declared as `set()` which will initialize a empty set. Also `set([sequence])` can be executed to declare a set with elements" ] }, { "cell_type": "code", - "execution_count": 64, - "metadata": { - "collapsed": false - }, + "execution_count": 95, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "set1 = set()\n", - "print type(set1)" + "type(set1)" ] }, { "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": false - }, + "execution_count": 96, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "set([1, 2, 3, 4])\n" - ] + "data": { + "text/plain": [ + "{1, 2, 3, 4}" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "set0 = set([1,2,2,3,3,4])\n", - "print set0" + "set0" ] }, { @@ -1717,15 +1733,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Built-in Functions" + "### Built-in Functions" ] }, { "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": false - }, + "execution_count": 97, + "metadata": {}, "outputs": [], "source": [ "set1 = set([1,2,3])" @@ -1733,10 +1747,8 @@ }, { "cell_type": "code", - "execution_count": 67, - "metadata": { - "collapsed": false - }, + "execution_count": 98, + "metadata": {}, "outputs": [], "source": [ "set2 = set([2,3,4,5])" @@ -1746,15 +1758,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**union( )** function returns a set which contains all the elements of both the sets without repition." + "`union( )` function returns a set which contains all the elements of both the sets without repition." ] }, { "cell_type": "code", - "execution_count": 68, - "metadata": { - "collapsed": false - }, + "execution_count": 99, + "metadata": {}, "outputs": [ { "data": { @@ -1762,7 +1772,7 @@ "{1, 2, 3, 4, 5}" ] }, - "execution_count": 68, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } @@ -1775,15 +1785,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**add( )** will add a particular element into the set. Note that the index of the newly added element is arbitrary and can be placed anywhere not neccessarily in the end." + "`add( )` will add a particular element into the set. Note that the index of the newly added element is arbitrary and can be placed anywhere not neccessarily in the end." ] }, { "cell_type": "code", - "execution_count": 69, - "metadata": { - "collapsed": false - }, + "execution_count": 100, + "metadata": {}, "outputs": [ { "data": { @@ -1791,7 +1799,7 @@ "{0, 1, 2, 3}" ] }, - "execution_count": 69, + "execution_count": 100, "metadata": {}, "output_type": "execute_result" } @@ -1805,15 +1813,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**intersection( )** function outputs a set which contains all the elements that are in both sets." + "`intersection( )` function outputs a set which contains all the elements that are in both sets." ] }, { "cell_type": "code", - "execution_count": 70, - "metadata": { - "collapsed": false - }, + "execution_count": 101, + "metadata": {}, "outputs": [ { "data": { @@ -1821,7 +1827,7 @@ "{2, 3}" ] }, - "execution_count": 70, + "execution_count": 101, "metadata": {}, "output_type": "execute_result" } @@ -1834,15 +1840,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**difference( )** function ouptuts a set which contains elements that are in set1 and not in set2." + "`difference( )` function ouptuts a set which contains elements that are in set1 and not in set2." ] }, { "cell_type": "code", - "execution_count": 71, - "metadata": { - "collapsed": false - }, + "execution_count": 102, + "metadata": {}, "outputs": [ { "data": { @@ -1850,7 +1854,7 @@ "{0, 1}" ] }, - "execution_count": 71, + "execution_count": 102, "metadata": {}, "output_type": "execute_result" } @@ -1863,15 +1867,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**symmetric_difference( )** function ouputs a function which contains elements that are in one of the sets." + "`symmetric_difference( )` function ouputs a function which contains elements that are in one of the sets." ] }, { "cell_type": "code", - "execution_count": 72, - "metadata": { - "collapsed": false - }, + "execution_count": 103, + "metadata": {}, "outputs": [ { "data": { @@ -1879,7 +1881,7 @@ "{0, 1, 4, 5}" ] }, - "execution_count": 72, + "execution_count": 103, "metadata": {}, "output_type": "execute_result" } @@ -1892,15 +1894,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**issubset( ), isdisjoint( ), issuperset( )** is used to check if the set1/set2 is a subset, disjoint or superset of set2/set1 respectively." + "`issubset( ), isdisjoint( ), issuperset( )` is used to check if the set1/set2 is a subset, disjoint or superset of set2/set1 respectively." ] }, { "cell_type": "code", - "execution_count": 73, - "metadata": { - "collapsed": false - }, + "execution_count": 104, + "metadata": {}, "outputs": [ { "data": { @@ -1908,7 +1908,7 @@ "False" ] }, - "execution_count": 73, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } @@ -1919,10 +1919,8 @@ }, { "cell_type": "code", - "execution_count": 74, - "metadata": { - "collapsed": false - }, + "execution_count": 105, + "metadata": {}, "outputs": [ { "data": { @@ -1930,7 +1928,7 @@ "False" ] }, - "execution_count": 74, + "execution_count": 105, "metadata": {}, "output_type": "execute_result" } @@ -1941,10 +1939,8 @@ }, { "cell_type": "code", - "execution_count": 75, - "metadata": { - "collapsed": false - }, + "execution_count": 106, + "metadata": {}, "outputs": [ { "data": { @@ -1952,7 +1948,7 @@ "False" ] }, - "execution_count": 75, + "execution_count": 106, "metadata": {}, "output_type": "execute_result" } @@ -1965,42 +1961,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**pop( )** is used to remove an arbitrary element in the set" + "`pop( )` is used to remove an arbitrary element in the set" ] }, { "cell_type": "code", - "execution_count": 76, - "metadata": { - "collapsed": false - }, + "execution_count": 107, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "set([1, 2, 3])\n" - ] + "data": { + "text/plain": [ + "{1, 2, 3}" + ] + }, + "execution_count": 107, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "set1.pop()\n", - "print set1" + "set1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**remove( )** function deletes the specified element from the set." + "`remove( )` function deletes the specified element from the set." ] }, { "cell_type": "code", - "execution_count": 77, - "metadata": { - "collapsed": false - }, + "execution_count": 108, + "metadata": {}, "outputs": [ { "data": { @@ -2008,7 +2003,7 @@ "{1, 3}" ] }, - "execution_count": 77, + "execution_count": 108, "metadata": {}, "output_type": "execute_result" } @@ -2022,15 +2017,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**clear( )** is used to clear all the elements and make that set an empty set." + "`clear( )` is used to clear all the elements and make that set an empty set." ] }, { "cell_type": "code", - "execution_count": 78, - "metadata": { - "collapsed": false - }, + "execution_count": 109, + "metadata": {}, "outputs": [ { "data": { @@ -2038,7 +2031,7 @@ "set()" ] }, - "execution_count": 78, + "execution_count": 109, "metadata": {}, "output_type": "execute_result" } @@ -2051,23 +2044,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/.ipynb_checkpoints/04-checkpoint.ipynb b/.ipynb_checkpoints/04-checkpoint.ipynb index 5a502ea..c626147 100644 --- a/.ipynb_checkpoints/04-checkpoint.ipynb +++ b/.ipynb_checkpoints/04-checkpoint.ipynb @@ -4,61 +4,103 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "## Strings, continued" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Strings" + "Strings are immutable sequences of unicode code points:" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 82, "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Taj Mahal is beautiful'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ - "Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes." + "string0 = 'Taj Mahal is beautiful'\n", + "display(String0 , type(String0))" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [], + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Taj Mahal is beautiful'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ - "String0 = 'Taj Mahal is beautiful'\n", "String1 = \"Taj Mahal is beautiful\"\n", - "String2 = '''Taj Mahal\n", - "is\n", - "beautiful'''" + "display(String1, type(String1))" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 79, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Taj Mahal is beautiful \n", - "Taj Mahal is beautiful \n", - "Taj Mahal\n", - "is\n", - "beautiful \n" - ] + "data": { + "text/plain": [ + "'Taj Mahal\\nis\\nbeautiful'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "print String0 , type(String0)\n", - "print String1, type(String1)\n", - "print String2, type(String2)" + "String2 = '''Taj Mahal\n", + "is\n", + "beautiful'''\n", + "display(String2, type(String2))" ] }, { @@ -70,58 +112,84 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 80, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "M\n", - "Mahal is beautiful\n" - ] + "data": { + "text/plain": [ + "'M'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "'Mahal is beautiful'" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "print String0[4]\n", - "print String0[4:]" + "display(String0[4], String0[4:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "###Built-in Functions" + "### Built-in Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**find( )** function returns the index value of the given data that is to found in the string. If it is not found it returns **-1**. Remember to not confuse the returned -1 for reverse indexing value." + "`find( )` function returns the index value of the given data that is to found in the string. If it is not found it returns `-1`. Remember to not confuse the returned -1 for reverse indexing value." ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 10, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "-1\n" - ] + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print String0.find('al')\n", - "print String0.find('am')" + "String0.find('al')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "String0.find('am')" ] }, { @@ -133,91 +201,110 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "a\n" - ] + "data": { + "text/plain": [ + "'a'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print String0[7]" + "String0[7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "One can also input **find( )** function between which index values it has to search." + "One can also input `find( )` function between which index values it has to search." ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "2\n" - ] + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print String0.find('j',1)\n", - "print String0.find('j',1,3)" + "String0.find('j',1)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "String0.find('j',1,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**capitalize( )** is used to capitalize the first element in the string." + "`capitalize( )` is used to capitalize the first element in the string." ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Observe the first letter in this sentence.\n" - ] + "data": { + "text/plain": [ + "'Observe the first letter in this sentence.'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "String3 = 'observe the first letter in this sentence.'\n", - "print String3.capitalize()" + "String3.capitalize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**center( )** is used to center align the string by specifying the field width." + "`center( )` is used to center align the string by specifying the field width." ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { "data": { @@ -225,7 +312,7 @@ "' Taj Mahal is beautiful '" ] }, - "execution_count": 8, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -243,10 +330,8 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "data": { @@ -254,7 +339,7 @@ "'------------------------Taj Mahal is beautiful------------------------'" ] }, - "execution_count": 9, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -267,15 +352,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**zfill( )** is used for zero padding by specifying the field width." + "`zfill( )` is used for zero padding by specifying the field width." ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "data": { @@ -283,7 +366,7 @@ "'00000000Taj Mahal is beautiful'" ] }, - "execution_count": 10, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -296,31 +379,68 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**expandtabs( )** allows you to change the spacing of the tab character. '\\t' which is by default set to 8 spaces." + "`expandtabs( )` allows you to change the spacing of the tab character. '\\t' which is by default set to 8 spaces." ] }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "h\te\tl\tl\to\n", - "h e l l o\n", - "h e l l o\n" - ] + "data": { + "text/plain": [ + "'h\\te\\tl\\tl\\to'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "s = 'h\\te\\tl\\tl\\to'\n", - "print s\n", - "print s.expandtabs(1)\n", - "print s.expandtabs()" + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'h e l l o'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.expandtabs(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'h e l l o'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.expandtabs()" ] }, { @@ -329,24 +449,54 @@ "collapsed": true }, "source": [ - "**index( )** works the same way as **find( )** function the only difference is find returns '-1' when the input element is not found in the string but **index( )** function throws a ValueError" + "`index( )` works the same way as `find( )` function the only difference is find returns '-1' when the input element is not found in the string but `index( )` function throws a `ValueError`" ] }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 26, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "4\n" - ] - }, + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "String0.index('Taj')" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "String0.index('Mahal',0)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ { "ename": "ValueError", "evalue": "substring not found", @@ -354,43 +504,40 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Taj'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: substring not found" ] } ], "source": [ - "print String0.index('Taj')\n", - "print String0.index('Mahal',0)\n", - "print String0.index('Mahal',10,20)" + "String0.index('Mahal',10,20)" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ - "**endswith( )** function is used to check if the given string ends with the particular char which is given as input." + "`endswith( )` function is used to check if the given string ends with the particular char which is given as input." ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 30, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "False\n" - ] + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print String0.endswith('y')" + "\"llamas\".endswith('y')" ] }, { @@ -402,68 +549,79 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 78, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "print String0.endswith('l',0)\n", - "print String0.endswith('M',0,5)" + "display(\"llamas\".endswith('as',0), \"llamas\".endswith('as',0,3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**count( )** function counts the number of char in the given string. The start and the stop index can also be specified or left blank. (These are Implicit arguments which will be dealt in functions)" + "`count( )` function counts the number of char in the given string. The start and the stop index can also be specified or left blank. (These are Implicit arguments which will be dealt in functions)" ] }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, + "execution_count": 77, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "4\n", - "2\n" - ] + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "print String0.count('a',0)\n", - "print String0.count('a',5,10)" + "s = \"alphabetical\"\n", + "display(s.count('a',0), s.count('a',5,10))" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ - "**join( )** function is used add a char in between the elements of the input string." + "`join( )` function is used add a char in between the elements of the input string." ] }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 37, + "metadata": {}, "outputs": [ { "data": { @@ -471,7 +629,7 @@ "'*a_a-'" ] }, - "execution_count": 16, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } @@ -491,216 +649,235 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**join( )** function can also be used to convert a list into a string." + "`join( )` function can also be used to convert a list into a string." ] }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 76, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['T', 'a', 'j', ' ', 'M', 'a', 'h', 'a', 'l', ' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n", - "Taj Mahal is beautiful\n" - ] + "data": { + "text/plain": [ + "['t',\n", + " 'h',\n", + " 'e',\n", + " ' ',\n", + " 'q',\n", + " 'u',\n", + " 'i',\n", + " 'c',\n", + " 'k',\n", + " ' ',\n", + " 'b',\n", + " 'r',\n", + " 'o',\n", + " 'w',\n", + " 'n',\n", + " ' ',\n", + " 'f',\n", + " 'o',\n", + " 'x']" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "'the quick brown fox'" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "a = list(String0)\n", - "print a\n", + "a = list('the quick brown fox')\n", "b = ''.join(a)\n", - "print b" + "display(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Before converting it into a string **join( )** function can be used to insert any char in between the list elements." + "Before converting it into a string `join( )` function can be used to insert any char in between the list elements." ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, + "execution_count": 40, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - " /i/s/ /b/e/a/u/t/i/f/u/l\n" - ] + "data": { + "text/plain": [ + "' /b/r/o/w/n/ /f/o/x'" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "c = '/'.join(a)[18:]\n", - "print c" + "c" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ - "**split( )** function is used to convert a string back to a list. Think of it as the opposite of the **join()** function." + "`split( )` function is used to convert a string back to a list. Think of it as the opposite of the `join()` function." ] }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, + "execution_count": 41, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n" - ] + "data": { + "text/plain": [ + "[' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x']" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "d = c.split('/')\n", - "print d" + "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "In **split( )** function one can also specify the number of times you want to split the string or the number of elements the new returned list should conatin. The number of elements is always one more than the specified number this is because it is split the number of times specified." + "In `split( )` function one can also specify the number of times you want to split the string or the number of elements the new returned list should conatin. The number of elements is always one more than the specified number this is because it is split the number of times specified." ] }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 42, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l']\n", - "4\n" - ] + "data": { + "text/plain": [ + "([' ', 'b', 'r', 'o/w/n/ /f/o/x'], 4)" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "e = c.split('/',3)\n", - "print e\n", - "print len(e)" + "e, len(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**lower( )** converts any capital letter to small letter." + "`lower( )` converts any capital letter to small letter." ] }, { "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, + "execution_count": 43, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Taj Mahal is beautiful\n", - "taj mahal is beautiful\n" - ] + "data": { + "text/plain": [ + "'why am i yelling?'" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print String0\n", - "print String0.lower()" + "s = \"WHY AM I YELLING?\"\n", + "s.lower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**upper( )** converts any small letter to capital letter." + "`upper( )` converts any small letter to capital letter." ] }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, + "execution_count": 44, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'TAJ MAHAL IS BEAUTIFUL'" + "'WHISPER'" ] }, - "execution_count": 22, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "String0.upper()" + "s = 'whisper'\n", + "s.upper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**replace( )** function replaces the element with another element." + "`replace( )` function replaces the element with another element." ] }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, + "execution_count": 45, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'Bengaluru is beautiful'" + "'green eggs'" ] }, - "execution_count": 23, + "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "String0.replace('Taj Mahal','Bengaluru')" + "\"green onions\".replace('onions','eggs')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**strip( )** function is used to delete elements from the right end and the left end which is not required." + "`strip( )` function is used to delete elements from the right end and the left end which is not required." ] }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": true - }, + "execution_count": 46, + "metadata": {}, "outputs": [], "source": [ "f = ' hello '" @@ -715,10 +892,8 @@ }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, + "execution_count": 47, + "metadata": {}, "outputs": [ { "data": { @@ -726,7 +901,7 @@ "'hello'" ] }, - "execution_count": 25, + "execution_count": 47, "metadata": {}, "output_type": "execute_result" } @@ -739,34 +914,30 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**strip( )** function, when a char is specified then it deletes that char if it is present in the two ends of the specified string." + "`strip( )` function, when a char is specified then it deletes that char if it is present in the two ends of the specified string." ] }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": true - }, + "execution_count": 50, + "metadata": {}, "outputs": [], "source": [ - "f = ' ***----hello---******* '" + "f = '***----hello---*******'" ] }, { "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, + "execution_count": 51, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "' ***----hello---******* '" + "'----hello---'" ] }, - "execution_count": 27, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -779,72 +950,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The asterisk had to be deleted but is not. This is because there is a space in both the right and left hand side. So in strip function. The characters need to be inputted in the specific order in which they are present." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "----hello---\n", - "hello\n" - ] - } - ], - "source": [ - "print f.strip(' *')\n", - "print f.strip(' *-')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": false - }, - "source": [ - "**lstrip( )** and **rstrip( )** function have the same functionality as strip function but the only difference is **lstrip( )** deletes only towards the left side and **rstrip( )** towards the right." + "`lstrip( )` and `rstrip( )` function have the same functionality as strip function but the only difference is `lstrip( )` deletes only towards the left side and `rstrip( )` towards the right." ] }, { "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, + "execution_count": 52, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "----hello---******* \n", - " ***----hello---\n" - ] + "data": { + "text/plain": [ + "('----hello---*******', '***----hello---')" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print f.lstrip(' *')\n", - "print f.rstrip(' *')" + "display(f.lstrip(' *'), f.rstrip(' *'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Dictionaries" + "## Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Dictionaries are more used like a database because here you can index a particular sequence with your user defined string." + "Dictionaries are mappings of immutable keys to mutable values." ] }, { @@ -856,77 +996,88 @@ }, { "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, + "execution_count": 75, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - " \n" - ] + "data": { + "text/plain": [ + "dict" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "dict" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ "d0 = {}\n", "d1 = dict()\n", - "print type(d0), type(d1)" + "display(type(d0), type(d1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Dictionary works somewhat like a list but with an added capability of assigning it's own index style." + "To assign to a `dict` you can use the `[]` operator:" ] }, { "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, + "execution_count": 55, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'OneTwo': 12, 'One': 1}\n" - ] + "data": { + "text/plain": [ + "{'One': 1, 'OneTwo': 12}" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "d0['One'] = 1\n", "d0['OneTwo'] = 12 \n", - "print d0" + "d0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "That is how a dictionary looks like. Now you are able to access '1' by the index value set at 'One'" + "You can also retrieve values by key with the `[]` operator:" ] }, { "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, + "execution_count": 57, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n" - ] + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print d0['One']" + "d0['One']" ] }, { @@ -938,10 +1089,8 @@ }, { "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, + "execution_count": 58, + "metadata": {}, "outputs": [], "source": [ "names = ['One', 'Two', 'Three', 'Four', 'Five']\n", @@ -952,92 +1101,93 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**zip( )** function is used to combine two lists" + "`zip( )` function is used to combine two lists" ] }, { "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, + "execution_count": 59, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)]\n" - ] + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "d2 = zip(names,numbers)\n", - "print d2" + "d2" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ "The two lists are combined to form a single list and each elements are clubbed with their respective elements from the other list inside a tuple. Tuples because that is what is assigned and the value should not change.\n", "\n", - "Further, To convert the above into a dictionary. **dict( )** function is used." + "Further, To convert the above into a dictionary. `dict( )` function is used." ] }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, + "execution_count": 61, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n" - ] + "data": { + "text/plain": [ + "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "a1 = dict(d2)\n", - "print a1" + "a1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "###Built-in Functions" + "### Built-in Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**clear( )** function is used to erase the entire database that was created." + "`clear( )` function is used to erase the entire database that was created." ] }, { "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, + "execution_count": 63, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{}\n" - ] + "data": { + "text/plain": [ + "{}" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "a1.clear()\n", - "print a1" + "a1" ] }, { @@ -1049,46 +1199,45 @@ }, { "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, + "execution_count": 70, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n" - ] + "data": { + "text/plain": [ + "{'One': 1, 'Two': 2, 'Three': 3, 'Five': 5, 'Four': 4}" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "for i in range(len(names)):\n", " a1[names[i]] = numbers[i]\n", - "print a1" + "a1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**values( )** function returns a list with all the assigned values in the dictionary." + "`values( )` function returns a list with all the assigned values in the dictionary." ] }, { "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, + "execution_count": 71, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[4, 5, 3, 2, 1]" + "dict_values([1, 2, 3, 5, 4])" ] }, - "execution_count": 38, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -1101,23 +1250,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**keys( )** function returns all the index or the keys to which contains the values that it was assigned to." + "`keys( )` function returns all the index or the keys to which contains the values that it was assigned to." ] }, { "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, + "execution_count": 72, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['Four', 'Five', 'Three', 'Two', 'One']" + "dict_keys(['One', 'Two', 'Three', 'Five', 'Four'])" ] }, - "execution_count": 39, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } @@ -1130,23 +1277,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**items( )** is returns a list containing both the list but each element in the dictionary is inside a tuple. This is same as the result that was obtained when zip function was used." + "`items( )` is returns a list containing both the list but each element in the dictionary is inside a tuple. This is same as the result that was obtained when zip function was used." ] }, { "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, + "execution_count": 73, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[('Four', 4), ('Five', 5), ('Three', 3), ('Two', 2), ('One', 1)]" + "dict_items([('One', 1), ('Two', 2), ('Three', 3), ('Five', 5), ('Four', 4)])" ] }, - "execution_count": 40, + "execution_count": 73, "metadata": {}, "output_type": "execute_result" } @@ -1159,51 +1304,65 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**pop( )** function is used to get the remove that particular element and this removed element can be assigned to a new variable. But remember only the value is stored and not the key. Because the is just a index value." + "`pop( )` function is used to get the remove that particular element and this removed element can be assigned to a new variable. But remember only the value is stored and not the key. Because the is just a index value." ] }, { "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false - }, + "execution_count": 74, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n", - "4\n" - ] + "data": { + "text/plain": [ + "{'One': 1, 'Two': 2, 'Three': 3, 'Five': 5}" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ "a2 = a1.pop('Four')\n", - "print a1\n", - "print a2" + "display(a1, a2)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/.ipynb_checkpoints/05-checkpoint.ipynb b/.ipynb_checkpoints/05-checkpoint.ipynb index d24f4ac..524ab74 100644 --- a/.ipynb_checkpoints/05-checkpoint.ipynb +++ b/.ipynb_checkpoints/05-checkpoint.ipynb @@ -4,38 +4,31 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "# Control Flow Statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Control Flow Statements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##If" + "## If-statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "The basic form of the if-statement is:\n", + "```\n", "if some_condition:\n", - " \n", - " algorithm" + " algorithm\n", + "```" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, + "execution_count": 2, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -48,35 +41,33 @@ "source": [ "x = 12\n", "if x >10:\n", - " print \"Hello\"" + " print(\"Hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##If-else" + "## If-else" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "You can also have an `else` block which is executed if the `if` statement is not true:\n", + "```python\n", "if some_condition:\n", - " \n", - " algorithm\n", - " \n", + " do something\n", "else:\n", - " \n", - " algorithm" + " do something else\n", + "```" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 3, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -89,24 +80,24 @@ "source": [ "x = 12\n", "if x > 10:\n", - " print \"hello\"\n", + " print(\"hello\")\n", "else:\n", - " print \"world\"" + " print(\"world\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##if-elif" + "## if-elif" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ + "You can have multiple condition using `elif`. They are tested in order, and the first which evaluates to true is executed. If none evaluate to true, then the optional `else` block is executed (if it exists):\n", + "```python\n", "if some_condition:\n", " \n", " algorithm\n", @@ -117,21 +108,20 @@ "\n", "else:\n", " \n", - " algorithm" + " algorithm\n", + "```" ] }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "x y:\n", - " print \"x>y\"\n", + " print(\"greater\")\n", "elif x < y:\n", - " print \"x y:\n", - " print \"x>y\"\n", + " print(\"greater\")\n", "elif x < y:\n", - " print \"x=7:\n", " break" ] @@ -390,7 +368,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Continue" + "## Continue" ] }, { @@ -402,10 +380,8 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -427,17 +403,17 @@ "source": [ "for i in range(10):\n", " if i>4:\n", - " print \"The end.\"\n", + " print(\"The end.\")\n", " continue\n", " elif i<7:\n", - " print i" + " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##List Comprehensions" + "## List Comprehensions" ] }, { @@ -449,17 +425,18 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, + "execution_count": 15, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n" - ] + "data": { + "text/plain": [ + "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -467,7 +444,7 @@ "for i in range(1,11):\n", " x = 27*i\n", " res.append(x)\n", - "print res" + "res" ] }, { @@ -479,10 +456,8 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { "data": { @@ -490,7 +465,7 @@ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, - "execution_count": 12, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -503,7 +478,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "That's it!. Only remember to enclose it in square brackets" + "That's it! Only remember to enclose it in square brackets" ] }, { @@ -515,10 +490,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { "data": { @@ -526,7 +499,7 @@ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, - "execution_count": 13, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -544,10 +517,8 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "data": { @@ -555,7 +526,7 @@ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, - "execution_count": 14, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -567,23 +538,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/.ipynb_checkpoints/06-checkpoint.ipynb b/.ipynb_checkpoints/06-checkpoint.ipynb index a642528..6efc7a0 100644 --- a/.ipynb_checkpoints/06-checkpoint.ipynb +++ b/.ipynb_checkpoints/06-checkpoint.ipynb @@ -4,14 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#Functions" + "# Functions" ] }, { @@ -32,14 +25,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "```python\n", "def funcname(arg1, arg2,... argN):\n", - " \n", - " ''' Document String'''\n", + " '''docstring'''\n", "\n", " statements\n", "\n", - "\n", - " return " + " return \n", + "````" ] }, { @@ -52,9 +45,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -66,8 +57,8 @@ } ], "source": [ - "print \"Hey Rajath!\"\n", - "print \"Rajath, How do you do?\"" + "print(\"Hey Rajath!\")\n", + "print(\"Rajath, How do you do?\")" ] }, { @@ -82,22 +73,18 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def firstfunc():\n", - " print \"Hey Rajath!\"\n", - " print \"Rajath, How do you do?\" " + " print(\"Hey Rajath!\")\n", + " print(\"Rajath, How do you do?\") " ] }, { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -116,28 +103,24 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**firstfunc()** every time just prints the message to a single person. We can make our function **firstfunc()** to accept arguements which will store the name and then prints respective to that accepted name. To do so, add a argument within the function as shown." + "`firstfunc()` every time just prints the message to a single person. We can make our function `firstfunc()` to accept arguements which will store the name and then prints respective to that accepted name. To do so, add a argument within the function as shown." ] }, { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def firstfunc(username):\n", - " print \"Hey\", username + '!'\n", - " print username + ',' ,\"How do you do?\"" + " print(\"Hey\", username + '!')\n", + " print(username + ',' ,\"How do you do?\")" ] }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -148,22 +131,20 @@ } ], "source": [ - "name1 = raw_input('Please enter your name : ')" + "name1 = input('Please enter your name : ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The name \"Guido\" is actually stored in name1. So we pass this variable to the function **firstfunc()** as the variable username because that is the variable that is defined for this function. i.e name1 is passed as username." + "The name you enter is actually stored in `name1`. So we pass this variable to the function `firstfunc()` as the variable username because that is the variable that is defined for this function. i.e name1 is passed as username." ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -182,39 +163,36 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Let us simplify this even further by defining another function **secondfunc()** which accepts the name and stores it inside a variable and then calls the **firstfunc()** from inside the function itself." + "Let us simplify this even further by defining another function `secondfunc()` which accepts the name and stores it inside a variable and then calls the `firstfunc()` from inside the function itself." ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 8, + "metadata": {}, "outputs": [], "source": [ "def firstfunc(username):\n", - " print \"Hey\", username + '!'\n", - " print username + ',' ,\"How do you do?\"\n", + " print(\"Hey\", username + '!')\n", + " print(username + ',' ,\"How do you do?\")\n", + " \n", "def secondfunc():\n", - " name = raw_input(\"Please enter your name : \")\n", + " name = input(\"Please enter your name : \")\n", " firstfunc(name)" ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 9, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Please enter your name : karthik\n", - "Hey karthik!\n", - "karthik, How do you do?\n" + "Please enter your name : Jones\n", + "Hey Jones!\n", + "Jones, How do you do?\n" ] } ], @@ -226,7 +204,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Return Statement" + "## Return Statement" ] }, { @@ -238,10 +216,8 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": true - }, + "execution_count": 10, + "metadata": {}, "outputs": [], "source": [ "def times(x,y):\n", @@ -253,34 +229,27 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The above defined **times( )** function accepts two arguements and return the variable z which contains the result of the product of the two arguements" + "The above defined `times( )` function accepts two arguements and return the variable z which contains the result of the product of the two arguements" ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "20\n" - ] + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "c = times(4,5)\n", - "print c" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The z value is stored in variable c and can be used for further operations." + "times(4,5)" ] }, { @@ -292,10 +261,8 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [], "source": [ "def times(x,y):\n", @@ -305,37 +272,35 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 15, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "20\n" - ] + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "c = times(4,5)\n", - "print c" + "times(4,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Since the **times( )** is now defined, we can document it as shown above. This document is returned whenever **times( )** function is called under **help( )** function." + "Since the `times( )` is now defined, we can document it as shown above. This document is returned whenever `times( )` function is called under `help( )` function." ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -362,10 +327,8 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": true - }, + "execution_count": 17, + "metadata": {}, "outputs": [], "source": [ "eglist = [10,50,30,12,6,8,100]" @@ -373,10 +336,8 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": true - }, + "execution_count": 18, + "metadata": {}, "outputs": [], "source": [ "def egfunc(eglist):\n", @@ -396,10 +357,8 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "data": { @@ -407,7 +366,7 @@ "(100, 6, 10, 100)" ] }, - "execution_count": 16, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -418,10 +377,8 @@ }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -436,32 +393,30 @@ ], "source": [ "a,b,c,d = egfunc(eglist)\n", - "print ' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d" + "print(' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Implicit arguments" + "## Default arguments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "When an argument of a function is common in majority of the cases or it is \"implicit\" this concept is used." + "You can specify default values for the right-most arguments in a function:" ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": true - }, + "execution_count": 21, + "metadata": {}, "outputs": [], "source": [ - "def implicitadd(x,y=3):\n", + "def implicitadd(x, y=3):\n", " return x+y" ] }, @@ -469,22 +424,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**implicitadd( )** is a function accepts two arguments but most of the times the first argument needs to be added just by 3. Hence the second argument is assigned the value 3. Here the second argument is implicit." + "`implicitadd( )` is a function accepts two arguments but most of the times the first argument needs to be added just by 3. Hence the second argument is assigned the value 3. Here the second argument is implicit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Now if the second argument is not defined when calling the **implicitadd( )** function then it considered as 3." + "Now if the second argument is not defined when calling the `implicitadd( )` function then it considered as 3." ] }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, + "execution_count": 22, + "metadata": {}, "outputs": [ { "data": { @@ -492,7 +445,7 @@ "7" ] }, - "execution_count": 19, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -510,10 +463,8 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 23, + "metadata": {}, "outputs": [ { "data": { @@ -521,7 +472,7 @@ "8" ] }, - "execution_count": 20, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -534,7 +485,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Any number of arguments" + "## Any number of arguments" ] }, { @@ -546,10 +497,8 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": true - }, + "execution_count": 25, + "metadata": {}, "outputs": [], "source": [ "def add_n(*args):\n", @@ -557,7 +506,7 @@ " reslist = []\n", " for i in args:\n", " reslist.append(i)\n", - " print reslist\n", + " print(reslist)\n", " return sum(reslist)" ] }, @@ -570,10 +519,8 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, + "execution_count": 26, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -588,7 +535,7 @@ "15" ] }, - "execution_count": 22, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -599,10 +546,8 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, + "execution_count": 27, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -617,7 +562,7 @@ "6" ] }, - "execution_count": 23, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -630,245 +575,114 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Global and Local Variables" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Whatever variable is declared inside a function is local variable and outside the function in global variable." - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "eg1 = [1,2,3,4,5]" + "## Lambda Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "In the below function we are appending a element to the declared list inside the function. eg2 variable declared inside the function is a local variable." - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def egfunc1():\n", - " def thirdfunc(arg1):\n", - " eg2 = arg1[:]\n", - " eg2.append(6)\n", - " print \"This is happening inside the function :\", eg2 \n", - " print \"This is happening before the function is called : \", eg1\n", - " thirdfunc(eg1)\n", - " print \"This is happening outside the function :\", eg1 \n", - " print \"Accessing a variable declared inside the function from outside :\" , eg2" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This is happening before the function is called : [1, 2, 3, 4, 5]\n", - "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", - "This is happening outside the function : [1, 2, 3, 4, 5]\n", - "Accessing a variable declared inside the function from outside :" - ] - }, - { - "ename": "NameError", - "evalue": "global name 'eg2' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0megfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36megfunc1\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mthirdfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"This is happening outside the function :\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"Accessing a variable declared inside the function from outside :\"\u001b[0m \u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: global name 'eg2' is not defined" - ] - } - ], - "source": [ - "egfunc1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If a **global** variable is defined as shown in the example below then that variable can be called from anywhere." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "eg3 = [1,2,3,4,5]" + "These are small functions which are not defined with any name and carry a single expression whose result is returned. Lambda functions comes very handy when operating with lists. These function are defined by the keyword `lambda` followed by the variables, a colon and the respective expression." ] }, { "cell_type": "code", "execution_count": 28, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "def egfunc1():\n", - " def thirdfunc(arg1):\n", - " global eg2\n", - " eg2 = arg1[:]\n", - " eg2.append(6)\n", - " print \"This is happening inside the function :\", eg2 \n", - " print \"This is happening before the function is called : \", eg1\n", - " thirdfunc(eg1)\n", - " print \"This is happening outside the function :\", eg1 \n", - " print \"Accessing a variable declared inside the function from outside :\" , eg2" + "z = lambda x: x * x" ] }, { "cell_type": "code", "execution_count": 29, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "This is happening before the function is called : [1, 2, 3, 4, 5]\n", - "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", - "This is happening outside the function : [1, 2, 3, 4, 5]\n", - "Accessing a variable declared inside the function from outside : [1, 2, 3, 4, 5, 6]\n" - ] + "data": { + "text/plain": [ + "64" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "egfunc1()" + "z(8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Lambda Functions" + "### map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "These are small functions which are not defined with any name and carry a single expression whose result is returned. Lambda functions comes very handy when operating with lists. These function are defined by the keyword **lambda** followed by the variables, a colon and the respective expression." + "`map( )` function basically executes the function that is defined to each of the list's element separately." ] }, { "cell_type": "code", "execution_count": 30, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "z = lambda x: x * x" + "list1 = [1,2,3,4,5,6,7,8,9]" ] }, { "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, + "execution_count": 32, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "64" + "" ] }, - "execution_count": 31, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "z(8)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###map" + "m = map(lambda x:x+2, list1)\n", + "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**map( )** function basically executes the function that is defined to each of the list's element separately." - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "list1 = [1,2,3,4,5,6,7,8,9]" + "As we see above, `map` produces a \"map\" which lazily evaluates its values. As with `range()`, we need to force the evalation of the `map` in order to see the values. One way to do that is by passing the `map` to the list constructor:" ] }, { "cell_type": "code", "execution_count": 33, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[3, 4, 5, 6, 7, 8, 9, 10, 11]\n" - ] + "data": { + "text/plain": [ + "[3, 4, 5, 6, 7, 8, 9, 10, 11]" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "eg = map(lambda x:x+2, list1)\n", - "print eg" + "list(m)" ] }, { @@ -881,9 +695,7 @@ { "cell_type": "code", "execution_count": 34, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "list2 = [9,8,7,6,5,4,3,2,1]" @@ -891,73 +703,71 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, + "execution_count": 37, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 10, 10, 10, 10, 10, 10, 10, 10]\n" - ] + "data": { + "text/plain": [ + "[10, 10, 10, 10, 10, 10, 10, 10, 10]" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "eg2 = map(lambda x,y:x+y, list1,list2)\n", - "print eg2" + "eg2 = list(map(lambda x,y:x+y, list1,list2))\n", + "eg2" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ "Not only lambda function but also other built in functions can also be used." ] }, { "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, + "execution_count": 38, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['10', '10', '10', '10', '10', '10', '10', '10', '10']\n" - ] + "data": { + "text/plain": [ + "['10', '10', '10', '10', '10', '10', '10', '10', '10']" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "eg3 = map(str,eg2)\n", - "print eg3" + "eg3 = list(map(str,eg2))\n", + "eg3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "###filter" + "### filter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**filter( )** function is used to filter out the values in a list. Note that **filter()** function returns the result in a new list." + "`filter( )` function is used to filter out the values in a list. Note that `filter()` function returns the result in a new list." ] }, { "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": true - }, + "execution_count": 39, + "metadata": {}, "outputs": [], "source": [ "list1 = [1,2,3,4,5,6,7,8,9]" @@ -972,10 +782,36 @@ }, { "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = filter(lambda x:x<5,list1)\n", + "f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Like `map`, `filter` is also lazy:" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, "outputs": [ { "data": { @@ -983,28 +819,26 @@ "[1, 2, 3, 4]" ] }, - "execution_count": 38, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "filter(lambda x:x<5,list1)" + "list(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Notice what happens when **map()** is used." + "Notice what happens when `map()` is used." ] }, { "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, + "execution_count": 43, + "metadata": {}, "outputs": [ { "data": { @@ -1012,28 +846,26 @@ "[True, True, True, True, False, False, False, False, False]" ] }, - "execution_count": 39, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "map(lambda x:x<5, list1)" + "list(map(lambda x:x<5, list1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "We can conclude that, whatever is returned true in **map( )** function that particular element is returned when **filter( )** function is used." + "We can conclude that, whatever is returned true in `map( )` function that particular element is returned when `filter( )` function is used." ] }, { "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, + "execution_count": 46, + "metadata": {}, "outputs": [ { "data": { @@ -1041,35 +873,42 @@ "[4, 8]" ] }, - "execution_count": 40, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "filter(lambda x:x%4==0,list1)" + "list(filter(lambda x:x%4==0,list1))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/.ipynb_checkpoints/07-checkpoint.ipynb b/.ipynb_checkpoints/07-checkpoint.ipynb index f233280..4d004e8 100644 --- a/.ipynb_checkpoints/07-checkpoint.ipynb +++ b/.ipynb_checkpoints/07-checkpoint.ipynb @@ -4,45 +4,39 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "# Classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Classes" + "Everything in Python is an object, and every object has a type. We can create our own types in Python by defining classes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Variables, Lists, Dictionaries etc in python is a object. Without getting into the theory part of Object Oriented Programming, explanation of the concepts will be done along this tutorial." + "A class is declared as follows:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "A class is declared as follows" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "class class_name:\n", + "```python\n", + "class ClassName:\n", + " class body\n", + "```\n", "\n", - " Functions" + "Let's define the simplest class we can:" ] }, { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "class FirstClass:\n", @@ -53,41 +47,44 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**pass** in python means do nothing. " + "`pass` in python means do nothing. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Above, a class object named \"FirstClass\" is declared now consider a \"egclass\" which has all the characteristics of \"FirstClass\". So all you have to do is, equate the \"egclass\" to \"FirstClass\". In python jargon this is called as creating an instance. \"egclass\" is the instance of \"FirstClass\"" + "We can create an instance of our class by calling it:" ] }, { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "egclass = FirstClass()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can see that our new object is of the right type:" + ] + }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "instance" + "__main__.FirstClass" ] }, - "execution_count": 3, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -96,20 +93,25 @@ "type(egclass)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Interestingly, we can also see that our class has a type. This hints at the nature of Python where everything - even a class - is an object:" + ] + }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "classobj" + "type" ] }, - "execution_count": 4, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -122,59 +124,22 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now let us add some \"functionality\" to the class. So that our \"FirstClass\" is defined in a better way. A function inside a class is called as a \"Method\" of that class" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Most of the classes will have a function named \"\\_\\_init\\_\\_\". These are called as magic methods. In this method you basically initialize the variables of that class or any other initial algorithms which is applicable to all methods is specified in this method. A variable inside a class is called an attribute." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "These helps simplify the process of initializing a instance. For example,\n", + "## __init__\n", "\n", - "Without the use of magic method or \\_\\_init\\_\\_ which is otherwise called as constructors. One had to define a **init( )** method and call the **init( )** function." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "eg0 = FirstClass()\n", - "eg0.init()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "But when the constructor is defined the \\_\\_init\\_\\_ is called thus intializing the instance created. " + "Many classes need some sort of initialization. We do this with the `__init__` method. This is called when an new instance of our class is created, before a reference to that object is made available to the caller." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "We will make our \"FirstClass\" to accept two variables name and symbol.\n", - "\n", - "I will be explaining about the \"self\" in a while." + "We'll give our `FirstClass` class a `__init__` that accepts two arguments`name` and `symbol`. We'll store those arguments on the instance:" ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, + "execution_count": 7, + "metadata": {}, "outputs": [], "source": [ "class FirstClass:\n", @@ -187,27 +152,32 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now that we have defined a function and added the \\_\\_init\\_\\_ method. We can create a instance of FirstClass which now accepts two arguments. " + "Now that we have defined a function and added the `__init__` method. We can create a instance of `FirstClass` which now accepts two arguments. " ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, + "execution_count": 9, + "metadata": {}, "outputs": [], "source": [ "eg1 = FirstClass('one',1)\n", "eg2 = FirstClass('two',2)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `self`\n", + "\n", + "You probably noticed the `self` argument in our `__init__`. This argument is automatically assigned to the instance on which the method is being executed. Python does this for all instance methods, not just `__init__`." + ] + }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 10, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -219,31 +189,54 @@ } ], "source": [ - "print eg1.name, eg1.symbol\n", - "print eg2.name, eg2.symbol" + "print(eg1.name, eg1.symbol)\n", + "print(eg2.name, eg2.symbol)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**dir( )** function comes very handy in looking into what the class contains and what all method it offers" + "`dir( )` function comes very handy in looking into what the class contains and what all method it offers" ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 11, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['__doc__', '__init__', '__module__']" + "['__class__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '__weakref__']" ] }, - "execution_count": 9, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -256,23 +249,48 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**dir( )** of an instance also shows it's defined attributes." + "`dir( )` of an instance also shows it's defined attributes." ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['__doc__', '__init__', '__module__', 'name', 'symbol']" + "['__class__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '__weakref__',\n", + " 'name',\n", + " 'symbol']" ] }, - "execution_count": 10, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -285,905 +303,234 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Changing the FirstClass function a bit," + "## Methods\n", + "\n", + "Methods are defined for classes using `def` inside the class body. `__init__` that we looked at above is an example of a method. Let us add some more methods to `FirstClass`:" ] }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 15, + "metadata": {}, "outputs": [], "source": [ "class FirstClass:\n", " def __init__(self,name,symbol):\n", - " self.n = name\n", - " self.s = symbol" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Changing self.name and self.symbol to self.n and self.s respectively will yield," + " self.name = name\n", + " self.symbol = symbol\n", + " \n", + " def square(self):\n", + " return self.symbol * self.symbol\n", + " \n", + " def cube(self):\n", + " return self.symbol * self.symbol * self.symbol\n", + " \n", + " def multiply(self, x):\n", + " return self.symbol * x" ] }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": true - }, + "execution_count": 16, + "metadata": {}, "outputs": [], "source": [ - "eg1 = FirstClass('one',1)\n", - "eg2 = FirstClass('two',2)" + "eg4 = FirstClass('Five',5)" ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { - "ename": "AttributeError", - "evalue": "FirstClass instance has no attribute 'name'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mAttributeError\u001b[0m: FirstClass instance has no attribute 'name'" + "name": "stdout", + "output_type": "stream", + "text": [ + "25\n", + "125\n" ] } ], "source": [ - "print eg1.name, eg1.symbol\n", - "print eg2.name, eg2.symbol" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "AttributeError, Remember variables are nothing but attributes inside a class? So this means we have not given the correct attribute for the instance." + "print(eg4.square())\n", + "print(eg4.cube())" ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['__doc__', '__init__', '__module__', 'n', 's']" + "10" ] }, - "execution_count": 14, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "dir(eg1)" + "eg4.multiply(2)" ] }, { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "one 1\n", - "two 2\n" - ] - } - ], + "cell_type": "markdown", + "metadata": {}, "source": [ - "print eg1.n, eg1.s\n", - "print eg2.n, eg2.s" + "## Inheritance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "So now we have solved the error. Now let us compare the two examples that we saw.\n", - "\n", - "When I declared self.name and self.symbol, there was no attribute error for eg1.name and eg1.symbol and when I declared self.n and self.s, there was no attribute error for eg1.n and eg1.s\n", - "\n", - "From the above we can conclude that self is nothing but the instance itself.\n", - "\n", - "Remember, self is not predefined it is userdefined. You can make use of anything you are comfortable with. But it has become a common practice to use self." + "In many cases you'll have several classes that share common elements. For example, you might be modeling animals on a farm. All of our animals will have a name and number of legs, so we'll create a base class (i.e. a class we plan to inherit from) called `Animal`:" ] }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": true - }, + "execution_count": 30, + "metadata": {}, "outputs": [], "source": [ - "class FirstClass:\n", - " def __init__(asdf1234,name,symbol):\n", - " asdf1234.n = name\n", - " asdf1234.s = symbol" + "class Animal:\n", + " def __init__(self, name, legs):\n", + " self.name = name\n", + " self.legs = legs" ] }, { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": true - }, - "outputs": [], + "cell_type": "markdown", + "metadata": {}, "source": [ - "eg1 = FirstClass('one',1)\n", - "eg2 = FirstClass('two',2)" + "Now we'll create a few *subclasses* of `Animal`. These subclasses will specify how many legs the animal has while leaving the actual name for individual instances. Let's start with llamas:" ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "one 1\n", - "two 2\n" - ] - } - ], + "execution_count": 32, + "metadata": {}, + "outputs": [], "source": [ - "print eg1.n, eg1.s\n", - "print eg2.n, eg2.s" + "class Llama(Animal):\n", + " def __init__(self, name, color):\n", + " super().__init__(name, 4)\n", + " self.color = color" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Since eg1 and eg2 are instances of FirstClass it need not necessarily be limited to FirstClass itself. It might extend itself by declaring other attributes without having the attribute to be declared inside the FirstClass." - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "eg1.cube = 1\n", - "eg2.cube = 8" + "The `__init__` for `Llama` accepts a name and color. It passes the name and the number 4 (that's how many legs our llamas have) to the base class `__init__`, i.e. `Animal.__init__`.\n", + "\n", + "We can construct a `Llama` and see how it works:" ] }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 35, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['__doc__', '__init__', '__module__', 'cube', 'n', 's']" + "'The llama named Llarry has 4 legs and off-white wool'" ] }, - "execution_count": 20, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "dir(eg1)" + "llarry = Llama('Llarry', 'off-white')\n", + "\n", + "'The llama named {} has {} legs and {} wool'.format(\n", + " llarry.name, llarry.legs, llarry.color)" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": true - }, + "metadata": {}, "source": [ - "Just like global and local variables as we saw earlier, even classes have it's own types of variables.\n", - "\n", - "Class Attribute : attributes defined outside the method and is applicable to all the instances.\n", + "### `super`\n", "\n", - "Instance Attribute : attributes defined inside a method and is applicable to only that method and is unique to each instance." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class FirstClass:\n", - " test = 'test'\n", - " def __init__(self,name,symbol):\n", - " self.name = name\n", - " self.symbol = symbol" + "The call to `super` in `Llama.__init__` essentially means \"call `__init__` on the base class\". `super()` is actually slightly more sophisticatd than that, but that's a sufficient explanation for our purposes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Here test is a class attribute and name is a instance attribute." + "### Another subclass\n", + "\n", + "We can create another `Animal` subclass to fill our farm and show how subclasses can centralize structure and behavior. Here's how our `Chicken` class looks:" ] }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": true - }, + "execution_count": 36, + "metadata": {}, "outputs": [], "source": [ - "eg3 = FirstClass('Three',3)" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "test Three\n" - ] - } - ], - "source": [ - "print eg3.test, eg3.name" + "class Chicken(Animal):\n", + " def __init__(self, name):\n", + " super().__init__(name, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Let us add some more methods to FirstClass." - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "class FirstClass:\n", - " def __init__(self,name,symbol):\n", - " self.name = name\n", - " self.symbol = symbol\n", - " def square(self):\n", - " return self.symbol * self.symbol\n", - " def cube(self):\n", - " return self.symbol * self.symbol * self.symbol\n", - " def multiply(self, x):\n", - " return self.symbol * x" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "eg4 = FirstClass('Five',5)" + "Chickens are simpler than Llamas in that they don't add any new attributes. But they follow the same pattern as Llamas by passing a user-provided name argument to the base-class initializer along with a hard-coded 2 legs. Let's see our chickens in action:" ] }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "25\n", - "125\n" - ] - } - ], - "source": [ - "print eg4.square()\n", - "print eg4.cube()" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "eg4.multiply(2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The above can also be written as," - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, + "execution_count": 37, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "10" + "'The chicken named Marco Pollo has 2 legs'" ] }, - "execution_count": 28, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "FirstClass.multiply(eg4,2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Inheritance" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "There might be cases where a new class would have all the previous characteristics of an already defined class. So the new class can \"inherit\" the previous class and add it's own methods to it. This is called as inheritance." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Consider class SoftwareEngineer which has a method salary." - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "class SoftwareEngineer:\n", - " def __init__(self,name,age):\n", - " self.name = name\n", - " self.age = age\n", - " def salary(self, value):\n", - " self.money = value\n", - " print self.name,\"earns\",self.money" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a = SoftwareEngineer('Kartik',26)" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Kartik earns 40000\n" - ] - } - ], - "source": [ - "a.salary(40000)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__', '__init__', '__module__', 'salary']" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(SoftwareEngineer)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now consider another class Artist which tells us about the amount of money an artist earns and his artform." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class Artist:\n", - " def __init__(self,name,age):\n", - " self.name = name\n", - " self.age = age\n", - " def money(self,value):\n", - " self.money = value\n", - " print self.name,\"earns\",self.money\n", - " def artform(self, job):\n", - " self.job = job\n", - " print self.name,\"is a\", self.job" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "b = Artist('Nitin',20)" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Nitin earns 50000\n", - "Nitin is a Musician\n" - ] - } - ], - "source": [ - "b.money(50000)\n", - "b.artform('Musician')" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__', '__init__', '__module__', 'artform', 'money']" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(Artist)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "money method and salary method are the same. So we can generalize the method to salary and inherit the SoftwareEngineer class to Artist class. Now the artist class becomes," - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class Artist(SoftwareEngineer):\n", - " def artform(self, job):\n", - " self.job = job\n", - " print self.name,\"is a\", self.job" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "c = Artist('Nishanth',21)" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__', '__init__', '__module__', 'artform', 'salary']" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(Artist)" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Nishanth earns 60000\n", - "Nishanth is a Dancer\n" - ] - } - ], - "source": [ - "c.salary(60000)\n", - "c.artform('Dancer')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Suppose say while inheriting a particular method is not suitable for the new class. One can override this method by defining again that method with the same name inside the new class." - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "class Artist(SoftwareEngineer):\n", - " def artform(self, job):\n", - " self.job = job\n", - " print self.name,\"is a\", self.job\n", - " def salary(self, value):\n", - " self.money = value\n", - " print self.name,\"earns\",self.money\n", - " print \"I am overriding the SoftwareEngineer class's salary method\"" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "c = Artist('Nishanth',21)" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Nishanth earns 60000\n", - "I am overriding the SoftwareEngineer class's salary method\n", - "Nishanth is a Dancer\n" - ] - } - ], - "source": [ - "c.salary(60000)\n", - "c.artform('Dancer')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If not sure how many times methods will be called it will become difficult to declare so many variables to carry each result hence it is better to declare a list and append the result." - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class emptylist:\n", - " def __init__(self):\n", - " self.data = []\n", - " def one(self,x):\n", - " self.data.append(x)\n", - " def two(self, x ):\n", - " self.data.append(x**2)\n", - " def three(self, x):\n", - " self.data.append(x**3)" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "xc = emptylist()" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1]\n" - ] - } - ], - "source": [ - "xc.one(1)\n", - "print xc.data" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Since xc.data is a list direct list operations can also be performed." - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 8]\n" - ] - } - ], - "source": [ - "xc.data.append(8)\n", - "print xc.data" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 8, 9]\n" - ] - } - ], - "source": [ - "xc.two(3)\n", - "print xc.data" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If the number of input arguments varies from instance to instance asterisk can be used as shown." - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class NotSure:\n", - " def __init__(self, *args):\n", - " self.data = ''.join(list(args)) " - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "yz = NotSure('I', 'Do' , 'Not', 'Know', 'What', 'To','Type')" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'IDoNotKnowWhatToType'" - ] - }, - "execution_count": 51, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "yz.data" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#Where to go from here?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Practice alone can help you get the hang of python. Give your self problem statements and solve them. You can also sign up to any competitive coding platform for problem statements. The more you code the more you discover and the more you start appreciating the language.\n", - "\n", - "\n", - "Now that you have been introduced to python, You can try out the different python libraries in the field of your interest. I highly recommend you to check out this curated list of Python frameworks, libraries and software http://awesome-python.com\n", - "\n", - "\n", - "The official python documentation : https://docs.python.org/2/\n", - "\n", - "\n", - "You can also check out Python practice programs written by my friend, Kartik Kannapur. Github Repo : https://github.com/rajathkumarmp/Python-Lectures \n", - "\n", - "\n", - "Enjoy solving problem statements because life is short, you need python!\n", - "\n", - "\n", - "Peace.\n", - "\n", - "\n", - "Rajath Kumar M.P ( rajathkumar dot exe at gmail dot com)" + "c = Chicken('Marco Pollo')\n", + "'The chicken named {} has {} legs'.format(c.name, c.legs)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/00.ipynb b/00.ipynb index 7298166..757067d 100644 --- a/00.ipynb +++ b/00.ipynb @@ -4,21 +4,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "# Python Overview" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Python-Lectures" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Introduction" + "## Introduction" ] }, { @@ -32,7 +25,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Installation" + "## Installation" ] }, { @@ -52,7 +45,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Installation from unofficial distributions" + "### Installation from unofficial distributions" ] }, { @@ -66,7 +59,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "####Anaconda" + "#### Anaconda" ] }, { @@ -82,7 +75,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "####Canopy" + "#### Canopy" ] }, { @@ -98,7 +91,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Launching IPython Notebook" + "## Launching IPython Notebook" ] }, { @@ -116,7 +109,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##How to learn from this resource?" + "## How to learn from this resource?" ] }, { @@ -136,7 +129,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##License" + "## Get the notebooks\n", + "\n", + "The notebooks are available [on github](https://github.com/abingham/Python-Lectures). They are based on the [original work by Rajath Kumar M. P.](https://github.com/rajathkmp/Python-Lectures)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## License" ] }, { @@ -149,23 +151,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/01.ipynb b/01.ipynb index 42e738e..49a6d63 100644 --- a/01.ipynb +++ b/01.ipynb @@ -4,22 +4,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#The Zen Of Python" + "# The Zen Of Python" ] }, { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -64,15 +55,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "A name that is used to denote something or a value is called a variable. In python, variables can be declared and values can be assigned to it as follows," + "A name that is used to refer to an object is called a variable. In Python, variables can be declared and values can be assigned to it as follows:" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ "x = 2\n", @@ -82,21 +71,22 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 46, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "7 Hey\n" - ] + "data": { + "text/plain": [ + "(2, 'Hey')" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print x+y, xy" + "x + y, xy" ] }, { @@ -108,10 +98,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, + "execution_count": 5, + "metadata": {}, "outputs": [], "source": [ "x = y = 1" @@ -119,21 +107,22 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 47, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 1\n" - ] + "data": { + "text/plain": [ + "(1, 1)" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print x,y" + "x, y" ] }, { @@ -147,7 +136,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Arithmetic Operators" + "## Arithmetic Operators" ] }, { @@ -167,10 +156,8 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "data": { @@ -178,7 +165,7 @@ "3" ] }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -189,10 +176,8 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 8, + "metadata": {}, "outputs": [ { "data": { @@ -200,7 +185,7 @@ "1" ] }, - "execution_count": 7, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -211,10 +196,8 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 9, + "metadata": {}, "outputs": [ { "data": { @@ -222,7 +205,7 @@ "2" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -233,18 +216,16 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 10, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "0" + "0.5" ] }, - "execution_count": 9, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -253,19 +234,10 @@ "1/2" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "0? This is because both the numerator and denominator are integers but the result is a float value hence an integer value is returned. By changing either the numerator or the denominator to float, correct answer can be obtained." - ] - }, { "cell_type": "code", "execution_count": 10, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -285,9 +257,7 @@ { "cell_type": "code", "execution_count": 11, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -308,36 +278,34 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Floor division is nothing but converting the result so obtained to the nearest integer." + "Floor division with `//` gives you the floor of the division result:" ] }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1.0" + "1" ] }, - "execution_count": 12, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "2.8//2.0" + "3 // 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Relational Operators" + "## Relational Operators" ] }, { @@ -356,10 +324,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": true - }, + "execution_count": 15, + "metadata": {}, "outputs": [], "source": [ "z = 1" @@ -367,10 +333,8 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { "data": { @@ -378,7 +342,7 @@ "True" ] }, - "execution_count": 14, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -389,10 +353,8 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { "data": { @@ -400,7 +362,7 @@ "False" ] }, - "execution_count": 15, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -413,7 +375,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Bitwise Operators" + "## Bitwise Operators" ] }, { @@ -432,43 +394,58 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": true - }, + "execution_count": 24, + "metadata": {}, "outputs": [], "source": [ - "a = 2 #10\n", - "b = 3 #11" + "a = 0b10 # decimal 2\n", + "b = 0b11 # decimal 3" ] }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 48, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "0b10\n" - ] + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print a & b\n", - "print bin(a&b)" + "a & b" ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'0b10'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bin(a&b)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, "outputs": [ { "data": { @@ -476,7 +453,7 @@ "2" ] }, - "execution_count": 18, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -489,19 +466,17 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "0000 0101 -> 5 \n", + "`0000 0101` -> 5 \n", "\n", "Shifting the digits by 1 to the right and zero padding\n", "\n", - "0000 0010 -> 2" + "`0000 0010` -> 2" ] }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, + "execution_count": 51, + "metadata": {}, "outputs": [ { "data": { @@ -509,7 +484,7 @@ "10" ] }, - "execution_count": 19, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -522,18 +497,18 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "0000 0101 -> 5 \n", + "`0000 0101` -> 5 \n", "\n", "Shifting the digits by 1 to the left and zero padding\n", "\n", - "0000 1010 -> 10" + "`0000 1010` -> 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Built-in Functions" + "# Built-in Functions" ] }, { @@ -547,22 +522,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Conversion from one system to another" + "## Conversion from one system to another" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Conversion from hexadecimal to decimal is done by adding prefix **0x** to the hexadecimal value or vice versa by using built in **hex( )**, Octal to decimal by adding prefix **0** to the octal value or vice versa by using built in function **oct( )**." + "Conversion from hexadecimal to decimal is done by adding prefix `0x` to the hexadecimal value or vice versa by using built in `hex( )`, Octal to decimal by adding prefix `0` to the octal value or vice versa by using built in function `oct( )`." ] }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 52, + "metadata": {}, "outputs": [ { "data": { @@ -570,7 +543,7 @@ "'0xaa'" ] }, - "execution_count": 20, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } @@ -581,10 +554,8 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, + "execution_count": 53, + "metadata": {}, "outputs": [ { "data": { @@ -592,7 +563,7 @@ "170" ] }, - "execution_count": 21, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } @@ -603,18 +574,16 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, + "execution_count": 54, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'010'" + "'0o10'" ] }, - "execution_count": 22, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -625,97 +594,89 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, + "execution_count": 55, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "8" + "2" ] }, - "execution_count": 23, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "010" + "0b10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**int( )** accepts two values when used for conversion, one is the value in a different number system and the other is its base. Note that input number in the different number system should be of string type." + "`int( )` accepts two values when used for conversion, one is the value in a different number system and the other is its base. Note that input number in the different number system should be of string type." ] }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, + "execution_count": 56, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "8\n", - "170\n", - "10\n" - ] + "data": { + "text/plain": [ + "(8, 170, 10)" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print int('010',8)\n", - "print int('0xaa',16)\n", - "print int('1010',2)" + "int('010',8), int('0xaa',16), int('1010',2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**int( )** can also be used to get only the integer value of a float number or can be used to convert a number which is of type string to integer format. Similarly, the function **str( )** can be used to convert the integer back to string format" + "`int( )` can also be used to get only the integer value of a float number or can be used to convert a number which is of type string to integer format. Similarly, the function `str( )` can be used to convert the integer back to string format" ] }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, + "execution_count": 57, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "7\n" - ] + "data": { + "text/plain": [ + "(7, 7)" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print int(7.7)\n", - "print int('7')" + "int(7.7), int('7')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Also note that function **bin( )** is used for binary and **float( )** for decimal/float values. **chr( )** is used for converting ASCII to its alphabet equivalent, **ord( )** is used for the other way round." + "Also note that function `bin( )` is used for binary and `float( )` for decimal/float values. `chr( )` is used for converting ASCII to its alphabet equivalent, `ord( )` is used for the other way round." ] }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, + "execution_count": 58, + "metadata": {}, "outputs": [ { "data": { @@ -723,7 +684,7 @@ "'b'" ] }, - "execution_count": 26, + "execution_count": 58, "metadata": {}, "output_type": "execute_result" } @@ -734,10 +695,8 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, + "execution_count": 59, + "metadata": {}, "outputs": [ { "data": { @@ -745,7 +704,7 @@ "98" ] }, - "execution_count": 27, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } @@ -758,78 +717,77 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Simplifying Arithmetic Operations" + "## Simplifying Arithmetic Operations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**round( )** function rounds the input value to a specified number of places or to the nearest integer. " + "`round( )` function rounds the input value to a specified number of places or to the nearest integer. " ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 60, "metadata": { - "collapsed": false, "scrolled": false }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "6.0\n", - "4.56\n" - ] + "data": { + "text/plain": [ + "(6, 4.56)" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print round(5.6231) \n", - "print round(4.55892, 2)" + "round(5.6231), round(4.55892, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**complex( )** is used to define a complex number and **abs( )** outputs the absolute value of the same." + "`complex( )` is used to define a complex number and `abs( )` outputs the absolute value of the same." ] }, { "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, + "execution_count": 61, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "5.38516480713\n" - ] + "data": { + "text/plain": [ + "5.385164807134504" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "c =complex('5+2j')\n", - "print abs(c)" + "c = complex('5+2j')\n", + "abs(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**divmod(x,y)** outputs the quotient and the remainder in a tuple(you will be learning about it in the further chapters) in the format (quotient, remainder). " + "`divmod(x,y)` outputs the quotient and the remainder in a tuple(you will be learning about it in the further chapters) in the format (quotient, remainder). " ] }, { "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, + "execution_count": 39, + "metadata": {}, "outputs": [ { "data": { @@ -837,7 +795,7 @@ "(4, 1)" ] }, - "execution_count": 30, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -850,256 +808,216 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**isinstance( )** returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once." + "`isinstance( )` returns True, if the first argument is an instance of that class. Multiple classes can also be checked at once." ] }, { "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, + "execution_count": 62, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n", - "True\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print isinstance(1, int)\n", - "print isinstance(1.0,int)\n", - "print isinstance(1.0,(int,float))" + "isinstance(1, int)" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 63, "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "**cmp(x,y)**\n", - "\n", - "|x ? y|Output|\n", - "|---|---|\n", - "| x < y | -1 |\n", - "| x == y | 0 |\n", - "| x > y | 1 |" + "isinstance(1.0,int)" ] }, { "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, + "execution_count": 64, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "-1\n", - "1\n", - "0\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print cmp(1,2)\n", - "print cmp(2,1)\n", - "print cmp(2,2)" + "isinstance(1.0,(int,float))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**pow(x,y,z)** can be used to find the power $x^y$ also the mod of the resulting value with the third specified number can be found i.e. : ($x^y$ % z)." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "27\n", - "2\n" - ] - } - ], - "source": [ - "print pow(3,3)\n", - "print pow(3,3,5)" + "`cmp(x,y)`\n", + "\n", + "|x ? y|Output|\n", + "|---|---|\n", + "| x < y | -1 |\n", + "| x == y | 0 |\n", + "| x > y | 1 |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**range( )** function outputs the integers of the specified range. It can also be used to generate a series by specifying the difference between the two numbers within a particular range. The elements are returned in a list (will be discussing in detail later.)" + "`pow(x,y,z)` can be used to find the power $x^y$ also the mod of the resulting value with the third specified number can be found i.e. : ($x^y$ % z)." ] }, { "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, + "execution_count": 65, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 2]\n", - "[2, 3, 4, 5, 6, 7, 8]\n", - "[2, 10, 18, 26]\n" - ] + "data": { + "text/plain": [ + "(27, 2)" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print range(3)\n", - "print range(2,9)\n", - "print range(2,27,8)" + "pow(3,3), pow(3,3,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Accepting User Inputs" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**raw_input( )** accepts input and stores it as a string. Hence, if the user inputs a integer, the code should convert the string to an integer and then proceed." + "`range( )` generates the sequence of integers of the specified range. It can also be used to generate a series by specifying the difference between the two numbers within a particular range. The elements are lazily generated, and will only be generated on demand (e.g. by passing them to the `list` constructor):" ] }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, + "execution_count": 66, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Type something here and it will be stored in variable abc \tHey\n" - ] + "data": { + "text/plain": [ + "range(0, 3)" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "abc = raw_input(\"Type something here and it will be stored in variable abc \\t\")" + "range(5)" ] }, { "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, + "execution_count": 67, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "str" + "[0, 1, 2]" ] }, - "execution_count": 36, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "type(abc)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**input( )**, this is used only for accepting only integer inputs." + "list(range(5))" ] }, { "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, + "execution_count": 68, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Only integer can be stored in variable abc \t275\n" - ] + "data": { + "text/plain": [ + "[2, 3, 4, 5, 6, 7, 8]" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "abc1 = input(\"Only integer can be stored in variable abc \\t\")" + "list(range(2, 9))" ] }, { "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, + "execution_count": 69, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "int" + "[2, 10, 18, 26]" ] }, - "execution_count": 38, + "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "type(abc1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note that **type( )** returns the format or the type of a variable or a number" + "list(range(2,27,8))" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/02.ipynb b/02.ipynb index c4cb722..587bde7 100644 --- a/02.ipynb +++ b/02.ipynb @@ -4,34 +4,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "# `print`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Print Statement" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The **print** statement can be used in the following different ways :\n", - "\n", - " - print \"Hello World\"\n", - " - print \"Hello\", \n", - " - print \"Hello\" + \n", - " - print \"Hello %s\" % " + "The `print` function is used to print one or more objects to the screen:" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, + "execution_count": 45, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -42,568 +28,392 @@ } ], "source": [ - "print \"Hello World\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In Python, single, double and triple quotes are used to denote a string.\n", - "Most use single quotes when declaring a single character. \n", - "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines." + "print(\"Hello World\")" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 56, "metadata": { - "collapsed": false + "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Hey\n" + "Hello world !\n" ] } ], "source": [ - "print 'Hey'" + "print(\"Hello\", \"world\", \"!\")" ] }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 57, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "My name is Rajath Kumar M.P.\n", - "\n", - "I love Python.\n" + "1 2.3 [4, 5, 6]\n" ] } ], "source": [ - "print \"\"\"My name is Rajath Kumar M.P.\n", - "\n", - "I love Python.\"\"\"" + "print(1, 2.3, [4, 5, 6])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement." + "# Strings\n", + "\n", + "In Python, single, double and triple quotes are used to denote a string.\n", + "Most use single quotes when declaring a single character. \n", + "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines." ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false, - "scrolled": true - }, + "execution_count": 46, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n", - "Hello World !\n" - ] + "data": { + "text/plain": [ + "'Hey'" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "string1 = 'World'\n", - "print 'Hello', string1\n", - "\n", - "string2 = '!'\n", - "print 'Hello', string1, string2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings." + "'Hey'" ] }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 47, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "HelloWorld!\n" - ] + "data": { + "text/plain": [ + "'My name is Rajath Kumar M.P.\\n\\nI love Python.'" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print 'Hello' + string1 + string2" + "\"\"\"My name is Rajath Kumar M.P.\n", + "\n", + "I love Python.\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**%s** is used to refer to a variable which contains a string." + "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement." ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 48, "metadata": { - "collapsed": false + "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Hello World\n" + "Hello World\n", + "Hello World !\n" ] } ], "source": [ - "print \"Hello %s\" % string1" + "string1 = 'World'\n", + "print('Hello', string1)\n", + "\n", + "string2 = '!'\n", + "print('Hello', string1, string2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Similarly, when using other data types\n", - "\n", - " - %s -> string\n", - " - %d -> Integer\n", - " - %f -> Float\n", - " - %o -> Octal\n", - " - %x -> Hexadecimal\n", - " - %e -> exponential\n", - " \n", - "This can be used for conversions inside the print statement itself." + "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings." ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 49, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Actual Number = 18\n", - "Float of the number = 18.000000\n", - "Octal equivalent of the number = 22\n", - "Hexadecimal equivalent of the number = 12\n", - "Exponential equivalent of the number = 1.800000e+01\n" - ] + "data": { + "text/plain": [ + "'HelloWorld!'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print \"Actual Number = %d\" %18\n", - "print \"Float of the number = %f\" %18\n", - "print \"Octal equivalent of the number = %o\" %18\n", - "print \"Hexadecimal equivalent of the number = %x\" %18\n", - "print \"Exponential equivalent of the number = %e\" %18" + "'Hello' + string1 + string2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "When referring to multiple variables parenthesis is used." + "The `format()` method on strings allows you to interpolate objects into templates. While string formatting can get quite sophisticated, in general you just use `{}` where you want your arguments to go:" ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 50, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World !\n" - ] + "data": { + "text/plain": [ + "'Hello World'" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print \"Hello %s %s\" %(string1,string2)" + "\"Hello {}\".format(string1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Other Examples" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The following are other different ways the print statement can be put to use." + "This works for all kinds of objects, not just string interpolants:" ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I want %d to be printed here\n" - ] - } - ], - "source": [ - "print \"I want %%d to be printed %s\" %'here'" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "_A_A_A_A_A_A_A_A_A_A\n" - ] - } - ], - "source": [ - "print '_A'*10" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Jan\n", - "Feb\n", - "Mar\n", - "Apr\n", - "May\n", - "Jun\n", - "Jul\n", - "Aug\n" - ] - } - ], - "source": [ - "print \"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\"" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 51, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "I want \\n to be printed.\n" - ] + "data": { + "text/plain": [ + "'integers: 18'" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print \"I want \\\\n to be printed.\"" + "\"integers: {}\".format(18)" ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Routine:\n", - "\t- Eat\n", - "\t- Sleep\n", - "\t- Repeat\n", - "\n" - ] - } - ], - "source": [ - "print \"\"\"\n", - "Routine:\n", - "\\t- Eat\n", - "\\t- Sleep\\n\\t- Repeat\n", - "\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#PrecisionWidth and FieldWidth" - ] - }, - { - "cell_type": "markdown", + "execution_count": 52, "metadata": {}, - "source": [ - "Fieldwidth is the width of the entire number and precision is the width towards the right. One can alter these widths based on the requirements.\n", - "\n", - "The default Precision Width is set to 6." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, "outputs": [ { "data": { "text/plain": [ - "'3.121312'" + "'floats: 4.2'" ] }, - "execution_count": 14, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"%f\" % 3.121312312312" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Notice upto 6 decimal points are returned. To specify the number of decimal points, '%(fieldwidth).(precisionwidth)f' is used." + "\"floats: {}\".format(4.2)" ] }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, + "execution_count": 53, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'3.12131'" + "'lists: [1, 2, 3]'" ] }, - "execution_count": 15, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"%.5f\" % 3.121312312312" + "\"lists: {}\".format([1,2,3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "If the field width is set more than the necessary than the data right aligns itself to adjust to the specified values." + "You can pass multiple arguments to `format()` as well:" ] }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 54, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "' 3.12131'" + "'Hello World!'" ] }, - "execution_count": 16, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"%9.5f\" % 3.121312312312" + "\"Hello {}{}\".format(string1, string2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Zero padding is done by adding a 0 at the start of fieldwidth." + "# Precision and padding" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "By default `format()` will print the full precision of a `float`:" ] }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 55, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'00000000000003.12131'" + "'3.121312312312'" ] }, - "execution_count": 17, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"%020.5f\" % 3.121312312312" + "\"{}\".format(3.121312312312)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "For proper alignment, a space can be left blank in the field width so that when a negative number is used, proper alignment is maintained." + "You can limit this by using `{:.}` in the format specifier: " ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " 3.121312\n", - "-3.121312\n" - ] - } - ], - "source": [ - "print \"% 9f\" % 3.121312312312\n", - "print \"% 9f\" % -3.121312312312" - ] - }, - { - "cell_type": "markdown", + "execution_count": 43, "metadata": {}, - "source": [ - "'+' sign can be returned at the beginning of a positive number by adding a + sign at the beginning of the field width." - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "+3.121312\n", - "-3.121312\n" - ] + "data": { + "text/plain": [ + "'3.12'" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print \"%+9f\" % 3.121312312312\n", - "print \"% 9f\" % -3.121312312312" + "\"{:.3}\".format(3.121312312312)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "As mentioned above, the data right aligns itself when the field width mentioned is larger than the actualy field width. But left alignment can be done by specifying a negative symbol in the field width." + "Similarly, you can specify padding with `{:.}`:" ] }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 44, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'3.121 '" + "' 3.12'" ] }, - "execution_count": 20, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\"%-9.3f\" % 3.121312312312" + "'{:6.3}'.format(3.121312312312)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This just scratches the surface of Python string formatting. Read all about it in [PEP 3101](https://www.python.org/dev/peps/pep-3101/)." ] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/03.ipynb b/03.ipynb index 7a9de7f..823f5b0 100644 --- a/03.ipynb +++ b/03.ipynb @@ -4,28 +4,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "# Data Structures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Data Structures" + "In simple terms, a data structure is a collection or group of data in a particular structure." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "In simple terms, It is the the collection or group of data in a particular structure." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Lists" + "## Lists" ] }, { @@ -34,55 +27,36 @@ "source": [ "Lists are the most commonly used data structure. Think of it as a sequence of data that is enclosed in square brackets and data are separated by a comma. Each of these data can be accessed by calling it's index value.\n", "\n", - "Lists are declared by just equating a variable to '[ ]' or list." + "Lists can be declared in literal form with `[ ]`:" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [], "source": [ - "a = []" + "x = ['apple', 'pear', 'orange']" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print type(a)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "One can directly assign the sequence of data to a list x as shown." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "x = ['apple', 'orange']" + "type(x)" ] }, { @@ -96,15 +70,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In python, Indexing starts from 0. Thus now the list x, which has two elements will have apple at 0 index and orange at 1 index." + "In python, Indexing starts from 0. Thus now the list `x`, which has two elements will have apple at 0 index and orange at 1 index." ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "data": { @@ -112,7 +84,7 @@ "'apple'" ] }, - "execution_count": 4, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -130,10 +102,8 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 15, + "metadata": {}, "outputs": [ { "data": { @@ -141,7 +111,7 @@ "'orange'" ] }, - "execution_count": 5, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -154,15 +124,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "As you might have already guessed, x[0] = x[-2], x[1] = x[-1]. This concept can be extended towards lists with more many elements." + "As you might have already guessed, `x[0] = x[-2], x[1] = x[-1]`. This concept can be extended towards lists with more many elements." ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [], "source": [ "y = ['carrot','potato']" @@ -172,27 +140,28 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Here we have declared two lists x and y each containing its own data. Now, these two lists can again be put into another list say z which will have it's data as two lists. This list inside a list is called as nested lists and is how an array would be declared which we will see later." + "Here we have declared two lists `x` and `y` each containing its own data. Now, these two lists can again be put into another list say `z` which will have it's data as two lists. This list inside a list is called as nested lists and is how an array would be declared which we will see later." ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[['apple', 'orange'], ['carrot', 'potato']]\n" - ] + "data": { + "text/plain": [ + "[['apple', 'pear', 'orange'], ['carrot', 'potato']]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "z = [x,y]\n", - "print z" + "z = [x,y]\n", + "z" ] }, { @@ -202,42 +171,41 @@ "Indexing in nested lists can be quite confusing if you do not understand how indexing works in python. So let us break it down and then arrive at a conclusion.\n", "\n", "Let us access the data 'apple' in the above nested list.\n", - "First, at index 0 there is a list ['apple','orange'] and at index 1 there is another list ['carrot','potato']. Hence z[0] should give us the first list which contains 'apple'." + "First, at index 0 there is a list ['apple','orange'] and at index 1 there is another list ['carrot','potato']. Hence `z[0]` should give us the first list which contains 'apple'." ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'orange']\n" - ] + "data": { + "text/plain": [ + "['apple', 'pear', 'orange']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "z1 = z[0]\n", - "print z1" + "z1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Now observe that z1 is not at all a nested list thus to access 'apple', z1 should be indexed at 0." + "Now observe that `z1` is not at all a nested list thus to access 'apple', `z1` should be indexed at 0." ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "data": { @@ -245,7 +213,7 @@ "'apple'" ] }, - "execution_count": 9, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -258,15 +226,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Instead of doing the above, In python, you can access 'apple' by just writing the index values each time side by side." + "Instead of doing the above, In Python, you can access 'apple' by just writing the index values each time side by side." ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, "outputs": [ { "data": { @@ -274,7 +240,7 @@ "'apple'" ] }, - "execution_count": 10, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -287,7 +253,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If there was a list inside a list inside a list then you can access the innermost value by executing z[ ][ ][ ]." + "If there was a list inside a list inside a list then you can access the innermost value by executing `z[ ][ ][ ]`." ] }, { @@ -301,41 +267,50 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Indexing was only limited to accessing a single element, Slicing on the other hand is accessing a sequence of data inside the list. In other words \"slicing\" the list.\n", + "Indexing was only limited to accessing a single element. Slicing on the other hand is accessing a sequence of data inside the list. In other words \"slicing\" the list.\n", "\n", - "Slicing is done by defining the index values of the first element and the last element from the parent list that is required in the sliced list. It is written as parentlist[ a : b ] where a,b are the index values from the parent list. If a or b is not defined then the index value is considered to be the first value for a if a is not defined and the last value for b when b is not defined." + "Slicing is done by defining the index values of the first element and the last element from the parent list that is required in the sliced list. It is written as parentlist `[ a : b ]` where `a,b` are the index values from the parent list. If a or b is not defined then the index value is considered to be the first value for a if a is not defined and the last value for b when b is not defined." ] }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "num = [0,1,2,3,4,5,6,7,8,9]" + "num = [0,1,2,3,4,5,6,7,8,9]\n", + "num[0:4]" ] }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 23, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 2, 3]\n", - "[4, 5, 6, 7, 8, 9]\n" - ] + "data": { + "text/plain": [ + "[4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print num[0:4]\n", - "print num[4:]" + "num[4:]" ] }, { @@ -347,10 +322,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 25, + "metadata": {}, "outputs": [ { "data": { @@ -358,7 +331,7 @@ "[0, 3, 6]" ] }, - "execution_count": 13, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } @@ -371,22 +344,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Built in List Functions" + "### Built in List Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "To find the length of the list or the number of elements in a list, **len( )** is used." + "To find the length of the list or the number of elements in a list, `len( )` is used." ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 27, + "metadata": {}, "outputs": [ { "data": { @@ -394,7 +365,7 @@ "10" ] }, - "execution_count": 14, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -407,15 +378,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If the list consists of all integer elements then **min( )** and **max( )** gives the minimum and maximum value in the list." + "If the list consists of all integer elements then `min( )` and `max( )` gives the minimum and maximum value in the list." ] }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, + "execution_count": 28, + "metadata": {}, "outputs": [ { "data": { @@ -423,7 +392,7 @@ "0" ] }, - "execution_count": 15, + "execution_count": 28, "metadata": {}, "output_type": "execute_result" } @@ -434,10 +403,8 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 29, + "metadata": {}, "outputs": [ { "data": { @@ -445,7 +412,7 @@ "9" ] }, - "execution_count": 16, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } @@ -463,10 +430,8 @@ }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 30, + "metadata": {}, "outputs": [ { "data": { @@ -474,7 +439,7 @@ "[1, 2, 3, 5, 4, 7]" ] }, - "execution_count": 17, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -492,10 +457,8 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, + "execution_count": 31, + "metadata": {}, "outputs": [], "source": [ "names = ['Earth','Air','Fire','Water']" @@ -510,10 +473,8 @@ }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, + "execution_count": 32, + "metadata": {}, "outputs": [ { "data": { @@ -521,7 +482,7 @@ "True" ] }, - "execution_count": 19, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -532,10 +493,8 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 33, + "metadata": {}, "outputs": [ { "data": { @@ -543,7 +502,7 @@ "False" ] }, - "execution_count": 20, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -556,15 +515,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In a list with elements as string, **max( )** and **min( )** is applicable. **max( )** would return a string element whose ASCII value is the highest and the lowest when **min( )** is used. Note that only the first index of each element is considered each time and if they value is the same then second index considered so on and so forth." + "In a list with elements as string, `max( )` and `min( )` is applicable. `max( )` would return a string element whose ASCII value is the highest and the lowest when `min( )` is used. Note that only the first index of each element is considered each time and if they value is the same then second index considered so on and so forth." ] }, { "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": true - }, + "execution_count": 34, + "metadata": {}, "outputs": [], "source": [ "mlist = ['bzaa','ds','nc','az','z','klm']" @@ -572,23 +529,22 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, + "execution_count": 35, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "z\n", - "az\n" - ] + "data": { + "text/plain": [ + "('z', 'az')" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print max(mlist)\n", - "print min(mlist)" + "max(mlist), min(mlist)" ] }, { @@ -600,10 +556,8 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": true - }, + "execution_count": 36, + "metadata": {}, "outputs": [], "source": [ "nlist = ['1','94','93','1000']" @@ -611,77 +565,73 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, + "execution_count": 38, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "94\n", - "1\n" - ] + "data": { + "text/plain": [ + "('94', '1')" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print max(nlist)\n", - "print min(nlist)" + "max(nlist), min(nlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Even if the numbers are declared in a string the first index of each element is considered and the maximum and minimum values are returned accordingly." + "Here, the strings are still sorted as string (that is, lexically) even though they \"look like\" numbers." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "But if you want to find the **max( )** string element based on the length of the string then another parameter 'key=len' is declared inside the **max( )** and **min( )** function." + "But if you want to find the `max( )` string element based on the length of the string then another parameter 'key=len' is declared inside the `max( )` and `min( )` function." ] }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, + "execution_count": 39, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Earth\n", - "Air\n" - ] + "data": { + "text/plain": [ + "('Earth', 'Air')" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print max(names, key=len)\n", - "print min(names, key=len)" + "max(names, key=len), min(names, key=len)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "But even 'Water' has length 5. **max()** or **min()** function returns the first element when there are two or more elements with the same length.\n", + "But even 'Water' has length 5. `max()` or `min()` function returns the first element when there are two or more elements with the same length.\n", "\n", - "Any other built in function can be used or lambda function (will be discussed later) in place of len.\n", + "Any other built in function can be used or lambda function (will be discussed later) in place of `len`.\n", "\n", - "A string can be converted into a list by using the **list()** function." + "A string can be converted into a list by using the `list()` function." ] }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, + "execution_count": 40, + "metadata": {}, "outputs": [ { "data": { @@ -689,7 +639,7 @@ "['h', 'e', 'l', 'l', 'o']" ] }, - "execution_count": 26, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -702,15 +652,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**append( )** is used to add a element at the end of the list." + "`append( )` is used to add a element at the end of the list." ] }, { "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, + "execution_count": 41, + "metadata": {}, "outputs": [], "source": [ "lst = [1,1,4,8,7]" @@ -718,37 +666,36 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, + "execution_count": 42, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1]\n" - ] + "data": { + "text/plain": [ + "[1, 1, 4, 8, 7, 1]" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.append(1)\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**count( )** is used to count the number of a particular element that is present in the list. " + "`count( )` is used to count the number of a particular element that is present in the list. " ] }, { "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, + "execution_count": 43, + "metadata": {}, "outputs": [ { "data": { @@ -756,7 +703,7 @@ "3" ] }, - "execution_count": 29, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } @@ -769,15 +716,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**append( )** function can also be used to add a entire list at the end. Observe that the resultant list becomes a nested list." + "`append( )` function can also be used to add a entire list at the end. Observe that the resultant list becomes a nested list." ] }, { "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": true - }, + "execution_count": 44, + "metadata": {}, "outputs": [], "source": [ "lst1 = [5,4,2,8]" @@ -785,66 +730,64 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, + "execution_count": 45, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8]]\n" - ] + "data": { + "text/plain": [ + "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8]]" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.append(lst1)\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ - "But if nested list is not what is desired then **extend( )** function can be used." + "But if nested list is not what is desired then `extend( )` function can be used." ] }, { "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, + "execution_count": 46, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] - } - ], - "source": [ - "lst.extend(lst1)\n", - "print lst" - ] + "data": { + "text/plain": [ + "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2, 8]" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst.extend(lst1)\n", + "lst" + ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**index( )** is used to find the index value of a particular element. Note that if there are multiple elements of the same value then the first index value of that element is returned." + "`index( )` is used to find the index value of a particular element. Note that if there are multiple elements of the same value then the first index value of that element is returned." ] }, { "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, + "execution_count": 47, + "metadata": {}, "outputs": [ { "data": { @@ -852,7 +795,7 @@ "0" ] }, - "execution_count": 33, + "execution_count": 47, "metadata": {}, "output_type": "execute_result" } @@ -865,69 +808,69 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**insert(x,y)** is used to insert a element y at a specified index value x. **append( )** function made it only possible to insert at the end. " + "`insert(x,y)` is used to insert a element y at a specified index value x. `append( )` function made it only possible to insert at the end. " ] }, { "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, + "execution_count": 48, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] + "data": { + "text/plain": [ + "[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4, 2, 8]" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.insert(5, 'name')\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**insert(x,y)** inserts but does not replace element. If you want to replace the element with another element you simply assign the value to that particular index." + "`insert(x,y)` inserts but does not replace element. If you want to replace the element with another element you simply assign the value to that particular index." ] }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, + "execution_count": 49, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] + "data": { + "text/plain": [ + "[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2, 8]" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst[5] = 'Python'\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**pop( )** function return the last element in the list. This is similar to the operation of a stack. Hence it wouldn't be wrong to tell that lists can be used as a stack." + "`pop( )` function return the last element in the list. This is similar to the operation of a stack. Hence it wouldn't be wrong to tell that lists can be used as a stack." ] }, { "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, + "execution_count": 50, + "metadata": {}, "outputs": [ { "data": { @@ -935,7 +878,7 @@ "8" ] }, - "execution_count": 36, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -953,10 +896,8 @@ }, { "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, + "execution_count": 51, + "metadata": {}, "outputs": [ { "data": { @@ -964,7 +905,7 @@ "1" ] }, - "execution_count": 37, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -977,83 +918,84 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**pop( )** is used to remove element based on it's index value which can be assigned to a variable. One can also remove element by specifying the element itself using the **remove( )** function." + "`pop( )` is used to remove element based on it's index value which can be assigned to a variable. One can also remove element by specifying the element itself using the `remove( )` function." ] }, { "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, + "execution_count": 52, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n" - ] + "data": { + "text/plain": [ + "[1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.remove('Python')\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Alternative to **remove** function but with using index value is **del**" + "Alternative to `remove` function but with using index value is `del`" ] }, { "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, + "execution_count": 54, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n" - ] + "data": { + "text/plain": [ + "[1, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "del lst[1]\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ - "The entire elements present in the list can be reversed by using the **reverse()** function." + "The entire elements present in the list can be reversed by using the `reverse()` function." ] }, { "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, + "execution_count": 55, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 4, 5, [5, 4, 2, 8], 1, 7, 8, 1]\n" - ] + "data": { + "text/plain": [ + "[2, 4, 5, [5, 4, 2, 8], 1, 7, 8, 1]" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.reverse()\n", - "print lst" + "lst" ] }, { @@ -1062,27 +1004,29 @@ "source": [ "Note that the nested list [5,4,2,8] is treated as a single element of the parent list lst. Thus the elements inside the nested list is not reversed.\n", "\n", - "Python offers built in operation **sort( )** to arrange the elements in ascending order." + "Python offers built in operation `sort( )` to arrange the elements in ascending order." ] }, { "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false - }, + "execution_count": 57, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 2, 4, 5, 7, 8, [5, 4, 2, 8]]\n" - ] + "data": { + "text/plain": [ + "[1, 2, 3, 6, 7, 9, 42]" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ + "lst = [9, 6, 3, 7, 2, 1, 42]\n", "lst.sort()\n", - "print lst" + "lst" ] }, { @@ -1094,52 +1038,72 @@ }, { "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false - }, + "execution_count": 59, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[5, 4, 2, 8], 8, 7, 5, 4, 2, 1, 1]\n" - ] + "data": { + "text/plain": [ + "[42, 9, 7, 6, 3, 2, 1]" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lst.sort(reverse=True)\n", - "print lst" + "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Similarly for lists containing string elements, **sort( )** would sort the elements based on it's ASCII value in ascending and by specifying reverse=True in descending." + "Similarly for lists containing string elements, `sort( )` would sort the elements based on it's ASCII value in ascending and by specifying `reverse=True` in descending." ] }, { "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, + "execution_count": 61, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Air', 'Earth', 'Fire', 'Water']\n", - "['Water', 'Fire', 'Earth', 'Air']\n" - ] + "data": { + "text/plain": [ + "['Air', 'Earth', 'Fire', 'Water']" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "names.sort()\n", - "print names\n", + "names" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Water', 'Fire', 'Earth', 'Air']" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ "names.sort(reverse=True)\n", - "print names" + "names" ] }, { @@ -1151,25 +1115,44 @@ }, { "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, + "execution_count": 63, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Air', 'Fire', 'Water', 'Earth']\n", - "['Water', 'Earth', 'Fire', 'Air']\n" - ] + "data": { + "text/plain": [ + "['Air', 'Fire', 'Water', 'Earth']" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "names.sort(key=len)\n", - "print names\n", + "names" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Water', 'Earth', 'Fire', 'Air']" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ "names.sort(key=len,reverse=True)\n", - "print names" + "names" ] }, { @@ -1183,212 +1166,240 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Most of the new python programmers commit this mistake. Consider the following," + "To copy a list, it's not sufficient to simply assign a new name to it. Consider the following," ] }, { "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, + "execution_count": 66, + "metadata": {}, "outputs": [], "source": [ - "lista= [2,1,4,3]" + "lista = [2,1,4,3]" ] }, { "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, + "execution_count": 67, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] + "data": { + "text/plain": [ + "[2, 1, 4, 3]" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "listb = lista\n", - "print listb" + "listb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Here, We have declared a list, lista = [2,1,4,3]. This list is copied to listb by assigning it's value and it get's copied as seen. Now we perform some random operations on lista." + "Here, We have declared a list, `lista = [2,1,4,3]`. This list is assigned to `listb`. While we might expect `lista` and `listb` to be different lists, we can see that changes to `lista` are reflected in `listb`:" ] }, { "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, + "execution_count": 68, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4]\n", - "[2, 1, 4, 9]\n" - ] + "data": { + "text/plain": [ + "[2, 1, 4, 9]" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "lista.pop()\n", - "print lista\n", "lista.append(9)\n", - "print lista" + "lista" ] }, { "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false - }, + "execution_count": 70, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 9]\n" - ] + "data": { + "text/plain": [ + "[2, 1, 4, 9]" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print listb" + "listb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "listb has also changed though no operation has been performed on it. This is because you have assigned the same memory space of lista to listb. So how do fix this?\n", + "`listb` has also changed though no operation has been performed on it. This is because `lista` and `listb` refer to the same object. So how do fix this?\n", "\n", - "If you recall, in slicing we had seen that parentlist[a:b] returns a list from parent list with start index a and end index b and if a and b is not mentioned then by default it considers the first and last element. We use the same concept here. By doing so, we are assigning the data of lista to listb as a variable." + "One way to copy a list is to construct a new list around it:" ] }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([1, 2, 3, 4], [2, 1, 4, 3])" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista = [2,1,4,3]\n", + "listb = list(lista)\n", + "\n", + "lista.sort()\n", + "\n", + "lista, listb" + ] + }, + { + "cell_type": "markdown", "metadata": { "collapsed": true }, - "outputs": [], "source": [ - "lista = [2,1,4,3]" + "Another approach is to use slicing. Since slicing creates copies, a full slice of a list will effectively create a copy of it:" ] }, { "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, + "execution_count": 74, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] + "data": { + "text/plain": [ + "([1, 2, 3, 4], [1, 2, 3, 4])" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "listb = lista[:]\n", - "print listb" + "lista, listb" ] }, { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4]\n", - "[2, 1, 4, 9]\n" - ] - } - ], + "cell_type": "markdown", + "metadata": {}, "source": [ - "lista.pop()\n", - "print lista\n", - "lista.append(9)\n", - "print lista" + "These lists are different objects but have identical contents. We can see this by modifying one of them:" ] }, { "cell_type": "code", - "execution_count": 52, - "metadata": { - "collapsed": false - }, + "execution_count": 75, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] + "data": { + "text/plain": [ + "([1, 2, 3], [1, 2, 3, 4])" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print listb" + "lista.pop()\n", + "lista, listb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Tuples" + "## Tuples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Tuples are similar to lists but only big difference is the elements inside a list can be changed but in tuple it cannot be changed. Think of tuples as something which has to be True for a particular something and cannot be True for no other values. For better understanding, Recall **divmod()** function." + "Tuples are similar to lists but only big difference is the elements inside a list can be changed but in tuple it cannot be changed. We can create tuples literals similar to how we create lists but using `()` instead:" ] }, { "cell_type": "code", - "execution_count": 53, - "metadata": { - "collapsed": false - }, + "execution_count": 76, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "(3, 1)\n", - "\n" - ] + "data": { + "text/plain": [ + "(3, 8, 5)" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "xyz = divmod(10,3)\n", - "print xyz\n", - "print type(xyz)" + "xyz = (3, 8, 5)\n", + "xyz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Here the quotient has to be 3 and the remainder has to be 1. These values cannot be changed whatsoever when 10 is divided by 3. Hence divmod returns these values in a tuple." + "We can see that this is a tuple with the `type()` function:" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(xyz)" ] }, { @@ -1400,10 +1411,8 @@ }, { "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": false - }, + "execution_count": 79, + "metadata": {}, "outputs": [], "source": [ "tup = ()\n", @@ -1414,15 +1423,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If you want to directly declare a tuple it can be done by using a comma at the end of the data." + "One thing to keep in mind is that if you want to declare a tuple with a single element, you need a trailing comma:" ] }, { "cell_type": "code", - "execution_count": 55, - "metadata": { - "collapsed": false - }, + "execution_count": 81, + "metadata": {}, "outputs": [ { "data": { @@ -1430,42 +1437,40 @@ "(27,)" ] }, - "execution_count": 55, + "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "27," + "(27,)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "27 when multiplied by 2 yields 54, But when multiplied with a tuple the data is repeated twice." + "You can multiply a tuple by an integer to repeat it that many times:" ] }, { "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": false - }, + "execution_count": 83, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "(27, 27)" + "('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c')" ] }, - "execution_count": 56, + "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "2*(27,)" + "3 * ('a', 'b', 'c')" ] }, { @@ -1477,112 +1482,124 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 84, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1, 2, 3)\n", - "('H', 'e', 'l', 'l', 'o')\n" - ] + "data": { + "text/plain": [ + "(1, 2, 3)" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "tup3 = tuple([1,2,3])\n", - "print tup3\n", - "tup4 = tuple('Hello')\n", - "print tup4" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "It follows the same indexing and slicing as Lists." + "tup3" ] }, { "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": false - }, + "execution_count": 85, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "('H', 'e', 'l')\n" - ] + "data": { + "text/plain": [ + "('H', 'e', 'l', 'l', 'o')" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print tup3[1]\n", - "tup5 = tup4[:3]\n", - "print tup5" + "tup4 = tuple('Hello')\n", + "tup4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Mapping one tuple to another" + "It follows the same indexing and slicing as Lists." ] }, { "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "(a,b,c)= ('alpha','beta','gamma')" + "tup3[1]" ] }, { "cell_type": "code", - "execution_count": 60, - "metadata": { - "collapsed": false - }, + "execution_count": 87, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "alpha beta gamma\n" - ] + "data": { + "text/plain": [ + "('H', 'e', 'l')" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print a,b,c" + "tup5 = tup4[:3]\n", + "tup5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tuple unpacking\n", + "\n", + "You can assign the elements of a tuple to variables by separating the variable names by commans:" ] }, { "cell_type": "code", - "execution_count": 61, - "metadata": { - "collapsed": false - }, + "execution_count": 89, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "('R', 'a', 'j', 'a', 't', 'h', 'K', 'u', 'm', 'a', 'r', 'M', 'P')\n" - ] + "data": { + "text/plain": [ + "'The elements are alpha, beta, and gamma'" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "d = tuple('RajathKumarMP')\n", - "print d" + "a,b,c = ('alpha','beta','gamma')\n", + "\"The elements are {}, {}, and {}\".format(a, b, c)" ] }, { @@ -1596,15 +1613,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**count()** function counts the number of specified element that is present in the tuple." + "`count()` function counts the number of specified element that is present in the tuple." ] }, { "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, + "execution_count": 93, + "metadata": {}, "outputs": [ { "data": { @@ -1612,12 +1627,13 @@ "3" ] }, - "execution_count": 62, + "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "d = tuple('alphabetical')\n", "d.count('a')" ] }, @@ -1625,23 +1641,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**index()** function returns the index of the specified element. If the elements are more than one then the index of the first element of that specified element is returned" + "`index()` function returns the index of the specified element. If the elements are more than one then the index of the first element of that specified element is returned" ] }, { "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": false - }, + "execution_count": 94, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1" + "0" ] }, - "execution_count": 63, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } @@ -1654,56 +1668,58 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Sets" + "## Sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Sets are mainly used to eliminate repeated numbers in a sequence/list. It is also used to perform some standard set operations.\n", + "Sets are collections of unique, immutable objects. It is also used to perform some standard set operations.\n", "\n", - "Sets are declared as set() which will initialize a empty set. Also set([sequence]) can be executed to declare a set with elements" + "Sets are declared as `set()` which will initialize a empty set. Also `set([sequence])` can be executed to declare a set with elements" ] }, { "cell_type": "code", - "execution_count": 64, - "metadata": { - "collapsed": false - }, + "execution_count": 95, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "set1 = set()\n", - "print type(set1)" + "type(set1)" ] }, { "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": false - }, + "execution_count": 96, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "set([1, 2, 3, 4])\n" - ] + "data": { + "text/plain": [ + "{1, 2, 3, 4}" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "set0 = set([1,2,2,3,3,4])\n", - "print set0" + "set0" ] }, { @@ -1717,15 +1733,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Built-in Functions" + "### Built-in Functions" ] }, { "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": false - }, + "execution_count": 97, + "metadata": {}, "outputs": [], "source": [ "set1 = set([1,2,3])" @@ -1733,10 +1747,8 @@ }, { "cell_type": "code", - "execution_count": 67, - "metadata": { - "collapsed": false - }, + "execution_count": 98, + "metadata": {}, "outputs": [], "source": [ "set2 = set([2,3,4,5])" @@ -1746,15 +1758,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**union( )** function returns a set which contains all the elements of both the sets without repition." + "`union( )` function returns a set which contains all the elements of both the sets without repition." ] }, { "cell_type": "code", - "execution_count": 68, - "metadata": { - "collapsed": false - }, + "execution_count": 99, + "metadata": {}, "outputs": [ { "data": { @@ -1762,7 +1772,7 @@ "{1, 2, 3, 4, 5}" ] }, - "execution_count": 68, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } @@ -1775,15 +1785,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**add( )** will add a particular element into the set. Note that the index of the newly added element is arbitrary and can be placed anywhere not neccessarily in the end." + "`add( )` will add a particular element into the set. Note that the index of the newly added element is arbitrary and can be placed anywhere not neccessarily in the end." ] }, { "cell_type": "code", - "execution_count": 69, - "metadata": { - "collapsed": false - }, + "execution_count": 100, + "metadata": {}, "outputs": [ { "data": { @@ -1791,7 +1799,7 @@ "{0, 1, 2, 3}" ] }, - "execution_count": 69, + "execution_count": 100, "metadata": {}, "output_type": "execute_result" } @@ -1805,15 +1813,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**intersection( )** function outputs a set which contains all the elements that are in both sets." + "`intersection( )` function outputs a set which contains all the elements that are in both sets." ] }, { "cell_type": "code", - "execution_count": 70, - "metadata": { - "collapsed": false - }, + "execution_count": 101, + "metadata": {}, "outputs": [ { "data": { @@ -1821,7 +1827,7 @@ "{2, 3}" ] }, - "execution_count": 70, + "execution_count": 101, "metadata": {}, "output_type": "execute_result" } @@ -1834,15 +1840,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**difference( )** function ouptuts a set which contains elements that are in set1 and not in set2." + "`difference( )` function ouptuts a set which contains elements that are in set1 and not in set2." ] }, { "cell_type": "code", - "execution_count": 71, - "metadata": { - "collapsed": false - }, + "execution_count": 102, + "metadata": {}, "outputs": [ { "data": { @@ -1850,7 +1854,7 @@ "{0, 1}" ] }, - "execution_count": 71, + "execution_count": 102, "metadata": {}, "output_type": "execute_result" } @@ -1863,15 +1867,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**symmetric_difference( )** function ouputs a function which contains elements that are in one of the sets." + "`symmetric_difference( )` function ouputs a function which contains elements that are in one of the sets." ] }, { "cell_type": "code", - "execution_count": 72, - "metadata": { - "collapsed": false - }, + "execution_count": 103, + "metadata": {}, "outputs": [ { "data": { @@ -1879,7 +1881,7 @@ "{0, 1, 4, 5}" ] }, - "execution_count": 72, + "execution_count": 103, "metadata": {}, "output_type": "execute_result" } @@ -1892,15 +1894,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**issubset( ), isdisjoint( ), issuperset( )** is used to check if the set1/set2 is a subset, disjoint or superset of set2/set1 respectively." + "`issubset( ), isdisjoint( ), issuperset( )` is used to check if the set1/set2 is a subset, disjoint or superset of set2/set1 respectively." ] }, { "cell_type": "code", - "execution_count": 73, - "metadata": { - "collapsed": false - }, + "execution_count": 104, + "metadata": {}, "outputs": [ { "data": { @@ -1908,7 +1908,7 @@ "False" ] }, - "execution_count": 73, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } @@ -1919,10 +1919,8 @@ }, { "cell_type": "code", - "execution_count": 74, - "metadata": { - "collapsed": false - }, + "execution_count": 105, + "metadata": {}, "outputs": [ { "data": { @@ -1930,7 +1928,7 @@ "False" ] }, - "execution_count": 74, + "execution_count": 105, "metadata": {}, "output_type": "execute_result" } @@ -1941,10 +1939,8 @@ }, { "cell_type": "code", - "execution_count": 75, - "metadata": { - "collapsed": false - }, + "execution_count": 106, + "metadata": {}, "outputs": [ { "data": { @@ -1952,7 +1948,7 @@ "False" ] }, - "execution_count": 75, + "execution_count": 106, "metadata": {}, "output_type": "execute_result" } @@ -1965,42 +1961,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**pop( )** is used to remove an arbitrary element in the set" + "`pop( )` is used to remove an arbitrary element in the set" ] }, { "cell_type": "code", - "execution_count": 76, - "metadata": { - "collapsed": false - }, + "execution_count": 107, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "set([1, 2, 3])\n" - ] + "data": { + "text/plain": [ + "{1, 2, 3}" + ] + }, + "execution_count": 107, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "set1.pop()\n", - "print set1" + "set1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**remove( )** function deletes the specified element from the set." + "`remove( )` function deletes the specified element from the set." ] }, { "cell_type": "code", - "execution_count": 77, - "metadata": { - "collapsed": false - }, + "execution_count": 108, + "metadata": {}, "outputs": [ { "data": { @@ -2008,7 +2003,7 @@ "{1, 3}" ] }, - "execution_count": 77, + "execution_count": 108, "metadata": {}, "output_type": "execute_result" } @@ -2022,15 +2017,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**clear( )** is used to clear all the elements and make that set an empty set." + "`clear( )` is used to clear all the elements and make that set an empty set." ] }, { "cell_type": "code", - "execution_count": 78, - "metadata": { - "collapsed": false - }, + "execution_count": 109, + "metadata": {}, "outputs": [ { "data": { @@ -2038,7 +2031,7 @@ "set()" ] }, - "execution_count": 78, + "execution_count": 109, "metadata": {}, "output_type": "execute_result" } @@ -2051,23 +2044,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/04.ipynb b/04.ipynb index 5a502ea..c626147 100644 --- a/04.ipynb +++ b/04.ipynb @@ -4,61 +4,103 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "## Strings, continued" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Strings" + "Strings are immutable sequences of unicode code points:" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 82, "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Taj Mahal is beautiful'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ - "Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes." + "string0 = 'Taj Mahal is beautiful'\n", + "display(String0 , type(String0))" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [], + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Taj Mahal is beautiful'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ - "String0 = 'Taj Mahal is beautiful'\n", "String1 = \"Taj Mahal is beautiful\"\n", - "String2 = '''Taj Mahal\n", - "is\n", - "beautiful'''" + "display(String1, type(String1))" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 79, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Taj Mahal is beautiful \n", - "Taj Mahal is beautiful \n", - "Taj Mahal\n", - "is\n", - "beautiful \n" - ] + "data": { + "text/plain": [ + "'Taj Mahal\\nis\\nbeautiful'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "str" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "print String0 , type(String0)\n", - "print String1, type(String1)\n", - "print String2, type(String2)" + "String2 = '''Taj Mahal\n", + "is\n", + "beautiful'''\n", + "display(String2, type(String2))" ] }, { @@ -70,58 +112,84 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 80, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "M\n", - "Mahal is beautiful\n" - ] + "data": { + "text/plain": [ + "'M'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "'Mahal is beautiful'" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "print String0[4]\n", - "print String0[4:]" + "display(String0[4], String0[4:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "###Built-in Functions" + "### Built-in Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**find( )** function returns the index value of the given data that is to found in the string. If it is not found it returns **-1**. Remember to not confuse the returned -1 for reverse indexing value." + "`find( )` function returns the index value of the given data that is to found in the string. If it is not found it returns `-1`. Remember to not confuse the returned -1 for reverse indexing value." ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 10, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "-1\n" - ] + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print String0.find('al')\n", - "print String0.find('am')" + "String0.find('al')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "String0.find('am')" ] }, { @@ -133,91 +201,110 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "a\n" - ] + "data": { + "text/plain": [ + "'a'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print String0[7]" + "String0[7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "One can also input **find( )** function between which index values it has to search." + "One can also input `find( )` function between which index values it has to search." ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "2\n" - ] + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print String0.find('j',1)\n", - "print String0.find('j',1,3)" + "String0.find('j',1)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "String0.find('j',1,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**capitalize( )** is used to capitalize the first element in the string." + "`capitalize( )` is used to capitalize the first element in the string." ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Observe the first letter in this sentence.\n" - ] + "data": { + "text/plain": [ + "'Observe the first letter in this sentence.'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "String3 = 'observe the first letter in this sentence.'\n", - "print String3.capitalize()" + "String3.capitalize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**center( )** is used to center align the string by specifying the field width." + "`center( )` is used to center align the string by specifying the field width." ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { "data": { @@ -225,7 +312,7 @@ "' Taj Mahal is beautiful '" ] }, - "execution_count": 8, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -243,10 +330,8 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "data": { @@ -254,7 +339,7 @@ "'------------------------Taj Mahal is beautiful------------------------'" ] }, - "execution_count": 9, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -267,15 +352,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**zfill( )** is used for zero padding by specifying the field width." + "`zfill( )` is used for zero padding by specifying the field width." ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "data": { @@ -283,7 +366,7 @@ "'00000000Taj Mahal is beautiful'" ] }, - "execution_count": 10, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -296,31 +379,68 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**expandtabs( )** allows you to change the spacing of the tab character. '\\t' which is by default set to 8 spaces." + "`expandtabs( )` allows you to change the spacing of the tab character. '\\t' which is by default set to 8 spaces." ] }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "h\te\tl\tl\to\n", - "h e l l o\n", - "h e l l o\n" - ] + "data": { + "text/plain": [ + "'h\\te\\tl\\tl\\to'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "s = 'h\\te\\tl\\tl\\to'\n", - "print s\n", - "print s.expandtabs(1)\n", - "print s.expandtabs()" + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'h e l l o'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.expandtabs(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'h e l l o'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.expandtabs()" ] }, { @@ -329,24 +449,54 @@ "collapsed": true }, "source": [ - "**index( )** works the same way as **find( )** function the only difference is find returns '-1' when the input element is not found in the string but **index( )** function throws a ValueError" + "`index( )` works the same way as `find( )` function the only difference is find returns '-1' when the input element is not found in the string but `index( )` function throws a `ValueError`" ] }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 26, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "4\n" - ] - }, + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "String0.index('Taj')" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "String0.index('Mahal',0)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ { "ename": "ValueError", "evalue": "substring not found", @@ -354,43 +504,40 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Taj'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: substring not found" ] } ], "source": [ - "print String0.index('Taj')\n", - "print String0.index('Mahal',0)\n", - "print String0.index('Mahal',10,20)" + "String0.index('Mahal',10,20)" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ - "**endswith( )** function is used to check if the given string ends with the particular char which is given as input." + "`endswith( )` function is used to check if the given string ends with the particular char which is given as input." ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 30, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "False\n" - ] + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print String0.endswith('y')" + "\"llamas\".endswith('y')" ] }, { @@ -402,68 +549,79 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 78, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "print String0.endswith('l',0)\n", - "print String0.endswith('M',0,5)" + "display(\"llamas\".endswith('as',0), \"llamas\".endswith('as',0,3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**count( )** function counts the number of char in the given string. The start and the stop index can also be specified or left blank. (These are Implicit arguments which will be dealt in functions)" + "`count( )` function counts the number of char in the given string. The start and the stop index can also be specified or left blank. (These are Implicit arguments which will be dealt in functions)" ] }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, + "execution_count": 77, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "4\n", - "2\n" - ] + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "print String0.count('a',0)\n", - "print String0.count('a',5,10)" + "s = \"alphabetical\"\n", + "display(s.count('a',0), s.count('a',5,10))" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ - "**join( )** function is used add a char in between the elements of the input string." + "`join( )` function is used add a char in between the elements of the input string." ] }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 37, + "metadata": {}, "outputs": [ { "data": { @@ -471,7 +629,7 @@ "'*a_a-'" ] }, - "execution_count": 16, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } @@ -491,216 +649,235 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**join( )** function can also be used to convert a list into a string." + "`join( )` function can also be used to convert a list into a string." ] }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 76, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['T', 'a', 'j', ' ', 'M', 'a', 'h', 'a', 'l', ' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n", - "Taj Mahal is beautiful\n" - ] + "data": { + "text/plain": [ + "['t',\n", + " 'h',\n", + " 'e',\n", + " ' ',\n", + " 'q',\n", + " 'u',\n", + " 'i',\n", + " 'c',\n", + " 'k',\n", + " ' ',\n", + " 'b',\n", + " 'r',\n", + " 'o',\n", + " 'w',\n", + " 'n',\n", + " ' ',\n", + " 'f',\n", + " 'o',\n", + " 'x']" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "'the quick brown fox'" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ - "a = list(String0)\n", - "print a\n", + "a = list('the quick brown fox')\n", "b = ''.join(a)\n", - "print b" + "display(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Before converting it into a string **join( )** function can be used to insert any char in between the list elements." + "Before converting it into a string `join( )` function can be used to insert any char in between the list elements." ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, + "execution_count": 40, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - " /i/s/ /b/e/a/u/t/i/f/u/l\n" - ] + "data": { + "text/plain": [ + "' /b/r/o/w/n/ /f/o/x'" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "c = '/'.join(a)[18:]\n", - "print c" + "c" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ - "**split( )** function is used to convert a string back to a list. Think of it as the opposite of the **join()** function." + "`split( )` function is used to convert a string back to a list. Think of it as the opposite of the `join()` function." ] }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, + "execution_count": 41, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n" - ] + "data": { + "text/plain": [ + "[' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x']" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "d = c.split('/')\n", - "print d" + "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "In **split( )** function one can also specify the number of times you want to split the string or the number of elements the new returned list should conatin. The number of elements is always one more than the specified number this is because it is split the number of times specified." + "In `split( )` function one can also specify the number of times you want to split the string or the number of elements the new returned list should conatin. The number of elements is always one more than the specified number this is because it is split the number of times specified." ] }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 42, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l']\n", - "4\n" - ] + "data": { + "text/plain": [ + "([' ', 'b', 'r', 'o/w/n/ /f/o/x'], 4)" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "e = c.split('/',3)\n", - "print e\n", - "print len(e)" + "e, len(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**lower( )** converts any capital letter to small letter." + "`lower( )` converts any capital letter to small letter." ] }, { "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, + "execution_count": 43, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Taj Mahal is beautiful\n", - "taj mahal is beautiful\n" - ] + "data": { + "text/plain": [ + "'why am i yelling?'" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print String0\n", - "print String0.lower()" + "s = \"WHY AM I YELLING?\"\n", + "s.lower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**upper( )** converts any small letter to capital letter." + "`upper( )` converts any small letter to capital letter." ] }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, + "execution_count": 44, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'TAJ MAHAL IS BEAUTIFUL'" + "'WHISPER'" ] }, - "execution_count": 22, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "String0.upper()" + "s = 'whisper'\n", + "s.upper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**replace( )** function replaces the element with another element." + "`replace( )` function replaces the element with another element." ] }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, + "execution_count": 45, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'Bengaluru is beautiful'" + "'green eggs'" ] }, - "execution_count": 23, + "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "String0.replace('Taj Mahal','Bengaluru')" + "\"green onions\".replace('onions','eggs')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**strip( )** function is used to delete elements from the right end and the left end which is not required." + "`strip( )` function is used to delete elements from the right end and the left end which is not required." ] }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": true - }, + "execution_count": 46, + "metadata": {}, "outputs": [], "source": [ "f = ' hello '" @@ -715,10 +892,8 @@ }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, + "execution_count": 47, + "metadata": {}, "outputs": [ { "data": { @@ -726,7 +901,7 @@ "'hello'" ] }, - "execution_count": 25, + "execution_count": 47, "metadata": {}, "output_type": "execute_result" } @@ -739,34 +914,30 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**strip( )** function, when a char is specified then it deletes that char if it is present in the two ends of the specified string." + "`strip( )` function, when a char is specified then it deletes that char if it is present in the two ends of the specified string." ] }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": true - }, + "execution_count": 50, + "metadata": {}, "outputs": [], "source": [ - "f = ' ***----hello---******* '" + "f = '***----hello---*******'" ] }, { "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, + "execution_count": 51, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "' ***----hello---******* '" + "'----hello---'" ] }, - "execution_count": 27, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -779,72 +950,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The asterisk had to be deleted but is not. This is because there is a space in both the right and left hand side. So in strip function. The characters need to be inputted in the specific order in which they are present." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "----hello---\n", - "hello\n" - ] - } - ], - "source": [ - "print f.strip(' *')\n", - "print f.strip(' *-')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": false - }, - "source": [ - "**lstrip( )** and **rstrip( )** function have the same functionality as strip function but the only difference is **lstrip( )** deletes only towards the left side and **rstrip( )** towards the right." + "`lstrip( )` and `rstrip( )` function have the same functionality as strip function but the only difference is `lstrip( )` deletes only towards the left side and `rstrip( )` towards the right." ] }, { "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, + "execution_count": 52, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "----hello---******* \n", - " ***----hello---\n" - ] + "data": { + "text/plain": [ + "('----hello---*******', '***----hello---')" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print f.lstrip(' *')\n", - "print f.rstrip(' *')" + "display(f.lstrip(' *'), f.rstrip(' *'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Dictionaries" + "## Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Dictionaries are more used like a database because here you can index a particular sequence with your user defined string." + "Dictionaries are mappings of immutable keys to mutable values." ] }, { @@ -856,77 +996,88 @@ }, { "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, + "execution_count": 75, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - " \n" - ] + "data": { + "text/plain": [ + "dict" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "dict" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ "d0 = {}\n", "d1 = dict()\n", - "print type(d0), type(d1)" + "display(type(d0), type(d1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Dictionary works somewhat like a list but with an added capability of assigning it's own index style." + "To assign to a `dict` you can use the `[]` operator:" ] }, { "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, + "execution_count": 55, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'OneTwo': 12, 'One': 1}\n" - ] + "data": { + "text/plain": [ + "{'One': 1, 'OneTwo': 12}" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "d0['One'] = 1\n", "d0['OneTwo'] = 12 \n", - "print d0" + "d0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "That is how a dictionary looks like. Now you are able to access '1' by the index value set at 'One'" + "You can also retrieve values by key with the `[]` operator:" ] }, { "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, + "execution_count": 57, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n" - ] + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "print d0['One']" + "d0['One']" ] }, { @@ -938,10 +1089,8 @@ }, { "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, + "execution_count": 58, + "metadata": {}, "outputs": [], "source": [ "names = ['One', 'Two', 'Three', 'Four', 'Five']\n", @@ -952,92 +1101,93 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**zip( )** function is used to combine two lists" + "`zip( )` function is used to combine two lists" ] }, { "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, + "execution_count": 59, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)]\n" - ] + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "d2 = zip(names,numbers)\n", - "print d2" + "d2" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ "The two lists are combined to form a single list and each elements are clubbed with their respective elements from the other list inside a tuple. Tuples because that is what is assigned and the value should not change.\n", "\n", - "Further, To convert the above into a dictionary. **dict( )** function is used." + "Further, To convert the above into a dictionary. `dict( )` function is used." ] }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, + "execution_count": 61, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n" - ] + "data": { + "text/plain": [ + "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "a1 = dict(d2)\n", - "print a1" + "a1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "###Built-in Functions" + "### Built-in Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**clear( )** function is used to erase the entire database that was created." + "`clear( )` function is used to erase the entire database that was created." ] }, { "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, + "execution_count": 63, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{}\n" - ] + "data": { + "text/plain": [ + "{}" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "a1.clear()\n", - "print a1" + "a1" ] }, { @@ -1049,46 +1199,45 @@ }, { "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, + "execution_count": 70, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n" - ] + "data": { + "text/plain": [ + "{'One': 1, 'Two': 2, 'Three': 3, 'Five': 5, 'Four': 4}" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "for i in range(len(names)):\n", " a1[names[i]] = numbers[i]\n", - "print a1" + "a1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**values( )** function returns a list with all the assigned values in the dictionary." + "`values( )` function returns a list with all the assigned values in the dictionary." ] }, { "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, + "execution_count": 71, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[4, 5, 3, 2, 1]" + "dict_values([1, 2, 3, 5, 4])" ] }, - "execution_count": 38, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -1101,23 +1250,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**keys( )** function returns all the index or the keys to which contains the values that it was assigned to." + "`keys( )` function returns all the index or the keys to which contains the values that it was assigned to." ] }, { "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, + "execution_count": 72, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['Four', 'Five', 'Three', 'Two', 'One']" + "dict_keys(['One', 'Two', 'Three', 'Five', 'Four'])" ] }, - "execution_count": 39, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } @@ -1130,23 +1277,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**items( )** is returns a list containing both the list but each element in the dictionary is inside a tuple. This is same as the result that was obtained when zip function was used." + "`items( )` is returns a list containing both the list but each element in the dictionary is inside a tuple. This is same as the result that was obtained when zip function was used." ] }, { "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, + "execution_count": 73, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[('Four', 4), ('Five', 5), ('Three', 3), ('Two', 2), ('One', 1)]" + "dict_items([('One', 1), ('Two', 2), ('Three', 3), ('Five', 5), ('Four', 4)])" ] }, - "execution_count": 40, + "execution_count": 73, "metadata": {}, "output_type": "execute_result" } @@ -1159,51 +1304,65 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**pop( )** function is used to get the remove that particular element and this removed element can be assigned to a new variable. But remember only the value is stored and not the key. Because the is just a index value." + "`pop( )` function is used to get the remove that particular element and this removed element can be assigned to a new variable. But remember only the value is stored and not the key. Because the is just a index value." ] }, { "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false - }, + "execution_count": 74, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n", - "4\n" - ] + "data": { + "text/plain": [ + "{'One': 1, 'Two': 2, 'Three': 3, 'Five': 5}" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ "a2 = a1.pop('Four')\n", - "print a1\n", - "print a2" + "display(a1, a2)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/05.ipynb b/05.ipynb index d24f4ac..524ab74 100644 --- a/05.ipynb +++ b/05.ipynb @@ -4,38 +4,31 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "# Control Flow Statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Control Flow Statements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##If" + "## If-statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "The basic form of the if-statement is:\n", + "```\n", "if some_condition:\n", - " \n", - " algorithm" + " algorithm\n", + "```" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, + "execution_count": 2, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -48,35 +41,33 @@ "source": [ "x = 12\n", "if x >10:\n", - " print \"Hello\"" + " print(\"Hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##If-else" + "## If-else" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "You can also have an `else` block which is executed if the `if` statement is not true:\n", + "```python\n", "if some_condition:\n", - " \n", - " algorithm\n", - " \n", + " do something\n", "else:\n", - " \n", - " algorithm" + " do something else\n", + "```" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 3, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -89,24 +80,24 @@ "source": [ "x = 12\n", "if x > 10:\n", - " print \"hello\"\n", + " print(\"hello\")\n", "else:\n", - " print \"world\"" + " print(\"world\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##if-elif" + "## if-elif" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ + "You can have multiple condition using `elif`. They are tested in order, and the first which evaluates to true is executed. If none evaluate to true, then the optional `else` block is executed (if it exists):\n", + "```python\n", "if some_condition:\n", " \n", " algorithm\n", @@ -117,21 +108,20 @@ "\n", "else:\n", " \n", - " algorithm" + " algorithm\n", + "```" ] }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "x y:\n", - " print \"x>y\"\n", + " print(\"greater\")\n", "elif x < y:\n", - " print \"x y:\n", - " print \"x>y\"\n", + " print(\"greater\")\n", "elif x < y:\n", - " print \"x=7:\n", " break" ] @@ -390,7 +368,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Continue" + "## Continue" ] }, { @@ -402,10 +380,8 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -427,17 +403,17 @@ "source": [ "for i in range(10):\n", " if i>4:\n", - " print \"The end.\"\n", + " print(\"The end.\")\n", " continue\n", " elif i<7:\n", - " print i" + " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##List Comprehensions" + "## List Comprehensions" ] }, { @@ -449,17 +425,18 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, + "execution_count": 15, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n" - ] + "data": { + "text/plain": [ + "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -467,7 +444,7 @@ "for i in range(1,11):\n", " x = 27*i\n", " res.append(x)\n", - "print res" + "res" ] }, { @@ -479,10 +456,8 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { "data": { @@ -490,7 +465,7 @@ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, - "execution_count": 12, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -503,7 +478,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "That's it!. Only remember to enclose it in square brackets" + "That's it! Only remember to enclose it in square brackets" ] }, { @@ -515,10 +490,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { "data": { @@ -526,7 +499,7 @@ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, - "execution_count": 13, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -544,10 +517,8 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "data": { @@ -555,7 +526,7 @@ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, - "execution_count": 14, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -567,23 +538,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/06.ipynb b/06.ipynb index a642528..6efc7a0 100644 --- a/06.ipynb +++ b/06.ipynb @@ -4,14 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#Functions" + "# Functions" ] }, { @@ -32,14 +25,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "```python\n", "def funcname(arg1, arg2,... argN):\n", - " \n", - " ''' Document String'''\n", + " '''docstring'''\n", "\n", " statements\n", "\n", - "\n", - " return " + " return \n", + "````" ] }, { @@ -52,9 +45,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -66,8 +57,8 @@ } ], "source": [ - "print \"Hey Rajath!\"\n", - "print \"Rajath, How do you do?\"" + "print(\"Hey Rajath!\")\n", + "print(\"Rajath, How do you do?\")" ] }, { @@ -82,22 +73,18 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def firstfunc():\n", - " print \"Hey Rajath!\"\n", - " print \"Rajath, How do you do?\" " + " print(\"Hey Rajath!\")\n", + " print(\"Rajath, How do you do?\") " ] }, { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -116,28 +103,24 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**firstfunc()** every time just prints the message to a single person. We can make our function **firstfunc()** to accept arguements which will store the name and then prints respective to that accepted name. To do so, add a argument within the function as shown." + "`firstfunc()` every time just prints the message to a single person. We can make our function `firstfunc()` to accept arguements which will store the name and then prints respective to that accepted name. To do so, add a argument within the function as shown." ] }, { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def firstfunc(username):\n", - " print \"Hey\", username + '!'\n", - " print username + ',' ,\"How do you do?\"" + " print(\"Hey\", username + '!')\n", + " print(username + ',' ,\"How do you do?\")" ] }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -148,22 +131,20 @@ } ], "source": [ - "name1 = raw_input('Please enter your name : ')" + "name1 = input('Please enter your name : ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The name \"Guido\" is actually stored in name1. So we pass this variable to the function **firstfunc()** as the variable username because that is the variable that is defined for this function. i.e name1 is passed as username." + "The name you enter is actually stored in `name1`. So we pass this variable to the function `firstfunc()` as the variable username because that is the variable that is defined for this function. i.e name1 is passed as username." ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -182,39 +163,36 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Let us simplify this even further by defining another function **secondfunc()** which accepts the name and stores it inside a variable and then calls the **firstfunc()** from inside the function itself." + "Let us simplify this even further by defining another function `secondfunc()` which accepts the name and stores it inside a variable and then calls the `firstfunc()` from inside the function itself." ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 8, + "metadata": {}, "outputs": [], "source": [ "def firstfunc(username):\n", - " print \"Hey\", username + '!'\n", - " print username + ',' ,\"How do you do?\"\n", + " print(\"Hey\", username + '!')\n", + " print(username + ',' ,\"How do you do?\")\n", + " \n", "def secondfunc():\n", - " name = raw_input(\"Please enter your name : \")\n", + " name = input(\"Please enter your name : \")\n", " firstfunc(name)" ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 9, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Please enter your name : karthik\n", - "Hey karthik!\n", - "karthik, How do you do?\n" + "Please enter your name : Jones\n", + "Hey Jones!\n", + "Jones, How do you do?\n" ] } ], @@ -226,7 +204,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Return Statement" + "## Return Statement" ] }, { @@ -238,10 +216,8 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": true - }, + "execution_count": 10, + "metadata": {}, "outputs": [], "source": [ "def times(x,y):\n", @@ -253,34 +229,27 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The above defined **times( )** function accepts two arguements and return the variable z which contains the result of the product of the two arguements" + "The above defined `times( )` function accepts two arguements and return the variable z which contains the result of the product of the two arguements" ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "20\n" - ] + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "c = times(4,5)\n", - "print c" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The z value is stored in variable c and can be used for further operations." + "times(4,5)" ] }, { @@ -292,10 +261,8 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [], "source": [ "def times(x,y):\n", @@ -305,37 +272,35 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 15, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "20\n" - ] + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "c = times(4,5)\n", - "print c" + "times(4,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Since the **times( )** is now defined, we can document it as shown above. This document is returned whenever **times( )** function is called under **help( )** function." + "Since the `times( )` is now defined, we can document it as shown above. This document is returned whenever `times( )` function is called under `help( )` function." ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -362,10 +327,8 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": true - }, + "execution_count": 17, + "metadata": {}, "outputs": [], "source": [ "eglist = [10,50,30,12,6,8,100]" @@ -373,10 +336,8 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": true - }, + "execution_count": 18, + "metadata": {}, "outputs": [], "source": [ "def egfunc(eglist):\n", @@ -396,10 +357,8 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "data": { @@ -407,7 +366,7 @@ "(100, 6, 10, 100)" ] }, - "execution_count": 16, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -418,10 +377,8 @@ }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -436,32 +393,30 @@ ], "source": [ "a,b,c,d = egfunc(eglist)\n", - "print ' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d" + "print(' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Implicit arguments" + "## Default arguments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "When an argument of a function is common in majority of the cases or it is \"implicit\" this concept is used." + "You can specify default values for the right-most arguments in a function:" ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": true - }, + "execution_count": 21, + "metadata": {}, "outputs": [], "source": [ - "def implicitadd(x,y=3):\n", + "def implicitadd(x, y=3):\n", " return x+y" ] }, @@ -469,22 +424,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**implicitadd( )** is a function accepts two arguments but most of the times the first argument needs to be added just by 3. Hence the second argument is assigned the value 3. Here the second argument is implicit." + "`implicitadd( )` is a function accepts two arguments but most of the times the first argument needs to be added just by 3. Hence the second argument is assigned the value 3. Here the second argument is implicit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Now if the second argument is not defined when calling the **implicitadd( )** function then it considered as 3." + "Now if the second argument is not defined when calling the `implicitadd( )` function then it considered as 3." ] }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, + "execution_count": 22, + "metadata": {}, "outputs": [ { "data": { @@ -492,7 +445,7 @@ "7" ] }, - "execution_count": 19, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -510,10 +463,8 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 23, + "metadata": {}, "outputs": [ { "data": { @@ -521,7 +472,7 @@ "8" ] }, - "execution_count": 20, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -534,7 +485,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Any number of arguments" + "## Any number of arguments" ] }, { @@ -546,10 +497,8 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": true - }, + "execution_count": 25, + "metadata": {}, "outputs": [], "source": [ "def add_n(*args):\n", @@ -557,7 +506,7 @@ " reslist = []\n", " for i in args:\n", " reslist.append(i)\n", - " print reslist\n", + " print(reslist)\n", " return sum(reslist)" ] }, @@ -570,10 +519,8 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, + "execution_count": 26, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -588,7 +535,7 @@ "15" ] }, - "execution_count": 22, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -599,10 +546,8 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, + "execution_count": 27, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -617,7 +562,7 @@ "6" ] }, - "execution_count": 23, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -630,245 +575,114 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Global and Local Variables" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Whatever variable is declared inside a function is local variable and outside the function in global variable." - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "eg1 = [1,2,3,4,5]" + "## Lambda Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "In the below function we are appending a element to the declared list inside the function. eg2 variable declared inside the function is a local variable." - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def egfunc1():\n", - " def thirdfunc(arg1):\n", - " eg2 = arg1[:]\n", - " eg2.append(6)\n", - " print \"This is happening inside the function :\", eg2 \n", - " print \"This is happening before the function is called : \", eg1\n", - " thirdfunc(eg1)\n", - " print \"This is happening outside the function :\", eg1 \n", - " print \"Accessing a variable declared inside the function from outside :\" , eg2" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This is happening before the function is called : [1, 2, 3, 4, 5]\n", - "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", - "This is happening outside the function : [1, 2, 3, 4, 5]\n", - "Accessing a variable declared inside the function from outside :" - ] - }, - { - "ename": "NameError", - "evalue": "global name 'eg2' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0megfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36megfunc1\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mthirdfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"This is happening outside the function :\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"Accessing a variable declared inside the function from outside :\"\u001b[0m \u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: global name 'eg2' is not defined" - ] - } - ], - "source": [ - "egfunc1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If a **global** variable is defined as shown in the example below then that variable can be called from anywhere." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "eg3 = [1,2,3,4,5]" + "These are small functions which are not defined with any name and carry a single expression whose result is returned. Lambda functions comes very handy when operating with lists. These function are defined by the keyword `lambda` followed by the variables, a colon and the respective expression." ] }, { "cell_type": "code", "execution_count": 28, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "def egfunc1():\n", - " def thirdfunc(arg1):\n", - " global eg2\n", - " eg2 = arg1[:]\n", - " eg2.append(6)\n", - " print \"This is happening inside the function :\", eg2 \n", - " print \"This is happening before the function is called : \", eg1\n", - " thirdfunc(eg1)\n", - " print \"This is happening outside the function :\", eg1 \n", - " print \"Accessing a variable declared inside the function from outside :\" , eg2" + "z = lambda x: x * x" ] }, { "cell_type": "code", "execution_count": 29, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "This is happening before the function is called : [1, 2, 3, 4, 5]\n", - "This is happening inside the function : [1, 2, 3, 4, 5, 6]\n", - "This is happening outside the function : [1, 2, 3, 4, 5]\n", - "Accessing a variable declared inside the function from outside : [1, 2, 3, 4, 5, 6]\n" - ] + "data": { + "text/plain": [ + "64" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "egfunc1()" + "z(8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Lambda Functions" + "### map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "These are small functions which are not defined with any name and carry a single expression whose result is returned. Lambda functions comes very handy when operating with lists. These function are defined by the keyword **lambda** followed by the variables, a colon and the respective expression." + "`map( )` function basically executes the function that is defined to each of the list's element separately." ] }, { "cell_type": "code", "execution_count": 30, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "z = lambda x: x * x" + "list1 = [1,2,3,4,5,6,7,8,9]" ] }, { "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, + "execution_count": 32, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "64" + "" ] }, - "execution_count": 31, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "z(8)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###map" + "m = map(lambda x:x+2, list1)\n", + "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**map( )** function basically executes the function that is defined to each of the list's element separately." - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "list1 = [1,2,3,4,5,6,7,8,9]" + "As we see above, `map` produces a \"map\" which lazily evaluates its values. As with `range()`, we need to force the evalation of the `map` in order to see the values. One way to do that is by passing the `map` to the list constructor:" ] }, { "cell_type": "code", "execution_count": 33, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[3, 4, 5, 6, 7, 8, 9, 10, 11]\n" - ] + "data": { + "text/plain": [ + "[3, 4, 5, 6, 7, 8, 9, 10, 11]" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "eg = map(lambda x:x+2, list1)\n", - "print eg" + "list(m)" ] }, { @@ -881,9 +695,7 @@ { "cell_type": "code", "execution_count": 34, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "list2 = [9,8,7,6,5,4,3,2,1]" @@ -891,73 +703,71 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, + "execution_count": 37, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 10, 10, 10, 10, 10, 10, 10, 10]\n" - ] + "data": { + "text/plain": [ + "[10, 10, 10, 10, 10, 10, 10, 10, 10]" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "eg2 = map(lambda x,y:x+y, list1,list2)\n", - "print eg2" + "eg2 = list(map(lambda x,y:x+y, list1,list2))\n", + "eg2" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ "Not only lambda function but also other built in functions can also be used." ] }, { "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, + "execution_count": 38, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "['10', '10', '10', '10', '10', '10', '10', '10', '10']\n" - ] + "data": { + "text/plain": [ + "['10', '10', '10', '10', '10', '10', '10', '10', '10']" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "eg3 = map(str,eg2)\n", - "print eg3" + "eg3 = list(map(str,eg2))\n", + "eg3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "###filter" + "### filter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**filter( )** function is used to filter out the values in a list. Note that **filter()** function returns the result in a new list." + "`filter( )` function is used to filter out the values in a list. Note that `filter()` function returns the result in a new list." ] }, { "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": true - }, + "execution_count": 39, + "metadata": {}, "outputs": [], "source": [ "list1 = [1,2,3,4,5,6,7,8,9]" @@ -972,10 +782,36 @@ }, { "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = filter(lambda x:x<5,list1)\n", + "f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Like `map`, `filter` is also lazy:" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, "outputs": [ { "data": { @@ -983,28 +819,26 @@ "[1, 2, 3, 4]" ] }, - "execution_count": 38, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "filter(lambda x:x<5,list1)" + "list(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Notice what happens when **map()** is used." + "Notice what happens when `map()` is used." ] }, { "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, + "execution_count": 43, + "metadata": {}, "outputs": [ { "data": { @@ -1012,28 +846,26 @@ "[True, True, True, True, False, False, False, False, False]" ] }, - "execution_count": 39, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "map(lambda x:x<5, list1)" + "list(map(lambda x:x<5, list1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "We can conclude that, whatever is returned true in **map( )** function that particular element is returned when **filter( )** function is used." + "We can conclude that, whatever is returned true in `map( )` function that particular element is returned when `filter( )` function is used." ] }, { "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, + "execution_count": 46, + "metadata": {}, "outputs": [ { "data": { @@ -1041,35 +873,42 @@ "[4, 8]" ] }, - "execution_count": 40, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "filter(lambda x:x%4==0,list1)" + "list(filter(lambda x:x%4==0,list1))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/07.ipynb b/07.ipynb index f233280..4d004e8 100644 --- a/07.ipynb +++ b/07.ipynb @@ -4,45 +4,39 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" + "# Classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#Classes" + "Everything in Python is an object, and every object has a type. We can create our own types in Python by defining classes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Variables, Lists, Dictionaries etc in python is a object. Without getting into the theory part of Object Oriented Programming, explanation of the concepts will be done along this tutorial." + "A class is declared as follows:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "A class is declared as follows" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "class class_name:\n", + "```python\n", + "class ClassName:\n", + " class body\n", + "```\n", "\n", - " Functions" + "Let's define the simplest class we can:" ] }, { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "class FirstClass:\n", @@ -53,41 +47,44 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**pass** in python means do nothing. " + "`pass` in python means do nothing. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Above, a class object named \"FirstClass\" is declared now consider a \"egclass\" which has all the characteristics of \"FirstClass\". So all you have to do is, equate the \"egclass\" to \"FirstClass\". In python jargon this is called as creating an instance. \"egclass\" is the instance of \"FirstClass\"" + "We can create an instance of our class by calling it:" ] }, { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "egclass = FirstClass()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can see that our new object is of the right type:" + ] + }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "instance" + "__main__.FirstClass" ] }, - "execution_count": 3, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -96,20 +93,25 @@ "type(egclass)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Interestingly, we can also see that our class has a type. This hints at the nature of Python where everything - even a class - is an object:" + ] + }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "classobj" + "type" ] }, - "execution_count": 4, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -122,59 +124,22 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now let us add some \"functionality\" to the class. So that our \"FirstClass\" is defined in a better way. A function inside a class is called as a \"Method\" of that class" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Most of the classes will have a function named \"\\_\\_init\\_\\_\". These are called as magic methods. In this method you basically initialize the variables of that class or any other initial algorithms which is applicable to all methods is specified in this method. A variable inside a class is called an attribute." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "These helps simplify the process of initializing a instance. For example,\n", + "## __init__\n", "\n", - "Without the use of magic method or \\_\\_init\\_\\_ which is otherwise called as constructors. One had to define a **init( )** method and call the **init( )** function." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "eg0 = FirstClass()\n", - "eg0.init()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "But when the constructor is defined the \\_\\_init\\_\\_ is called thus intializing the instance created. " + "Many classes need some sort of initialization. We do this with the `__init__` method. This is called when an new instance of our class is created, before a reference to that object is made available to the caller." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "We will make our \"FirstClass\" to accept two variables name and symbol.\n", - "\n", - "I will be explaining about the \"self\" in a while." + "We'll give our `FirstClass` class a `__init__` that accepts two arguments`name` and `symbol`. We'll store those arguments on the instance:" ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, + "execution_count": 7, + "metadata": {}, "outputs": [], "source": [ "class FirstClass:\n", @@ -187,27 +152,32 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now that we have defined a function and added the \\_\\_init\\_\\_ method. We can create a instance of FirstClass which now accepts two arguments. " + "Now that we have defined a function and added the `__init__` method. We can create a instance of `FirstClass` which now accepts two arguments. " ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, + "execution_count": 9, + "metadata": {}, "outputs": [], "source": [ "eg1 = FirstClass('one',1)\n", "eg2 = FirstClass('two',2)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `self`\n", + "\n", + "You probably noticed the `self` argument in our `__init__`. This argument is automatically assigned to the instance on which the method is being executed. Python does this for all instance methods, not just `__init__`." + ] + }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 10, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -219,31 +189,54 @@ } ], "source": [ - "print eg1.name, eg1.symbol\n", - "print eg2.name, eg2.symbol" + "print(eg1.name, eg1.symbol)\n", + "print(eg2.name, eg2.symbol)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**dir( )** function comes very handy in looking into what the class contains and what all method it offers" + "`dir( )` function comes very handy in looking into what the class contains and what all method it offers" ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 11, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['__doc__', '__init__', '__module__']" + "['__class__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '__weakref__']" ] }, - "execution_count": 9, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -256,23 +249,48 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**dir( )** of an instance also shows it's defined attributes." + "`dir( )` of an instance also shows it's defined attributes." ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['__doc__', '__init__', '__module__', 'name', 'symbol']" + "['__class__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '__weakref__',\n", + " 'name',\n", + " 'symbol']" ] }, - "execution_count": 10, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -285,905 +303,234 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Changing the FirstClass function a bit," + "## Methods\n", + "\n", + "Methods are defined for classes using `def` inside the class body. `__init__` that we looked at above is an example of a method. Let us add some more methods to `FirstClass`:" ] }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 15, + "metadata": {}, "outputs": [], "source": [ "class FirstClass:\n", " def __init__(self,name,symbol):\n", - " self.n = name\n", - " self.s = symbol" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Changing self.name and self.symbol to self.n and self.s respectively will yield," + " self.name = name\n", + " self.symbol = symbol\n", + " \n", + " def square(self):\n", + " return self.symbol * self.symbol\n", + " \n", + " def cube(self):\n", + " return self.symbol * self.symbol * self.symbol\n", + " \n", + " def multiply(self, x):\n", + " return self.symbol * x" ] }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": true - }, + "execution_count": 16, + "metadata": {}, "outputs": [], "source": [ - "eg1 = FirstClass('one',1)\n", - "eg2 = FirstClass('two',2)" + "eg4 = FirstClass('Five',5)" ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { - "ename": "AttributeError", - "evalue": "FirstClass instance has no attribute 'name'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mAttributeError\u001b[0m: FirstClass instance has no attribute 'name'" + "name": "stdout", + "output_type": "stream", + "text": [ + "25\n", + "125\n" ] } ], "source": [ - "print eg1.name, eg1.symbol\n", - "print eg2.name, eg2.symbol" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "AttributeError, Remember variables are nothing but attributes inside a class? So this means we have not given the correct attribute for the instance." + "print(eg4.square())\n", + "print(eg4.cube())" ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['__doc__', '__init__', '__module__', 'n', 's']" + "10" ] }, - "execution_count": 14, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "dir(eg1)" + "eg4.multiply(2)" ] }, { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "one 1\n", - "two 2\n" - ] - } - ], + "cell_type": "markdown", + "metadata": {}, "source": [ - "print eg1.n, eg1.s\n", - "print eg2.n, eg2.s" + "## Inheritance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "So now we have solved the error. Now let us compare the two examples that we saw.\n", - "\n", - "When I declared self.name and self.symbol, there was no attribute error for eg1.name and eg1.symbol and when I declared self.n and self.s, there was no attribute error for eg1.n and eg1.s\n", - "\n", - "From the above we can conclude that self is nothing but the instance itself.\n", - "\n", - "Remember, self is not predefined it is userdefined. You can make use of anything you are comfortable with. But it has become a common practice to use self." + "In many cases you'll have several classes that share common elements. For example, you might be modeling animals on a farm. All of our animals will have a name and number of legs, so we'll create a base class (i.e. a class we plan to inherit from) called `Animal`:" ] }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": true - }, + "execution_count": 30, + "metadata": {}, "outputs": [], "source": [ - "class FirstClass:\n", - " def __init__(asdf1234,name,symbol):\n", - " asdf1234.n = name\n", - " asdf1234.s = symbol" + "class Animal:\n", + " def __init__(self, name, legs):\n", + " self.name = name\n", + " self.legs = legs" ] }, { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": true - }, - "outputs": [], + "cell_type": "markdown", + "metadata": {}, "source": [ - "eg1 = FirstClass('one',1)\n", - "eg2 = FirstClass('two',2)" + "Now we'll create a few *subclasses* of `Animal`. These subclasses will specify how many legs the animal has while leaving the actual name for individual instances. Let's start with llamas:" ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "one 1\n", - "two 2\n" - ] - } - ], + "execution_count": 32, + "metadata": {}, + "outputs": [], "source": [ - "print eg1.n, eg1.s\n", - "print eg2.n, eg2.s" + "class Llama(Animal):\n", + " def __init__(self, name, color):\n", + " super().__init__(name, 4)\n", + " self.color = color" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Since eg1 and eg2 are instances of FirstClass it need not necessarily be limited to FirstClass itself. It might extend itself by declaring other attributes without having the attribute to be declared inside the FirstClass." - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "eg1.cube = 1\n", - "eg2.cube = 8" + "The `__init__` for `Llama` accepts a name and color. It passes the name and the number 4 (that's how many legs our llamas have) to the base class `__init__`, i.e. `Animal.__init__`.\n", + "\n", + "We can construct a `Llama` and see how it works:" ] }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 35, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['__doc__', '__init__', '__module__', 'cube', 'n', 's']" + "'The llama named Llarry has 4 legs and off-white wool'" ] }, - "execution_count": 20, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "dir(eg1)" + "llarry = Llama('Llarry', 'off-white')\n", + "\n", + "'The llama named {} has {} legs and {} wool'.format(\n", + " llarry.name, llarry.legs, llarry.color)" ] }, { "cell_type": "markdown", - "metadata": { - "collapsed": true - }, + "metadata": {}, "source": [ - "Just like global and local variables as we saw earlier, even classes have it's own types of variables.\n", - "\n", - "Class Attribute : attributes defined outside the method and is applicable to all the instances.\n", + "### `super`\n", "\n", - "Instance Attribute : attributes defined inside a method and is applicable to only that method and is unique to each instance." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class FirstClass:\n", - " test = 'test'\n", - " def __init__(self,name,symbol):\n", - " self.name = name\n", - " self.symbol = symbol" + "The call to `super` in `Llama.__init__` essentially means \"call `__init__` on the base class\". `super()` is actually slightly more sophisticatd than that, but that's a sufficient explanation for our purposes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Here test is a class attribute and name is a instance attribute." + "### Another subclass\n", + "\n", + "We can create another `Animal` subclass to fill our farm and show how subclasses can centralize structure and behavior. Here's how our `Chicken` class looks:" ] }, { "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": true - }, + "execution_count": 36, + "metadata": {}, "outputs": [], "source": [ - "eg3 = FirstClass('Three',3)" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "test Three\n" - ] - } - ], - "source": [ - "print eg3.test, eg3.name" + "class Chicken(Animal):\n", + " def __init__(self, name):\n", + " super().__init__(name, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Let us add some more methods to FirstClass." - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "class FirstClass:\n", - " def __init__(self,name,symbol):\n", - " self.name = name\n", - " self.symbol = symbol\n", - " def square(self):\n", - " return self.symbol * self.symbol\n", - " def cube(self):\n", - " return self.symbol * self.symbol * self.symbol\n", - " def multiply(self, x):\n", - " return self.symbol * x" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "eg4 = FirstClass('Five',5)" + "Chickens are simpler than Llamas in that they don't add any new attributes. But they follow the same pattern as Llamas by passing a user-provided name argument to the base-class initializer along with a hard-coded 2 legs. Let's see our chickens in action:" ] }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "25\n", - "125\n" - ] - } - ], - "source": [ - "print eg4.square()\n", - "print eg4.cube()" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "eg4.multiply(2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The above can also be written as," - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, + "execution_count": 37, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "10" + "'The chicken named Marco Pollo has 2 legs'" ] }, - "execution_count": 28, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "FirstClass.multiply(eg4,2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Inheritance" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "There might be cases where a new class would have all the previous characteristics of an already defined class. So the new class can \"inherit\" the previous class and add it's own methods to it. This is called as inheritance." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Consider class SoftwareEngineer which has a method salary." - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "class SoftwareEngineer:\n", - " def __init__(self,name,age):\n", - " self.name = name\n", - " self.age = age\n", - " def salary(self, value):\n", - " self.money = value\n", - " print self.name,\"earns\",self.money" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a = SoftwareEngineer('Kartik',26)" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Kartik earns 40000\n" - ] - } - ], - "source": [ - "a.salary(40000)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__', '__init__', '__module__', 'salary']" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(SoftwareEngineer)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now consider another class Artist which tells us about the amount of money an artist earns and his artform." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class Artist:\n", - " def __init__(self,name,age):\n", - " self.name = name\n", - " self.age = age\n", - " def money(self,value):\n", - " self.money = value\n", - " print self.name,\"earns\",self.money\n", - " def artform(self, job):\n", - " self.job = job\n", - " print self.name,\"is a\", self.job" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "b = Artist('Nitin',20)" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Nitin earns 50000\n", - "Nitin is a Musician\n" - ] - } - ], - "source": [ - "b.money(50000)\n", - "b.artform('Musician')" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__', '__init__', '__module__', 'artform', 'money']" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(Artist)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "money method and salary method are the same. So we can generalize the method to salary and inherit the SoftwareEngineer class to Artist class. Now the artist class becomes," - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class Artist(SoftwareEngineer):\n", - " def artform(self, job):\n", - " self.job = job\n", - " print self.name,\"is a\", self.job" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "c = Artist('Nishanth',21)" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__', '__init__', '__module__', 'artform', 'salary']" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(Artist)" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Nishanth earns 60000\n", - "Nishanth is a Dancer\n" - ] - } - ], - "source": [ - "c.salary(60000)\n", - "c.artform('Dancer')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Suppose say while inheriting a particular method is not suitable for the new class. One can override this method by defining again that method with the same name inside the new class." - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "class Artist(SoftwareEngineer):\n", - " def artform(self, job):\n", - " self.job = job\n", - " print self.name,\"is a\", self.job\n", - " def salary(self, value):\n", - " self.money = value\n", - " print self.name,\"earns\",self.money\n", - " print \"I am overriding the SoftwareEngineer class's salary method\"" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "c = Artist('Nishanth',21)" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Nishanth earns 60000\n", - "I am overriding the SoftwareEngineer class's salary method\n", - "Nishanth is a Dancer\n" - ] - } - ], - "source": [ - "c.salary(60000)\n", - "c.artform('Dancer')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If not sure how many times methods will be called it will become difficult to declare so many variables to carry each result hence it is better to declare a list and append the result." - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class emptylist:\n", - " def __init__(self):\n", - " self.data = []\n", - " def one(self,x):\n", - " self.data.append(x)\n", - " def two(self, x ):\n", - " self.data.append(x**2)\n", - " def three(self, x):\n", - " self.data.append(x**3)" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "xc = emptylist()" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1]\n" - ] - } - ], - "source": [ - "xc.one(1)\n", - "print xc.data" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Since xc.data is a list direct list operations can also be performed." - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 8]\n" - ] - } - ], - "source": [ - "xc.data.append(8)\n", - "print xc.data" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 8, 9]\n" - ] - } - ], - "source": [ - "xc.two(3)\n", - "print xc.data" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If the number of input arguments varies from instance to instance asterisk can be used as shown." - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class NotSure:\n", - " def __init__(self, *args):\n", - " self.data = ''.join(list(args)) " - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "yz = NotSure('I', 'Do' , 'Not', 'Know', 'What', 'To','Type')" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'IDoNotKnowWhatToType'" - ] - }, - "execution_count": 51, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "yz.data" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#Where to go from here?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Practice alone can help you get the hang of python. Give your self problem statements and solve them. You can also sign up to any competitive coding platform for problem statements. The more you code the more you discover and the more you start appreciating the language.\n", - "\n", - "\n", - "Now that you have been introduced to python, You can try out the different python libraries in the field of your interest. I highly recommend you to check out this curated list of Python frameworks, libraries and software http://awesome-python.com\n", - "\n", - "\n", - "The official python documentation : https://docs.python.org/2/\n", - "\n", - "\n", - "You can also check out Python practice programs written by my friend, Kartik Kannapur. Github Repo : https://github.com/rajathkumarmp/Python-Lectures \n", - "\n", - "\n", - "Enjoy solving problem statements because life is short, you need python!\n", - "\n", - "\n", - "Peace.\n", - "\n", - "\n", - "Rajath Kumar M.P ( rajathkumar dot exe at gmail dot com)" + "c = Chicken('Marco Pollo')\n", + "'The chicken named {} has {} legs'.format(c.name, c.legs)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.6.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Python.pdf b/Python.pdf deleted file mode 100644 index f6b047e..0000000 Binary files a/Python.pdf and /dev/null differ diff --git a/README.md b/README.md index 169e342..0bb8a3c 100644 --- a/README.md +++ b/README.md @@ -1,92 +1 @@ - -# Python-Lectures - - - - -Note: [Andreas Ernst](http://users.monash.edu/~andrease/) has improvised and updated the repo to python 3, [Link](https://gitlab.erc.monash.edu.au/andrease/Python4Maths/tree/master) - -## Introduction - -Python is a modern, robust, high level programming language. It is very easy to pick up even if you are completely new to programming. - -## Installation - -Mac OS X and Linux comes pre installed with python. Windows users can download python from https://www.python.org/downloads/ . - -To install IPython run, - - $ pip install ipython[all] - -This will install all the necessary dependencies for the notebook, qtconsole, tests etc. - -### Installation from unofficial distributions - -Installing all the necessary libraries might prove troublesome. Anaconda and Canopy comes pre packaged with all the necessary python libraries and also IPython. - -#### Anaconda - -Download Anaconda from https://www.continuum.io/downloads - -Anaconda is completely free and includes more than 300 python packages. Both python 2.7 and 3.4 options are available. - -#### Canopy - -Download Canopy from https://store.enthought.com/downloads/#default - -Canopy has a premium version which offers 300+ python packages. But the free version works just fine. Canopy as of now supports only 2.7 but it comes with its own text editor and IPython environment. - -## Launching IPython Notebook - -From the terminal - - ipython notebook - -In Canopy and Anaconda, Open the respective terminals and execute the above. - -## How to learn from this resource? - -You can download the pdf copy from here : [Get Started with Python](https://github.com/rajathkumarmp/Python-Lectures/blob/master/Python.pdf) - -It is better to download all the ipython notebooks from this repository https://github.com/rajathkumarmp/Python-Lectures and learn it on the notebook itself rather than having to refer to a pdf. - -Launch ipython notebook from the folder which contains the notebooks. Open each one of them - - Cell > All Output > Clear - -This will clear all the outputs and now you can understand each statement and learn interactively. - -## Table of contents - - - -[00 - Introduction and Installation](http://nbviewer.ipython.org/github/rajathkumarmp/Python-Lectures/blob/master/00.ipynb) - - -[01 - Variable, Operators and Built-in Functions](http://nbviewer.ipython.org/github/rajathkumarmp/Python-Lectures/blob/master/01.ipynb) - - -[02 - Print Statement, Precision and FieldWidth](http://nbviewer.ipython.org/github/rajathkumarmp/Python-Lectures/blob/master/02.ipynb) - - -[03 - Lists, Tuples and Sets](http://nbviewer.ipython.org/github/rajathkumarmp/Python-Lectures/blob/master/03.ipynb) - - -[04 - Strings and Dictionaries](http://nbviewer.ipython.org/github/rajathkumarmp/Python-Lectures/blob/master/04.ipynb) - - -[05 - Control Flow Statements](http://nbviewer.ipython.org/github/rajathkumarmp/Python-Lectures/blob/master/05.ipynb) - - -[06 - Functions](http://nbviewer.ipython.org/github/rajathkumarmp/Python-Lectures/blob/master/06.ipynb) - - -[07 - Classes](http://nbviewer.ipython.org/github/rajathkumarmp/Python-Lectures/blob/master/07.ipynb) - - - -These are online read-only versions. - -## License - -This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ +Jupyter notebooks covering the very basics of Python 3. diff --git a/tex/.texpadtmp/00.aux b/tex/.texpadtmp/00.aux deleted file mode 100644 index 2aff996..0000000 --- a/tex/.texpadtmp/00.aux +++ /dev/null @@ -1,147 +0,0 @@ -\relax -\providecommand\hyper@newdestlabel[2]{} -\providecommand\HyperFirstAtBeginDocument{\AtBeginDocument} -\HyperFirstAtBeginDocument{\ifx\hyper@anchor\@undefined -\global\let\oldcontentsline\contentsline -\gdef\contentsline#1#2#3#4{\oldcontentsline{#1}{#2}{#3}} -\global\let\oldnewlabel\newlabel -\gdef\newlabel#1#2{\newlabelxx{#1}#2} -\gdef\newlabelxx#1#2#3#4#5#6{\oldnewlabel{#1}{{#2}{#3}}} -\AtEndDocument{\ifx\hyper@anchor\@undefined -\let\contentsline\oldcontentsline -\let\newlabel\oldnewlabel -\fi} -\fi} -\global\let\hyper@last\relax -\gdef\HyperFirstAtBeginDocument#1{#1} -\providecommand\HyField@AuxAddToFields[1]{} -\providecommand\HyField@AuxAddToCoFields[2]{} -\providecommand \oddpage@label [2]{} -\@writefile{toc}{\contentsline {section}{\numberline {1}Python}{3}{section.1}} -\newlabel{python}{{1}{3}{Python}{section.1}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {1.1}Introduction}{3}{subsection.1.1}} -\newlabel{introduction}{{1.1}{3}{Introduction}{subsection.1.1}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {1.2}Installation}{3}{subsection.1.2}} -\newlabel{installation}{{1.2}{3}{Installation}{subsection.1.2}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {1.2.1}Installation from unofficial distributions}{3}{subsubsection.1.2.1}} -\newlabel{installation-from-unofficial-distributions}{{1.2.1}{3}{Installation from unofficial distributions}{subsubsection.1.2.1}{}} -\newlabel{anaconda}{{1.2.1}{3}{Anaconda}{section*.2}{}} -\@writefile{toc}{\contentsline {paragraph}{Anaconda}{3}{section*.2}} -\newlabel{canopy}{{1.2.1}{3}{Canopy}{section*.3}{}} -\@writefile{toc}{\contentsline {paragraph}{Canopy}{3}{section*.3}} -\@writefile{toc}{\contentsline {subsection}{\numberline {1.3}Launching IPython Notebook}{3}{subsection.1.3}} -\newlabel{launching-ipython-notebook}{{1.3}{3}{Launching IPython Notebook}{subsection.1.3}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {1.4}How to learn from this resource?}{3}{subsection.1.4}} -\newlabel{how-to-learn-from-this-resource}{{1.4}{3}{How to learn from this resource?}{subsection.1.4}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {1.5}License}{3}{subsection.1.5}} -\newlabel{license}{{1.5}{3}{License}{subsection.1.5}{}} -\gdef \LT@i {\LT@entry - {1}{38.50012pt}\LT@entry - {1}{75.13902pt}} -\@writefile{toc}{\contentsline {section}{\numberline {2}The Zen Of Python}{4}{section.2}} -\newlabel{the-zen-of-python}{{2}{4}{The Zen Of Python}{section.2}{}} -\@writefile{toc}{\contentsline {section}{\numberline {3}Variables}{4}{section.3}} -\newlabel{variables}{{3}{4}{Variables}{section.3}{}} -\@writefile{toc}{\contentsline {section}{\numberline {4}Operators}{4}{section.4}} -\newlabel{operators}{{4}{4}{Operators}{section.4}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {4.1}Arithmetic Operators}{4}{subsection.4.1}} -\newlabel{arithmetic-operators}{{4.1}{4}{Arithmetic Operators}{subsection.4.1}{}} -\gdef \LT@ii {\LT@entry - {1}{38.50012pt}\LT@entry - {2}{110.80576pt}} -\@writefile{toc}{\contentsline {subsection}{\numberline {4.2}Relational Operators}{5}{subsection.4.2}} -\newlabel{relational-operators}{{4.2}{5}{Relational Operators}{subsection.4.2}{}} -\gdef \LT@iii {\LT@entry - {1}{38.50012pt}\LT@entry - {1}{75.13902pt}} -\@writefile{toc}{\contentsline {subsection}{\numberline {4.3}Bitwise Operators}{6}{subsection.4.3}} -\newlabel{bitwise-operators}{{4.3}{6}{Bitwise Operators}{subsection.4.3}{}} -\@writefile{toc}{\contentsline {section}{\numberline {5}Built-in Functions}{6}{section.5}} -\newlabel{built-in-functions}{{5}{6}{Built-in Functions}{section.5}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1}Conversion from one system to another}{6}{subsection.5.1}} -\newlabel{conversion-from-one-system-to-another}{{5.1}{6}{Conversion from one system to another}{subsection.5.1}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {5.2}Simplifying Arithmetic Operations}{7}{subsection.5.2}} -\newlabel{simplifying-arithmetic-operations}{{5.2}{7}{Simplifying Arithmetic Operations}{subsection.5.2}{}} -\gdef \LT@iv {\LT@entry - {2}{38.77788pt}\LT@entry - {1}{38.22234pt}} -\@writefile{toc}{\contentsline {subsection}{\numberline {5.3}Accepting User Inputs}{9}{subsection.5.3}} -\newlabel{accepting-user-inputs}{{5.3}{9}{Accepting User Inputs}{subsection.5.3}{}} -\@writefile{toc}{\contentsline {section}{\numberline {6}Print Statement}{10}{section.6}} -\newlabel{print-statement}{{6}{10}{Print Statement}{section.6}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {6.1}Other Examples}{11}{subsection.6.1}} -\newlabel{other-examples}{{6.1}{11}{Other Examples}{subsection.6.1}{}} -\@writefile{toc}{\contentsline {section}{\numberline {7}PrecisionWidth and FieldWidth}{12}{section.7}} -\newlabel{precisionwidth-and-fieldwidth}{{7}{12}{PrecisionWidth and FieldWidth}{section.7}{}} -\@writefile{toc}{\contentsline {section}{\numberline {8}Data Structures}{13}{section.8}} -\newlabel{data-structures}{{8}{13}{Data Structures}{section.8}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {8.1}Lists}{13}{subsection.8.1}} -\newlabel{lists}{{8.1}{13}{Lists}{subsection.8.1}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {8.1.1}Indexing}{13}{subsubsection.8.1.1}} -\newlabel{indexing}{{8.1.1}{13}{Indexing}{subsubsection.8.1.1}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {8.1.2}Slicing}{14}{subsubsection.8.1.2}} -\newlabel{slicing}{{8.1.2}{14}{Slicing}{subsubsection.8.1.2}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {8.1.3}Built in List Functions}{14}{subsubsection.8.1.3}} -\newlabel{built-in-list-functions}{{8.1.3}{14}{Built in List Functions}{subsubsection.8.1.3}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {8.1.4}Copying a list}{18}{subsubsection.8.1.4}} -\newlabel{copying-a-list}{{8.1.4}{18}{Copying a list}{subsubsection.8.1.4}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {8.2}Tuples}{19}{subsection.8.2}} -\newlabel{tuples}{{8.2}{19}{Tuples}{subsection.8.2}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {8.2.1}Mapping one tuple to another}{20}{subsubsection.8.2.1}} -\newlabel{mapping-one-tuple-to-another}{{8.2.1}{20}{Mapping one tuple to another}{subsubsection.8.2.1}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {8.2.2}Built In Tuple functions}{20}{subsubsection.8.2.2}} -\newlabel{built-in-tuple-functions}{{8.2.2}{20}{Built In Tuple functions}{subsubsection.8.2.2}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {8.3}Sets}{20}{subsection.8.3}} -\newlabel{sets}{{8.3}{20}{Sets}{subsection.8.3}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {8.3.1}Built-in Functions}{20}{subsubsection.8.3.1}} -\newlabel{built-in-functions}{{8.3.1}{20}{Built-in Functions}{subsubsection.8.3.1}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {8.4}Strings}{22}{subsection.8.4}} -\newlabel{strings}{{8.4}{22}{Strings}{subsection.8.4}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {8.4.1}Built-in Functions}{22}{subsubsection.8.4.1}} -\newlabel{built-in-functions}{{8.4.1}{22}{Built-in Functions}{subsubsection.8.4.1}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {8.5}Dictionaries}{26}{subsection.8.5}} -\newlabel{dictionaries}{{8.5}{26}{Dictionaries}{subsection.8.5}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {8.5.1}Built-in Functions}{26}{subsubsection.8.5.1}} -\newlabel{built-in-functions}{{8.5.1}{26}{Built-in Functions}{subsubsection.8.5.1}{}} -\@writefile{toc}{\contentsline {section}{\numberline {9}Control Flow Statements}{28}{section.9}} -\newlabel{control-flow-statements}{{9}{28}{Control Flow Statements}{section.9}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {9.1}If}{28}{subsection.9.1}} -\newlabel{if}{{9.1}{28}{If}{subsection.9.1}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {9.2}If-else}{28}{subsection.9.2}} -\newlabel{if-else}{{9.2}{28}{If-else}{subsection.9.2}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {9.3}if-elif}{28}{subsection.9.3}} -\newlabel{if-elif}{{9.3}{28}{if-elif}{subsection.9.3}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {9.4}Loops}{29}{subsection.9.4}} -\newlabel{loops}{{9.4}{29}{Loops}{subsection.9.4}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {9.4.1}For}{29}{subsubsection.9.4.1}} -\newlabel{for}{{9.4.1}{29}{For}{subsubsection.9.4.1}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {9.4.2}While}{30}{subsubsection.9.4.2}} -\newlabel{while}{{9.4.2}{30}{While}{subsubsection.9.4.2}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {9.5}Break}{30}{subsection.9.5}} -\newlabel{break}{{9.5}{30}{Break}{subsection.9.5}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {9.6}Continue}{30}{subsection.9.6}} -\newlabel{continue}{{9.6}{30}{Continue}{subsection.9.6}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {9.7}List Comprehensions}{31}{subsection.9.7}} -\newlabel{list-comprehensions}{{9.7}{31}{List Comprehensions}{subsection.9.7}{}} -\@writefile{toc}{\contentsline {section}{\numberline {10}Functions}{32}{section.10}} -\newlabel{functions}{{10}{32}{Functions}{section.10}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {10.1}Return Statement}{33}{subsection.10.1}} -\newlabel{return-statement}{{10.1}{33}{Return Statement}{subsection.10.1}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {10.2}Implicit arguments}{34}{subsection.10.2}} -\newlabel{implicit-arguments}{{10.2}{34}{Implicit arguments}{subsection.10.2}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {10.3}Any number of arguments}{34}{subsection.10.3}} -\newlabel{any-number-of-arguments}{{10.3}{34}{Any number of arguments}{subsection.10.3}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {10.4}Global and Local Variables}{35}{subsection.10.4}} -\newlabel{global-and-local-variables}{{10.4}{35}{Global and Local Variables}{subsection.10.4}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {10.5}Lambda Functions}{36}{subsection.10.5}} -\newlabel{lambda-functions}{{10.5}{36}{Lambda Functions}{subsection.10.5}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {10.5.1}map}{36}{subsubsection.10.5.1}} -\newlabel{map}{{10.5.1}{36}{map}{subsubsection.10.5.1}{}} -\@writefile{toc}{\contentsline {subsubsection}{\numberline {10.5.2}filter}{37}{subsubsection.10.5.2}} -\newlabel{filter}{{10.5.2}{37}{filter}{subsubsection.10.5.2}{}} -\@writefile{toc}{\contentsline {section}{\numberline {11}Classes}{38}{section.11}} -\newlabel{classes}{{11}{38}{Classes}{section.11}{}} -\@writefile{toc}{\contentsline {subsection}{\numberline {11.1}Inheritance}{41}{subsection.11.1}} -\newlabel{inheritance}{{11.1}{41}{Inheritance}{subsection.11.1}{}} -\@writefile{toc}{\contentsline {section}{\numberline {12}Where to go from here?}{43}{section.12}} -\newlabel{where-to-go-from-here}{{12}{43}{Where to go from here?}{section.12}{}} diff --git a/tex/.texpadtmp/00.log b/tex/.texpadtmp/00.log deleted file mode 100644 index b873918..0000000 --- a/tex/.texpadtmp/00.log +++ /dev/null @@ -1,2344 +0,0 @@ -This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014) (preloaded format=pdflatex 2014.5.25) 2 OCT 2015 16:03 -entering extended mode - restricted \write18 enabled. - file:line:error style messages enabled. - %&-line parsing enabled. -**00.tex -(./00.tex -LaTeX2e <2014/05/01> -Babel <3.9k> and hyphenation patterns for 78 languages loaded. -(/usr/local/texlive/2014/texmf-dist/tex/latex/base/article.cls -Document Class: article 2007/10/19 v1.4h Standard LaTeX document class -(/usr/local/texlive/2014/texmf-dist/tex/latex/base/size10.clo -File: size10.clo 2007/10/19 v1.4h Standard LaTeX file (size option) -) -\c@part=\count79 -\c@section=\count80 -\c@subsection=\count81 -\c@subsubsection=\count82 -\c@paragraph=\count83 -\c@subparagraph=\count84 -\c@figure=\count85 -\c@table=\count86 -\abovecaptionskip=\skip41 -\belowcaptionskip=\skip42 -\bibindent=\dimen102 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/graphics/graphicx.sty -Package: graphicx 2014/04/25 v1.0g Enhanced LaTeX Graphics (DPC,SPQR) - (/usr/local/texlive/2014/texmf-dist/tex/latex/graphics/keyval.sty -Package: keyval 2014/05/08 v1.15 key=value parser (DPC) -\KV@toks@=\toks14 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/graphics/graphics.sty -Package: graphics 2009/02/05 v1.0o Standard LaTeX Graphics (DPC,SPQR) - (/usr/local/texlive/2014/texmf-dist/tex/latex/graphics/trig.sty -Package: trig 1999/03/16 v1.09 sin cos tan (DPC) -) (/usr/local/texlive/2014/texmf-dist/tex/latex/latexconfig/graphics.cfg -File: graphics.cfg 2010/04/23 v1.9 graphics configuration of TeX Live -) -Package graphics Info: Driver file: pdftex.def on input line 91. - (/usr/local/texlive/2014/texmf-dist/tex/latex/pdftex-def/pdftex.def -File: pdftex.def 2011/05/27 v0.06d Graphics/color for pdfTeX - (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/infwarerr.sty -Package: infwarerr 2010/04/08 v1.3 Providing info/warning/error messages (HO) -) (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/ltxcmds.sty -Package: ltxcmds 2011/11/09 v1.22 LaTeX kernel commands for general use (HO) -) -\Gread@gobject=\count87 -)) -\Gin@req@height=\dimen103 -\Gin@req@width=\dimen104 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/adjustbox/adjustbox.sty -Package: adjustbox 2012/05/21 v1.0 Adjusting TeX boxes (trim, clip, ...) - (/usr/local/texlive/2014/texmf-dist/tex/latex/xkeyval/xkeyval.sty -Package: xkeyval 2014/05/09 v2.6d package option processing (HA) - (/usr/local/texlive/2014/texmf-dist/tex/generic/xkeyval/xkeyval.tex -\XKV@toks=\toks15 -\XKV@tempa@toks=\toks16 -\XKV@depth=\count88 -File: xkeyval.tex 2014/05/09 v2.6d key=value parser (HA) -)) (/usr/local/texlive/2014/texmf-dist/tex/latex/adjustbox/adjcalc.sty -Package: adjcalc 2012/05/16 v1.1 Provides advanced setlength with multiple back-ends (calc, etex, pgfmath) -) (/usr/local/texlive/2014/texmf-dist/tex/latex/adjustbox/trimclip.sty -Package: trimclip 2012/05/16 v1.0 Trim and clip general TeX material - (/usr/local/texlive/2014/texmf-dist/tex/latex/collectbox/collectbox.sty -Package: collectbox 2012/05/17 v0.4b Collect macro arguments as boxes -\collectedbox=\box26 -) -\tc@llx=\dimen105 -\tc@lly=\dimen106 -\tc@urx=\dimen107 -\tc@ury=\dimen108 -Package trimclip Info: Using driver 'tc-pdftex.def'. - (/usr/local/texlive/2014/texmf-dist/tex/latex/adjustbox/tc-pdftex.def -File: tc-pdftex.def 2012/05/13 v1.0 Clipping driver for pdftex -)) -\adjbox@Width=\dimen109 -\adjbox@Height=\dimen110 -\adjbox@Depth=\dimen111 -\adjbox@Totalheight=\dimen112 - (/usr/local/texlive/2014/texmf-dist/tex/latex/ifoddpage/ifoddpage.sty -Package: ifoddpage 2011/09/13 v1.0 Conditionals for odd/even page detection -\c@checkoddpage=\count89 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/varwidth/varwidth.sty -Package: varwidth 2009/03/30 ver 0.92; Variable-width minipages -\@vwid@box=\box27 -\sift@deathcycles=\count90 -\@vwid@loff=\dimen113 -\@vwid@roff=\dimen114 -)) (/usr/local/texlive/2014/texmf-dist/tex/latex/graphics/color.sty -Package: color 2014/04/23 v1.1a Standard LaTeX Color (DPC) - (/usr/local/texlive/2014/texmf-dist/tex/latex/latexconfig/color.cfg -File: color.cfg 2007/01/18 v1.5 color configuration of teTeX/TeXLive -) -Package color Info: Driver file: pdftex.def on input line 137. -) (/usr/local/texlive/2014/texmf-dist/tex/latex/tools/enumerate.sty -Package: enumerate 1999/03/05 v3.00 enumerate extensions (DPC) -\@enLab=\toks17 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/geometry/geometry.sty -Package: geometry 2010/09/12 v5.6 Page Geometry - (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/ifpdf.sty -Package: ifpdf 2011/01/30 v2.3 Provides the ifpdf switch (HO) -Package ifpdf Info: pdfTeX in PDF mode is detected. -) (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/ifvtex.sty -Package: ifvtex 2010/03/01 v1.5 Detect VTeX and its facilities (HO) -Package ifvtex Info: VTeX not detected. -) (/usr/local/texlive/2014/texmf-dist/tex/generic/ifxetex/ifxetex.sty -Package: ifxetex 2010/09/12 v0.6 Provides ifxetex conditional -) -\Gm@cnth=\count91 -\Gm@cntv=\count92 -\c@Gm@tempcnt=\count93 -\Gm@bindingoffset=\dimen115 -\Gm@wd@mp=\dimen116 -\Gm@odd@mp=\dimen117 -\Gm@even@mp=\dimen118 -\Gm@layoutwidth=\dimen119 -\Gm@layoutheight=\dimen120 -\Gm@layouthoffset=\dimen121 -\Gm@layoutvoffset=\dimen122 -\Gm@dimlist=\toks18 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/amsmath/amsmath.sty -Package: amsmath 2013/01/14 v2.14 AMS math features -\@mathmargin=\skip43 - -For additional information on amsmath, use the `?' option. -(/usr/local/texlive/2014/texmf-dist/tex/latex/amsmath/amstext.sty -Package: amstext 2000/06/29 v2.01 - (/usr/local/texlive/2014/texmf-dist/tex/latex/amsmath/amsgen.sty -File: amsgen.sty 1999/11/30 v2.0 -\@emptytoks=\toks19 -\ex@=\dimen123 -)) (/usr/local/texlive/2014/texmf-dist/tex/latex/amsmath/amsbsy.sty -Package: amsbsy 1999/11/29 v1.2d -\pmbraise@=\dimen124 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/amsmath/amsopn.sty -Package: amsopn 1999/12/14 v2.01 operator names -) -\inf@bad=\count94 -LaTeX Info: Redefining \frac on input line 210. -\uproot@=\count95 -\leftroot@=\count96 -LaTeX Info: Redefining \overline on input line 306. -\classnum@=\count97 -\DOTSCASE@=\count98 -LaTeX Info: Redefining \ldots on input line 378. -LaTeX Info: Redefining \dots on input line 381. -LaTeX Info: Redefining \cdots on input line 466. -\Mathstrutbox@=\box28 -\strutbox@=\box29 -\big@size=\dimen125 -LaTeX Font Info: Redeclaring font encoding OML on input line 566. -LaTeX Font Info: Redeclaring font encoding OMS on input line 567. -\macc@depth=\count99 -\c@MaxMatrixCols=\count100 -\dotsspace@=\muskip10 -\c@parentequation=\count101 -\dspbrk@lvl=\count102 -\tag@help=\toks20 -\row@=\count103 -\column@=\count104 -\maxfields@=\count105 -\andhelp@=\toks21 -\eqnshift@=\dimen126 -\alignsep@=\dimen127 -\tagshift@=\dimen128 -\tagwidth@=\dimen129 -\totwidth@=\dimen130 -\lineht@=\dimen131 -\@envbody=\toks22 -\multlinegap=\skip44 -\multlinetaggap=\skip45 -\mathdisplay@stack=\toks23 -LaTeX Info: Redefining \[ on input line 2665. -LaTeX Info: Redefining \] on input line 2666. -) (/usr/local/texlive/2014/texmf-dist/tex/latex/amsfonts/amssymb.sty -Package: amssymb 2013/01/14 v3.01 AMS font symbols - (/usr/local/texlive/2014/texmf-dist/tex/latex/amsfonts/amsfonts.sty -Package: amsfonts 2013/01/14 v3.01 Basic AMSFonts support -\symAMSa=\mathgroup4 -\symAMSb=\mathgroup5 -LaTeX Font Info: Overwriting math alphabet `\mathfrak' in version `bold' -(Font) U/euf/m/n --> U/euf/b/n on input line 106. -)) (/usr/local/texlive/2014/texmf-dist/tex/latex/eurosym/eurosym.sty -Package: eurosym 1998/08/06 v1.1 European currency symbol ``Euro'' -\@eurobox=\box30 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/ucs/ucs.sty -Package: ucs 2013/05/11 v2.2 UCS: Unicode input support - (/usr/local/texlive/2014/texmf-dist/tex/latex/ucs/data/uni-global.def -File: uni-global.def 2013/05/13 UCS: Unicode global data -) -\uc@secondtry=\count106 -\uc@combtoks=\toks24 -\uc@combtoksb=\toks25 -\uc@temptokena=\toks26 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/base/inputenc.sty -Package: inputenc 2014/04/30 v1.2b Input encoding file -\inpenc@prehook=\toks27 -\inpenc@posthook=\toks28 - (/usr/local/texlive/2014/texmf-dist/tex/latex/ucs/utf8x.def -File: utf8x.def 2004/10/17 UCS: Input encoding UTF-8 -)) (/usr/local/texlive/2014/texmf-dist/tex/latex/fancyvrb/fancyvrb.sty -Package: fancyvrb 2008/02/07 - Style option: `fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix <2008/02/07> (tvz) -\FV@CodeLineNo=\count107 -\FV@InFile=\read1 -\FV@TabBox=\box31 -\c@FancyVerbLine=\count108 -\FV@StepNumber=\count109 -\FV@OutFile=\write3 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/oberdiek/grffile.sty -Package: grffile 2012/04/05 v1.16 Extended file name support for graphics (HO) - (/usr/local/texlive/2014/texmf-dist/tex/latex/oberdiek/kvoptions.sty -Package: kvoptions 2011/06/30 v3.11 Key value format for package options (HO) - (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/kvsetkeys.sty -Package: kvsetkeys 2012/04/25 v1.16 Key value parser (HO) - (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/etexcmds.sty -Package: etexcmds 2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO) - (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/ifluatex.sty -Package: ifluatex 2010/03/01 v1.3 Provides the ifluatex switch (HO) -Package ifluatex Info: LuaTeX not detected. -) -Package etexcmds Info: Could not find \expanded. -(etexcmds) That can mean that you are not using pdfTeX 1.50 or -(etexcmds) that some package has redefined \expanded. -(etexcmds) In the latter case, load this package earlier. -))) (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/pdftexcmds.sty -Package: pdftexcmds 2011/11/29 v0.20 Utility functions of pdfTeX for LuaTeX (HO) -Package pdftexcmds Info: LuaTeX not detected. -Package pdftexcmds Info: \pdf@primitive is available. -Package pdftexcmds Info: \pdf@ifprimitive is available. -Package pdftexcmds Info: \pdfdraftmode found. -) -Package grffile Info: Option `multidot' is set to `true'. -Package grffile Info: Option `extendedchars' is set to `false'. -Package grffile Info: Option `space' is set to `true'. -Package grffile Info: \Gin@ii of package `graphicx' fixed on input line 486. -) (/usr/local/texlive/2014/texmf-dist/tex/latex/hyperref/hyperref.sty -Package: hyperref 2012/11/06 v6.83m Hypertext links for LaTeX - (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/hobsub-hyperref.sty -Package: hobsub-hyperref 2012/05/28 v1.13 Bundle oberdiek, subset hyperref (HO) - (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/hobsub-generic.sty -Package: hobsub-generic 2012/05/28 v1.13 Bundle oberdiek, subset generic (HO) -Package: hobsub 2012/05/28 v1.13 Construct package bundles (HO) -Package hobsub Info: Skipping package `infwarerr' (already loaded). -Package hobsub Info: Skipping package `ltxcmds' (already loaded). -Package hobsub Info: Skipping package `ifluatex' (already loaded). -Package hobsub Info: Skipping package `ifvtex' (already loaded). -Package: intcalc 2007/09/27 v1.1 Expandable calculations with integers (HO) -Package hobsub Info: Skipping package `ifpdf' (already loaded). -Package hobsub Info: Skipping package `etexcmds' (already loaded). -Package hobsub Info: Skipping package `kvsetkeys' (already loaded). -Package: kvdefinekeys 2011/04/07 v1.3 Define keys (HO) -Package hobsub Info: Skipping package `pdftexcmds' (already loaded). -Package: pdfescape 2011/11/25 v1.13 Implements pdfTeX's escape features (HO) -Package: bigintcalc 2012/04/08 v1.3 Expandable calculations on big integers (HO) -Package: bitset 2011/01/30 v1.1 Handle bit-vector datatype (HO) -Package: uniquecounter 2011/01/30 v1.2 Provide unlimited unique counter (HO) -) -Package hobsub Info: Skipping package `hobsub' (already loaded). -Package: letltxmacro 2010/09/02 v1.4 Let assignment for LaTeX macros (HO) -Package: hopatch 2012/05/28 v1.2 Wrapper for package hooks (HO) -Package: xcolor-patch 2011/01/30 xcolor patch -Package: atveryend 2011/06/30 v1.8 Hooks at the very end of document (HO) -Package atveryend Info: \enddocument detected (standard20110627). -Package: atbegshi 2011/10/05 v1.16 At begin shipout hook (HO) -Package: refcount 2011/10/16 v3.4 Data extraction from label references (HO) -Package: hycolor 2011/01/30 v1.7 Color options for hyperref/bookmark (HO) -) (/usr/local/texlive/2014/texmf-dist/tex/latex/oberdiek/auxhook.sty -Package: auxhook 2011/03/04 v1.3 Hooks for auxiliary files (HO) -) -\@linkdim=\dimen132 -\Hy@linkcounter=\count110 -\Hy@pagecounter=\count111 - (/usr/local/texlive/2014/texmf-dist/tex/latex/hyperref/pd1enc.def -File: pd1enc.def 2012/11/06 v6.83m Hyperref: PDFDocEncoding definition (HO) -) -\Hy@SavedSpaceFactor=\count112 - (/usr/local/texlive/2014/texmf-dist/tex/latex/latexconfig/hyperref.cfg -File: hyperref.cfg 2002/06/06 v1.2 hyperref configuration of TeXLive -) -Package hyperref Info: Hyper figures OFF on input line 4443. -Package hyperref Info: Link nesting OFF on input line 4448. -Package hyperref Info: Hyper index ON on input line 4451. -Package hyperref Info: Plain pages OFF on input line 4458. -Package hyperref Info: Backreferencing OFF on input line 4463. -Package hyperref Info: Implicit mode ON; LaTeX internals redefined. -Package hyperref Info: Bookmarks ON on input line 4688. -\c@Hy@tempcnt=\count113 - (/usr/local/texlive/2014/texmf-dist/tex/latex/url/url.sty -\Urlmuskip=\muskip11 -Package: url 2013/09/16 ver 3.4 Verb mode for urls, etc. -) -LaTeX Info: Redefining \url on input line 5041. -\XeTeXLinkMargin=\dimen133 -\Fld@menulength=\count114 -\Field@Width=\dimen134 -\Fld@charsize=\dimen135 -Package hyperref Info: Hyper figures OFF on input line 6295. -Package hyperref Info: Link nesting OFF on input line 6300. -Package hyperref Info: Hyper index ON on input line 6303. -Package hyperref Info: backreferencing OFF on input line 6310. -Package hyperref Info: Link coloring OFF on input line 6315. -Package hyperref Info: Link coloring with OCG OFF on input line 6320. -Package hyperref Info: PDF/A mode OFF on input line 6325. -LaTeX Info: Redefining \ref on input line 6365. -LaTeX Info: Redefining \pageref on input line 6369. -\Hy@abspage=\count115 -\c@Item=\count116 -\c@Hfootnote=\count117 -) - -Package hyperref Message: Driver (autodetected): hpdftex. - -(/usr/local/texlive/2014/texmf-dist/tex/latex/hyperref/hpdftex.def -File: hpdftex.def 2012/11/06 v6.83m Hyperref driver for pdfTeX -\Fld@listcount=\count118 -\c@bookmark@seq@number=\count119 - (/usr/local/texlive/2014/texmf-dist/tex/latex/oberdiek/rerunfilecheck.sty -Package: rerunfilecheck 2011/04/15 v1.7 Rerun checks for auxiliary files (HO) -Package uniquecounter Info: New unique counter `rerunfilecheck' on input line 282. -) -\Hy@SectionHShift=\skip46 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/tools/longtable.sty -Package: longtable 2004/02/01 v4.11 Multi-page Table package (DPC) -\LTleft=\skip47 -\LTright=\skip48 -\LTpre=\skip49 -\LTpost=\skip50 -\LTchunksize=\count120 -\LTcapwidth=\dimen136 -\LT@head=\box32 -\LT@firsthead=\box33 -\LT@foot=\box34 -\LT@lastfoot=\box35 -\LT@cols=\count121 -\LT@rows=\count122 -\c@LT@tables=\count123 -\c@LT@chunks=\count124 -\LT@p@ftn=\toks29 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/booktabs/booktabs.sty -Package: booktabs 2005/04/14 v1.61803 publication quality tables -\heavyrulewidth=\dimen137 -\lightrulewidth=\dimen138 -\cmidrulewidth=\dimen139 -\belowrulesep=\dimen140 -\belowbottomsep=\dimen141 -\aboverulesep=\dimen142 -\abovetopsep=\dimen143 -\cmidrulesep=\dimen144 -\cmidrulekern=\dimen145 -\defaultaddspace=\dimen146 -\@cmidla=\count125 -\@cmidlb=\count126 -\@aboverulesep=\dimen147 -\@belowrulesep=\dimen148 -\@thisruleclass=\count127 -\@lastruleclass=\count128 -\@thisrulewidth=\dimen149 -) -Package color Info: Redefining color red on input line 51. -Package color Info: Redefining color green on input line 52. -Package color Info: Redefining color blue on input line 54. -Package color Info: Redefining color cyan on input line 56. -Package color Info: Redefining color lightgray on input line 57. -Package hyperref Info: Option `breaklinks' set `true' on input line 211. -Package hyperref Info: Option `colorlinks' set `true' on input line 211. - (/Users/rajathkumar/Github/pythonclass/tex/.texpadtmp/00.aux - -LaTeX Warning: Label `built-in-functions' multiply defined. - - -LaTeX Warning: Label `built-in-functions' multiply defined. - - -LaTeX Warning: Label `built-in-functions' multiply defined. - -) -\openout1 = `00.aux'. - -LaTeX Font Info: Checking defaults for OML/cmm/m/it on input line 218. -LaTeX Font Info: ... okay on input line 218. -LaTeX Font Info: Checking defaults for T1/cmr/m/n on input line 218. -LaTeX Font Info: ... okay on input line 218. -LaTeX Font Info: Checking defaults for OT1/cmr/m/n on input line 218. -LaTeX Font Info: ... okay on input line 218. -LaTeX Font Info: Checking defaults for OMS/cmsy/m/n on input line 218. -LaTeX Font Info: ... okay on input line 218. -LaTeX Font Info: Checking defaults for OMX/cmex/m/n on input line 218. -LaTeX Font Info: ... okay on input line 218. -LaTeX Font Info: Checking defaults for U/cmr/m/n on input line 218. -LaTeX Font Info: ... okay on input line 218. -LaTeX Font Info: Checking defaults for PD1/pdf/m/n on input line 218. -LaTeX Font Info: ... okay on input line 218. - (/usr/local/texlive/2014/texmf-dist/tex/context/base/supp-pdf.mkii -[Loading MPS to PDF converter (version 2006.09.02).] -\scratchcounter=\count129 -\scratchdimen=\dimen150 -\scratchbox=\box36 -\nofMPsegments=\count130 -\nofMParguments=\count131 -\everyMPshowfont=\toks30 -\MPscratchCnt=\count132 -\MPscratchDim=\dimen151 -\MPnumerator=\count133 -\makeMPintoPDFobject=\count134 -\everyMPtoPDFconversion=\toks31 -) (/usr/local/texlive/2014/texmf-dist/tex/latex/oberdiek/epstopdf-base.sty -Package: epstopdf-base 2010/02/09 v2.5 Base part for package epstopdf - (/usr/local/texlive/2014/texmf-dist/tex/latex/oberdiek/grfext.sty -Package: grfext 2010/08/19 v1.1 Manage graphics extensions (HO) -) -Package grfext Info: Graphics extension search list: -(grfext) [.png,.pdf,.jpg,.mps,.jpeg,.jbig2,.jb2,.PNG,.PDF,.JPG,.JPEG,.JBIG2,.JB2,.eps] -(grfext) \AppendGraphicsExtensions on input line 452. - (/usr/local/texlive/2014/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg -File: epstopdf-sys.cfg 2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live -)) -*geometry* driver: auto-detecting -*geometry* detected driver: pdftex - -Package geometry Warning: The marginal notes overrun the paper. - Add 3.73001pt and more to the right margin. - -*geometry* verbose mode - [ preamble ] result: -* driver: pdftex -* paper: -* layout: -* layoutoffset:(h,v)=(0.0pt,0.0pt) -* modes: -* h-part:(L,W,R)=(72.26999pt, 469.75502pt, 72.26999pt) -* v-part:(T,H,B)=(72.26999pt, 650.43001pt, 72.26999pt) -* \paperwidth=614.295pt -* \paperheight=794.96999pt -* \textwidth=469.75502pt -* \textheight=650.43001pt -* \oddsidemargin=0.0pt -* \evensidemargin=0.0pt -* \topmargin=-37.0pt -* \headheight=12.0pt -* \headsep=25.0pt -* \topskip=10.0pt -* \footskip=30.0pt -* \marginparwidth=65.0pt -* \marginparsep=11.0pt -* \columnsep=10.0pt -* \skip\footins=9.0pt plus 4.0pt minus 2.0pt -* \hoffset=0.0pt -* \voffset=0.0pt -* \mag=1000 -* \@twocolumnfalse -* \@twosidefalse -* \@mparswitchfalse -* \@reversemarginfalse -* (1in=72.27pt=25.4mm, 1cm=28.453pt) - -(/usr/local/texlive/2014/texmf-dist/tex/latex/ucs/ucsencs.def -File: ucsencs.def 2011/01/21 Fixes to fontencodings LGR, T3 -) -\AtBeginShipoutBox=\box37 -Package hyperref Info: Link coloring ON on input line 218. - (/usr/local/texlive/2014/texmf-dist/tex/latex/hyperref/nameref.sty -Package: nameref 2012/10/27 v2.43 Cross-referencing by name of section - (/usr/local/texlive/2014/texmf-dist/tex/generic/oberdiek/gettitlestring.sty -Package: gettitlestring 2010/12/03 v1.4 Cleanup title references (HO) -) -\c@section@level=\count135 -) -LaTeX Info: Redefining \ref on input line 218. -LaTeX Info: Redefining \pageref on input line 218. -LaTeX Info: Redefining \nameref on input line 218. - (/Users/rajathkumar/Github/pythonclass/tex/.texpadtmp/00.out) (/Users/rajathkumar/Github/pythonclass/tex/.texpadtmp/00.out) -\@outlinefile=\write4 -\openout4 = `00.out'. - -LaTeX Font Info: Try loading font information for U+msa on input line 225. - (/usr/local/texlive/2014/texmf-dist/tex/latex/amsfonts/umsa.fd -File: umsa.fd 2013/01/14 v3.01 AMS symbols A -) -LaTeX Font Info: Try loading font information for U+msb on input line 225. - (/usr/local/texlive/2014/texmf-dist/tex/latex/amsfonts/umsb.fd -File: umsb.fd 2013/01/14 v3.01 AMS symbols B -) [1 - -{/usr/local/texlive/2014/texmf-var/fonts/map/pdftex/updmap/pdftex.map}] (/Users/rajathkumar/Github/pythonclass/tex/.texpadtmp/00.tocpdfTeX warning (ext4): destination with the same identifier (name{page.1}) has been already used, duplicate ignored - - \relax -l.49 ...0.1}Return Statement}{32}{subsection.10.1} - [1]) -\tf@toc=\write5 -\openout5 = `00.toc'. - - [2] [3] (./01.tex -Package color Info: Redefining color orange on input line 18. -Package color Info: Redefining color darkorange on input line 19. -Package color Info: Redefining color darkgreen on input line 20. -Package color Info: Redefining color myteal on input line 21. -Package color Info: Redefining color gray on input line 22. -Package color Info: Redefining color lightgray on input line 23. -Package color Info: Redefining color mediumgray on input line 24. -Package color Info: Redefining color inputbackground on input line 25. -Package color Info: Redefining color outputbackground on input line 26. -Package color Info: Redefining color traceback on input line 27. -Package color Info: Redefining color red on input line 29. -Package color Info: Redefining color green on input line 30. -Package color Info: Redefining color brown on input line 31. -Package color Info: Redefining color blue on input line 32. -Package color Info: Redefining color purple on input line 33. -Package color Info: Redefining color cyan on input line 34. -Package color Info: Redefining color lightgray on input line 35. -Package color Info: Redefining color darkgray on input line 38. -Package color Info: Redefining color lightred on input line 39. -Package color Info: Redefining color lightgreen on input line 40. -Package color Info: Redefining color lightblue on input line 41. -Package color Info: Redefining color lightpurple on input line 42. -Package color Info: Redefining color lightcyan on input line 43. - - -./01.tex:49: LaTeX Error: Command \Shaded already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.49 \newenvironment{Shaded}{}{} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:50: LaTeX Error: Command \KeywordTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.50 ...color[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:51: LaTeX Error: Command \DataTypeTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.51 ...[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:52: LaTeX Error: Command \DecValTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.52 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:53: LaTeX Error: Command \BaseNTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.53 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:54: LaTeX Error: Command \FloatTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.54 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:55: LaTeX Error: Command \CharTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.55 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:56: LaTeX Error: Command \StringTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.56 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:57: LaTeX Error: Command \CommentTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.57 ...color[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:58: LaTeX Error: Command \OtherTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.58 ...[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:59: LaTeX Error: Command \AlertTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.59 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:60: LaTeX Error: Command \FunctionTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.60 ...[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:61: LaTeX Error: Command \RegionMarkerTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.61 \newcommand{\RegionMarkerTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:62: LaTeX Error: Command \ErrorTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.62 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./01.tex:63: LaTeX Error: Command \NormalTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.63 \newcommand{\NormalTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -Package color Info: Redefining color incolor on input line 175. -Package color Info: Redefining color outcolor on input line 176. -Package hyperref Info: Option `breaklinks' set `true' on input line 190. - -Package hyperref Warning: Option `colorlinks' has already been used, -(hyperref) setting the option has no effect on input line 190. - - -./01.tex:196: LaTeX Error: Can be used only in preamble. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.196 \begin{document} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -LaTeX Font Info: Font shape `OT1/cmtt/bx/n' in size <10> not available -(Font) Font shape `OT1/cmtt/m/n' tried instead on input line 207. -[4] -LaTeX Font Info: Try loading font information for OML+cmr on input line 360. - (/usr/local/texlive/2014/texmf-dist/tex/latex/base/omlcmr.fd -File: omlcmr.fd 1999/05/25 v2.5h Standard LaTeX font definitions -) -LaTeX Font Info: Font shape `OML/cmr/m/n' in size <10> not available -(Font) Font shape `OML/cmm/m/it' tried instead on input line 360. - [5] [6] [7] [8] [9] (./02.tex -Package color Info: Redefining color orange on input line 16. -Package color Info: Redefining color darkorange on input line 17. -Package color Info: Redefining color darkgreen on input line 18. -Package color Info: Redefining color myteal on input line 19. -Package color Info: Redefining color gray on input line 20. -Package color Info: Redefining color lightgray on input line 21. -Package color Info: Redefining color mediumgray on input line 22. -Package color Info: Redefining color inputbackground on input line 23. -Package color Info: Redefining color outputbackground on input line 24. -Package color Info: Redefining color traceback on input line 25. -Package color Info: Redefining color red on input line 27. -Package color Info: Redefining color green on input line 28. -Package color Info: Redefining color brown on input line 29. -Package color Info: Redefining color blue on input line 30. -Package color Info: Redefining color purple on input line 31. -Package color Info: Redefining color cyan on input line 32. -Package color Info: Redefining color lightgray on input line 33. -Package color Info: Redefining color darkgray on input line 36. -Package color Info: Redefining color lightred on input line 37. -Package color Info: Redefining color lightgreen on input line 38. -Package color Info: Redefining color lightblue on input line 39. -Package color Info: Redefining color lightpurple on input line 40. -Package color Info: Redefining color lightcyan on input line 41. - - -./02.tex:47: LaTeX Error: Command \Shaded already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.47 \newenvironment{Shaded}{}{} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:48: LaTeX Error: Command \KeywordTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.48 ...color[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:49: LaTeX Error: Command \DataTypeTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.49 ...[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:50: LaTeX Error: Command \DecValTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.50 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:51: LaTeX Error: Command \BaseNTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.51 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:52: LaTeX Error: Command \FloatTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.52 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:53: LaTeX Error: Command \CharTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.53 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:54: LaTeX Error: Command \StringTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.54 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:55: LaTeX Error: Command \CommentTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.55 ...color[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:56: LaTeX Error: Command \OtherTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.56 ...[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:57: LaTeX Error: Command \AlertTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.57 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:58: LaTeX Error: Command \FunctionTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.58 ...[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:59: LaTeX Error: Command \RegionMarkerTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.59 \newcommand{\RegionMarkerTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:60: LaTeX Error: Command \ErrorTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.60 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./02.tex:61: LaTeX Error: Command \NormalTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.61 \newcommand{\NormalTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -Package color Info: Redefining color incolor on input line 173. -Package color Info: Redefining color outcolor on input line 174. -Package hyperref Info: Option `breaklinks' set `true' on input line 188. - -Package hyperref Warning: Option `colorlinks' has already been used, -(hyperref) setting the option has no effect on input line 188. - - -./02.tex:192: LaTeX Error: Can be used only in preamble. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.192 \begin{document} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -[10] -LaTeX Font Info: Try loading font information for OMS+cmtt on input line 362. -LaTeX Font Info: No file OMScmtt.fd. on input line 362. - - -LaTeX Font Warning: Font shape `OMS/cmtt/m/n' undefined -(Font) using `OMS/cmsy/m/n' instead -(Font) for symbol `textbackslash' on input line 362. - -[11] [12] (./03.tex -Package color Info: Redefining color orange on input line 16. -Package color Info: Redefining color darkorange on input line 17. -Package color Info: Redefining color darkgreen on input line 18. -Package color Info: Redefining color myteal on input line 19. -Package color Info: Redefining color gray on input line 20. -Package color Info: Redefining color lightgray on input line 21. -Package color Info: Redefining color mediumgray on input line 22. -Package color Info: Redefining color inputbackground on input line 23. -Package color Info: Redefining color outputbackground on input line 24. -Package color Info: Redefining color traceback on input line 25. -Package color Info: Redefining color red on input line 27. -Package color Info: Redefining color green on input line 28. -Package color Info: Redefining color brown on input line 29. -Package color Info: Redefining color blue on input line 30. -Package color Info: Redefining color purple on input line 31. -Package color Info: Redefining color cyan on input line 32. -Package color Info: Redefining color lightgray on input line 33. -Package color Info: Redefining color darkgray on input line 36. -Package color Info: Redefining color lightred on input line 37. -Package color Info: Redefining color lightgreen on input line 38. -Package color Info: Redefining color lightblue on input line 39. -Package color Info: Redefining color lightpurple on input line 40. -Package color Info: Redefining color lightcyan on input line 41. - - -./03.tex:47: LaTeX Error: Command \Shaded already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.47 \newenvironment{Shaded}{}{} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:48: LaTeX Error: Command \KeywordTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.48 ...color[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:49: LaTeX Error: Command \DataTypeTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.49 ...[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:50: LaTeX Error: Command \DecValTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.50 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:51: LaTeX Error: Command \BaseNTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.51 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:52: LaTeX Error: Command \FloatTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.52 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:53: LaTeX Error: Command \CharTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.53 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:54: LaTeX Error: Command \StringTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.54 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:55: LaTeX Error: Command \CommentTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.55 ...color[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:56: LaTeX Error: Command \OtherTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.56 ...[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:57: LaTeX Error: Command \AlertTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.57 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:58: LaTeX Error: Command \FunctionTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.58 ...[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:59: LaTeX Error: Command \RegionMarkerTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.59 \newcommand{\RegionMarkerTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:60: LaTeX Error: Command \ErrorTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.60 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./03.tex:61: LaTeX Error: Command \NormalTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.61 \newcommand{\NormalTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -Package color Info: Redefining color incolor on input line 173. -Package color Info: Redefining color outcolor on input line 174. -Package hyperref Info: Option `breaklinks' set `true' on input line 188. - -Package hyperref Warning: Option `colorlinks' has already been used, -(hyperref) setting the option has no effect on input line 188. - - -./03.tex:195: LaTeX Error: Can be used only in preamble. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.195 \begin{document} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -[13] [14] [15] [16] [17] [18] [19] [20] [21] (./04.tex -Package color Info: Redefining color orange on input line 15. -Package color Info: Redefining color darkorange on input line 16. -Package color Info: Redefining color darkgreen on input line 17. -Package color Info: Redefining color myteal on input line 18. -Package color Info: Redefining color gray on input line 19. -Package color Info: Redefining color lightgray on input line 20. -Package color Info: Redefining color mediumgray on input line 21. -Package color Info: Redefining color inputbackground on input line 22. -Package color Info: Redefining color outputbackground on input line 23. -Package color Info: Redefining color traceback on input line 24. -Package color Info: Redefining color red on input line 26. -Package color Info: Redefining color green on input line 27. -Package color Info: Redefining color brown on input line 28. -Package color Info: Redefining color blue on input line 29. -Package color Info: Redefining color purple on input line 30. -Package color Info: Redefining color cyan on input line 31. -Package color Info: Redefining color lightgray on input line 32. -Package color Info: Redefining color darkgray on input line 35. -Package color Info: Redefining color lightred on input line 36. -Package color Info: Redefining color lightgreen on input line 37. -Package color Info: Redefining color lightblue on input line 38. -Package color Info: Redefining color lightpurple on input line 39. -Package color Info: Redefining color lightcyan on input line 40. - - -./04.tex:46: LaTeX Error: Command \Shaded already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.46 \newenvironment{Shaded}{}{} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:47: LaTeX Error: Command \KeywordTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.47 ...color[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:48: LaTeX Error: Command \DataTypeTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.48 ...[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:49: LaTeX Error: Command \DecValTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.49 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:50: LaTeX Error: Command \BaseNTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.50 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:51: LaTeX Error: Command \FloatTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.51 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:52: LaTeX Error: Command \CharTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.52 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:53: LaTeX Error: Command \StringTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.53 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:54: LaTeX Error: Command \CommentTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.54 ...color[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:55: LaTeX Error: Command \OtherTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.55 ...[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:56: LaTeX Error: Command \AlertTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.56 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:57: LaTeX Error: Command \FunctionTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.57 ...[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:58: LaTeX Error: Command \RegionMarkerTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.58 \newcommand{\RegionMarkerTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:59: LaTeX Error: Command \ErrorTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.59 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./04.tex:60: LaTeX Error: Command \NormalTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.60 \newcommand{\NormalTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -Package color Info: Redefining color incolor on input line 172. -Package color Info: Redefining color outcolor on input line 173. -Package hyperref Info: Option `breaklinks' set `true' on input line 187. - -Package hyperref Warning: Option `colorlinks' has already been used, -(hyperref) setting the option has no effect on input line 187. - - -./04.tex:192: LaTeX Error: Can be used only in preamble. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.192 \begin{document} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -[22] [23] [24] -LaTeX Font Info: Try loading font information for OMS+cmr on input line 577. - (/usr/local/texlive/2014/texmf-dist/tex/latex/base/omscmr.fd -File: omscmr.fd 1999/05/25 v2.5h Standard LaTeX font definitions -) -LaTeX Font Info: Font shape `OMS/cmr/m/n' in size <10> not available -(Font) Font shape `OMS/cmsy/m/n' tried instead on input line 577. - [25] [26] [27] (./05.tex -Package color Info: Redefining color orange on input line 15. -Package color Info: Redefining color darkorange on input line 16. -Package color Info: Redefining color darkgreen on input line 17. -Package color Info: Redefining color myteal on input line 18. -Package color Info: Redefining color gray on input line 19. -Package color Info: Redefining color lightgray on input line 20. -Package color Info: Redefining color mediumgray on input line 21. -Package color Info: Redefining color inputbackground on input line 22. -Package color Info: Redefining color outputbackground on input line 23. -Package color Info: Redefining color traceback on input line 24. -Package color Info: Redefining color red on input line 26. -Package color Info: Redefining color green on input line 27. -Package color Info: Redefining color brown on input line 28. -Package color Info: Redefining color blue on input line 29. -Package color Info: Redefining color purple on input line 30. -Package color Info: Redefining color cyan on input line 31. -Package color Info: Redefining color lightgray on input line 32. -Package color Info: Redefining color darkgray on input line 35. -Package color Info: Redefining color lightred on input line 36. -Package color Info: Redefining color lightgreen on input line 37. -Package color Info: Redefining color lightblue on input line 38. -Package color Info: Redefining color lightpurple on input line 39. -Package color Info: Redefining color lightcyan on input line 40. - - -./05.tex:46: LaTeX Error: Command \Shaded already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.46 \newenvironment{Shaded}{}{} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:47: LaTeX Error: Command \KeywordTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.47 ...color[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:48: LaTeX Error: Command \DataTypeTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.48 ...[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:49: LaTeX Error: Command \DecValTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.49 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:50: LaTeX Error: Command \BaseNTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.50 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:51: LaTeX Error: Command \FloatTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.51 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:52: LaTeX Error: Command \CharTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.52 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:53: LaTeX Error: Command \StringTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.53 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:54: LaTeX Error: Command \CommentTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.54 ...color[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:55: LaTeX Error: Command \OtherTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.55 ...[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:56: LaTeX Error: Command \AlertTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.56 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:57: LaTeX Error: Command \FunctionTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.57 ...[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:58: LaTeX Error: Command \RegionMarkerTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.58 \newcommand{\RegionMarkerTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:59: LaTeX Error: Command \ErrorTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.59 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./05.tex:60: LaTeX Error: Command \NormalTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.60 \newcommand{\NormalTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -Package color Info: Redefining color incolor on input line 172. -Package color Info: Redefining color outcolor on input line 173. -Package hyperref Info: Option `breaklinks' set `true' on input line 187. - -Package hyperref Warning: Option `colorlinks' has already been used, -(hyperref) setting the option has no effect on input line 187. - - -./05.tex:193: LaTeX Error: Can be used only in preamble. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.193 \begin{document} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -[28] [29] [30] [31] (./06.tex -Package color Info: Redefining color orange on input line 15. -Package color Info: Redefining color darkorange on input line 16. -Package color Info: Redefining color darkgreen on input line 17. -Package color Info: Redefining color myteal on input line 18. -Package color Info: Redefining color gray on input line 19. -Package color Info: Redefining color lightgray on input line 20. -Package color Info: Redefining color mediumgray on input line 21. -Package color Info: Redefining color inputbackground on input line 22. -Package color Info: Redefining color outputbackground on input line 23. -Package color Info: Redefining color traceback on input line 24. -Package color Info: Redefining color red on input line 26. -Package color Info: Redefining color green on input line 27. -Package color Info: Redefining color brown on input line 28. -Package color Info: Redefining color blue on input line 29. -Package color Info: Redefining color purple on input line 30. -Package color Info: Redefining color cyan on input line 31. -Package color Info: Redefining color lightgray on input line 32. -Package color Info: Redefining color darkgray on input line 35. -Package color Info: Redefining color lightred on input line 36. -Package color Info: Redefining color lightgreen on input line 37. -Package color Info: Redefining color lightblue on input line 38. -Package color Info: Redefining color lightpurple on input line 39. -Package color Info: Redefining color lightcyan on input line 40. - - -./06.tex:46: LaTeX Error: Command \Shaded already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.46 \newenvironment{Shaded}{}{} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:47: LaTeX Error: Command \KeywordTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.47 ...color[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:48: LaTeX Error: Command \DataTypeTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.48 ...[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:49: LaTeX Error: Command \DecValTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.49 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:50: LaTeX Error: Command \BaseNTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.50 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:51: LaTeX Error: Command \FloatTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.51 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:52: LaTeX Error: Command \CharTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.52 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:53: LaTeX Error: Command \StringTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.53 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:54: LaTeX Error: Command \CommentTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.54 ...color[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:55: LaTeX Error: Command \OtherTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.55 ...[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:56: LaTeX Error: Command \AlertTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.56 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:57: LaTeX Error: Command \FunctionTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.57 ...[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:58: LaTeX Error: Command \RegionMarkerTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.58 \newcommand{\RegionMarkerTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:59: LaTeX Error: Command \ErrorTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.59 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./06.tex:60: LaTeX Error: Command \NormalTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.60 \newcommand{\NormalTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -Package color Info: Redefining color incolor on input line 172. -Package color Info: Redefining color outcolor on input line 173. -Package hyperref Info: Option `breaklinks' set `true' on input line 187. - -Package hyperref Warning: Option `colorlinks' has already been used, -(hyperref) setting the option has no effect on input line 187. - - -./06.tex:192: LaTeX Error: Can be used only in preamble. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.192 \begin{document} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -[32] [33] [34] [35] [36] [37] (./07.tex -Package color Info: Redefining color orange on input line 15. -Package color Info: Redefining color darkorange on input line 16. -Package color Info: Redefining color darkgreen on input line 17. -Package color Info: Redefining color myteal on input line 18. -Package color Info: Redefining color gray on input line 19. -Package color Info: Redefining color lightgray on input line 20. -Package color Info: Redefining color mediumgray on input line 21. -Package color Info: Redefining color inputbackground on input line 22. -Package color Info: Redefining color outputbackground on input line 23. -Package color Info: Redefining color traceback on input line 24. -Package color Info: Redefining color red on input line 26. -Package color Info: Redefining color green on input line 27. -Package color Info: Redefining color brown on input line 28. -Package color Info: Redefining color blue on input line 29. -Package color Info: Redefining color purple on input line 30. -Package color Info: Redefining color cyan on input line 31. -Package color Info: Redefining color lightgray on input line 32. -Package color Info: Redefining color darkgray on input line 35. -Package color Info: Redefining color lightred on input line 36. -Package color Info: Redefining color lightgreen on input line 37. -Package color Info: Redefining color lightblue on input line 38. -Package color Info: Redefining color lightpurple on input line 39. -Package color Info: Redefining color lightcyan on input line 40. - - -./07.tex:46: LaTeX Error: Command \Shaded already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.46 \newenvironment{Shaded}{}{} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:47: LaTeX Error: Command \KeywordTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.47 ...color[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:48: LaTeX Error: Command \DataTypeTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.48 ...[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:49: LaTeX Error: Command \DecValTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.49 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:50: LaTeX Error: Command \BaseNTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.50 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:51: LaTeX Error: Command \FloatTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.51 ...[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:52: LaTeX Error: Command \CharTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.52 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:53: LaTeX Error: Command \StringTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.53 ...[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:54: LaTeX Error: Command \CommentTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.54 ...color[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:55: LaTeX Error: Command \OtherTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.55 ...[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:56: LaTeX Error: Command \AlertTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.56 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:57: LaTeX Error: Command \FunctionTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.57 ...[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:58: LaTeX Error: Command \RegionMarkerTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.58 \newcommand{\RegionMarkerTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:59: LaTeX Error: Command \ErrorTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.59 ...color[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - - -./07.tex:60: LaTeX Error: Command \NormalTok already defined. - Or name \end... illegal, see p.192 of the manual. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.60 \newcommand{\NormalTok}[1]{{#1}} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -Package color Info: Redefining color incolor on input line 172. -Package color Info: Redefining color outcolor on input line 173. -Package hyperref Info: Option `breaklinks' set `true' on input line 187. - -Package hyperref Warning: Option `colorlinks' has already been used, -(hyperref) setting the option has no effect on input line 187. - - -./07.tex:192: LaTeX Error: Can be used only in preamble. - -See the LaTeX manual or LaTeX Companion for explanation. -Type H for immediate help. - ... - -l.192 \begin{document} - -Your command was ignored. -Type I to replace it with another command, -or to continue without it. - -[38] [39] [40] [41] [42] -Package atveryend Info: Empty hook `BeforeClearDocument' on input line 748. - [43] -Package atveryend Info: Empty hook `AfterLastShipout' on input line 748. - (/Users/rajathkumar/Github/pythonclass/tex/.texpadtmp/00.aux) -Package atveryend Info: Executing hook `AtVeryEndDocument' on input line 748. -Package atveryend Info: Executing hook `AtEndAfterFileList' on input line 748. -Package rerunfilecheck Info: File `00.out' has not changed. -(rerunfilecheck) Checksum: . - - -LaTeX Font Warning: Some font shapes were not available, defaults substituted. - - -LaTeX Warning: There were multiply-defined labels. - -Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 748. - ) ) ) ) ) ) ) ) -(\end occurred inside a group at level 7) - -### semi simple group (level 7) entered at line 192 (\begingroup) -### semi simple group (level 6) entered at line 192 (\begingroup) -### semi simple group (level 5) entered at line 193 (\begingroup) -### semi simple group (level 4) entered at line 192 (\begingroup) -### semi simple group (level 3) entered at line 195 (\begingroup) -### semi simple group (level 2) entered at line 192 (\begingroup) -### semi simple group (level 1) entered at line 196 (\begingroup) -### bottom level -Here is how much of TeX's memory you used: - 8481 strings out of 493117 - 125919 string characters out of 6135433 - 248616 words of memory out of 5000000 - 11656 multiletter control sequences out of 15000+600000 - 12484 words of font info for 49 fonts, out of 8000000 for 9000 - 1141 hyphenation exceptions out of 8191 - 36i,6n,35p,617b,2376s stack positions out of 5000i,500n,10000p,200000b,80000s - -Output written on /Users/rajathkumar/Github/pythonclass/tex/.texpadtmp/00.pdf (44 pages, 223221 bytes). -PDF statistics: - 551 PDF objects out of 1000 (max. 8388607) - 489 compressed objects within 5 object streams - 107 named destinations out of 1000 (max. 500000) - 449 words of extra memory for PDF output out of 10000 (max. 10000000) - diff --git a/tex/.texpadtmp/00.out b/tex/.texpadtmp/00.out deleted file mode 100644 index 025d507..0000000 --- a/tex/.texpadtmp/00.out +++ /dev/null @@ -1,56 +0,0 @@ -\BOOKMARK [1][-]{section.1}{Python}{}% 1 -\BOOKMARK [2][-]{subsection.1.1}{Introduction}{section.1}% 2 -\BOOKMARK [2][-]{subsection.1.2}{Installation}{section.1}% 3 -\BOOKMARK [3][-]{subsubsection.1.2.1}{Installation from unofficial distributions}{subsection.1.2}% 4 -\BOOKMARK [2][-]{subsection.1.3}{Launching IPython Notebook}{section.1}% 5 -\BOOKMARK [2][-]{subsection.1.4}{How to learn from this resource?}{section.1}% 6 -\BOOKMARK [2][-]{subsection.1.5}{License}{section.1}% 7 -\BOOKMARK [1][-]{section.2}{The Zen Of Python}{}% 8 -\BOOKMARK [1][-]{section.3}{Variables}{}% 9 -\BOOKMARK [1][-]{section.4}{Operators}{}% 10 -\BOOKMARK [2][-]{subsection.4.1}{Arithmetic Operators}{section.4}% 11 -\BOOKMARK [2][-]{subsection.4.2}{Relational Operators}{section.4}% 12 -\BOOKMARK [2][-]{subsection.4.3}{Bitwise Operators}{section.4}% 13 -\BOOKMARK [1][-]{section.5}{Built-in Functions}{}% 14 -\BOOKMARK [2][-]{subsection.5.1}{Conversion from one system to another}{section.5}% 15 -\BOOKMARK [2][-]{subsection.5.2}{Simplifying Arithmetic Operations}{section.5}% 16 -\BOOKMARK [2][-]{subsection.5.3}{Accepting User Inputs}{section.5}% 17 -\BOOKMARK [1][-]{section.6}{Print Statement}{}% 18 -\BOOKMARK [2][-]{subsection.6.1}{Other Examples}{section.6}% 19 -\BOOKMARK [1][-]{section.7}{PrecisionWidth and FieldWidth}{}% 20 -\BOOKMARK [1][-]{section.8}{Data Structures}{}% 21 -\BOOKMARK [2][-]{subsection.8.1}{Lists}{section.8}% 22 -\BOOKMARK [3][-]{subsubsection.8.1.1}{Indexing}{subsection.8.1}% 23 -\BOOKMARK [3][-]{subsubsection.8.1.2}{Slicing}{subsection.8.1}% 24 -\BOOKMARK [3][-]{subsubsection.8.1.3}{Built in List Functions}{subsection.8.1}% 25 -\BOOKMARK [3][-]{subsubsection.8.1.4}{Copying a list}{subsection.8.1}% 26 -\BOOKMARK [2][-]{subsection.8.2}{Tuples}{section.8}% 27 -\BOOKMARK [3][-]{subsubsection.8.2.1}{Mapping one tuple to another}{subsection.8.2}% 28 -\BOOKMARK [3][-]{subsubsection.8.2.2}{Built In Tuple functions}{subsection.8.2}% 29 -\BOOKMARK [2][-]{subsection.8.3}{Sets}{section.8}% 30 -\BOOKMARK [3][-]{subsubsection.8.3.1}{Built-in Functions}{subsection.8.3}% 31 -\BOOKMARK [2][-]{subsection.8.4}{Strings}{section.8}% 32 -\BOOKMARK [3][-]{subsubsection.8.4.1}{Built-in Functions}{subsection.8.4}% 33 -\BOOKMARK [2][-]{subsection.8.5}{Dictionaries}{section.8}% 34 -\BOOKMARK [3][-]{subsubsection.8.5.1}{Built-in Functions}{subsection.8.5}% 35 -\BOOKMARK [1][-]{section.9}{Control Flow Statements}{}% 36 -\BOOKMARK [2][-]{subsection.9.1}{If}{section.9}% 37 -\BOOKMARK [2][-]{subsection.9.2}{If-else}{section.9}% 38 -\BOOKMARK [2][-]{subsection.9.3}{if-elif}{section.9}% 39 -\BOOKMARK [2][-]{subsection.9.4}{Loops}{section.9}% 40 -\BOOKMARK [3][-]{subsubsection.9.4.1}{For}{subsection.9.4}% 41 -\BOOKMARK [3][-]{subsubsection.9.4.2}{While}{subsection.9.4}% 42 -\BOOKMARK [2][-]{subsection.9.5}{Break}{section.9}% 43 -\BOOKMARK [2][-]{subsection.9.6}{Continue}{section.9}% 44 -\BOOKMARK [2][-]{subsection.9.7}{List Comprehensions}{section.9}% 45 -\BOOKMARK [1][-]{section.10}{Functions}{}% 46 -\BOOKMARK [2][-]{subsection.10.1}{Return Statement}{section.10}% 47 -\BOOKMARK [2][-]{subsection.10.2}{Implicit arguments}{section.10}% 48 -\BOOKMARK [2][-]{subsection.10.3}{Any number of arguments}{section.10}% 49 -\BOOKMARK [2][-]{subsection.10.4}{Global and Local Variables}{section.10}% 50 -\BOOKMARK [2][-]{subsection.10.5}{Lambda Functions}{section.10}% 51 -\BOOKMARK [3][-]{subsubsection.10.5.1}{map}{subsection.10.5}% 52 -\BOOKMARK [3][-]{subsubsection.10.5.2}{filter}{subsection.10.5}% 53 -\BOOKMARK [1][-]{section.11}{Classes}{}% 54 -\BOOKMARK [2][-]{subsection.11.1}{Inheritance}{section.11}% 55 -\BOOKMARK [1][-]{section.12}{Where to go from here?}{}% 56 diff --git a/tex/.texpadtmp/00.synctex.gz b/tex/.texpadtmp/00.synctex.gz deleted file mode 100644 index 7755c98..0000000 Binary files a/tex/.texpadtmp/00.synctex.gz and /dev/null differ diff --git a/tex/.texpadtmp/00.toc b/tex/.texpadtmp/00.toc deleted file mode 100644 index 83dfd04..0000000 --- a/tex/.texpadtmp/00.toc +++ /dev/null @@ -1,58 +0,0 @@ -\contentsline {section}{\numberline {1}Python}{3}{section.1} -\contentsline {subsection}{\numberline {1.1}Introduction}{3}{subsection.1.1} -\contentsline {subsection}{\numberline {1.2}Installation}{3}{subsection.1.2} -\contentsline {subsubsection}{\numberline {1.2.1}Installation from unofficial distributions}{3}{subsubsection.1.2.1} -\contentsline {paragraph}{Anaconda}{3}{section*.2} -\contentsline {paragraph}{Canopy}{3}{section*.3} -\contentsline {subsection}{\numberline {1.3}Launching IPython Notebook}{3}{subsection.1.3} -\contentsline {subsection}{\numberline {1.4}How to learn from this resource?}{3}{subsection.1.4} -\contentsline {subsection}{\numberline {1.5}License}{3}{subsection.1.5} -\contentsline {section}{\numberline {2}The Zen Of Python}{4}{section.2} -\contentsline {section}{\numberline {3}Variables}{4}{section.3} -\contentsline {section}{\numberline {4}Operators}{4}{section.4} -\contentsline {subsection}{\numberline {4.1}Arithmetic Operators}{4}{subsection.4.1} -\contentsline {subsection}{\numberline {4.2}Relational Operators}{5}{subsection.4.2} -\contentsline {subsection}{\numberline {4.3}Bitwise Operators}{6}{subsection.4.3} -\contentsline {section}{\numberline {5}Built-in Functions}{6}{section.5} -\contentsline {subsection}{\numberline {5.1}Conversion from one system to another}{6}{subsection.5.1} -\contentsline {subsection}{\numberline {5.2}Simplifying Arithmetic Operations}{7}{subsection.5.2} -\contentsline {subsection}{\numberline {5.3}Accepting User Inputs}{9}{subsection.5.3} -\contentsline {section}{\numberline {6}Print Statement}{10}{section.6} -\contentsline {subsection}{\numberline {6.1}Other Examples}{11}{subsection.6.1} -\contentsline {section}{\numberline {7}PrecisionWidth and FieldWidth}{12}{section.7} -\contentsline {section}{\numberline {8}Data Structures}{13}{section.8} -\contentsline {subsection}{\numberline {8.1}Lists}{13}{subsection.8.1} -\contentsline {subsubsection}{\numberline {8.1.1}Indexing}{13}{subsubsection.8.1.1} -\contentsline {subsubsection}{\numberline {8.1.2}Slicing}{14}{subsubsection.8.1.2} -\contentsline {subsubsection}{\numberline {8.1.3}Built in List Functions}{14}{subsubsection.8.1.3} -\contentsline {subsubsection}{\numberline {8.1.4}Copying a list}{18}{subsubsection.8.1.4} -\contentsline {subsection}{\numberline {8.2}Tuples}{19}{subsection.8.2} -\contentsline {subsubsection}{\numberline {8.2.1}Mapping one tuple to another}{20}{subsubsection.8.2.1} -\contentsline {subsubsection}{\numberline {8.2.2}Built In Tuple functions}{20}{subsubsection.8.2.2} -\contentsline {subsection}{\numberline {8.3}Sets}{20}{subsection.8.3} -\contentsline {subsubsection}{\numberline {8.3.1}Built-in Functions}{20}{subsubsection.8.3.1} -\contentsline {subsection}{\numberline {8.4}Strings}{22}{subsection.8.4} -\contentsline {subsubsection}{\numberline {8.4.1}Built-in Functions}{22}{subsubsection.8.4.1} -\contentsline {subsection}{\numberline {8.5}Dictionaries}{26}{subsection.8.5} -\contentsline {subsubsection}{\numberline {8.5.1}Built-in Functions}{26}{subsubsection.8.5.1} -\contentsline {section}{\numberline {9}Control Flow Statements}{28}{section.9} -\contentsline {subsection}{\numberline {9.1}If}{28}{subsection.9.1} -\contentsline {subsection}{\numberline {9.2}If-else}{28}{subsection.9.2} -\contentsline {subsection}{\numberline {9.3}if-elif}{28}{subsection.9.3} -\contentsline {subsection}{\numberline {9.4}Loops}{29}{subsection.9.4} -\contentsline {subsubsection}{\numberline {9.4.1}For}{29}{subsubsection.9.4.1} -\contentsline {subsubsection}{\numberline {9.4.2}While}{30}{subsubsection.9.4.2} -\contentsline {subsection}{\numberline {9.5}Break}{30}{subsection.9.5} -\contentsline {subsection}{\numberline {9.6}Continue}{30}{subsection.9.6} -\contentsline {subsection}{\numberline {9.7}List Comprehensions}{31}{subsection.9.7} -\contentsline {section}{\numberline {10}Functions}{32}{section.10} -\contentsline {subsection}{\numberline {10.1}Return Statement}{33}{subsection.10.1} -\contentsline {subsection}{\numberline {10.2}Implicit arguments}{34}{subsection.10.2} -\contentsline {subsection}{\numberline {10.3}Any number of arguments}{34}{subsection.10.3} -\contentsline {subsection}{\numberline {10.4}Global and Local Variables}{35}{subsection.10.4} -\contentsline {subsection}{\numberline {10.5}Lambda Functions}{36}{subsection.10.5} -\contentsline {subsubsection}{\numberline {10.5.1}map}{36}{subsubsection.10.5.1} -\contentsline {subsubsection}{\numberline {10.5.2}filter}{37}{subsubsection.10.5.2} -\contentsline {section}{\numberline {11}Classes}{38}{section.11} -\contentsline {subsection}{\numberline {11.1}Inheritance}{41}{subsection.11.1} -\contentsline {section}{\numberline {12}Where to go from here?}{43}{section.12} diff --git a/tex/00.tex b/tex/00.tex deleted file mode 100644 index ee50b3e..0000000 --- a/tex/00.tex +++ /dev/null @@ -1,323 +0,0 @@ - -% Default to the notebook output style - - - - -% Inherit from the specified cell style. - - - - - -\documentclass{article} - - - - \usepackage{graphicx} % Used to insert images - \usepackage{adjustbox} % Used to constrain images to a maximum size - \usepackage{color} % Allow colors to be defined - \usepackage{enumerate} % Needed for markdown enumerations to work - \usepackage{geometry} % Used to adjust the document margins - \usepackage{amsmath} % Equations - \usepackage{amssymb} % Equations - \usepackage{eurosym} % defines \euro - \usepackage[mathletters]{ucs} % Extended unicode (utf-8) support - \usepackage[utf8x]{inputenc} % Allow utf-8 characters in the tex document - \usepackage{fancyvrb} % verbatim replacement that allows latex - \usepackage{grffile} % extends the file name processing of package graphics - % to support a larger range - % The hyperref package gives us a pdf with properly built - % internal navigation ('pdf bookmarks' for the table of contents, - % internal cross-reference links, web links for URLs, etc.) - \usepackage{hyperref} - \usepackage{longtable} % longtable support required by pandoc >1.10 - \usepackage{booktabs} % table support for pandoc > 1.12.2 - - - - - \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} - \definecolor{darkorange}{rgb}{.71,0.21,0.01} - \definecolor{darkgreen}{rgb}{.12,.54,.11} - \definecolor{myteal}{rgb}{.26, .44, .56} - \definecolor{gray}{gray}{0.45} - \definecolor{lightgray}{gray}{.95} - \definecolor{mediumgray}{gray}{.8} - \definecolor{inputbackground}{rgb}{.95, .95, .85} - \definecolor{outputbackground}{rgb}{.95, .95, .95} - \definecolor{traceback}{rgb}{1, .95, .95} - % ansi colors - \definecolor{red}{rgb}{.6,0,0} - \definecolor{green}{rgb}{0,.65,0} - \definecolor{brown}{rgb}{0.6,0.6,0} - \definecolor{blue}{rgb}{0,.145,.698} - \definecolor{purple}{rgb}{.698,.145,.698} - \definecolor{cyan}{rgb}{0,.698,.698} - \definecolor{lightgray}{gray}{0.5} - - % bright ansi colors - \definecolor{darkgray}{gray}{0.25} - \definecolor{lightred}{rgb}{1.0,0.39,0.28} - \definecolor{lightgreen}{rgb}{0.48,0.99,0.0} - \definecolor{lightblue}{rgb}{0.53,0.81,0.92} - \definecolor{lightpurple}{rgb}{0.87,0.63,0.87} - \definecolor{lightcyan}{rgb}{0.5,1.0,0.83} - - % commands and environments needed by pandoc snippets - % extracted from the output of `pandoc -s` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} - % Add ',fontsize=\small' for more characters per line - \newenvironment{Shaded}{}{} - \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - \newcommand{\RegionMarkerTok}[1]{{#1}} - \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\NormalTok}[1]{{#1}} - - % Define a nice break command that doesn't care if a line doesn't already - % exist. - \def\br{\hspace*{\fill} \\* } - % Math Jax compatability definitions - \def\gt{>} - \def\lt{<} - % Document parameters - - - - - % Pygments definitions - -\makeatletter -\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% - \let\PY@ul=\relax \let\PY@tc=\relax% - \let\PY@bc=\relax \let\PY@ff=\relax} -\def\PY@tok#1{\csname PY@tok@#1\endcsname} -\def\PY@toks#1+{\ifx\relax#1\empty\else% - \PY@tok{#1}\expandafter\PY@toks\fi} -\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% - \PY@it{\PY@bf{\PY@ff{#1}}}}}}} -\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} - -\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} -\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} -\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} -\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} -\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} -\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} -\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} -\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} -\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} -\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} -\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} -\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} -\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} - -\def\PYZbs{\char`\\} -\def\PYZus{\char`\_} -\def\PYZob{\char`\{} -\def\PYZcb{\char`\}} -\def\PYZca{\char`\^} -\def\PYZam{\char`\&} -\def\PYZlt{\char`\<} -\def\PYZgt{\char`\>} -\def\PYZsh{\char`\#} -\def\PYZpc{\char`\%} -\def\PYZdl{\char`\$} -\def\PYZhy{\char`\-} -\def\PYZsq{\char`\'} -\def\PYZdq{\char`\"} -\def\PYZti{\char`\~} -% for compatibility with earlier versions -\def\PYZat{@} -\def\PYZlb{[} -\def\PYZrb{]} -\makeatother - - - % Exact colors from NB - \definecolor{incolor}{rgb}{0.0, 0.0, 0.5} - \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0} - - - - - % Prevent overflowing lines due to hard-to-break entities - \sloppy - % Setup hyperref package - \hypersetup{ - breaklinks=true, % so long urls are correctly broken across lines - colorlinks=true, - urlcolor=blue, - linkcolor=darkorange, - citecolor=darkgreen, - } - % Slightly bigger margins than the latex defaults - - \geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in} - - - - \begin{document} - -\begin{titlepage} - \title{\Huge{Get Started with Python}} - \author{Rajath Kumar M.P. \\ \\ Department of Electronics and Communication Engineering \\ RNS Institute of Technology, Bangalore \\ \\rajathkumar.exe@gmail.com} - \date{} - \maketitle - \thispagestyle{empty} -\end{titlepage} - - - - - - \tableofcontents - - \newpage - - - - \section{Python}\label{python} - - \subsection{Introduction}\label{introduction} - - Python is a modern, robust, high level programming language. It is very -easy to pick up even if you are completely new to programming. - - \subsection{Installation}\label{installation} - - Mac OS X and Linux comes pre installed with python. Windows users can -download python from https://www.python.org/downloads/ . - -To install IPython run, - -\begin{verbatim} -$ pip install ipython[all] -\end{verbatim} - -This will install all the necessary dependencies for the notebook, -qtconsole, tests etc. - - \subsubsection{Installation from unofficial -distributions}\label{installation-from-unofficial-distributions} - - Installing all the necessary libraries might prove troublesome. Anaconda -and Canopy comes pre packaged with all the necessary python libraries -and also IPython. - - \paragraph{Anaconda}\label{anaconda} - - Download Anaconda from https://www.continuum.io/downloads - -Anaconda is completely free and includes more than 300 python packages. -Both python 2.7 and 3.4 options are available. - - \paragraph{Canopy}\label{canopy} - - Download Canopy from https://store.enthought.com/downloads/\#default - -Canopy has a premium version which offers 300+ python packages. But the -free version works just fine. Canopy as of now supports only 2.7 but it -comes with its own text editor and IPython environment. - - \subsection{Launching IPython -Notebook}\label{launching-ipython-notebook} - - From the terminal - -\begin{verbatim} -ipython notebook -\end{verbatim} - -In Canopy and Anaconda, Open the respective terminals and execute the -above. - - \subsection{How to learn from this -resource?}\label{how-to-learn-from-this-resource} - - Download all the ipython notebooks from this repository -https://github.com/rajathkumarmp/Python-Lectures - -Launch ipython notebook from the folder which contains the notebooks. -Open each one of them - -\begin{verbatim} -Cell > All Output > Clear -\end{verbatim} - -This will clear all the outputs and now you can understand each -statement and learn interactively. - - \subsection{License}\label{license} - - This work is licensed under the Creative Commons Attribution 3.0 -Unported License. To view a copy of this license, visit -http://creativecommons.org/licenses/by/3.0/ - - - % Add a bibliography block to the postdoc - - \newpage - - \input{01} - - - \end{document} diff --git a/tex/01.tex b/tex/01.tex deleted file mode 100644 index 225a5e0..0000000 --- a/tex/01.tex +++ /dev/null @@ -1,702 +0,0 @@ - -% Default to the notebook output style - - - - -% Inherit from the specified cell style. - - - - - - - - - - - \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} - \definecolor{darkorange}{rgb}{.71,0.21,0.01} - \definecolor{darkgreen}{rgb}{.12,.54,.11} - \definecolor{myteal}{rgb}{.26, .44, .56} - \definecolor{gray}{gray}{0.45} - \definecolor{lightgray}{gray}{.95} - \definecolor{mediumgray}{gray}{.8} - \definecolor{inputbackground}{rgb}{.95, .95, .85} - \definecolor{outputbackground}{rgb}{.95, .95, .95} - \definecolor{traceback}{rgb}{1, .95, .95} - % ansi colors - \definecolor{red}{rgb}{.6,0,0} - \definecolor{green}{rgb}{0,.65,0} - \definecolor{brown}{rgb}{0.6,0.6,0} - \definecolor{blue}{rgb}{0,.145,.698} - \definecolor{purple}{rgb}{.698,.145,.698} - \definecolor{cyan}{rgb}{0,.698,.698} - \definecolor{lightgray}{gray}{0.5} - - % bright ansi colors - \definecolor{darkgray}{gray}{0.25} - \definecolor{lightred}{rgb}{1.0,0.39,0.28} - \definecolor{lightgreen}{rgb}{0.48,0.99,0.0} - \definecolor{lightblue}{rgb}{0.53,0.81,0.92} - \definecolor{lightpurple}{rgb}{0.87,0.63,0.87} - \definecolor{lightcyan}{rgb}{0.5,1.0,0.83} - - % commands and environments needed by pandoc snippets - % extracted from the output of `pandoc -s` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} - % Add ',fontsize=\small' for more characters per line - \newenvironment{Shaded}{}{} - \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - \newcommand{\RegionMarkerTok}[1]{{#1}} - \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\NormalTok}[1]{{#1}} - - % Define a nice break command that doesn't care if a line doesn't already - % exist. - \def\br{\hspace*{\fill} \\* } - % Math Jax compatability definitions - \def\gt{>} - \def\lt{<} - % Document parameters - \title{} - - - - - % Pygments definitions - -\makeatletter -\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% - \let\PY@ul=\relax \let\PY@tc=\relax% - \let\PY@bc=\relax \let\PY@ff=\relax} -\def\PY@tok#1{\csname PY@tok@#1\endcsname} -\def\PY@toks#1+{\ifx\relax#1\empty\else% - \PY@tok{#1}\expandafter\PY@toks\fi} -\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% - \PY@it{\PY@bf{\PY@ff{#1}}}}}}} -\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} - -\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} -\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} -\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} -\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} -\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} -\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} -\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} -\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} -\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} -\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} -\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} -\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} -\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} - -\def\PYZbs{\char`\\} -\def\PYZus{\char`\_} -\def\PYZob{\char`\{} -\def\PYZcb{\char`\}} -\def\PYZca{\char`\^} -\def\PYZam{\char`\&} -\def\PYZlt{\char`\<} -\def\PYZgt{\char`\>} -\def\PYZsh{\char`\#} -\def\PYZpc{\char`\%} -\def\PYZdl{\char`\$} -\def\PYZhy{\char`\-} -\def\PYZsq{\char`\'} -\def\PYZdq{\char`\"} -\def\PYZti{\char`\~} -% for compatibility with earlier versions -\def\PYZat{@} -\def\PYZlb{[} -\def\PYZrb{]} -\makeatother - - - % Exact colors from NB - \definecolor{incolor}{rgb}{0.0, 0.0, 0.5} - \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0} - - - - - % Prevent overflowing lines due to hard-to-break entities - \sloppy - % Setup hyperref package - \hypersetup{ - breaklinks=true, % so long urls are correctly broken across lines - colorlinks=true, - urlcolor=blue, - linkcolor=darkorange, - citecolor=darkgreen, - } - % Slightly bigger margins than the latex defaults - - - - - \begin{document} - - - \maketitle - - - - - \section{The Zen Of Python}\label{the-zen-of-python} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}1}]:} \PY{k+kn}{import} \PY{n+nn}{this} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -The Zen of Python, by Tim Peters - -Beautiful is better than ugly. -Explicit is better than implicit. -Simple is better than complex. -Complex is better than complicated. -Flat is better than nested. -Sparse is better than dense. -Readability counts. -Special cases aren't special enough to break the rules. -Although practicality beats purity. -Errors should never pass silently. -Unless explicitly silenced. -In the face of ambiguity, refuse the temptation to guess. -There should be one-- and preferably only one --obvious way to do it. -Although that way may not be obvious at first unless you're Dutch. -Now is better than never. -Although never is often better than *right* now. -If the implementation is hard to explain, it's a bad idea. -If the implementation is easy to explain, it may be a good idea. -Namespaces are one honking great idea -- let's do more of those! - \end{Verbatim} - - \section{Variables}\label{variables} - - A name that is used to denote something or a value is called a variable. -In python, variables can be declared and values can be assigned to it as -follows, - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}2}]:} \PY{n}{x} \PY{o}{=} \PY{l+m+mi}{2} - \PY{n}{y} \PY{o}{=} \PY{l+m+mi}{5} - \PY{n}{xy} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{Hey}\PY{l+s}{\PYZsq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}3}]:} \PY{k}{print} \PY{n}{x}\PY{o}{+}\PY{n}{y}\PY{p}{,} \PY{n}{xy} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -7 Hey - \end{Verbatim} - - Multiple variables can be assigned with the same value. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}4}]:} \PY{n}{x} \PY{o}{=} \PY{n}{y} \PY{o}{=} \PY{l+m+mi}{1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}5}]:} \PY{k}{print} \PY{n}{x}\PY{p}{,}\PY{n}{y} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -1 1 - \end{Verbatim} - - \section{Operators}\label{operators} - - \subsection{Arithmetic Operators}\label{arithmetic-operators} - - \begin{longtable}[c]{@{}ll@{}} -\toprule -Symbol & Task Performed\tabularnewline -\midrule -\endhead -+ & Addition\tabularnewline -- & Subtraction\tabularnewline -/ & division\tabularnewline -\% & mod\tabularnewline -* & multiplication\tabularnewline -// & floor division\tabularnewline -** & to the power of\tabularnewline -\bottomrule -\end{longtable} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}6}]:} \PY{l+m+mi}{1}\PY{o}{+}\PY{l+m+mi}{2} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}6}]:} 3 -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}7}]:} \PY{l+m+mi}{2}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}7}]:} 1 -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}8}]:} \PY{l+m+mi}{1}\PY{o}{*}\PY{l+m+mi}{2} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}8}]:} 2 -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}9}]:} \PY{l+m+mi}{1}\PY{o}{/}\PY{l+m+mi}{2} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}9}]:} 0 -\end{Verbatim} - - 0? This is because both the numerator and denominator are integers but -the result is a float value hence an integer value is returned. By -changing either the numerator or the denominator to float, correct -answer can be obtained. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}10}]:} \PY{l+m+mi}{1}\PY{o}{/}\PY{l+m+mf}{2.0} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}10}]:} 0.5 -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}11}]:} \PY{l+m+mi}{15}\PY{o}{\PYZpc{}}\PY{k}{10} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}11}]:} 5 -\end{Verbatim} - - Floor division is nothing but converting the result so obtained to the -nearest integer. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}12}]:} \PY{l+m+mf}{2.8}\PY{o}{/}\PY{o}{/}\PY{l+m+mf}{2.0} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}12}]:} 1.0 -\end{Verbatim} - - \subsection{Relational Operators}\label{relational-operators} - - \begin{longtable}[c]{@{}ll@{}} -\toprule -Symbol & Task Performed\tabularnewline -\midrule -\endhead -== & True, if it is equal\tabularnewline -!= & True, if not equal to\tabularnewline -\textless{} & less than\tabularnewline -\textgreater{} & greater than\tabularnewline -\textless{}= & less than or equal to\tabularnewline -\textgreater{}= & greater than or equal to\tabularnewline -\bottomrule -\end{longtable} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}13}]:} \PY{n}{z} \PY{o}{=} \PY{l+m+mi}{1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}14}]:} \PY{n}{z} \PY{o}{==} \PY{l+m+mi}{1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}14}]:} True -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}15}]:} \PY{n}{z} \PY{o}{\PYZgt{}} \PY{l+m+mi}{1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}15}]:} False -\end{Verbatim} - - \subsection{Bitwise Operators}\label{bitwise-operators} - - \begin{longtable}[c]{@{}ll@{}} -\toprule -Symbol & Task Performed\tabularnewline -\midrule -\endhead -\& & Logical And\tabularnewline -l & Logical OR\tabularnewline -\^{} & XOR\tabularnewline -\textasciitilde{} & Negate\tabularnewline -\textgreater{}\textgreater{} & Right shift\tabularnewline -\textless{}\textless{} & Left shift\tabularnewline -\bottomrule -\end{longtable} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}16}]:} \PY{n}{a} \PY{o}{=} \PY{l+m+mi}{2} \PY{c}{\PYZsh{}10} - \PY{n}{b} \PY{o}{=} \PY{l+m+mi}{3} \PY{c}{\PYZsh{}11} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}17}]:} \PY{k}{print} \PY{n}{a} \PY{o}{\PYZam{}} \PY{n}{b} - \PY{k}{print} \PY{n+nb}{bin}\PY{p}{(}\PY{n}{a}\PY{o}{\PYZam{}}\PY{n}{b}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -2 -0b10 - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}18}]:} \PY{l+m+mi}{5} \PY{o}{\PYZgt{}\PYZgt{}} \PY{l+m+mi}{1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}18}]:} 2 -\end{Verbatim} - - 0000 0101 -\textgreater{} 5 - -Shifting the digits by 1 to the right and zero padding - -0000 0010 -\textgreater{} 2 - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}19}]:} \PY{l+m+mi}{5} \PY{o}{\PYZlt{}\PYZlt{}} \PY{l+m+mi}{1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}19}]:} 10 -\end{Verbatim} - - 0000 0101 -\textgreater{} 5 - -Shifting the digits by 1 to the left and zero padding - -0000 1010 -\textgreater{} 10 - - \section{Built-in Functions}\label{built-in-functions} - - Python comes loaded with pre-built functions - - \subsection{Conversion from one system to -another}\label{conversion-from-one-system-to-another} - - Conversion from hexadecimal to decimal is done by adding prefix -\textbf{0x} to the hexadecimal value or vice versa by using built in -\textbf{hex( )}, Octal to decimal by adding prefix \textbf{0} to the -octal value or vice versa by using built in function \textbf{oct( )}. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}20}]:} \PY{n+nb}{hex}\PY{p}{(}\PY{l+m+mi}{170}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}20}]:} '0xaa' -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}21}]:} \PY{l+m+mh}{0xAA} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}21}]:} 170 -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}22}]:} \PY{n+nb}{oct}\PY{p}{(}\PY{l+m+mi}{8}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}22}]:} '010' -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}23}]:} \PY{l+m+mo}{010} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}23}]:} 8 -\end{Verbatim} - - \textbf{int( )} accepts two values when used for conversion, one is the -value in a different number system and the other is its base. Note that -input number in the different number system should be of string type. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}24}]:} \PY{k}{print} \PY{n+nb}{int}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{010}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{int}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{0xaa}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{16}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{int}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{1010}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -8 -170 -10 - \end{Verbatim} - - \textbf{int( )} can also be used to get only the integer value of a -float number or can be used to convert a number which is of type string -to integer format. Similarly, the function \textbf{str( )} can be used -to convert the integer back to string format - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}25}]:} \PY{k}{print} \PY{n+nb}{int}\PY{p}{(}\PY{l+m+mf}{7.7}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{int}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{7}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -7 -7 - \end{Verbatim} - - Also note that function \textbf{bin( )} is used for binary and -\textbf{float( )} for decimal/float values. \textbf{chr( )} is used for -converting ASCII to its alphabet equivalent, \textbf{ord( )} is used for -the other way round. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}26}]:} \PY{n+nb}{chr}\PY{p}{(}\PY{l+m+mi}{98}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}26}]:} 'b' -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}27}]:} \PY{n+nb}{ord}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{b}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}27}]:} 98 -\end{Verbatim} - - \subsection{Simplifying Arithmetic -Operations}\label{simplifying-arithmetic-operations} - - \textbf{round( )} function rounds the input value to a specified number -of places or to the nearest integer. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}28}]:} \PY{k}{print} \PY{n+nb}{round}\PY{p}{(}\PY{l+m+mf}{5.6231}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{round}\PY{p}{(}\PY{l+m+mf}{4.55892}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -6.0 -4.56 - \end{Verbatim} - - \textbf{complex( )} is used to define a complex number and \textbf{abs( -)} outputs the absolute value of the same. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}29}]:} \PY{n}{c} \PY{o}{=}\PY{n+nb}{complex}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{5+2j}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{abs}\PY{p}{(}\PY{n}{c}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -5.38516480713 - \end{Verbatim} - - \textbf{divmod(x,y)} outputs the quotient and the remainder in a -tuple(you will be learning about it in the further chapters) in the -format (quotient, remainder). - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}30}]:} \PY{n+nb}{divmod}\PY{p}{(}\PY{l+m+mi}{9}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}30}]:} (4, 1) -\end{Verbatim} - - \textbf{isinstance( )} returns True, if the first argument is an -instance of that class. Multiple classes can also be checked at once. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}31}]:} \PY{k}{print} \PY{n+nb}{isinstance}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,} \PY{n+nb}{int}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{isinstance}\PY{p}{(}\PY{l+m+mf}{1.0}\PY{p}{,}\PY{n+nb}{int}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{isinstance}\PY{p}{(}\PY{l+m+mf}{1.0}\PY{p}{,}\PY{p}{(}\PY{n+nb}{int}\PY{p}{,}\PY{n+nb}{float}\PY{p}{)}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -True -False -True - \end{Verbatim} - - \textbf{cmp(x,y)} - -\begin{longtable}[c]{@{}ll@{}} -\toprule -x ? y & Output\tabularnewline -\midrule -\endhead -x \textless{} y & -1\tabularnewline -x == y & 0\tabularnewline -x \textgreater{} y & 1\tabularnewline -\bottomrule -\end{longtable} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}32}]:} \PY{k}{print} \PY{n+nb}{cmp}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{cmp}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{cmp}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] --1 -1 -0 - \end{Verbatim} - - \textbf{pow(x,y,z)} can be used to find the power \(x^y\) also the mod -of the resulting value with the third specified number can be found i.e. -: (\(x^y\) \% z). - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}33}]:} \PY{k}{print} \PY{n+nb}{pow}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{pow}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -27 -2 - \end{Verbatim} - - \textbf{range( )} function outputs the integers of the specified range. -It can also be used to generate a series by specifying the difference -between the two numbers within a particular range. The elements are -returned in a list (will be discussing in detail later.) - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}34}]:} \PY{k}{print} \PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{9}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{27}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[0, 1, 2] -[2, 3, 4, 5, 6, 7, 8] -[2, 10, 18, 26] - \end{Verbatim} - - \subsection{Accepting User Inputs}\label{accepting-user-inputs} - - \textbf{raw\_input( )} accepts input and stores it as a string. Hence, -if the user inputs a integer, the code should convert the string to an -integer and then proceed. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}35}]:} \PY{n}{abc} \PY{o}{=} \PY{n+nb}{raw\PYZus{}input}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{Type something here and it will be stored in variable abc }\PY{l+s+se}{\PYZbs{}t}\PY{l+s}{\PYZdq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Type something here and it will be stored in variable abc Hey - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}36}]:} \PY{n+nb}{type}\PY{p}{(}\PY{n}{abc}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}36}]:} str -\end{Verbatim} - - \textbf{input( )}, this is used only for accepting only integer inputs. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}37}]:} \PY{n}{abc1} \PY{o}{=} \PY{n+nb}{input}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{Only integer can be stored in in variable abc }\PY{l+s+se}{\PYZbs{}t}\PY{l+s}{\PYZdq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Only integer can be stored in in variable abc 275 - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}38}]:} \PY{n+nb}{type}\PY{p}{(}\PY{n}{abc1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}38}]:} int -\end{Verbatim} - - Note that \textbf{type( )} returns the format or the type of a variable -or a number - - - % Add a bibliography block to the postdoc - \newpage - - \input{02} - - \end{document} diff --git a/tex/02.tex b/tex/02.tex deleted file mode 100644 index 0096ffd..0000000 --- a/tex/02.tex +++ /dev/null @@ -1,473 +0,0 @@ - -% Default to the notebook output style - - - - -% Inherit from the specified cell style. - - - - - - - - - \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} - \definecolor{darkorange}{rgb}{.71,0.21,0.01} - \definecolor{darkgreen}{rgb}{.12,.54,.11} - \definecolor{myteal}{rgb}{.26, .44, .56} - \definecolor{gray}{gray}{0.45} - \definecolor{lightgray}{gray}{.95} - \definecolor{mediumgray}{gray}{.8} - \definecolor{inputbackground}{rgb}{.95, .95, .85} - \definecolor{outputbackground}{rgb}{.95, .95, .95} - \definecolor{traceback}{rgb}{1, .95, .95} - % ansi colors - \definecolor{red}{rgb}{.6,0,0} - \definecolor{green}{rgb}{0,.65,0} - \definecolor{brown}{rgb}{0.6,0.6,0} - \definecolor{blue}{rgb}{0,.145,.698} - \definecolor{purple}{rgb}{.698,.145,.698} - \definecolor{cyan}{rgb}{0,.698,.698} - \definecolor{lightgray}{gray}{0.5} - - % bright ansi colors - \definecolor{darkgray}{gray}{0.25} - \definecolor{lightred}{rgb}{1.0,0.39,0.28} - \definecolor{lightgreen}{rgb}{0.48,0.99,0.0} - \definecolor{lightblue}{rgb}{0.53,0.81,0.92} - \definecolor{lightpurple}{rgb}{0.87,0.63,0.87} - \definecolor{lightcyan}{rgb}{0.5,1.0,0.83} - - % commands and environments needed by pandoc snippets - % extracted from the output of `pandoc -s` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} - % Add ',fontsize=\small' for more characters per line - \newenvironment{Shaded}{}{} - \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - \newcommand{\RegionMarkerTok}[1]{{#1}} - \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\NormalTok}[1]{{#1}} - - % Define a nice break command that doesn't care if a line doesn't already - % exist. - \def\br{\hspace*{\fill} \\* } - % Math Jax compatability definitions - \def\gt{>} - \def\lt{<} - % Document parameters - \title{} - - - - - % Pygments definitions - -\makeatletter -\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% - \let\PY@ul=\relax \let\PY@tc=\relax% - \let\PY@bc=\relax \let\PY@ff=\relax} -\def\PY@tok#1{\csname PY@tok@#1\endcsname} -\def\PY@toks#1+{\ifx\relax#1\empty\else% - \PY@tok{#1}\expandafter\PY@toks\fi} -\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% - \PY@it{\PY@bf{\PY@ff{#1}}}}}}} -\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} - -\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} -\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} -\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} -\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} -\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} -\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} -\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} -\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} -\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} -\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} -\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} -\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} -\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} - -\def\PYZbs{\char`\\} -\def\PYZus{\char`\_} -\def\PYZob{\char`\{} -\def\PYZcb{\char`\}} -\def\PYZca{\char`\^} -\def\PYZam{\char`\&} -\def\PYZlt{\char`\<} -\def\PYZgt{\char`\>} -\def\PYZsh{\char`\#} -\def\PYZpc{\char`\%} -\def\PYZdl{\char`\$} -\def\PYZhy{\char`\-} -\def\PYZsq{\char`\'} -\def\PYZdq{\char`\"} -\def\PYZti{\char`\~} -% for compatibility with earlier versions -\def\PYZat{@} -\def\PYZlb{[} -\def\PYZrb{]} -\makeatother - - - % Exact colors from NB - \definecolor{incolor}{rgb}{0.0, 0.0, 0.5} - \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0} - - - - - % Prevent overflowing lines due to hard-to-break entities - \sloppy - % Setup hyperref package - \hypersetup{ - breaklinks=true, % so long urls are correctly broken across lines - colorlinks=true, - urlcolor=blue, - linkcolor=darkorange, - citecolor=darkgreen, - } - % Slightly bigger margins than the latex defaults - - - \begin{document} - - - \maketitle - - - - - \section{Print Statement}\label{print-statement} - - The \textbf{print} statement can be used in the following different ways -: - -\begin{verbatim} -- print "Hello World" -- print "Hello", -- print "Hello" + -- print "Hello %s" % -\end{verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}1}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Hello World}\PY{l+s}{\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Hello World - \end{Verbatim} - - In Python, single, double and triple quotes are used to denote a string. -Most use single quotes when declaring a single character. Double quotes -when declaring a line and triple quotes when declaring a -paragraph/multiple lines. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}2}]:} \PY{k}{print} \PY{l+s}{\PYZsq{}}\PY{l+s}{Hey}\PY{l+s}{\PYZsq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Hey - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}3}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}\PYZdq{}\PYZdq{}}\PY{l+s}{My name is Rajath Kumar M.P.} - - \PY{l+s}{I love Python.}\PY{l+s}{\PYZdq{}\PYZdq{}\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -My name is Rajath Kumar M.P. - -I love Python. - \end{Verbatim} - - Strings can be assigned to variable say \emph{string1} and -\emph{string2} which can called when using the print statement. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}4}]:} \PY{n}{string1} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{World}\PY{l+s}{\PYZsq{}} - \PY{k}{print} \PY{l+s}{\PYZsq{}}\PY{l+s}{Hello}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{n}{string1} - - \PY{n}{string2} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{!}\PY{l+s}{\PYZsq{}} - \PY{k}{print} \PY{l+s}{\PYZsq{}}\PY{l+s}{Hello}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{n}{string1}\PY{p}{,} \PY{n}{string2} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Hello World -Hello World ! - \end{Verbatim} - - String concatenation is the ``addition'' of two strings. Observe that -while concatenating there will be no space between the strings. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}5}]:} \PY{k}{print} \PY{l+s}{\PYZsq{}}\PY{l+s}{Hello}\PY{l+s}{\PYZsq{}} \PY{o}{+} \PY{n}{string1} \PY{o}{+} \PY{n}{string2} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -HelloWorld! - \end{Verbatim} - - \textbf{\%s} is used to refer to a variable which contains a string. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}6}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Hello }\PY{l+s+si}{\PYZpc{}s}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}} \PY{n}{string1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Hello World - \end{Verbatim} - - Similarly, when using other data types - -\begin{verbatim} -- %s -> string -- %d -> Integer -- %f -> Float -- %o -> Octal -- %x -> Hexadecimal -- %e -> exponential -\end{verbatim} - -This can be used for conversions inside the print statement itself. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}7}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Actual Number = }\PY{l+s+si}{\PYZpc{}d}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}}\PY{k}{18} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Float of the number = }\PY{l+s+si}{\PYZpc{}f}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}}\PY{k}{18} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Octal equivalent of the number = }\PY{l+s+si}{\PYZpc{}o}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}}\PY{k}{18} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Hexadecimal equivalent of the number = }\PY{l+s+si}{\PYZpc{}x}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}}\PY{k}{18} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Exponential equivalent of the number = }\PY{l+s+si}{\PYZpc{}e}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}}\PY{k}{18} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Actual Number = 18 -Float of the number = 18.000000 -Octal equivalent of the number = 22 -Hexadecimal equivalent of the number = 12 -Exponential equivalent of the number = 1.800000e+01 - \end{Verbatim} - - When referring to multiple variables parenthesis is used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}8}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Hello }\PY{l+s+si}{\PYZpc{}s}\PY{l+s}{ }\PY{l+s+si}{\PYZpc{}s}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}}\PY{p}{(}\PY{n}{string1}\PY{p}{,}\PY{n}{string2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Hello World ! - \end{Verbatim} - - \subsection{Other Examples}\label{other-examples} - - The following are other different ways the print statement can be put to -use. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}9}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{I want }\PY{l+s+si}{\PYZpc{}\PYZpc{}}\PY{l+s}{d to be printed }\PY{l+s+si}{\PYZpc{}s}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}}\PY{l+s}{\PYZsq{}}\PY{l+s}{here}\PY{l+s}{\PYZsq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -I want \%d to be printed here - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}10}]:} \PY{k}{print} \PY{l+s}{\PYZsq{}}\PY{l+s}{\PYZus{}A}\PY{l+s}{\PYZsq{}}\PY{o}{*}\PY{l+m+mi}{10} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -\_A\_A\_A\_A\_A\_A\_A\_A\_A\_A - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}11}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Jan}\PY{l+s+se}{\PYZbs{}n}\PY{l+s}{Feb}\PY{l+s+se}{\PYZbs{}n}\PY{l+s}{Mar}\PY{l+s+se}{\PYZbs{}n}\PY{l+s}{Apr}\PY{l+s+se}{\PYZbs{}n}\PY{l+s}{May}\PY{l+s+se}{\PYZbs{}n}\PY{l+s}{Jun}\PY{l+s+se}{\PYZbs{}n}\PY{l+s}{Jul}\PY{l+s+se}{\PYZbs{}n}\PY{l+s}{Aug}\PY{l+s}{\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Jan -Feb -Mar -Apr -May -Jun -Jul -Aug - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}12}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{I want }\PY{l+s+se}{\PYZbs{}\PYZbs{}}\PY{l+s}{n to be printed.}\PY{l+s}{\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -I want \textbackslash{}n to be printed. - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}13}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}\PYZdq{}\PYZdq{}} - \PY{l+s}{Routine:} - \PY{l+s+se}{\PYZbs{}t}\PY{l+s}{\PYZhy{} Eat} - \PY{l+s+se}{\PYZbs{}t}\PY{l+s}{\PYZhy{} Sleep}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+se}{\PYZbs{}t}\PY{l+s}{\PYZhy{} Repeat} - \PY{l+s}{\PYZdq{}\PYZdq{}\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Routine: - - Eat - - Sleep - - Repeat - \end{Verbatim} - - \section{PrecisionWidth and -FieldWidth}\label{precisionwidth-and-fieldwidth} - - Fieldwidth is the width of the entire number and precision is the width -towards the right. One can alter these widths based on the requirements. - -The default Precision Width is set to 6. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}14}]:} \PY{l+s}{\PYZdq{}}\PY{l+s+si}{\PYZpc{}f}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}} \PY{l+m+mf}{3.121312312312} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}14}]:} '3.121312' -\end{Verbatim} - - Notice upto 6 decimal points are returned. To specify the number of -decimal points, `\%(fieldwidth).(precisionwidth)f' is used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}15}]:} \PY{l+s}{\PYZdq{}}\PY{l+s+si}{\PYZpc{}.5f}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}} \PY{l+m+mf}{3.121312312312} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}15}]:} '3.12131' -\end{Verbatim} - - If the field width is set more than the necessary than the data right -aligns itself to adjust to the specified values. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}16}]:} \PY{l+s}{\PYZdq{}}\PY{l+s+si}{\PYZpc{}9.5f}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}} \PY{l+m+mf}{3.121312312312} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}16}]:} ' 3.12131' -\end{Verbatim} - - Zero padding is done by adding a 0 at the start of fieldwidth. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}17}]:} \PY{l+s}{\PYZdq{}}\PY{l+s+si}{\PYZpc{}020.5f}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}} \PY{l+m+mf}{3.121312312312} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}17}]:} '00000000000003.12131' -\end{Verbatim} - - For proper alignment, a space can be left blank in the field width so -that when a negative number is used, proper alignment is maintained. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}18}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s+si}{\PYZpc{} 9f}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}} \PY{l+m+mf}{3.121312312312} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s+si}{\PYZpc{} 9f}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}} \PY{o}{\PYZhy{}}\PY{l+m+mf}{3.121312312312} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -3.121312 --3.121312 - \end{Verbatim} - - `+' sign can be returned at the beginning of a positive number by adding -a + sign at the beginning of the field width. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}19}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s+si}{\PYZpc{}+9f}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}} \PY{l+m+mf}{3.121312312312} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s+si}{\PYZpc{} 9f}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}} \PY{o}{\PYZhy{}}\PY{l+m+mf}{3.121312312312} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -+3.121312 --3.121312 - \end{Verbatim} - - As mentioned above, the data right aligns itself when the field width -mentioned is larger than the actualy field width. But left alignment can -be done by specifying a negative symbol in the field width. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}20}]:} \PY{l+s}{\PYZdq{}}\PY{l+s+si}{\PYZpc{}\PYZhy{}9.3f}\PY{l+s}{\PYZdq{}} \PY{o}{\PYZpc{}} \PY{l+m+mf}{3.121312312312} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}20}]:} '3.121 ' -\end{Verbatim} - - - % Add a bibliography block to the postdoc - \newpage - - \input{03} - - - \end{document} diff --git a/tex/03.tex b/tex/03.tex deleted file mode 100644 index 310ef16..0000000 --- a/tex/03.tex +++ /dev/null @@ -1,1102 +0,0 @@ - -% Default to the notebook output style - - - - -% Inherit from the specified cell style. - - - - - - - - - \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} - \definecolor{darkorange}{rgb}{.71,0.21,0.01} - \definecolor{darkgreen}{rgb}{.12,.54,.11} - \definecolor{myteal}{rgb}{.26, .44, .56} - \definecolor{gray}{gray}{0.45} - \definecolor{lightgray}{gray}{.95} - \definecolor{mediumgray}{gray}{.8} - \definecolor{inputbackground}{rgb}{.95, .95, .85} - \definecolor{outputbackground}{rgb}{.95, .95, .95} - \definecolor{traceback}{rgb}{1, .95, .95} - % ansi colors - \definecolor{red}{rgb}{.6,0,0} - \definecolor{green}{rgb}{0,.65,0} - \definecolor{brown}{rgb}{0.6,0.6,0} - \definecolor{blue}{rgb}{0,.145,.698} - \definecolor{purple}{rgb}{.698,.145,.698} - \definecolor{cyan}{rgb}{0,.698,.698} - \definecolor{lightgray}{gray}{0.5} - - % bright ansi colors - \definecolor{darkgray}{gray}{0.25} - \definecolor{lightred}{rgb}{1.0,0.39,0.28} - \definecolor{lightgreen}{rgb}{0.48,0.99,0.0} - \definecolor{lightblue}{rgb}{0.53,0.81,0.92} - \definecolor{lightpurple}{rgb}{0.87,0.63,0.87} - \definecolor{lightcyan}{rgb}{0.5,1.0,0.83} - - % commands and environments needed by pandoc snippets - % extracted from the output of `pandoc -s` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} - % Add ',fontsize=\small' for more characters per line - \newenvironment{Shaded}{}{} - \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - \newcommand{\RegionMarkerTok}[1]{{#1}} - \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\NormalTok}[1]{{#1}} - - % Define a nice break command that doesn't care if a line doesn't already - % exist. - \def\br{\hspace*{\fill} \\* } - % Math Jax compatability definitions - \def\gt{>} - \def\lt{<} - % Document parameters - \title{} - - - - - % Pygments definitions - -\makeatletter -\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% - \let\PY@ul=\relax \let\PY@tc=\relax% - \let\PY@bc=\relax \let\PY@ff=\relax} -\def\PY@tok#1{\csname PY@tok@#1\endcsname} -\def\PY@toks#1+{\ifx\relax#1\empty\else% - \PY@tok{#1}\expandafter\PY@toks\fi} -\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% - \PY@it{\PY@bf{\PY@ff{#1}}}}}}} -\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} - -\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} -\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} -\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} -\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} -\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} -\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} -\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} -\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} -\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} -\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} -\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} -\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} -\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} - -\def\PYZbs{\char`\\} -\def\PYZus{\char`\_} -\def\PYZob{\char`\{} -\def\PYZcb{\char`\}} -\def\PYZca{\char`\^} -\def\PYZam{\char`\&} -\def\PYZlt{\char`\<} -\def\PYZgt{\char`\>} -\def\PYZsh{\char`\#} -\def\PYZpc{\char`\%} -\def\PYZdl{\char`\$} -\def\PYZhy{\char`\-} -\def\PYZsq{\char`\'} -\def\PYZdq{\char`\"} -\def\PYZti{\char`\~} -% for compatibility with earlier versions -\def\PYZat{@} -\def\PYZlb{[} -\def\PYZrb{]} -\makeatother - - - % Exact colors from NB - \definecolor{incolor}{rgb}{0.0, 0.0, 0.5} - \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0} - - - - - % Prevent overflowing lines due to hard-to-break entities - \sloppy - % Setup hyperref package - \hypersetup{ - breaklinks=true, % so long urls are correctly broken across lines - colorlinks=true, - urlcolor=blue, - linkcolor=darkorange, - citecolor=darkgreen, - } - % Slightly bigger margins than the latex defaults - - - - - - \begin{document} - - - \maketitle - - - - - \section{Data Structures}\label{data-structures} - - In simple terms, It is the the collection or group of data in a -particular structure. - - \subsection{Lists}\label{lists} - - Lists are the most commonly used data structure. Think of it as a -sequence of data that is enclosed in square brackets and data are -separated by a comma. Each of these data can be accessed by calling it's -index value. - -Lists are declared by just equating a variable to `{[} {]}' or list. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}1}]:} \PY{n}{a} \PY{o}{=} \PY{p}{[}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}2}]:} \PY{k}{print} \PY{n+nb}{type}\PY{p}{(}\PY{n}{a}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] - - \end{Verbatim} - - One can directly assign the sequence of data to a list x as shown. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}3}]:} \PY{n}{x} \PY{o}{=} \PY{p}{[}\PY{l+s}{\PYZsq{}}\PY{l+s}{apple}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{orange}\PY{l+s}{\PYZsq{}}\PY{p}{]} -\end{Verbatim} - - \subsubsection{Indexing}\label{indexing} - - In python, Indexing starts from 0. Thus now the list x, which has two -elements will have apple at 0 index and orange at 1 index. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}4}]:} \PY{n}{x}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}4}]:} 'apple' -\end{Verbatim} - - Indexing can also be done in reverse order. That is the last element can -be accessed first. Here, indexing starts from -1. Thus index value -1 -will be orange and index -2 will be apple. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}5}]:} \PY{n}{x}\PY{p}{[}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}5}]:} 'orange' -\end{Verbatim} - - As you might have already guessed, x{[}0{]} = x{[}-2{]}, x{[}1{]} = -x{[}-1{]}. This concept can be extended towards lists with more many -elements. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}6}]:} \PY{n}{y} \PY{o}{=} \PY{p}{[}\PY{l+s}{\PYZsq{}}\PY{l+s}{carrot}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{potato}\PY{l+s}{\PYZsq{}}\PY{p}{]} -\end{Verbatim} - - Here we have declared two lists x and y each containing its own data. -Now, these two lists can again be put into another list say z which will -have it's data as two lists. This list inside a list is called as nested -lists and is how an array would be declared which we will see later. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}7}]:} \PY{n}{z} \PY{o}{=} \PY{p}{[}\PY{n}{x}\PY{p}{,}\PY{n}{y}\PY{p}{]} - \PY{k}{print} \PY{n}{z} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[['apple', 'orange'], ['carrot', 'potato']] - \end{Verbatim} - - Indexing in nested lists can be quite confusing if you do not understand -how indexing works in python. So let us break it down and then arrive at -a conclusion. - -Let us access the data `apple' in the above nested list. First, at index -0 there is a list {[}`apple',`orange'{]} and at index 1 there is another -list {[}`carrot',`potato'{]}. Hence z{[}0{]} should give us the first -list which contains `apple'. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}8}]:} \PY{n}{z1} \PY{o}{=} \PY{n}{z}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} - \PY{k}{print} \PY{n}{z1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -['apple', 'orange'] - \end{Verbatim} - - Now observe that z1 is not at all a nested list thus to access `apple', -z1 should be indexed at 0. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}9}]:} \PY{n}{z1}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}9}]:} 'apple' -\end{Verbatim} - - Instead of doing the above, In python, you can access `apple' by just -writing the index values each time side by side. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}10}]:} \PY{n}{z}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}10}]:} 'apple' -\end{Verbatim} - - If there was a list inside a list inside a list then you can access the -innermost value by executing z{[} {]}{[} {]}{[} {]}. - - \subsubsection{Slicing}\label{slicing} - - Indexing was only limited to accessing a single element, Slicing on the -other hand is accessing a sequence of data inside the list. In other -words ``slicing'' the list. - -Slicing is done by defining the index values of the first element and -the last element from the parent list that is required in the sliced -list. It is written as parentlist{[} a : b {]} where a,b are the index -values from the parent list. If a or b is not defined then the index -value is considered to be the first value for a if a is not defined and -the last value for b when b is not defined. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}11}]:} \PY{n}{num} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{,}\PY{l+m+mi}{9}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}12}]:} \PY{k}{print} \PY{n}{num}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{:}\PY{l+m+mi}{4}\PY{p}{]} - \PY{k}{print} \PY{n}{num}\PY{p}{[}\PY{l+m+mi}{4}\PY{p}{:}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[0, 1, 2, 3] -[4, 5, 6, 7, 8, 9] - \end{Verbatim} - - You can also slice a parent list with a fixed length or step length. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}13}]:} \PY{n}{num}\PY{p}{[}\PY{p}{:}\PY{l+m+mi}{9}\PY{p}{:}\PY{l+m+mi}{3}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}13}]:} [0, 3, 6] -\end{Verbatim} - - \subsubsection{Built in List Functions}\label{built-in-list-functions} - - To find the length of the list or the number of elements in a list, -\textbf{len( )} is used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}14}]:} \PY{n+nb}{len}\PY{p}{(}\PY{n}{num}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}14}]:} 10 -\end{Verbatim} - - If the list consists of all integer elements then \textbf{min( )} and -\textbf{max( )} gives the minimum and maximum value in the list. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}15}]:} \PY{n+nb}{min}\PY{p}{(}\PY{n}{num}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}15}]:} 0 -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}16}]:} \PY{n+nb}{max}\PY{p}{(}\PY{n}{num}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}16}]:} 9 -\end{Verbatim} - - Lists can be concatenated by adding, `+' them. The resultant list will -contain all the elements of the lists that were added. The resultant -list will not be a nested list. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}17}]:} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{]} \PY{o}{+} \PY{p}{[}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}17}]:} [1, 2, 3, 5, 4, 7] -\end{Verbatim} - - There might arise a requirement where you might need to check if a -particular element is there in a predefined list. Consider the below -list. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}18}]:} \PY{n}{names} \PY{o}{=} \PY{p}{[}\PY{l+s}{\PYZsq{}}\PY{l+s}{Earth}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{Air}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{Fire}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{Water}\PY{l+s}{\PYZsq{}}\PY{p}{]} -\end{Verbatim} - - To check if `Fire' and `Rajath' is present in the list names. A -conventional approach would be to use a for loop and iterate over the -list and use the if condition. But in python you can use `a in b' -concept which would return `True' if a is present in b and `False' if -not. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}19}]:} \PY{l+s}{\PYZsq{}}\PY{l+s}{Fire}\PY{l+s}{\PYZsq{}} \PY{o+ow}{in} \PY{n}{names} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}19}]:} True -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}20}]:} \PY{l+s}{\PYZsq{}}\PY{l+s}{Rajath}\PY{l+s}{\PYZsq{}} \PY{o+ow}{in} \PY{n}{names} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}20}]:} False -\end{Verbatim} - - In a list with elements as string, \textbf{max( )} and \textbf{min( )} -is applicable. \textbf{max( )} would return a string element whose ASCII -value is the highest and the lowest when \textbf{min( )} is used. Note -that only the first index of each element is considered each time and if -they value is the same then second index considered so on and so forth. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}21}]:} \PY{n}{mlist} \PY{o}{=} \PY{p}{[}\PY{l+s}{\PYZsq{}}\PY{l+s}{bzaa}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{ds}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{nc}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{az}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{z}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{klm}\PY{l+s}{\PYZsq{}}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}22}]:} \PY{k}{print} \PY{n+nb}{max}\PY{p}{(}\PY{n}{mlist}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{min}\PY{p}{(}\PY{n}{mlist}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -z -az - \end{Verbatim} - - Here the first index of each element is considered and thus z has the -highest ASCII value thus it is returned and minimum ASCII is a. But what -if numbers are declared as strings? - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}23}]:} \PY{n}{nlist} \PY{o}{=} \PY{p}{[}\PY{l+s}{\PYZsq{}}\PY{l+s}{1}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{94}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{93}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{1000}\PY{l+s}{\PYZsq{}}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}24}]:} \PY{k}{print} \PY{n+nb}{max}\PY{p}{(}\PY{n}{nlist}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{min}\PY{p}{(}\PY{n}{nlist}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -94 -1 - \end{Verbatim} - - Even if the numbers are declared in a string the first index of each -element is considered and the maximum and minimum values are returned -accordingly. - - But if you want to find the \textbf{max( )} string element based on the -length of the string then another parameter `key=len' is declared inside -the \textbf{max( )} and \textbf{min( )} function. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}25}]:} \PY{k}{print} \PY{n+nb}{max}\PY{p}{(}\PY{n}{names}\PY{p}{,} \PY{n}{key}\PY{o}{=}\PY{n+nb}{len}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{min}\PY{p}{(}\PY{n}{names}\PY{p}{,} \PY{n}{key}\PY{o}{=}\PY{n+nb}{len}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Earth -Air - \end{Verbatim} - - But even `Water' has length 5. \textbf{max()} or \textbf{min()} function -returns the first element when there are two or more elements with the -same length. - -Any other built in function can be used or lambda function (will be -discussed later) in place of len. - -A string can be converted into a list by using the \textbf{list()} -function. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}26}]:} \PY{n+nb}{list}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{hello}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}26}]:} ['h', 'e', 'l', 'l', 'o'] -\end{Verbatim} - - \textbf{append( )} is used to add a element at the end of the list. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}27}]:} \PY{n}{lst} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}28}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{)} - \PY{k}{print} \PY{n}{lst} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 1, 4, 8, 7, 1] - \end{Verbatim} - - \textbf{count( )} is used to count the number of a particular element -that is present in the list. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}29}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{count}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}29}]:} 3 -\end{Verbatim} - - \textbf{append( )} function can also be used to add a entire list at the -end. Observe that the resultant list becomes a nested list. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}30}]:} \PY{n}{lst1} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}31}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{lst1}\PY{p}{)} - \PY{k}{print} \PY{n}{lst} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 1, 4, 8, 7, 1, [5, 4, 2, 8]] - \end{Verbatim} - - But if nested list is not what is desired then \textbf{extend( )} -function can be used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}32}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{extend}\PY{p}{(}\PY{n}{lst1}\PY{p}{)} - \PY{k}{print} \PY{n}{lst} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2, 8] - \end{Verbatim} - - \textbf{index( )} is used to find the index value of a particular -element. Note that if there are multiple elements of the same value then -the first index value of that element is returned. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}33}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{index}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}33}]:} 0 -\end{Verbatim} - - \textbf{insert(x,y)} is used to insert a element y at a specified index -value x. \textbf{append( )} function made it only possible to insert at -the end. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}34}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{insert}\PY{p}{(}\PY{l+m+mi}{5}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{name}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n}{lst} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4, 2, 8] - \end{Verbatim} - - \textbf{insert(x,y)} inserts but does not replace element. If you want -to replace the element with another element you simply assign the value -to that particular index. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}35}]:} \PY{n}{lst}\PY{p}{[}\PY{l+m+mi}{5}\PY{p}{]} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{Python}\PY{l+s}{\PYZsq{}} - \PY{k}{print} \PY{n}{lst} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2, 8] - \end{Verbatim} - - \textbf{pop( )} function return the last element in the list. This is -similar to the operation of a stack. Hence it wouldn't be wrong to tell -that lists can be used as a stack. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}36}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{pop}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}36}]:} 8 -\end{Verbatim} - - Index value can be specified to pop a ceratin element corresponding to -that index value. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}37}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{pop}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}37}]:} 1 -\end{Verbatim} - - \textbf{pop( )} is used to remove element based on it's index value -which can be assigned to a variable. One can also remove element by -specifying the element itself using the \textbf{remove( )} function. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}38}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{remove}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Python}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n}{lst} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2] - \end{Verbatim} - - Alternative to \textbf{remove} function but with using index value is -\textbf{del} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}39}]:} \PY{k}{del} \PY{n}{lst}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]} - \PY{k}{print} \PY{n}{lst} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2] - \end{Verbatim} - - The entire elements present in the list can be reversed by using the -\textbf{reverse()} function. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}40}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{reverse}\PY{p}{(}\PY{p}{)} - \PY{k}{print} \PY{n}{lst} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[2, 4, 5, [5, 4, 2, 8], 1, 7, 8, 1] - \end{Verbatim} - - Note that the nested list {[}5,4,2,8{]} is treated as a single element -of the parent list lst. Thus the elements inside the nested list is not -reversed. - -Python offers built in operation \textbf{sort( )} to arrange the -elements in ascending order. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}41}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{sort}\PY{p}{(}\PY{p}{)} - \PY{k}{print} \PY{n}{lst} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 1, 2, 4, 5, 7, 8, [5, 4, 2, 8]] - \end{Verbatim} - - For descending order, By default the reverse condition will be False for -reverse. Hence changing it to True would arrange the elements in -descending order. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}42}]:} \PY{n}{lst}\PY{o}{.}\PY{n}{sort}\PY{p}{(}\PY{n}{reverse}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)} - \PY{k}{print} \PY{n}{lst} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[[5, 4, 2, 8], 8, 7, 5, 4, 2, 1, 1] - \end{Verbatim} - - Similarly for lists containing string elements, \textbf{sort( )} would -sort the elements based on it's ASCII value in ascending and by -specifying reverse=True in descending. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}43}]:} \PY{n}{names}\PY{o}{.}\PY{n}{sort}\PY{p}{(}\PY{p}{)} - \PY{k}{print} \PY{n}{names} - \PY{n}{names}\PY{o}{.}\PY{n}{sort}\PY{p}{(}\PY{n}{reverse}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)} - \PY{k}{print} \PY{n}{names} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -['Air', 'Earth', 'Fire', 'Water'] -['Water', 'Fire', 'Earth', 'Air'] - \end{Verbatim} - - To sort based on length key=len should be specified as shown. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}44}]:} \PY{n}{names}\PY{o}{.}\PY{n}{sort}\PY{p}{(}\PY{n}{key}\PY{o}{=}\PY{n+nb}{len}\PY{p}{)} - \PY{k}{print} \PY{n}{names} - \PY{n}{names}\PY{o}{.}\PY{n}{sort}\PY{p}{(}\PY{n}{key}\PY{o}{=}\PY{n+nb}{len}\PY{p}{,}\PY{n}{reverse}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)} - \PY{k}{print} \PY{n}{names} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -['Air', 'Fire', 'Water', 'Earth'] -['Water', 'Earth', 'Fire', 'Air'] - \end{Verbatim} - - \subsubsection{Copying a list}\label{copying-a-list} - - Most of the new python programmers commit this mistake. Consider the -following, - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}45}]:} \PY{n}{lista}\PY{o}{=} \PY{p}{[}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}46}]:} \PY{n}{listb} \PY{o}{=} \PY{n}{lista} - \PY{k}{print} \PY{n}{listb} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[2, 1, 4, 3] - \end{Verbatim} - - Here, We have declared a list, lista = {[}2,1,4,3{]}. This list is -copied to listb by assigning it's value and it get's copied as seen. Now -we perform some random operations on lista. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}47}]:} \PY{n}{lista}\PY{o}{.}\PY{n}{pop}\PY{p}{(}\PY{p}{)} - \PY{k}{print} \PY{n}{lista} - \PY{n}{lista}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{l+m+mi}{9}\PY{p}{)} - \PY{k}{print} \PY{n}{lista} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[2, 1, 4] -[2, 1, 4, 9] - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}48}]:} \PY{k}{print} \PY{n}{listb} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[2, 1, 4, 9] - \end{Verbatim} - - listb has also changed though no operation has been performed on it. -This is because you have assigned the same memory space of lista to -listb. So how do fix this? - -If you recall, in slicing we had seen that parentlist{[}a:b{]} returns a -list from parent list with start index a and end index b and if a and b -is not mentioned then by default it considers the first and last -element. We use the same concept here. By doing so, we are assigning the -data of lista to listb as a variable. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}49}]:} \PY{n}{lista} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}50}]:} \PY{n}{listb} \PY{o}{=} \PY{n}{lista}\PY{p}{[}\PY{p}{:}\PY{p}{]} - \PY{k}{print} \PY{n}{listb} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[2, 1, 4, 3] - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}51}]:} \PY{n}{lista}\PY{o}{.}\PY{n}{pop}\PY{p}{(}\PY{p}{)} - \PY{k}{print} \PY{n}{lista} - \PY{n}{lista}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{l+m+mi}{9}\PY{p}{)} - \PY{k}{print} \PY{n}{lista} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[2, 1, 4] -[2, 1, 4, 9] - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}52}]:} \PY{k}{print} \PY{n}{listb} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[2, 1, 4, 3] - \end{Verbatim} - - \subsection{Tuples}\label{tuples} - - Tuples are similar to lists but only big difference is the elements -inside a list can be changed but in tuple it cannot be changed. Think of -tuples as something which has to be True for a particular something and -cannot be True for no other values. For better understanding, Recall -\textbf{divmod()} function. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}53}]:} \PY{n}{xyz} \PY{o}{=} \PY{n+nb}{divmod}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)} - \PY{k}{print} \PY{n}{xyz} - \PY{k}{print} \PY{n+nb}{type}\PY{p}{(}\PY{n}{xyz}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -(3, 1) - - \end{Verbatim} - - Here the quotient has to be 3 and the remainder has to be 1. These -values cannot be changed whatsoever when 10 is divided by 3. Hence -divmod returns these values in a tuple. - - To define a tuple, A variable is assigned to paranthesis ( ) or tuple( -). - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}54}]:} \PY{n}{tup} \PY{o}{=} \PY{p}{(}\PY{p}{)} - \PY{n}{tup2} \PY{o}{=} \PY{n+nb}{tuple}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - If you want to directly declare a tuple it can be done by using a comma -at the end of the data. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}55}]:} \PY{l+m+mi}{27}\PY{p}{,} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}55}]:} (27,) -\end{Verbatim} - - 27 when multiplied by 2 yields 54, But when multiplied with a tuple the -data is repeated twice. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}56}]:} \PY{l+m+mi}{2}\PY{o}{*}\PY{p}{(}\PY{l+m+mi}{27}\PY{p}{,}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}56}]:} (27, 27) -\end{Verbatim} - - Values can be assigned while declaring a tuple. It takes a list as input -and converts it into a tuple or it takes a string and converts it into a -tuple. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}57}]:} \PY{n}{tup3} \PY{o}{=} \PY{n+nb}{tuple}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{]}\PY{p}{)} - \PY{k}{print} \PY{n}{tup3} - \PY{n}{tup4} \PY{o}{=} \PY{n+nb}{tuple}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Hello}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n}{tup4} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -(1, 2, 3) -('H', 'e', 'l', 'l', 'o') - \end{Verbatim} - - It follows the same indexing and slicing as Lists. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}58}]:} \PY{k}{print} \PY{n}{tup3}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]} - \PY{n}{tup5} \PY{o}{=} \PY{n}{tup4}\PY{p}{[}\PY{p}{:}\PY{l+m+mi}{3}\PY{p}{]} - \PY{k}{print} \PY{n}{tup5} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -2 -('H', 'e', 'l') - \end{Verbatim} - - \subsubsection{Mapping one tuple to -another}\label{mapping-one-tuple-to-another} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}59}]:} \PY{p}{(}\PY{n}{a}\PY{p}{,}\PY{n}{b}\PY{p}{,}\PY{n}{c}\PY{p}{)}\PY{o}{=} \PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{alpha}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{beta}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{gamma}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}60}]:} \PY{k}{print} \PY{n}{a}\PY{p}{,}\PY{n}{b}\PY{p}{,}\PY{n}{c} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -alpha beta gamma - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}61}]:} \PY{n}{d} \PY{o}{=} \PY{n+nb}{tuple}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{RajathKumarMP}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n}{d} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -('R', 'a', 'j', 'a', 't', 'h', 'K', 'u', 'm', 'a', 'r', 'M', 'P') - \end{Verbatim} - - \subsubsection{Built In Tuple functions}\label{built-in-tuple-functions} - - \textbf{count()} function counts the number of specified element that is -present in the tuple. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}62}]:} \PY{n}{d}\PY{o}{.}\PY{n}{count}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{a}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}62}]:} 3 -\end{Verbatim} - - \textbf{index()} function returns the index of the specified element. If -the elements are more than one then the index of the first element of -that specified element is returned - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}63}]:} \PY{n}{d}\PY{o}{.}\PY{n}{index}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{a}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}63}]:} 1 -\end{Verbatim} - - \subsection{Sets}\label{sets} - - Sets are mainly used to eliminate repeated numbers in a sequence/list. -It is also used to perform some standard set operations. - -Sets are declared as set() which will initialize a empty set. Also -set({[}sequence{]}) can be executed to declare a set with elements - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}64}]:} \PY{n}{set1} \PY{o}{=} \PY{n+nb}{set}\PY{p}{(}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{type}\PY{p}{(}\PY{n}{set1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] - - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}65}]:} \PY{n}{set0} \PY{o}{=} \PY{n+nb}{set}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{]}\PY{p}{)} - \PY{k}{print} \PY{n}{set0} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -set([1, 2, 3, 4]) - \end{Verbatim} - - elements 2,3 which are repeated twice are seen only once. Thus in a set -each element is distinct. - - \subsubsection{Built-in Functions}\label{built-in-functions} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}66}]:} \PY{n}{set1} \PY{o}{=} \PY{n+nb}{set}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{]}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}67}]:} \PY{n}{set2} \PY{o}{=} \PY{n+nb}{set}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{]}\PY{p}{)} -\end{Verbatim} - - \textbf{union( )} function returns a set which contains all the elements -of both the sets without repition. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}68}]:} \PY{n}{set1}\PY{o}{.}\PY{n}{union}\PY{p}{(}\PY{n}{set2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}68}]:} \{1, 2, 3, 4, 5\} -\end{Verbatim} - - \textbf{add( )} will add a particular element into the set. Note that -the index of the newly added element is arbitrary and can be placed -anywhere not neccessarily in the end. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}69}]:} \PY{n}{set1}\PY{o}{.}\PY{n}{add}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{)} - \PY{n}{set1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}69}]:} \{0, 1, 2, 3\} -\end{Verbatim} - - \textbf{intersection( )} function outputs a set which contains all the -elements that are in both sets. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}70}]:} \PY{n}{set1}\PY{o}{.}\PY{n}{intersection}\PY{p}{(}\PY{n}{set2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}70}]:} \{2, 3\} -\end{Verbatim} - - \textbf{difference( )} function ouptuts a set which contains elements -that are in set1 and not in set2. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}71}]:} \PY{n}{set1}\PY{o}{.}\PY{n}{difference}\PY{p}{(}\PY{n}{set2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}71}]:} \{0, 1\} -\end{Verbatim} - - \textbf{symmetric\_difference( )} function ouputs a function which -contains elements that are in one of the sets. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}72}]:} \PY{n}{set2}\PY{o}{.}\PY{n}{symmetric\PYZus{}difference}\PY{p}{(}\PY{n}{set1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}72}]:} \{0, 1, 4, 5\} -\end{Verbatim} - - \textbf{issubset( ), isdisjoint( ), issuperset( )} is used to check if -the set1/set2 is a subset, disjoint or superset of set2/set1 -respectively. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}73}]:} \PY{n}{set1}\PY{o}{.}\PY{n}{issubset}\PY{p}{(}\PY{n}{set2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}73}]:} False -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}74}]:} \PY{n}{set2}\PY{o}{.}\PY{n}{isdisjoint}\PY{p}{(}\PY{n}{set1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}74}]:} False -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}75}]:} \PY{n}{set2}\PY{o}{.}\PY{n}{issuperset}\PY{p}{(}\PY{n}{set1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}75}]:} False -\end{Verbatim} - - \textbf{pop( )} is used to remove an arbitrary element in the set - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}76}]:} \PY{n}{set1}\PY{o}{.}\PY{n}{pop}\PY{p}{(}\PY{p}{)} - \PY{k}{print} \PY{n}{set1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -set([1, 2, 3]) - \end{Verbatim} - - \textbf{remove( )} function deletes the specified element from the set. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}77}]:} \PY{n}{set1}\PY{o}{.}\PY{n}{remove}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{)} - \PY{n}{set1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}77}]:} \{1, 3\} -\end{Verbatim} - - \textbf{clear( )} is used to clear all the elements and make that set an -empty set. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}78}]:} \PY{n}{set1}\PY{o}{.}\PY{n}{clear}\PY{p}{(}\PY{p}{)} - \PY{n}{set1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}78}]:} set() -\end{Verbatim} - - - % Add a bibliography block to the postdoc - \newpage - - \input{04} - - - \end{document} diff --git a/tex/04.tex b/tex/04.tex deleted file mode 100644 index 1f0fc89..0000000 --- a/tex/04.tex +++ /dev/null @@ -1,731 +0,0 @@ - -% Default to the notebook output style - - - - -% Inherit from the specified cell style. - - - - - - - - \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} - \definecolor{darkorange}{rgb}{.71,0.21,0.01} - \definecolor{darkgreen}{rgb}{.12,.54,.11} - \definecolor{myteal}{rgb}{.26, .44, .56} - \definecolor{gray}{gray}{0.45} - \definecolor{lightgray}{gray}{.95} - \definecolor{mediumgray}{gray}{.8} - \definecolor{inputbackground}{rgb}{.95, .95, .85} - \definecolor{outputbackground}{rgb}{.95, .95, .95} - \definecolor{traceback}{rgb}{1, .95, .95} - % ansi colors - \definecolor{red}{rgb}{.6,0,0} - \definecolor{green}{rgb}{0,.65,0} - \definecolor{brown}{rgb}{0.6,0.6,0} - \definecolor{blue}{rgb}{0,.145,.698} - \definecolor{purple}{rgb}{.698,.145,.698} - \definecolor{cyan}{rgb}{0,.698,.698} - \definecolor{lightgray}{gray}{0.5} - - % bright ansi colors - \definecolor{darkgray}{gray}{0.25} - \definecolor{lightred}{rgb}{1.0,0.39,0.28} - \definecolor{lightgreen}{rgb}{0.48,0.99,0.0} - \definecolor{lightblue}{rgb}{0.53,0.81,0.92} - \definecolor{lightpurple}{rgb}{0.87,0.63,0.87} - \definecolor{lightcyan}{rgb}{0.5,1.0,0.83} - - % commands and environments needed by pandoc snippets - % extracted from the output of `pandoc -s` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} - % Add ',fontsize=\small' for more characters per line - \newenvironment{Shaded}{}{} - \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - \newcommand{\RegionMarkerTok}[1]{{#1}} - \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\NormalTok}[1]{{#1}} - - % Define a nice break command that doesn't care if a line doesn't already - % exist. - \def\br{\hspace*{\fill} \\* } - % Math Jax compatability definitions - \def\gt{>} - \def\lt{<} - % Document parameters - \title{} - - - - - % Pygments definitions - -\makeatletter -\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% - \let\PY@ul=\relax \let\PY@tc=\relax% - \let\PY@bc=\relax \let\PY@ff=\relax} -\def\PY@tok#1{\csname PY@tok@#1\endcsname} -\def\PY@toks#1+{\ifx\relax#1\empty\else% - \PY@tok{#1}\expandafter\PY@toks\fi} -\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% - \PY@it{\PY@bf{\PY@ff{#1}}}}}}} -\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} - -\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} -\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} -\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} -\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} -\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} -\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} -\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} -\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} -\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} -\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} -\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} -\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} -\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} - -\def\PYZbs{\char`\\} -\def\PYZus{\char`\_} -\def\PYZob{\char`\{} -\def\PYZcb{\char`\}} -\def\PYZca{\char`\^} -\def\PYZam{\char`\&} -\def\PYZlt{\char`\<} -\def\PYZgt{\char`\>} -\def\PYZsh{\char`\#} -\def\PYZpc{\char`\%} -\def\PYZdl{\char`\$} -\def\PYZhy{\char`\-} -\def\PYZsq{\char`\'} -\def\PYZdq{\char`\"} -\def\PYZti{\char`\~} -% for compatibility with earlier versions -\def\PYZat{@} -\def\PYZlb{[} -\def\PYZrb{]} -\makeatother - - - % Exact colors from NB - \definecolor{incolor}{rgb}{0.0, 0.0, 0.5} - \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0} - - - - - % Prevent overflowing lines due to hard-to-break entities - \sloppy - % Setup hyperref package - \hypersetup{ - breaklinks=true, % so long urls are correctly broken across lines - colorlinks=true, - urlcolor=blue, - linkcolor=darkorange, - citecolor=darkgreen, - } - % Slightly bigger margins than the latex defaults - - - - \begin{document} - - - \maketitle - - - - - \subsection{Strings}\label{strings} - - Strings are ordered text based data which are represented by enclosing -the same in single/double/triple quotes. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}1}]:} \PY{n}{String0} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{Taj Mahal is beautiful}\PY{l+s}{\PYZsq{}} - \PY{n}{String1} \PY{o}{=} \PY{l+s}{\PYZdq{}}\PY{l+s}{Taj Mahal is beautiful}\PY{l+s}{\PYZdq{}} - \PY{n}{String2} \PY{o}{=} \PY{l+s}{\PYZsq{}\PYZsq{}\PYZsq{}}\PY{l+s}{Taj Mahal} - \PY{l+s}{is} - \PY{l+s}{beautiful}\PY{l+s}{\PYZsq{}\PYZsq{}\PYZsq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}2}]:} \PY{k}{print} \PY{n}{String0} \PY{p}{,} \PY{n+nb}{type}\PY{p}{(}\PY{n}{String0}\PY{p}{)} - \PY{k}{print} \PY{n}{String1}\PY{p}{,} \PY{n+nb}{type}\PY{p}{(}\PY{n}{String1}\PY{p}{)} - \PY{k}{print} \PY{n}{String2}\PY{p}{,} \PY{n+nb}{type}\PY{p}{(}\PY{n}{String2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Taj Mahal is beautiful -Taj Mahal is beautiful -Taj Mahal -is -beautiful - \end{Verbatim} - - String Indexing and Slicing are similar to Lists which was explained in -detail earlier. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}3}]:} \PY{k}{print} \PY{n}{String0}\PY{p}{[}\PY{l+m+mi}{4}\PY{p}{]} - \PY{k}{print} \PY{n}{String0}\PY{p}{[}\PY{l+m+mi}{4}\PY{p}{:}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -M -Mahal is beautiful - \end{Verbatim} - - \subsubsection{Built-in Functions}\label{built-in-functions} - - \textbf{find( )} function returns the index value of the given data that -is to found in the string. If it is not found it returns \textbf{-1}. -Remember to not confuse the returned -1 for reverse indexing value. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}4}]:} \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{find}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{al}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{find}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{am}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -7 --1 - \end{Verbatim} - - The index value returned is the index of the first element in the input -data. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}5}]:} \PY{k}{print} \PY{n}{String0}\PY{p}{[}\PY{l+m+mi}{7}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -a - \end{Verbatim} - - One can also input \textbf{find( )} function between which index values -it has to search. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}6}]:} \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{find}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{j}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)} - \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{find}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{j}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -2 -2 - \end{Verbatim} - - \textbf{capitalize( )} is used to capitalize the first element in the -string. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}7}]:} \PY{n}{String3} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{observe the first letter in this sentence.}\PY{l+s}{\PYZsq{}} - \PY{k}{print} \PY{n}{String3}\PY{o}{.}\PY{n}{capitalize}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Observe the first letter in this sentence. - \end{Verbatim} - - \textbf{center( )} is used to center align the string by specifying the -field width. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}8}]:} \PY{n}{String0}\PY{o}{.}\PY{n}{center}\PY{p}{(}\PY{l+m+mi}{70}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}8}]:} ' Taj Mahal is beautiful ' -\end{Verbatim} - - One can also fill the left out spaces with any other character. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}9}]:} \PY{n}{String0}\PY{o}{.}\PY{n}{center}\PY{p}{(}\PY{l+m+mi}{70}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{\PYZhy{}}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}9}]:} '------------------------Taj Mahal is beautiful------------------------' -\end{Verbatim} - - \textbf{zfill( )} is used for zero padding by specifying the field -width. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}10}]:} \PY{n}{String0}\PY{o}{.}\PY{n}{zfill}\PY{p}{(}\PY{l+m+mi}{30}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}10}]:} '00000000Taj Mahal is beautiful' -\end{Verbatim} - - \textbf{expandtabs( )} allows you to change the spacing of the tab -character. `\t' which is by default set to 8 spaces. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}11}]:} \PY{n}{s} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{h}\PY{l+s+se}{\PYZbs{}t}\PY{l+s}{e}\PY{l+s+se}{\PYZbs{}t}\PY{l+s}{l}\PY{l+s+se}{\PYZbs{}t}\PY{l+s}{l}\PY{l+s+se}{\PYZbs{}t}\PY{l+s}{o}\PY{l+s}{\PYZsq{}} - \PY{k}{print} \PY{n}{s} - \PY{k}{print} \PY{n}{s}\PY{o}{.}\PY{n}{expandtabs}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{)} - \PY{k}{print} \PY{n}{s}\PY{o}{.}\PY{n}{expandtabs}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -h e l l o -h e l l o -h e l l o - \end{Verbatim} - - \textbf{index( )} works the same way as \textbf{find( )} function the -only difference is find returns `-1' when the input element is not found -in the string but \textbf{index( )} function throws a ValueError - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}12}]:} \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{index}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Taj}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{index}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Mahal}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{)} - \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{index}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Mahal}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{10}\PY{p}{,}\PY{l+m+mi}{20}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -0 -4 - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] - - --------------------------------------------------------------------------- - - ValueError Traceback (most recent call last) - - in () - 1 print String0.index('Taj') - 2 print String0.index('Mahal',0) - ----> 3 print String0.index('Mahal',10,20) - - - ValueError: substring not found - - \end{Verbatim} - - \textbf{endswith( )} function is used to check if the given string ends -with the particular char which is given as input. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}13}]:} \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{endswith}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{y}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -False - \end{Verbatim} - - The start and stop index values can also be specified. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}14}]:} \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{endswith}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{l}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{)} - \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{endswith}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{M}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -True -True - \end{Verbatim} - - \textbf{count( )} function counts the number of char in the given -string. The start and the stop index can also be specified or left -blank. (These are Implicit arguments which will be dealt in functions) - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}15}]:} \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{count}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{a}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{)} - \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{count}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{a}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{10}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -4 -2 - \end{Verbatim} - - \textbf{join( )} function is used add a char in between the elements of -the input string. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}16}]:} \PY{l+s}{\PYZsq{}}\PY{l+s}{a}\PY{l+s}{\PYZsq{}}\PY{o}{.}\PY{n}{join}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{*\PYZus{}\PYZhy{}}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}16}]:} '*a\_a-' -\end{Verbatim} - - '*\_-`is the input string and char 'a' is added in between each element - - \textbf{join( )} function can also be used to convert a list into a -string. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}17}]:} \PY{n}{a} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{n}{String0}\PY{p}{)} - \PY{k}{print} \PY{n}{a} - \PY{n}{b} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{\PYZsq{}}\PY{o}{.}\PY{n}{join}\PY{p}{(}\PY{n}{a}\PY{p}{)} - \PY{k}{print} \PY{n}{b} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -['T', 'a', 'j', ' ', 'M', 'a', 'h', 'a', 'l', ' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l'] -Taj Mahal is beautiful - \end{Verbatim} - - Before converting it into a string \textbf{join( )} function can be used -to insert any char in between the list elements. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}18}]:} \PY{n}{c} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{/}\PY{l+s}{\PYZsq{}}\PY{o}{.}\PY{n}{join}\PY{p}{(}\PY{n}{a}\PY{p}{)}\PY{p}{[}\PY{l+m+mi}{18}\PY{p}{:}\PY{p}{]} - \PY{k}{print} \PY{n}{c} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -/i/s/ /b/e/a/u/t/i/f/u/l - \end{Verbatim} - - \textbf{split( )} function is used to convert a string back to a list. -Think of it as the opposite of the \textbf{join()} function. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}19}]:} \PY{n}{d} \PY{o}{=} \PY{n}{c}\PY{o}{.}\PY{n}{split}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{/}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n}{d} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l'] - \end{Verbatim} - - In \textbf{split( )} function one can also specify the number of times -you want to split the string or the number of elements the new returned -list should conatin. The number of elements is always one more than the -specified number this is because it is split the number of times -specified. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}20}]:} \PY{n}{e} \PY{o}{=} \PY{n}{c}\PY{o}{.}\PY{n}{split}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{/}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)} - \PY{k}{print} \PY{n}{e} - \PY{k}{print} \PY{n+nb}{len}\PY{p}{(}\PY{n}{e}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l'] -4 - \end{Verbatim} - - \textbf{lower( )} converts any capital letter to small letter. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}21}]:} \PY{k}{print} \PY{n}{String0} - \PY{k}{print} \PY{n}{String0}\PY{o}{.}\PY{n}{lower}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Taj Mahal is beautiful -taj mahal is beautiful - \end{Verbatim} - - \textbf{upper( )} converts any small letter to capital letter. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}22}]:} \PY{n}{String0}\PY{o}{.}\PY{n}{upper}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}22}]:} 'TAJ MAHAL IS BEAUTIFUL' -\end{Verbatim} - - \textbf{replace( )} function replaces the element with another element. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}23}]:} \PY{n}{String0}\PY{o}{.}\PY{n}{replace}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Taj Mahal}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{Bengaluru}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}23}]:} 'Bengaluru is beautiful' -\end{Verbatim} - - \textbf{strip( )} function is used to delete elements from the right end -and the left end which is not required. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}24}]:} \PY{n}{f} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{ hello }\PY{l+s}{\PYZsq{}} -\end{Verbatim} - - If no char is specified then it will delete all the spaces that is -present in the right and left hand side of the data. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}25}]:} \PY{n}{f}\PY{o}{.}\PY{n}{strip}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}25}]:} 'hello' -\end{Verbatim} - - \textbf{strip( )} function, when a char is specified then it deletes -that char if it is present in the two ends of the specified string. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}26}]:} \PY{n}{f} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{ ***\PYZhy{}\PYZhy{}\PYZhy{}\PYZhy{}hello\PYZhy{}\PYZhy{}\PYZhy{}******* }\PY{l+s}{\PYZsq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}27}]:} \PY{n}{f}\PY{o}{.}\PY{n}{strip}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{*}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}27}]:} ' ***----hello---******* ' -\end{Verbatim} - - The asterisk had to be deleted but is not. This is because there is a -space in both the right and left hand side. So in strip function. The -characters need to be inputted in the specific order in which they are -present. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}28}]:} \PY{k}{print} \PY{n}{f}\PY{o}{.}\PY{n}{strip}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{ *}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n}{f}\PY{o}{.}\PY{n}{strip}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{ *\PYZhy{}}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -----hello--- -hello - \end{Verbatim} - - \textbf{lstrip( )} and \textbf{rstrip( )} function have the same -functionality as strip function but the only difference is -\textbf{lstrip( )} deletes only towards the left side and -\textbf{rstrip( )} towards the right. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}29}]:} \PY{k}{print} \PY{n}{f}\PY{o}{.}\PY{n}{lstrip}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{ *}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n}{f}\PY{o}{.}\PY{n}{rstrip}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{ *}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -----hello---******* - ***----hello--- - \end{Verbatim} - - \subsection{Dictionaries}\label{dictionaries} - - Dictionaries are more used like a database because here you can index a -particular sequence with your user defined string. - - To define a dictionary, equate a variable to \{ \} or dict() - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}30}]:} \PY{n}{d0} \PY{o}{=} \PY{p}{\PYZob{}}\PY{p}{\PYZcb{}} - \PY{n}{d1} \PY{o}{=} \PY{n+nb}{dict}\PY{p}{(}\PY{p}{)} - \PY{k}{print} \PY{n+nb}{type}\PY{p}{(}\PY{n}{d0}\PY{p}{)}\PY{p}{,} \PY{n+nb}{type}\PY{p}{(}\PY{n}{d1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] - - \end{Verbatim} - - Dictionary works somewhat like a list but with an added capability of -assigning it's own index style. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}31}]:} \PY{n}{d0}\PY{p}{[}\PY{l+s}{\PYZsq{}}\PY{l+s}{One}\PY{l+s}{\PYZsq{}}\PY{p}{]} \PY{o}{=} \PY{l+m+mi}{1} - \PY{n}{d0}\PY{p}{[}\PY{l+s}{\PYZsq{}}\PY{l+s}{OneTwo}\PY{l+s}{\PYZsq{}}\PY{p}{]} \PY{o}{=} \PY{l+m+mi}{12} - \PY{k}{print} \PY{n}{d0} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -\{'OneTwo': 12, 'One': 1\} - \end{Verbatim} - - That is how a dictionary looks like. Now you are able to access `1' by -the index value set at `One' - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}32}]:} \PY{k}{print} \PY{n}{d0}\PY{p}{[}\PY{l+s}{\PYZsq{}}\PY{l+s}{One}\PY{l+s}{\PYZsq{}}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -1 - \end{Verbatim} - - Two lists which are related can be merged to form a dictionary. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}33}]:} \PY{n}{names} \PY{o}{=} \PY{p}{[}\PY{l+s}{\PYZsq{}}\PY{l+s}{One}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{Two}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{Three}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{Four}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{Five}\PY{l+s}{\PYZsq{}}\PY{p}{]} - \PY{n}{numbers} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{,} \PY{l+m+mi}{4}\PY{p}{,} \PY{l+m+mi}{5}\PY{p}{]} -\end{Verbatim} - - \textbf{zip( )} function is used to combine two lists - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}34}]:} \PY{n}{d2} \PY{o}{=} \PY{n+nb}{zip}\PY{p}{(}\PY{n}{names}\PY{p}{,}\PY{n}{numbers}\PY{p}{)} - \PY{k}{print} \PY{n}{d2} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)] - \end{Verbatim} - - The two lists are combined to form a single list and each elements are -clubbed with their respective elements from the other list inside a -tuple. Tuples because that is what is assigned and the value should not -change. - -Further, To convert the above into a dictionary. \textbf{dict( )} -function is used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}35}]:} \PY{n}{a1} \PY{o}{=} \PY{n+nb}{dict}\PY{p}{(}\PY{n}{d2}\PY{p}{)} - \PY{k}{print} \PY{n}{a1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -\{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1\} - \end{Verbatim} - - \subsubsection{Built-in Functions}\label{built-in-functions} - - \textbf{clear( )} function is used to erase the entire database that was -created. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}36}]:} \PY{n}{a1}\PY{o}{.}\PY{n}{clear}\PY{p}{(}\PY{p}{)} - \PY{k}{print} \PY{n}{a1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -\{\} - \end{Verbatim} - - Dictionary can also be built using loops. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}37}]:} \PY{k}{for} \PY{n}{i} \PY{o+ow}{in} \PY{n+nb}{range}\PY{p}{(}\PY{n+nb}{len}\PY{p}{(}\PY{n}{names}\PY{p}{)}\PY{p}{)}\PY{p}{:} - \PY{n}{a1}\PY{p}{[}\PY{n}{names}\PY{p}{[}\PY{n}{i}\PY{p}{]}\PY{p}{]} \PY{o}{=} \PY{n}{numbers}\PY{p}{[}\PY{n}{i}\PY{p}{]} - \PY{k}{print} \PY{n}{a1} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -\{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1\} - \end{Verbatim} - - \textbf{values( )} function returns a list with all the assigned values -in the dictionary. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}38}]:} \PY{n}{a1}\PY{o}{.}\PY{n}{values}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}38}]:} [4, 5, 3, 2, 1] -\end{Verbatim} - - \textbf{keys( )} function returns all the index or the keys to which -contains the values that it was assigned to. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}39}]:} \PY{n}{a1}\PY{o}{.}\PY{n}{keys}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}39}]:} ['Four', 'Five', 'Three', 'Two', 'One'] -\end{Verbatim} - - \textbf{items( )} is returns a list containing both the list but each -element in the dictionary is inside a tuple. This is same as the result -that was obtained when zip function was used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}40}]:} \PY{n}{a1}\PY{o}{.}\PY{n}{items}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}40}]:} [('Four', 4), ('Five', 5), ('Three', 3), ('Two', 2), ('One', 1)] -\end{Verbatim} - - \textbf{pop( )} function is used to get the remove that particular -element and this removed element can be assigned to a new variable. But -remember only the value is stored and not the key. Because the is just a -index value. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}41}]:} \PY{n}{a2} \PY{o}{=} \PY{n}{a1}\PY{o}{.}\PY{n}{pop}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Four}\PY{l+s}{\PYZsq{}}\PY{p}{)} - \PY{k}{print} \PY{n}{a1} - \PY{k}{print} \PY{n}{a2} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -\{'Five': 5, 'Three': 3, 'Two': 2, 'One': 1\} -4 - \end{Verbatim} - - - % Add a bibliography block to the postdoc - - \newpage - - \input{05} - - \end{document} diff --git a/tex/05.tex b/tex/05.tex deleted file mode 100644 index b8535de..0000000 --- a/tex/05.tex +++ /dev/null @@ -1,500 +0,0 @@ - -% Default to the notebook output style - - - - -% Inherit from the specified cell style. - - - - - - - - \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} - \definecolor{darkorange}{rgb}{.71,0.21,0.01} - \definecolor{darkgreen}{rgb}{.12,.54,.11} - \definecolor{myteal}{rgb}{.26, .44, .56} - \definecolor{gray}{gray}{0.45} - \definecolor{lightgray}{gray}{.95} - \definecolor{mediumgray}{gray}{.8} - \definecolor{inputbackground}{rgb}{.95, .95, .85} - \definecolor{outputbackground}{rgb}{.95, .95, .95} - \definecolor{traceback}{rgb}{1, .95, .95} - % ansi colors - \definecolor{red}{rgb}{.6,0,0} - \definecolor{green}{rgb}{0,.65,0} - \definecolor{brown}{rgb}{0.6,0.6,0} - \definecolor{blue}{rgb}{0,.145,.698} - \definecolor{purple}{rgb}{.698,.145,.698} - \definecolor{cyan}{rgb}{0,.698,.698} - \definecolor{lightgray}{gray}{0.5} - - % bright ansi colors - \definecolor{darkgray}{gray}{0.25} - \definecolor{lightred}{rgb}{1.0,0.39,0.28} - \definecolor{lightgreen}{rgb}{0.48,0.99,0.0} - \definecolor{lightblue}{rgb}{0.53,0.81,0.92} - \definecolor{lightpurple}{rgb}{0.87,0.63,0.87} - \definecolor{lightcyan}{rgb}{0.5,1.0,0.83} - - % commands and environments needed by pandoc snippets - % extracted from the output of `pandoc -s` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} - % Add ',fontsize=\small' for more characters per line - \newenvironment{Shaded}{}{} - \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - \newcommand{\RegionMarkerTok}[1]{{#1}} - \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\NormalTok}[1]{{#1}} - - % Define a nice break command that doesn't care if a line doesn't already - % exist. - \def\br{\hspace*{\fill} \\* } - % Math Jax compatability definitions - \def\gt{>} - \def\lt{<} - % Document parameters - \title{} - - - - - % Pygments definitions - -\makeatletter -\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% - \let\PY@ul=\relax \let\PY@tc=\relax% - \let\PY@bc=\relax \let\PY@ff=\relax} -\def\PY@tok#1{\csname PY@tok@#1\endcsname} -\def\PY@toks#1+{\ifx\relax#1\empty\else% - \PY@tok{#1}\expandafter\PY@toks\fi} -\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% - \PY@it{\PY@bf{\PY@ff{#1}}}}}}} -\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} - -\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} -\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} -\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} -\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} -\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} -\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} -\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} -\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} -\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} -\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} -\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} -\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} -\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} - -\def\PYZbs{\char`\\} -\def\PYZus{\char`\_} -\def\PYZob{\char`\{} -\def\PYZcb{\char`\}} -\def\PYZca{\char`\^} -\def\PYZam{\char`\&} -\def\PYZlt{\char`\<} -\def\PYZgt{\char`\>} -\def\PYZsh{\char`\#} -\def\PYZpc{\char`\%} -\def\PYZdl{\char`\$} -\def\PYZhy{\char`\-} -\def\PYZsq{\char`\'} -\def\PYZdq{\char`\"} -\def\PYZti{\char`\~} -% for compatibility with earlier versions -\def\PYZat{@} -\def\PYZlb{[} -\def\PYZrb{]} -\makeatother - - - % Exact colors from NB - \definecolor{incolor}{rgb}{0.0, 0.0, 0.5} - \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0} - - - - - % Prevent overflowing lines due to hard-to-break entities - \sloppy - % Setup hyperref package - \hypersetup{ - breaklinks=true, % so long urls are correctly broken across lines - colorlinks=true, - urlcolor=blue, - linkcolor=darkorange, - citecolor=darkgreen, - } - % Slightly bigger margins than the latex defaults - - - - - \begin{document} - - - \maketitle - - - - - \section{Control Flow Statements}\label{control-flow-statements} - - \subsection{If}\label{if} - - if some\_condition: - -\begin{verbatim} -algorithm -\end{verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}1}]:} \PY{n}{x} \PY{o}{=} \PY{l+m+mi}{12} - \PY{k}{if} \PY{n}{x} \PY{o}{\PYZgt{}}\PY{l+m+mi}{10}\PY{p}{:} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Hello}\PY{l+s}{\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Hello - \end{Verbatim} - - \subsection{If-else}\label{if-else} - - if some\_condition: - -\begin{verbatim} -algorithm -\end{verbatim} - -else: - -\begin{verbatim} -algorithm -\end{verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}2}]:} \PY{n}{x} \PY{o}{=} \PY{l+m+mi}{12} - \PY{k}{if} \PY{n}{x} \PY{o}{\PYZgt{}} \PY{l+m+mi}{10}\PY{p}{:} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{hello}\PY{l+s}{\PYZdq{}} - \PY{k}{else}\PY{p}{:} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{world}\PY{l+s}{\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -hello - \end{Verbatim} - - \subsection{if-elif}\label{if-elif} - - if some\_condition: - -\begin{verbatim} -algorithm -\end{verbatim} - -elif some\_condition: - -\begin{verbatim} -algorithm -\end{verbatim} - -else: - -\begin{verbatim} -algorithm -\end{verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}3}]:} \PY{n}{x} \PY{o}{=} \PY{l+m+mi}{10} - \PY{n}{y} \PY{o}{=} \PY{l+m+mi}{12} - \PY{k}{if} \PY{n}{x} \PY{o}{\PYZgt{}} \PY{n}{y}\PY{p}{:} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{x\PYZgt{}y}\PY{l+s}{\PYZdq{}} - \PY{k}{elif} \PY{n}{x} \PY{o}{\PYZlt{}} \PY{n}{y}\PY{p}{:} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{x\PYZlt{}y}\PY{l+s}{\PYZdq{}} - \PY{k}{else}\PY{p}{:} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{x=y}\PY{l+s}{\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -x} - \def\lt{<} - % Document parameters - \title{} - - - - - % Pygments definitions - -\makeatletter -\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% - \let\PY@ul=\relax \let\PY@tc=\relax% - \let\PY@bc=\relax \let\PY@ff=\relax} -\def\PY@tok#1{\csname PY@tok@#1\endcsname} -\def\PY@toks#1+{\ifx\relax#1\empty\else% - \PY@tok{#1}\expandafter\PY@toks\fi} -\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% - \PY@it{\PY@bf{\PY@ff{#1}}}}}}} -\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} - -\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} -\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} -\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} -\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} -\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} -\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} -\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} -\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} -\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} -\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} -\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} -\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} -\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} - -\def\PYZbs{\char`\\} -\def\PYZus{\char`\_} -\def\PYZob{\char`\{} -\def\PYZcb{\char`\}} -\def\PYZca{\char`\^} -\def\PYZam{\char`\&} -\def\PYZlt{\char`\<} -\def\PYZgt{\char`\>} -\def\PYZsh{\char`\#} -\def\PYZpc{\char`\%} -\def\PYZdl{\char`\$} -\def\PYZhy{\char`\-} -\def\PYZsq{\char`\'} -\def\PYZdq{\char`\"} -\def\PYZti{\char`\~} -% for compatibility with earlier versions -\def\PYZat{@} -\def\PYZlb{[} -\def\PYZrb{]} -\makeatother - - - % Exact colors from NB - \definecolor{incolor}{rgb}{0.0, 0.0, 0.5} - \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0} - - - - - % Prevent overflowing lines due to hard-to-break entities - \sloppy - % Setup hyperref package - \hypersetup{ - breaklinks=true, % so long urls are correctly broken across lines - colorlinks=true, - urlcolor=blue, - linkcolor=darkorange, - citecolor=darkgreen, - } - % Slightly bigger margins than the latex defaults - - - - \begin{document} - - - \maketitle - - - - - \section{Functions}\label{functions} - - Most of the times, In a algorithm the statements keep repeating and it -will be a tedious job to execute the same statements again and again and -will consume a lot of memory and is not efficient. Enter Functions. - - This is the basic syntax of a function - - def funcname(arg1, arg2,\ldots{} argN): - -\begin{verbatim} -''' Document String''' - -statements - - -return -\end{verbatim} - - Read the above syntax as, A function by name ``funcname'' is defined, -which accepts arguements ``arg1,arg2,\ldots{}.argN''. The function is -documented and it is `''Document String'''. The function after executing -the statements returns a ``value''. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}1}]:} \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Hey Rajath!}\PY{l+s}{\PYZdq{}} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Rajath, How do you do?}\PY{l+s}{\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Hey Rajath! -Rajath, How do you do? - \end{Verbatim} - - Instead of writing the above two statements every single time it can be -replaced by defining a function which would do the job in just one line. - -Defining a function firstfunc(). - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}2}]:} \PY{k}{def} \PY{n+nf}{firstfunc}\PY{p}{(}\PY{p}{)}\PY{p}{:} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Hey Rajath!}\PY{l+s}{\PYZdq{}} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Rajath, How do you do?}\PY{l+s}{\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}3}]:} \PY{n}{firstfunc}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Hey Rajath! -Rajath, How do you do? - \end{Verbatim} - - \textbf{firstfunc()} every time just prints the message to a single -person. We can make our function \textbf{firstfunc()} to accept -arguements which will store the name and then prints respective to that -accepted name. To do so, add a argument within the function as shown. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}4}]:} \PY{k}{def} \PY{n+nf}{firstfunc}\PY{p}{(}\PY{n}{username}\PY{p}{)}\PY{p}{:} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Hey}\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n}{username} \PY{o}{+} \PY{l+s}{\PYZsq{}}\PY{l+s}{!}\PY{l+s}{\PYZsq{}} - \PY{k}{print} \PY{n}{username} \PY{o}{+} \PY{l+s}{\PYZsq{}}\PY{l+s}{,}\PY{l+s}{\PYZsq{}} \PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{How do you do?}\PY{l+s}{\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}5}]:} \PY{n}{name1} \PY{o}{=} \PY{n+nb}{raw\PYZus{}input}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Please enter your name : }\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Please enter your name : Guido - \end{Verbatim} - - The name ``Guido'' is actually stored in name1. So we pass this variable -to the function \textbf{firstfunc()} as the variable username because -that is the variable that is defined for this function. i.e name1 is -passed as username. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}6}]:} \PY{n}{firstfunc}\PY{p}{(}\PY{n}{name1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Hey Guido! -Guido, How do you do? - \end{Verbatim} - - Let us simplify this even further by defining another function -\textbf{secondfunc()} which accepts the name and stores it inside a -variable and then calls the \textbf{firstfunc()} from inside the -function itself. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}7}]:} \PY{k}{def} \PY{n+nf}{firstfunc}\PY{p}{(}\PY{n}{username}\PY{p}{)}\PY{p}{:} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Hey}\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n}{username} \PY{o}{+} \PY{l+s}{\PYZsq{}}\PY{l+s}{!}\PY{l+s}{\PYZsq{}} - \PY{k}{print} \PY{n}{username} \PY{o}{+} \PY{l+s}{\PYZsq{}}\PY{l+s}{,}\PY{l+s}{\PYZsq{}} \PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{How do you do?}\PY{l+s}{\PYZdq{}} - \PY{k}{def} \PY{n+nf}{secondfunc}\PY{p}{(}\PY{p}{)}\PY{p}{:} - \PY{n}{name} \PY{o}{=} \PY{n+nb}{raw\PYZus{}input}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{Please enter your name : }\PY{l+s}{\PYZdq{}}\PY{p}{)} - \PY{n}{firstfunc}\PY{p}{(}\PY{n}{name}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}8}]:} \PY{n}{secondfunc}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Please enter your name : karthik -Hey karthik! -karthik, How do you do? - \end{Verbatim} - - \subsection{Return Statement}\label{return-statement} - - When the function results in some value and that value has to be stored -in a variable or needs to be sent back or returned for further operation -to the main algorithm, return statement is used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}9}]:} \PY{k}{def} \PY{n+nf}{times}\PY{p}{(}\PY{n}{x}\PY{p}{,}\PY{n}{y}\PY{p}{)}\PY{p}{:} - \PY{n}{z} \PY{o}{=} \PY{n}{x}\PY{o}{*}\PY{n}{y} - \PY{k}{return} \PY{n}{z} -\end{Verbatim} - - The above defined \textbf{times( )} function accepts two arguements and -return the variable z which contains the result of the product of the -two arguements - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}10}]:} \PY{n}{c} \PY{o}{=} \PY{n}{times}\PY{p}{(}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)} - \PY{k}{print} \PY{n}{c} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -20 - \end{Verbatim} - - The z value is stored in variable c and can be used for further -operations. - - Instead of declaring another variable the entire statement itself can be -used in the return statement as shown. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}11}]:} \PY{k}{def} \PY{n+nf}{times}\PY{p}{(}\PY{n}{x}\PY{p}{,}\PY{n}{y}\PY{p}{)}\PY{p}{:} - \PY{l+s+sd}{\PYZsq{}\PYZsq{}\PYZsq{}This multiplies the two input arguments\PYZsq{}\PYZsq{}\PYZsq{}} - \PY{k}{return} \PY{n}{x}\PY{o}{*}\PY{n}{y} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}12}]:} \PY{n}{c} \PY{o}{=} \PY{n}{times}\PY{p}{(}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)} - \PY{k}{print} \PY{n}{c} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -20 - \end{Verbatim} - - Since the \textbf{times( )} is now defined, we can document it as shown -above. This document is returned whenever \textbf{times( )} function is -called under \textbf{help( )} function. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}13}]:} \PY{n}{help}\PY{p}{(}\PY{n}{times}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Help on function times in module \_\_main\_\_: - -times(x, y) - This multiplies the two input arguments - \end{Verbatim} - - Multiple variable can also be returned, But keep in mind the order. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}14}]:} \PY{n}{eglist} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{10}\PY{p}{,}\PY{l+m+mi}{50}\PY{p}{,}\PY{l+m+mi}{30}\PY{p}{,}\PY{l+m+mi}{12}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{,}\PY{l+m+mi}{100}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}15}]:} \PY{k}{def} \PY{n+nf}{egfunc}\PY{p}{(}\PY{n}{eglist}\PY{p}{)}\PY{p}{:} - \PY{n}{highest} \PY{o}{=} \PY{n+nb}{max}\PY{p}{(}\PY{n}{eglist}\PY{p}{)} - \PY{n}{lowest} \PY{o}{=} \PY{n+nb}{min}\PY{p}{(}\PY{n}{eglist}\PY{p}{)} - \PY{n}{first} \PY{o}{=} \PY{n}{eglist}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} - \PY{n}{last} \PY{o}{=} \PY{n}{eglist}\PY{p}{[}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]} - \PY{k}{return} \PY{n}{highest}\PY{p}{,}\PY{n}{lowest}\PY{p}{,}\PY{n}{first}\PY{p}{,}\PY{n}{last} -\end{Verbatim} - - If the function is just called without any variable for it to be -assigned to, the result is returned inside a tuple. But if the variables -are mentioned then the result is assigned to the variable in a -particular order which is declared in the return statement. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}16}]:} \PY{n}{egfunc}\PY{p}{(}\PY{n}{eglist}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}16}]:} (100, 6, 10, 100) -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}17}]:} \PY{n}{a}\PY{p}{,}\PY{n}{b}\PY{p}{,}\PY{n}{c}\PY{p}{,}\PY{n}{d} \PY{o}{=} \PY{n}{egfunc}\PY{p}{(}\PY{n}{eglist}\PY{p}{)} - \PY{k}{print} \PY{l+s}{\PYZsq{}}\PY{l+s}{ a =}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{n}{a}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s}{ b =}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{n}{b}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s}{ c =}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{n}{c}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s}{ d =}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{n}{d} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -a = 100 - b = 6 - c = 10 - d = 100 - \end{Verbatim} - - \subsection{Implicit arguments}\label{implicit-arguments} - - When an argument of a function is common in majority of the cases or it -is ``implicit'' this concept is used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}18}]:} \PY{k}{def} \PY{n+nf}{implicitadd}\PY{p}{(}\PY{n}{x}\PY{p}{,}\PY{n}{y}\PY{o}{=}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{:} - \PY{k}{return} \PY{n}{x}\PY{o}{+}\PY{n}{y} -\end{Verbatim} - - \textbf{implicitadd( )} is a function accepts two arguments but most of -the times the first argument needs to be added just by 3. Hence the -second argument is assigned the value 3. Here the second argument is -implicit. - - Now if the second argument is not defined when calling the -\textbf{implicitadd( )} function then it considered as 3. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}19}]:} \PY{n}{implicitadd}\PY{p}{(}\PY{l+m+mi}{4}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}19}]:} 7 -\end{Verbatim} - - But if the second argument is specified then this value overrides the -implicit value assigned to the argument - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}20}]:} \PY{n}{implicitadd}\PY{p}{(}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}20}]:} 8 -\end{Verbatim} - - \subsection{Any number of arguments}\label{any-number-of-arguments} - - If the number of arguments that is to be accepted by a function is not -known then a asterisk symbol is used before the argument. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}21}]:} \PY{k}{def} \PY{n+nf}{add\PYZus{}n}\PY{p}{(}\PY{o}{*}\PY{n}{args}\PY{p}{)}\PY{p}{:} - \PY{n}{res} \PY{o}{=} \PY{l+m+mi}{0} - \PY{n}{reslist} \PY{o}{=} \PY{p}{[}\PY{p}{]} - \PY{k}{for} \PY{n}{i} \PY{o+ow}{in} \PY{n}{args}\PY{p}{:} - \PY{n}{reslist}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{i}\PY{p}{)} - \PY{k}{print} \PY{n}{reslist} - \PY{k}{return} \PY{n+nb}{sum}\PY{p}{(}\PY{n}{reslist}\PY{p}{)} -\end{Verbatim} - - The above function accepts any number of arguments, defines a list and -appends all the arguments into that list and return the sum of all the -arguments. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}22}]:} \PY{n}{add\PYZus{}n}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 2, 3, 4, 5] - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}22}]:} 15 -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}23}]:} \PY{n}{add\PYZus{}n}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 2, 3] - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}23}]:} 6 -\end{Verbatim} - - \subsection{Global and Local -Variables}\label{global-and-local-variables} - - Whatever variable is declared inside a function is local variable and -outside the function in global variable. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}24}]:} \PY{n}{eg1} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{]} -\end{Verbatim} - - In the below function we are appending a element to the declared list -inside the function. eg2 variable declared inside the function is a -local variable. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}25}]:} \PY{k}{def} \PY{n+nf}{egfunc1}\PY{p}{(}\PY{p}{)}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{thirdfunc}\PY{p}{(}\PY{n}{arg1}\PY{p}{)}\PY{p}{:} - \PY{n}{eg2} \PY{o}{=} \PY{n}{arg1}\PY{p}{[}\PY{p}{:}\PY{p}{]} - \PY{n}{eg2}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{l+m+mi}{6}\PY{p}{)} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{This is happening inside the function :}\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n}{eg2} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{This is happening before the function is called : }\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n}{eg1} - \PY{n}{thirdfunc}\PY{p}{(}\PY{n}{eg1}\PY{p}{)} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{This is happening outside the function :}\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n}{eg1} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Accessing a variable declared inside the function from outside :}\PY{l+s}{\PYZdq{}} \PY{p}{,} \PY{n}{eg2} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}26}]:} \PY{n}{egfunc1}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -This is happening before the function is called : [1, 2, 3, 4, 5] -This is happening inside the function : [1, 2, 3, 4, 5, 6] -This is happening outside the function : [1, 2, 3, 4, 5] -Accessing a variable declared inside the function from outside : - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] - - --------------------------------------------------------------------------- - - NameError Traceback (most recent call last) - - in () - ----> 1 egfunc1() - - - in egfunc1() - 7 thirdfunc(eg1) - 8 print "This is happening outside the function :", eg1 - ----> 9 print "Accessing a variable declared inside the function from outside :" , eg2 - - - NameError: global name 'eg2' is not defined - - \end{Verbatim} - - If a \textbf{global} variable is defined as shown in the example below -then that variable can be called from anywhere. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}27}]:} \PY{n}{eg3} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] - - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}28}]:} \PY{k}{def} \PY{n+nf}{egfunc1}\PY{p}{(}\PY{p}{)}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{thirdfunc}\PY{p}{(}\PY{n}{arg1}\PY{p}{)}\PY{p}{:} - \PY{k}{global} \PY{n}{eg2} - \PY{n}{eg2} \PY{o}{=} \PY{n}{arg1}\PY{p}{[}\PY{p}{:}\PY{p}{]} - \PY{n}{eg2}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{l+m+mi}{6}\PY{p}{)} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{This is happening inside the function :}\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n}{eg2} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{This is happening before the function is called : }\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n}{eg1} - \PY{n}{thirdfunc}\PY{p}{(}\PY{n}{eg1}\PY{p}{)} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{This is happening outside the function :}\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n}{eg1} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{Accessing a variable declared inside the function from outside :}\PY{l+s}{\PYZdq{}} \PY{p}{,} \PY{n}{eg2} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}29}]:} \PY{n}{egfunc1}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -This is happening before the function is called : [1, 2, 3, 4, 5] -This is happening inside the function : [1, 2, 3, 4, 5, 6] -This is happening outside the function : [1, 2, 3, 4, 5] -Accessing a variable declared inside the function from outside : [1, 2, 3, 4, 5, 6] - \end{Verbatim} - - \subsection{Lambda Functions}\label{lambda-functions} - - These are small functions which are not defined with any name and carry -a single expression whose result is returned. Lambda functions comes -very handy when operating with lists. These function are defined by the -keyword \textbf{lambda} followed by the variables, a colon and the -respective expression. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}30}]:} \PY{n}{z} \PY{o}{=} \PY{k}{lambda} \PY{n}{x}\PY{p}{:} \PY{n}{x} \PY{o}{*} \PY{n}{x} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}31}]:} \PY{n}{z}\PY{p}{(}\PY{l+m+mi}{8}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}31}]:} 64 -\end{Verbatim} - - \subsubsection{map}\label{map} - - \textbf{map( )} function basically executes the function that is defined -to each of the list's element separately. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}32}]:} \PY{n}{list1} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{,}\PY{l+m+mi}{9}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}33}]:} \PY{n}{eg} \PY{o}{=} \PY{n+nb}{map}\PY{p}{(}\PY{k}{lambda} \PY{n}{x}\PY{p}{:}\PY{n}{x}\PY{o}{+}\PY{l+m+mi}{2}\PY{p}{,} \PY{n}{list1}\PY{p}{)} - \PY{k}{print} \PY{n}{eg} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[3, 4, 5, 6, 7, 8, 9, 10, 11] - \end{Verbatim} - - You can also add two lists. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}34}]:} \PY{n}{list2} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{9}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{]} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}35}]:} \PY{n}{eg2} \PY{o}{=} \PY{n+nb}{map}\PY{p}{(}\PY{k}{lambda} \PY{n}{x}\PY{p}{,}\PY{n}{y}\PY{p}{:}\PY{n}{x}\PY{o}{+}\PY{n}{y}\PY{p}{,} \PY{n}{list1}\PY{p}{,}\PY{n}{list2}\PY{p}{)} - \PY{k}{print} \PY{n}{eg2} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[10, 10, 10, 10, 10, 10, 10, 10, 10] - \end{Verbatim} - - Not only lambda function but also other built in functions can also be -used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}36}]:} \PY{n}{eg3} \PY{o}{=} \PY{n+nb}{map}\PY{p}{(}\PY{n+nb}{str}\PY{p}{,}\PY{n}{eg2}\PY{p}{)} - \PY{k}{print} \PY{n}{eg3} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -['10', '10', '10', '10', '10', '10', '10', '10', '10'] - \end{Verbatim} - - \subsubsection{filter}\label{filter} - - \textbf{filter( )} function is used to filter out the values in a list. -Note that \textbf{filter()} function returns the result in a new list. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}37}]:} \PY{n}{list1} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{,}\PY{l+m+mi}{9}\PY{p}{]} -\end{Verbatim} - - To get the elements which are less than 5, - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}38}]:} \PY{n+nb}{filter}\PY{p}{(}\PY{k}{lambda} \PY{n}{x}\PY{p}{:}\PY{n}{x}\PY{o}{\PYZlt{}}\PY{l+m+mi}{5}\PY{p}{,}\PY{n}{list1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}38}]:} [1, 2, 3, 4] -\end{Verbatim} - - Notice what happens when \textbf{map()} is used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}39}]:} \PY{n+nb}{map}\PY{p}{(}\PY{k}{lambda} \PY{n}{x}\PY{p}{:}\PY{n}{x}\PY{o}{\PYZlt{}}\PY{l+m+mi}{5}\PY{p}{,} \PY{n}{list1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}39}]:} [True, True, True, True, False, False, False, False, False] -\end{Verbatim} - - We can conclude that, whatever is returned true in \textbf{map( )} -function that particular element is returned when \textbf{filter( )} -function is used. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}40}]:} \PY{n+nb}{filter}\PY{p}{(}\PY{k}{lambda} \PY{n}{x}\PY{p}{:}\PY{n}{x}\PY{o}{\PYZpc{}}\PY{k}{4}==0,list1) -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}40}]:} [4, 8] -\end{Verbatim} - - - % Add a bibliography block to the postdoc - - \newpage - - \input{07} - - \end{document} diff --git a/tex/07.tex b/tex/07.tex deleted file mode 100644 index cd3ee46..0000000 --- a/tex/07.tex +++ /dev/null @@ -1,748 +0,0 @@ - -% Default to the notebook output style - - - - -% Inherit from the specified cell style. - - - - - - - - \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} - \definecolor{darkorange}{rgb}{.71,0.21,0.01} - \definecolor{darkgreen}{rgb}{.12,.54,.11} - \definecolor{myteal}{rgb}{.26, .44, .56} - \definecolor{gray}{gray}{0.45} - \definecolor{lightgray}{gray}{.95} - \definecolor{mediumgray}{gray}{.8} - \definecolor{inputbackground}{rgb}{.95, .95, .85} - \definecolor{outputbackground}{rgb}{.95, .95, .95} - \definecolor{traceback}{rgb}{1, .95, .95} - % ansi colors - \definecolor{red}{rgb}{.6,0,0} - \definecolor{green}{rgb}{0,.65,0} - \definecolor{brown}{rgb}{0.6,0.6,0} - \definecolor{blue}{rgb}{0,.145,.698} - \definecolor{purple}{rgb}{.698,.145,.698} - \definecolor{cyan}{rgb}{0,.698,.698} - \definecolor{lightgray}{gray}{0.5} - - % bright ansi colors - \definecolor{darkgray}{gray}{0.25} - \definecolor{lightred}{rgb}{1.0,0.39,0.28} - \definecolor{lightgreen}{rgb}{0.48,0.99,0.0} - \definecolor{lightblue}{rgb}{0.53,0.81,0.92} - \definecolor{lightpurple}{rgb}{0.87,0.63,0.87} - \definecolor{lightcyan}{rgb}{0.5,1.0,0.83} - - % commands and environments needed by pandoc snippets - % extracted from the output of `pandoc -s` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} - % Add ',fontsize=\small' for more characters per line - \newenvironment{Shaded}{}{} - \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - \newcommand{\RegionMarkerTok}[1]{{#1}} - \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\NormalTok}[1]{{#1}} - - % Define a nice break command that doesn't care if a line doesn't already - % exist. - \def\br{\hspace*{\fill} \\* } - % Math Jax compatability definitions - \def\gt{>} - \def\lt{<} - % Document parameters - \title{} - - - - - % Pygments definitions - -\makeatletter -\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% - \let\PY@ul=\relax \let\PY@tc=\relax% - \let\PY@bc=\relax \let\PY@ff=\relax} -\def\PY@tok#1{\csname PY@tok@#1\endcsname} -\def\PY@toks#1+{\ifx\relax#1\empty\else% - \PY@tok{#1}\expandafter\PY@toks\fi} -\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% - \PY@it{\PY@bf{\PY@ff{#1}}}}}}} -\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} - -\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} -\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} -\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} -\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} -\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} -\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} -\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} -\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} -\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} -\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} -\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} -\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} -\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} - -\def\PYZbs{\char`\\} -\def\PYZus{\char`\_} -\def\PYZob{\char`\{} -\def\PYZcb{\char`\}} -\def\PYZca{\char`\^} -\def\PYZam{\char`\&} -\def\PYZlt{\char`\<} -\def\PYZgt{\char`\>} -\def\PYZsh{\char`\#} -\def\PYZpc{\char`\%} -\def\PYZdl{\char`\$} -\def\PYZhy{\char`\-} -\def\PYZsq{\char`\'} -\def\PYZdq{\char`\"} -\def\PYZti{\char`\~} -% for compatibility with earlier versions -\def\PYZat{@} -\def\PYZlb{[} -\def\PYZrb{]} -\makeatother - - - % Exact colors from NB - \definecolor{incolor}{rgb}{0.0, 0.0, 0.5} - \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0} - - - - - % Prevent overflowing lines due to hard-to-break entities - \sloppy - % Setup hyperref package - \hypersetup{ - breaklinks=true, % so long urls are correctly broken across lines - colorlinks=true, - urlcolor=blue, - linkcolor=darkorange, - citecolor=darkgreen, - } - % Slightly bigger margins than the latex defaults - - - - \begin{document} - - - \maketitle - - - - - \section{Classes}\label{classes} - - Variables, Lists, Dictionaries etc in python is a object. Without -getting into the theory part of Object Oriented Programming, explanation -of the concepts will be done along this tutorial. - - A class is declared as follows - - class class\_name: - -\begin{verbatim} -Functions -\end{verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}1}]:} \PY{k}{class} \PY{n+nc}{FirstClass}\PY{p}{:} - \PY{k}{pass} -\end{Verbatim} - - \textbf{pass} in python means do nothing. - - Above, a class object named ``FirstClass'' is declared now consider a -``egclass'' which has all the characteristics of ``FirstClass''. So all -you have to do is, equate the ``egclass'' to ``FirstClass''. In python -jargon this is called as creating an instance. ``egclass'' is the -instance of ``FirstClass'' - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}2}]:} \PY{n}{egclass} \PY{o}{=} \PY{n}{FirstClass}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}3}]:} \PY{n+nb}{type}\PY{p}{(}\PY{n}{egclass}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}3}]:} instance -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}4}]:} \PY{n+nb}{type}\PY{p}{(}\PY{n}{FirstClass}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}4}]:} classobj -\end{Verbatim} - - Now let us add some ``functionality'' to the class. So that our -``FirstClass'' is defined in a better way. A function inside a class is -called as a ``Method'' of that class - - Most of the classes will have a function named ``\_\_init\_\_''. These -are called as magic methods. In this method you basically initialize the -variables of that class or any other initial algorithms which is -applicable to all methods is specified in this method. A variable inside -a class is called an attribute. - - These helps simplify the process of initializing a instance. For -example, - -Without the use of magic method or \_\_init\_\_ which is otherwise -called as constructors. One had to define a \textbf{init( )} method and -call the \textbf{init( )} function. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor} }]:} \PY{n}{eg0} \PY{o}{=} \PY{n}{FirstClass}\PY{p}{(}\PY{p}{)} - \PY{n}{eg0}\PY{o}{.}\PY{n}{init}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - But when the constructor is defined the \_\_init\_\_ is called thus -intializing the instance created. - - We will make our ``FirstClass'' to accept two variables name and symbol. - -I will be explaining about the ``self'' in a while. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}6}]:} \PY{k}{class} \PY{n+nc}{FirstClass}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,}\PY{n}{name}\PY{p}{,}\PY{n}{symbol}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name} \PY{o}{=} \PY{n}{name} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{symbol} \PY{o}{=} \PY{n}{symbol} -\end{Verbatim} - - Now that we have defined a function and added the \_\_init\_\_ method. -We can create a instance of FirstClass which now accepts two arguments. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}7}]:} \PY{n}{eg1} \PY{o}{=} \PY{n}{FirstClass}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{one}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)} - \PY{n}{eg2} \PY{o}{=} \PY{n}{FirstClass}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{two}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}8}]:} \PY{k}{print} \PY{n}{eg1}\PY{o}{.}\PY{n}{name}\PY{p}{,} \PY{n}{eg1}\PY{o}{.}\PY{n}{symbol} - \PY{k}{print} \PY{n}{eg2}\PY{o}{.}\PY{n}{name}\PY{p}{,} \PY{n}{eg2}\PY{o}{.}\PY{n}{symbol} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -one 1 -two 2 - \end{Verbatim} - - \textbf{dir( )} function comes very handy in looking into what the class -contains and what all method it offers - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}9}]:} \PY{n+nb}{dir}\PY{p}{(}\PY{n}{FirstClass}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}9}]:} ['\_\_doc\_\_', '\_\_init\_\_', '\_\_module\_\_'] -\end{Verbatim} - - \textbf{dir( )} of an instance also shows it's defined attributes. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}10}]:} \PY{n+nb}{dir}\PY{p}{(}\PY{n}{eg1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}10}]:} ['\_\_doc\_\_', '\_\_init\_\_', '\_\_module\_\_', 'name', 'symbol'] -\end{Verbatim} - - Changing the FirstClass function a bit, - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}11}]:} \PY{k}{class} \PY{n+nc}{FirstClass}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,}\PY{n}{name}\PY{p}{,}\PY{n}{symbol}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{n} \PY{o}{=} \PY{n}{name} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{s} \PY{o}{=} \PY{n}{symbol} -\end{Verbatim} - - Changing self.name and self.symbol to self.n and self.s respectively -will yield, - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}12}]:} \PY{n}{eg1} \PY{o}{=} \PY{n}{FirstClass}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{one}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)} - \PY{n}{eg2} \PY{o}{=} \PY{n}{FirstClass}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{two}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}13}]:} \PY{k}{print} \PY{n}{eg1}\PY{o}{.}\PY{n}{name}\PY{p}{,} \PY{n}{eg1}\PY{o}{.}\PY{n}{symbol} - \PY{k}{print} \PY{n}{eg2}\PY{o}{.}\PY{n}{name}\PY{p}{,} \PY{n}{eg2}\PY{o}{.}\PY{n}{symbol} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] - - --------------------------------------------------------------------------- - - AttributeError Traceback (most recent call last) - - in () - ----> 1 print eg1.name, eg1.symbol - 2 print eg2.name, eg2.symbol - - - AttributeError: FirstClass instance has no attribute 'name' - - \end{Verbatim} - - AttributeError, Remember variables are nothing but attributes inside a -class? So this means we have not given the correct attribute for the -instance. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}14}]:} \PY{n+nb}{dir}\PY{p}{(}\PY{n}{eg1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}14}]:} ['\_\_doc\_\_', '\_\_init\_\_', '\_\_module\_\_', 'n', 's'] -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}15}]:} \PY{k}{print} \PY{n}{eg1}\PY{o}{.}\PY{n}{n}\PY{p}{,} \PY{n}{eg1}\PY{o}{.}\PY{n}{s} - \PY{k}{print} \PY{n}{eg2}\PY{o}{.}\PY{n}{n}\PY{p}{,} \PY{n}{eg2}\PY{o}{.}\PY{n}{s} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -one 1 -two 2 - \end{Verbatim} - - So now we have solved the error. Now let us compare the two examples -that we saw. - -When I declared self.name and self.symbol, there was no attribute error -for eg1.name and eg1.symbol and when I declared self.n and self.s, there -was no attribute error for eg1.n and eg1.s - -From the above we can conclude that self is nothing but the instance -itself. - -Remember, self is not predefined it is userdefined. You can make use of -anything you are comfortable with. But it has become a common practice -to use self. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}16}]:} \PY{k}{class} \PY{n+nc}{FirstClass}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n}{asdf1234}\PY{p}{,}\PY{n}{name}\PY{p}{,}\PY{n}{symbol}\PY{p}{)}\PY{p}{:} - \PY{n}{asdf1234}\PY{o}{.}\PY{n}{n} \PY{o}{=} \PY{n}{name} - \PY{n}{asdf1234}\PY{o}{.}\PY{n}{s} \PY{o}{=} \PY{n}{symbol} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}17}]:} \PY{n}{eg1} \PY{o}{=} \PY{n}{FirstClass}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{one}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{)} - \PY{n}{eg2} \PY{o}{=} \PY{n}{FirstClass}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{two}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}18}]:} \PY{k}{print} \PY{n}{eg1}\PY{o}{.}\PY{n}{n}\PY{p}{,} \PY{n}{eg1}\PY{o}{.}\PY{n}{s} - \PY{k}{print} \PY{n}{eg2}\PY{o}{.}\PY{n}{n}\PY{p}{,} \PY{n}{eg2}\PY{o}{.}\PY{n}{s} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -one 1 -two 2 - \end{Verbatim} - - Since eg1 and eg2 are instances of FirstClass it need not necessarily be -limited to FirstClass itself. It might extend itself by declaring other -attributes without having the attribute to be declared inside the -FirstClass. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}19}]:} \PY{n}{eg1}\PY{o}{.}\PY{n}{cube} \PY{o}{=} \PY{l+m+mi}{1} - \PY{n}{eg2}\PY{o}{.}\PY{n}{cube} \PY{o}{=} \PY{l+m+mi}{8} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}20}]:} \PY{n+nb}{dir}\PY{p}{(}\PY{n}{eg1}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}20}]:} ['\_\_doc\_\_', '\_\_init\_\_', '\_\_module\_\_', 'cube', 'n', 's'] -\end{Verbatim} - - Just like global and local variables as we saw earlier, even classes -have it's own types of variables. - -Class Attribute : attributes defined outside the method and is -applicable to all the instances. - -Instance Attribute : attributes defined inside a method and is -applicable to only that method and is unique to each instance. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}21}]:} \PY{k}{class} \PY{n+nc}{FirstClass}\PY{p}{:} - \PY{n}{test} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{test}\PY{l+s}{\PYZsq{}} - \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,}\PY{n}{name}\PY{p}{,}\PY{n}{symbol}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name} \PY{o}{=} \PY{n}{name} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{symbol} \PY{o}{=} \PY{n}{symbol} -\end{Verbatim} - - Here test is a class attribute and name is a instance attribute. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}22}]:} \PY{n}{eg3} \PY{o}{=} \PY{n}{FirstClass}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Three}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}23}]:} \PY{k}{print} \PY{n}{eg3}\PY{o}{.}\PY{n}{test}\PY{p}{,} \PY{n}{eg3}\PY{o}{.}\PY{n}{name} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -test Three - \end{Verbatim} - - Let us add some more methods to FirstClass. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}24}]:} \PY{k}{class} \PY{n+nc}{FirstClass}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,}\PY{n}{name}\PY{p}{,}\PY{n}{symbol}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name} \PY{o}{=} \PY{n}{name} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{symbol} \PY{o}{=} \PY{n}{symbol} - \PY{k}{def} \PY{n+nf}{square}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{)}\PY{p}{:} - \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{symbol} \PY{o}{*} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{symbol} - \PY{k}{def} \PY{n+nf}{cube}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{)}\PY{p}{:} - \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{symbol} \PY{o}{*} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{symbol} \PY{o}{*} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{symbol} - \PY{k}{def} \PY{n+nf}{multiply}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{x}\PY{p}{)}\PY{p}{:} - \PY{k}{return} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{symbol} \PY{o}{*} \PY{n}{x} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}25}]:} \PY{n}{eg4} \PY{o}{=} \PY{n}{FirstClass}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Five}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}26}]:} \PY{k}{print} \PY{n}{eg4}\PY{o}{.}\PY{n}{square}\PY{p}{(}\PY{p}{)} - \PY{k}{print} \PY{n}{eg4}\PY{o}{.}\PY{n}{cube}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -25 -125 - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}27}]:} \PY{n}{eg4}\PY{o}{.}\PY{n}{multiply}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}27}]:} 10 -\end{Verbatim} - - The above can also be written as, - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}28}]:} \PY{n}{FirstClass}\PY{o}{.}\PY{n}{multiply}\PY{p}{(}\PY{n}{eg4}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}28}]:} 10 -\end{Verbatim} - - \subsection{Inheritance}\label{inheritance} - - There might be cases where a new class would have all the previous -characteristics of an already defined class. So the new class can -``inherit'' the previous class and add it's own methods to it. This is -called as inheritance. - - Consider class SoftwareEngineer which has a method salary. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}29}]:} \PY{k}{class} \PY{n+nc}{SoftwareEngineer}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,}\PY{n}{name}\PY{p}{,}\PY{n}{age}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name} \PY{o}{=} \PY{n}{name} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{age} \PY{o}{=} \PY{n}{age} - \PY{k}{def} \PY{n+nf}{salary}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{value}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{money} \PY{o}{=} \PY{n}{value} - \PY{k}{print} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{earns}\PY{l+s}{\PYZdq{}}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{money} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}30}]:} \PY{n}{a} \PY{o}{=} \PY{n}{SoftwareEngineer}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Kartik}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{26}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}31}]:} \PY{n}{a}\PY{o}{.}\PY{n}{salary}\PY{p}{(}\PY{l+m+mi}{40000}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Kartik earns 40000 - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}32}]:} \PY{n+nb}{dir}\PY{p}{(}\PY{n}{SoftwareEngineer}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}32}]:} ['\_\_doc\_\_', '\_\_init\_\_', '\_\_module\_\_', 'salary'] -\end{Verbatim} - - Now consider another class Artist which tells us about the amount of -money an artist earns and his artform. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}33}]:} \PY{k}{class} \PY{n+nc}{Artist}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,}\PY{n}{name}\PY{p}{,}\PY{n}{age}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name} \PY{o}{=} \PY{n}{name} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{age} \PY{o}{=} \PY{n}{age} - \PY{k}{def} \PY{n+nf}{money}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,}\PY{n}{value}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{money} \PY{o}{=} \PY{n}{value} - \PY{k}{print} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{earns}\PY{l+s}{\PYZdq{}}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{money} - \PY{k}{def} \PY{n+nf}{artform}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{job}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{job} \PY{o}{=} \PY{n}{job} - \PY{k}{print} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{is a}\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{job} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}34}]:} \PY{n}{b} \PY{o}{=} \PY{n}{Artist}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Nitin}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{20}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}35}]:} \PY{n}{b}\PY{o}{.}\PY{n}{money}\PY{p}{(}\PY{l+m+mi}{50000}\PY{p}{)} - \PY{n}{b}\PY{o}{.}\PY{n}{artform}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Musician}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Nitin earns 50000 -Nitin is a Musician - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}36}]:} \PY{n+nb}{dir}\PY{p}{(}\PY{n}{Artist}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}36}]:} ['\_\_doc\_\_', '\_\_init\_\_', '\_\_module\_\_', 'artform', 'money'] -\end{Verbatim} - - money method and salary method are the same. So we can generalize the -method to salary and inherit the SoftwareEngineer class to Artist class. -Now the artist class becomes, - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}37}]:} \PY{k}{class} \PY{n+nc}{Artist}\PY{p}{(}\PY{n}{SoftwareEngineer}\PY{p}{)}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{artform}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{job}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{job} \PY{o}{=} \PY{n}{job} - \PY{k}{print} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{is a}\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{job} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}38}]:} \PY{n}{c} \PY{o}{=} \PY{n}{Artist}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Nishanth}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{21}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}39}]:} \PY{n+nb}{dir}\PY{p}{(}\PY{n}{Artist}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}39}]:} ['\_\_doc\_\_', '\_\_init\_\_', '\_\_module\_\_', 'artform', 'salary'] -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}40}]:} \PY{n}{c}\PY{o}{.}\PY{n}{salary}\PY{p}{(}\PY{l+m+mi}{60000}\PY{p}{)} - \PY{n}{c}\PY{o}{.}\PY{n}{artform}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Dancer}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Nishanth earns 60000 -Nishanth is a Dancer - \end{Verbatim} - - Suppose say while inheriting a particular method is not suitable for the -new class. One can override this method by defining again that method -with the same name inside the new class. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}41}]:} \PY{k}{class} \PY{n+nc}{Artist}\PY{p}{(}\PY{n}{SoftwareEngineer}\PY{p}{)}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{artform}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{job}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{job} \PY{o}{=} \PY{n}{job} - \PY{k}{print} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{is a}\PY{l+s}{\PYZdq{}}\PY{p}{,} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{job} - \PY{k}{def} \PY{n+nf}{salary}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{value}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{money} \PY{o}{=} \PY{n}{value} - \PY{k}{print} \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{name}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{earns}\PY{l+s}{\PYZdq{}}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{money} - \PY{k}{print} \PY{l+s}{\PYZdq{}}\PY{l+s}{I am overriding the SoftwareEngineer class}\PY{l+s}{\PYZsq{}}\PY{l+s}{s salary method}\PY{l+s}{\PYZdq{}} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}42}]:} \PY{n}{c} \PY{o}{=} \PY{n}{Artist}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Nishanth}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+m+mi}{21}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}43}]:} \PY{n}{c}\PY{o}{.}\PY{n}{salary}\PY{p}{(}\PY{l+m+mi}{60000}\PY{p}{)} - \PY{n}{c}\PY{o}{.}\PY{n}{artform}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{Dancer}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -Nishanth earns 60000 -I am overriding the SoftwareEngineer class's salary method -Nishanth is a Dancer - \end{Verbatim} - - If not sure how many times methods will be called it will become -difficult to declare so many variables to carry each result hence it is -better to declare a list and append the result. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}44}]:} \PY{k}{class} \PY{n+nc}{emptylist}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{data} \PY{o}{=} \PY{p}{[}\PY{p}{]} - \PY{k}{def} \PY{n+nf}{one}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,}\PY{n}{x}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{data}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{x}\PY{p}{)} - \PY{k}{def} \PY{n+nf}{two}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{x} \PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{data}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{x}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)} - \PY{k}{def} \PY{n+nf}{three}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{n}{x}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{data}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{x}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{3}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}45}]:} \PY{n}{xc} \PY{o}{=} \PY{n}{emptylist}\PY{p}{(}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}46}]:} \PY{n}{xc}\PY{o}{.}\PY{n}{one}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{)} - \PY{k}{print} \PY{n}{xc}\PY{o}{.}\PY{n}{data} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1] - \end{Verbatim} - - Since xc.data is a list direct list operations can also be performed. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}47}]:} \PY{n}{xc}\PY{o}{.}\PY{n}{data}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{l+m+mi}{8}\PY{p}{)} - \PY{k}{print} \PY{n}{xc}\PY{o}{.}\PY{n}{data} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 8] - \end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}48}]:} \PY{n}{xc}\PY{o}{.}\PY{n}{two}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{)} - \PY{k}{print} \PY{n}{xc}\PY{o}{.}\PY{n}{data} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -[1, 8, 9] - \end{Verbatim} - - If the number of input arguments varies from instance to instance -asterisk can be used as shown. - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}49}]:} \PY{k}{class} \PY{n+nc}{NotSure}\PY{p}{:} - \PY{k}{def} \PY{n+nf}{\PYZus{}\PYZus{}init\PYZus{}\PYZus{}}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,} \PY{o}{*}\PY{n}{args}\PY{p}{)}\PY{p}{:} - \PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{data} \PY{o}{=} \PY{l+s}{\PYZsq{}}\PY{l+s}{\PYZsq{}}\PY{o}{.}\PY{n}{join}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n}{args}\PY{p}{)}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}50}]:} \PY{n}{yz} \PY{o}{=} \PY{n}{NotSure}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{I}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{Do}\PY{l+s}{\PYZsq{}} \PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{Not}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{Know}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{What}\PY{l+s}{\PYZsq{}}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{To}\PY{l+s}{\PYZsq{}}\PY{p}{,}\PY{l+s}{\PYZsq{}}\PY{l+s}{Type}\PY{l+s}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}51}]:} \PY{n}{yz}\PY{o}{.}\PY{n}{data} -\end{Verbatim} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{outcolor}Out[{\color{outcolor}51}]:} 'IDoNotKnowWhatToType' -\end{Verbatim} - - \section{Where to go from here?}\label{where-to-go-from-here} - - Practice alone can help you get the hang of python. Give your self -problem statements and solve them. You can also sign up to any -competitive coding platform for problem statements. The more you code -the more you discover and the more you start appreciating the language. - -Now that you have been introduced to python, You can try out the -different python libraries in the field of your interest. I highly -recommend you to check out this curated list of Python frameworks, -libraries and software http://awesome-python.com - -The official python documentation : https://docs.python.org/2/ - -Enjoy solving problem statements because life is short, you need python! - -Peace. - -Rajath Kumar M.P ( rajathkumar dot exe at gmail dot com) - - - % Add a bibliography block to the postdoc - - - - \end{document}