From 38d140c4dab175b47b314a3502689ef502956116 Mon Sep 17 00:00:00 2001 From: Maksim Bronsky Date: Fri, 30 Dec 2016 15:22:40 +0100 Subject: [PATCH] cleaned output cells, fixed markup cells --- .gitignore | 1 + .ipynb_checkpoints/00-checkpoint.ipynb | 171 -- .ipynb_checkpoints/01-checkpoint.ipynb | 1105 ------------- .ipynb_checkpoints/02-checkpoint.ipynb | 609 ------- .ipynb_checkpoints/03-checkpoint.ipynb | 2073 ------------------------ .ipynb_checkpoints/04-checkpoint.ipynb | 1209 -------------- .ipynb_checkpoints/05-checkpoint.ipynb | 589 ------- .ipynb_checkpoints/06-checkpoint.ipynb | 1075 ------------ .ipynb_checkpoints/07-checkpoint.ipynb | 1189 -------------- 00.ipynb | 20 +- 01.ipynb | 526 +----- 02.ipynb | 284 +--- 03.ipynb | 895 ++-------- 04.ipynb | 522 +----- 05.ipynb | 232 +-- 06.ipynb | 407 +---- 07.ipynb | 8 +- 17 files changed, 478 insertions(+), 10437 deletions(-) create mode 100644 .gitignore delete mode 100644 .ipynb_checkpoints/00-checkpoint.ipynb delete mode 100644 .ipynb_checkpoints/01-checkpoint.ipynb delete mode 100644 .ipynb_checkpoints/02-checkpoint.ipynb delete mode 100644 .ipynb_checkpoints/03-checkpoint.ipynb delete mode 100644 .ipynb_checkpoints/04-checkpoint.ipynb delete mode 100644 .ipynb_checkpoints/05-checkpoint.ipynb delete mode 100644 .ipynb_checkpoints/06-checkpoint.ipynb delete mode 100644 .ipynb_checkpoints/07-checkpoint.ipynb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..763513e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.ipynb_checkpoints diff --git a/.ipynb_checkpoints/00-checkpoint.ipynb b/.ipynb_checkpoints/00-checkpoint.ipynb deleted file mode 100644 index 4c95d8d..0000000 --- a/.ipynb_checkpoints/00-checkpoint.ipynb +++ /dev/null @@ -1,171 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "All the Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#Python-Lectures" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Introduction" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python is a modern, robust, high level programming language. It is very easy to pick up even if you are completely new to programming." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Installation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Mac OS X and Linux comes pre installed with python. Windows users can download python from https://www.python.org/downloads/ .\n", - "\n", - "To install IPython run,\n", - "\n", - " $ pip install ipython[all]\n", - " \n", - "This will install all the necessary dependencies for the notebook, qtconsole, tests etc." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###Installation from unofficial distributions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Installing all the necessary libraries might prove troublesome. Anaconda and Canopy comes pre packaged with all the necessary python libraries and also IPython." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "####Anaconda" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Download Anaconda from https://www.continuum.io/downloads\n", - "\n", - "Anaconda is completely free and includes more than 300 python packages. Both python 2.7 and 3.4 options are available." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "####Canopy" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Download Canopy from https://store.enthought.com/downloads/#default\n", - "\n", - "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." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Launching IPython Notebook" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "From the terminal\n", - "\n", - " ipython notebook\n", - "\n", - "In Canopy and Anaconda, Open the respective terminals and execute the above." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##How to learn from this resource?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Download all the ipython notebooks from this repository https://github.com/rajathkumarmp/Python-Lectures\n", - "\n", - "Launch ipython notebook from the folder which contains the notebooks. Open each one of them\n", - "\n", - " Cell > All Output > Clear\n", - " \n", - "This will clear all the outputs and now you can understand each statement and learn interactively." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##License" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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/" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/.ipynb_checkpoints/01-checkpoint.ipynb b/.ipynb_checkpoints/01-checkpoint.ipynb deleted file mode 100644 index a9668bc..0000000 --- a/.ipynb_checkpoints/01-checkpoint.ipynb +++ /dev/null @@ -1,1105 +0,0 @@ -{ - "cells": [ - { - "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" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The Zen of Python, by Tim Peters\n", - "\n", - "Beautiful is better than ugly.\n", - "Explicit is better than implicit.\n", - "Simple is better than complex.\n", - "Complex is better than complicated.\n", - "Flat is better than nested.\n", - "Sparse is better than dense.\n", - "Readability counts.\n", - "Special cases aren't special enough to break the rules.\n", - "Although practicality beats purity.\n", - "Errors should never pass silently.\n", - "Unless explicitly silenced.\n", - "In the face of ambiguity, refuse the temptation to guess.\n", - "There should be one-- and preferably only one --obvious way to do it.\n", - "Although that way may not be obvious at first unless you're Dutch.\n", - "Now is better than never.\n", - "Although never is often better than *right* now.\n", - "If the implementation is hard to explain, it's a bad idea.\n", - "If the implementation is easy to explain, it may be a good idea.\n", - "Namespaces are one honking great idea -- let's do more of those!\n" - ] - } - ], - "source": [ - "import this" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Variables" - ] - }, - { - "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," - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "x = 2\n", - "y = 5\n", - "xy = 'Hey'" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7 Hey\n" - ] - } - ], - "source": [ - "print x+y, xy" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Multiple variables can be assigned with the same value." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "x = y = 1" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 1\n" - ] - } - ], - "source": [ - "print x,y" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Operators" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Arithmetic Operators" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "| Symbol | Task Performed |\n", - "|----|---|\n", - "| + | Addition |\n", - "| - | Subtraction |\n", - "| / | division |\n", - "| % | mod |\n", - "| * | multiplication |\n", - "| // | floor division |\n", - "| ** | to the power of |" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "1+2" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "2-1" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "1*2" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "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 - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0.5" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "1/2.0" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "5" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "15%10" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Floor division is nothing but converting the result so obtained to the nearest integer." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1.0" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "2.8//2.0" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Relational Operators" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "| Symbol | Task Performed |\n", - "|----|---|\n", - "| == | True, if it is equal |\n", - "| != | True, if not equal to |\n", - "| < | less than |\n", - "| > | greater than |\n", - "| <= | less than or equal to |\n", - "| >= | greater than or equal to |" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "z = 1" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "z == 1" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "z > 1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Bitwise Operators" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "| Symbol | Task Performed |\n", - "|----|---|\n", - "| & | Logical And |\n", - "| l | Logical OR |\n", - "| ^ | XOR |\n", - "| ~ | Negate |\n", - "| >> | Right shift |\n", - "| << | Left shift |" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "a = 2 #10\n", - "b = 3 #11" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "0b10\n" - ] - } - ], - "source": [ - "print a & b\n", - "print bin(a&b)" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "5 >> 1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "0000 0101 -> 5 \n", - "\n", - "Shifting the digits by 1 to the right and zero padding\n", - "\n", - "0000 0010 -> 2" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "5 << 1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "0000 0101 -> 5 \n", - "\n", - "Shifting the digits by 1 to the left and zero padding\n", - "\n", - "0000 1010 -> 10" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#Built-in Functions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python comes loaded with pre-built functions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##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( )**." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'0xaa'" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "hex(170)" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "170" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "0xAA" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'010'" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "oct(8)" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "010" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "8\n", - "170\n", - "10\n" - ] - } - ], - "source": [ - "print int('010',8)\n", - "print int('0xaa',16)\n", - "print 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" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "7\n" - ] - } - ], - "source": [ - "print int(7.7)\n", - "print 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." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'b'" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "chr(98)" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "98" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ord('b')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##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. " - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false, - "scrolled": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "6.0\n", - "4.56\n" - ] - } - ], - "source": [ - "print round(5.6231) \n", - "print 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." - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5.38516480713\n" - ] - } - ], - "source": [ - "c =complex('5+2j')\n", - "print 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). " - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(4, 1)" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "divmod(9,2)" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n", - "True\n" - ] - } - ], - "source": [ - "print isinstance(1, int)\n", - "print isinstance(1.0,int)\n", - "print isinstance(1.0,(int,float))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**cmp(x,y)**\n", - "\n", - "|x ? y|Output|\n", - "|---|---|\n", - "| x < y | -1 |\n", - "| x == y | 0 |\n", - "| x > y | 1 |" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "-1\n", - "1\n", - "0\n" - ] - } - ], - "source": [ - "print cmp(1,2)\n", - "print cmp(2,1)\n", - "print cmp(2,2)" - ] - }, - { - "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)" - ] - }, - { - "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.)" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 2]\n", - "[2, 3, 4, 5, 6, 7, 8]\n", - "[2, 10, 18, 26]\n" - ] - } - ], - "source": [ - "print range(3)\n", - "print range(2,9)\n", - "print range(2,27,8)" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Type something here and it will be stored in variable abc \tHey\n" - ] - } - ], - "source": [ - "abc = raw_input(\"Type something here and it will be stored in variable abc \\t\")" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(abc)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**input( )**, this is used only for accepting only integer inputs." - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Only integer can be stored in in variable abc \t275\n" - ] - } - ], - "source": [ - "abc1 = input(\"Only integer can be stored in in variable abc \\t\")" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 38, - "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" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/.ipynb_checkpoints/02-checkpoint.ipynb b/.ipynb_checkpoints/02-checkpoint.ipynb deleted file mode 100644 index c4cb722..0000000 --- a/.ipynb_checkpoints/02-checkpoint.ipynb +++ /dev/null @@ -1,609 +0,0 @@ -{ - "cells": [ - { - "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": [ - "#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\" % " - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n" - ] - } - ], - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hey\n" - ] - } - ], - "source": [ - "print 'Hey'" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "My name is Rajath Kumar M.P.\n", - "\n", - "I love Python.\n" - ] - } - ], - "source": [ - "print \"\"\"My name is Rajath Kumar M.P.\n", - "\n", - "I love Python.\"\"\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n", - "Hello World !\n" - ] - } - ], - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "HelloWorld!\n" - ] - } - ], - "source": [ - "print 'Hello' + string1 + string2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**%s** is used to refer to a variable which contains a string." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n" - ] - } - ], - "source": [ - "print \"Hello %s\" % string1" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "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" - ] - } - ], - "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" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "When referring to multiple variables parenthesis is used." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World !\n" - ] - } - ], - "source": [ - "print \"Hello %s %s\" %(string1,string2)" - ] - }, - { - "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." - ] - }, - { - "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 - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I want \\n to be printed.\n" - ] - } - ], - "source": [ - "print \"I want \\\\n to be printed.\"" - ] - }, - { - "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", - "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'" - ] - }, - "execution_count": 14, - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'3.12131'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "\"%.5f\" % 3.121312312312" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "' 3.12131'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "\"%9.5f\" % 3.121312312312" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Zero padding is done by adding a 0 at the start of fieldwidth." - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'00000000000003.12131'" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "\"%020.5f\" % 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." - ] - }, - { - "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", - "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" - ] - } - ], - "source": [ - "print \"%+9f\" % 3.121312312312\n", - "print \"% 9f\" % -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." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'3.121 '" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "\"%-9.3f\" % 3.121312312312" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/.ipynb_checkpoints/03-checkpoint.ipynb b/.ipynb_checkpoints/03-checkpoint.ipynb deleted file mode 100644 index 7a9de7f..0000000 --- a/.ipynb_checkpoints/03-checkpoint.ipynb +++ /dev/null @@ -1,2073 +0,0 @@ -{ - "cells": [ - { - "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": [ - "#Data Structures" - ] - }, - { - "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" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a = []" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "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']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Indexing" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'apple'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'orange'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x[-1]" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "y = ['carrot','potato']" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[['apple', 'orange'], ['carrot', 'potato']]\n" - ] - } - ], - "source": [ - "z = [x,y]\n", - "print z" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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'." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'orange']\n" - ] - } - ], - "source": [ - "z1 = z[0]\n", - "print 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." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'apple'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "z1[0]" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'apple'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "z[0][0]" - ] - }, - { - "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[ ][ ][ ]." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Slicing" - ] - }, - { - "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", - "\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." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "num = [0,1,2,3,4,5,6,7,8,9]" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 2, 3]\n", - "[4, 5, 6, 7, 8, 9]\n" - ] - } - ], - "source": [ - "print num[0:4]\n", - "print num[4:]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You can also slice a parent list with a fixed length or step length." - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 3, 6]" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "num[:9:3]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###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." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(num)" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "min(num)" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "9" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "max(num)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 5, 4, 7]" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[1,2,3] + [5,4,7]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "names = ['Earth','Air','Fire','Water']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'Fire' in names" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'Rajath' in names" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "mlist = ['bzaa','ds','nc','az','z','klm']" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "z\n", - "az\n" - ] - } - ], - "source": [ - "print max(mlist)\n", - "print min(mlist)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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?" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "nlist = ['1','94','93','1000']" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "94\n", - "1\n" - ] - } - ], - "source": [ - "print max(nlist)\n", - "print 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." - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Earth\n", - "Air\n" - ] - } - ], - "source": [ - "print max(names, key=len)\n", - "print 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", - "\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." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['h', 'e', 'l', 'l', 'o']" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "list('hello')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**append( )** is used to add a element at the end of the list." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "lst = [1,1,4,8,7]" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1]\n" - ] - } - ], - "source": [ - "lst.append(1)\n", - "print lst" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**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 - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "lst.count(1)" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "lst1 = [5,4,2,8]" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8]]\n" - ] - } - ], - "source": [ - "lst.append(lst1)\n", - "print lst" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": false - }, - "source": [ - "But if nested list is not what is desired then **extend( )** function can be used." - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "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" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "lst.index(1)" - ] - }, - { - "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. " - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] - } - ], - "source": [ - "lst.insert(5, 'name')\n", - "print 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." - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] - } - ], - "source": [ - "lst[5] = 'Python'\n", - "print 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." - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "lst.pop()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Index value can be specified to pop a ceratin element corresponding to that index value." - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "lst.pop(0)" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n" - ] - } - ], - "source": [ - "lst.remove('Python')\n", - "print lst" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Alternative to **remove** function but with using index value is **del**" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n" - ] - } - ], - "source": [ - "del lst[1]\n", - "print lst" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": false - }, - "source": [ - "The entire elements present in the list can be reversed by using the **reverse()** function." - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 4, 5, [5, 4, 2, 8], 1, 7, 8, 1]\n" - ] - } - ], - "source": [ - "lst.reverse()\n", - "print lst" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 2, 4, 5, 7, 8, [5, 4, 2, 8]]\n" - ] - } - ], - "source": [ - "lst.sort()\n", - "print lst" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[5, 4, 2, 8], 8, 7, 5, 4, 2, 1, 1]\n" - ] - } - ], - "source": [ - "lst.sort(reverse=True)\n", - "print 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." - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Air', 'Earth', 'Fire', 'Water']\n", - "['Water', 'Fire', 'Earth', 'Air']\n" - ] - } - ], - "source": [ - "names.sort()\n", - "print names\n", - "names.sort(reverse=True)\n", - "print names" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To sort based on length key=len should be specified as shown." - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Air', 'Fire', 'Water', 'Earth']\n", - "['Water', 'Earth', 'Fire', 'Air']\n" - ] - } - ], - "source": [ - "names.sort(key=len)\n", - "print names\n", - "names.sort(key=len,reverse=True)\n", - "print names" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Copying a list" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Most of the new python programmers commit this mistake. Consider the following," - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "lista= [2,1,4,3]" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] - } - ], - "source": [ - "listb = lista\n", - "print 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." - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4]\n", - "[2, 1, 4, 9]\n" - ] - } - ], - "source": [ - "lista.pop()\n", - "print lista\n", - "lista.append(9)\n", - "print lista" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 9]\n" - ] - } - ], - "source": [ - "print 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", - "\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." - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "lista = [2,1,4,3]" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] - } - ], - "source": [ - "listb = lista[:]\n", - "print 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" - ] - } - ], - "source": [ - "lista.pop()\n", - "print lista\n", - "lista.append(9)\n", - "print lista" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] - } - ], - "source": [ - "print listb" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##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." - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(3, 1)\n", - "\n" - ] - } - ], - "source": [ - "xyz = divmod(10,3)\n", - "print xyz\n", - "print type(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." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To define a tuple, A variable is assigned to paranthesis ( ) or tuple( )." - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "tup = ()\n", - "tup2 = tuple()" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(27,)" - ] - }, - "execution_count": 55, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "27," - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "27 when multiplied by 2 yields 54, But when multiplied with a tuple the data is repeated twice." - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(27, 27)" - ] - }, - "execution_count": 56, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "2*(27,)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1, 2, 3)\n", - "('H', 'e', 'l', 'l', 'o')\n" - ] - } - ], - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "('H', 'e', 'l')\n" - ] - } - ], - "source": [ - "print tup3[1]\n", - "tup5 = tup4[:3]\n", - "print tup5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Mapping one tuple to another" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "(a,b,c)= ('alpha','beta','gamma')" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "alpha beta gamma\n" - ] - } - ], - "source": [ - "print a,b,c" - ] - }, - { - "cell_type": "code", - "execution_count": 61, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('R', 'a', 'j', 'a', 't', 'h', 'K', 'u', 'm', 'a', 'r', 'M', 'P')\n" - ] - } - ], - "source": [ - "d = tuple('RajathKumarMP')\n", - "print d" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Built In Tuple functions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**count()** function counts the number of specified element that is present in the tuple." - ] - }, - { - "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 62, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.count('a')" - ] - }, - { - "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" - ] - }, - { - "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.index('a')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##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", - "\n", - "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 - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "set1 = set()\n", - "print type(set1)" - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "set([1, 2, 3, 4])\n" - ] - } - ], - "source": [ - "set0 = set([1,2,2,3,3,4])\n", - "print set0" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "elements 2,3 which are repeated twice are seen only once. Thus in a set each element is distinct." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###Built-in Functions" - ] - }, - { - "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "set1 = set([1,2,3])" - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "set2 = set([2,3,4,5])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**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 - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 2, 3, 4, 5}" - ] - }, - "execution_count": 68, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set1.union(set2)" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 69, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{0, 1, 2, 3}" - ] - }, - "execution_count": 69, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set1.add(0)\n", - "set1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**intersection( )** function outputs a set which contains all the elements that are in both sets." - ] - }, - { - "cell_type": "code", - "execution_count": 70, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{2, 3}" - ] - }, - "execution_count": 70, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set1.intersection(set2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**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 - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{0, 1}" - ] - }, - "execution_count": 71, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set1.difference(set2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**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 - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{0, 1, 4, 5}" - ] - }, - "execution_count": 72, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set2.symmetric_difference(set1)" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 73, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 73, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set1.issubset(set2)" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 74, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set2.isdisjoint(set1)" - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 75, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set2.issuperset(set1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**pop( )** is used to remove an arbitrary element in the set" - ] - }, - { - "cell_type": "code", - "execution_count": 76, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "set([1, 2, 3])\n" - ] - } - ], - "source": [ - "set1.pop()\n", - "print set1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**remove( )** function deletes the specified element from the set." - ] - }, - { - "cell_type": "code", - "execution_count": 77, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 3}" - ] - }, - "execution_count": 77, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set1.remove(2)\n", - "set1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**clear( )** is used to clear all the elements and make that set an empty set." - ] - }, - { - "cell_type": "code", - "execution_count": 78, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "set()" - ] - }, - "execution_count": 78, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set1.clear()\n", - "set1" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/.ipynb_checkpoints/04-checkpoint.ipynb b/.ipynb_checkpoints/04-checkpoint.ipynb deleted file mode 100644 index 5a502ea..0000000 --- a/.ipynb_checkpoints/04-checkpoint.ipynb +++ /dev/null @@ -1,1209 +0,0 @@ -{ - "cells": [ - { - "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": [ - "##Strings" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "String0 = 'Taj Mahal is beautiful'\n", - "String1 = \"Taj Mahal is beautiful\"\n", - "String2 = '''Taj Mahal\n", - "is\n", - "beautiful'''" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Taj Mahal is beautiful \n", - "Taj Mahal is beautiful \n", - "Taj Mahal\n", - "is\n", - "beautiful \n" - ] - } - ], - "source": [ - "print String0 , type(String0)\n", - "print String1, type(String1)\n", - "print String2, type(String2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "String Indexing and Slicing are similar to Lists which was explained in detail earlier." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "M\n", - "Mahal is beautiful\n" - ] - } - ], - "source": [ - "print String0[4]\n", - "print String0[4:]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###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." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "-1\n" - ] - } - ], - "source": [ - "print String0.find('al')\n", - "print String0.find('am')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The index value returned is the index of the first element in the input data." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "a\n" - ] - } - ], - "source": [ - "print String0[7]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "One can also input **find( )** function between which index values it has to search." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "2\n" - ] - } - ], - "source": [ - "print String0.find('j',1)\n", - "print String0.find('j',1,3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**capitalize( )** is used to capitalize the first element in the string." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Observe the first letter in this sentence.\n" - ] - } - ], - "source": [ - "String3 = 'observe the first letter in this sentence.'\n", - "print String3.capitalize()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**center( )** is used to center align the string by specifying the field width." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "' Taj Mahal is beautiful '" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "String0.center(70)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "One can also fill the left out spaces with any other character." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'------------------------Taj Mahal is beautiful------------------------'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "String0.center(70,'-')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**zfill( )** is used for zero padding by specifying the field width." - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'00000000Taj Mahal is beautiful'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "String0.zfill(30)" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "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" - ] - } - ], - "source": [ - "s = 'h\\te\\tl\\tl\\to'\n", - "print s\n", - "print s.expandtabs(1)\n", - "print s.expandtabs()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "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" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "4\n" - ] - }, - { - "ename": "ValueError", - "evalue": "substring not found", - "output_type": "error", - "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;31mValueError\u001b[0m: substring not found" - ] - } - ], - "source": [ - "print String0.index('Taj')\n", - "print String0.index('Mahal',0)\n", - "print String0.index('Mahal',10,20)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": false - }, - "source": [ - "**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 - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "False\n" - ] - } - ], - "source": [ - "print String0.endswith('y')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The start and stop index values can also be specified." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n" - ] - } - ], - "source": [ - "print String0.endswith('l',0)\n", - "print String0.endswith('M',0,5)" - ] - }, - { - "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)" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4\n", - "2\n" - ] - } - ], - "source": [ - "print String0.count('a',0)\n", - "print String0.count('a',5,10)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": false - }, - "source": [ - "**join( )** function is used add a char in between the elements of the input string." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'*a_a-'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'a'.join('*_-')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "'*_-' is the input string and char 'a' is added in between each element" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**join( )** function can also be used to convert a list into a string." - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "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" - ] - } - ], - "source": [ - "a = list(String0)\n", - "print a\n", - "b = ''.join(a)\n", - "print 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." - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " /i/s/ /b/e/a/u/t/i/f/u/l\n" - ] - } - ], - "source": [ - "c = '/'.join(a)[18:]\n", - "print c" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": false - }, - "source": [ - "**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 - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n" - ] - } - ], - "source": [ - "d = c.split('/')\n", - "print 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." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l']\n", - "4\n" - ] - } - ], - "source": [ - "e = c.split('/',3)\n", - "print e\n", - "print len(e)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**lower( )** converts any capital letter to small letter." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Taj Mahal is beautiful\n", - "taj mahal is beautiful\n" - ] - } - ], - "source": [ - "print String0\n", - "print String0.lower()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**upper( )** converts any small letter to capital letter." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'TAJ MAHAL IS BEAUTIFUL'" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "String0.upper()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**replace( )** function replaces the element with another element." - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Bengaluru is beautiful'" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "String0.replace('Taj Mahal','Bengaluru')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**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 - }, - "outputs": [], - "source": [ - "f = ' hello '" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello'" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "f.strip()" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "f = ' ***----hello---******* '" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "' ***----hello---******* '" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "f.strip('*')" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "----hello---******* \n", - " ***----hello---\n" - ] - } - ], - "source": [ - "print f.lstrip(' *')\n", - "print f.rstrip(' *')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##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." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To define a dictionary, equate a variable to { } or dict()" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " \n" - ] - } - ], - "source": [ - "d0 = {}\n", - "d1 = dict()\n", - "print 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." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'OneTwo': 12, 'One': 1}\n" - ] - } - ], - "source": [ - "d0['One'] = 1\n", - "d0['OneTwo'] = 12 \n", - "print 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'" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n" - ] - } - ], - "source": [ - "print d0['One']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Two lists which are related can be merged to form a dictionary." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "names = ['One', 'Two', 'Three', 'Four', 'Five']\n", - "numbers = [1, 2, 3, 4, 5]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**zip( )** function is used to combine two lists" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)]\n" - ] - } - ], - "source": [ - "d2 = zip(names,numbers)\n", - "print d2" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": false - }, - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n" - ] - } - ], - "source": [ - "a1 = dict(d2)\n", - "print a1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###Built-in Functions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**clear( )** function is used to erase the entire database that was created." - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{}\n" - ] - } - ], - "source": [ - "a1.clear()\n", - "print a1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Dictionary can also be built using loops." - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n" - ] - } - ], - "source": [ - "for i in range(len(names)):\n", - " a1[names[i]] = numbers[i]\n", - "print a1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**values( )** function returns a list with all the assigned values in the dictionary." - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[4, 5, 3, 2, 1]" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a1.values()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**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 - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['Four', 'Five', 'Three', 'Two', 'One']" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a1.keys()" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[('Four', 4), ('Five', 5), ('Three', 3), ('Two', 2), ('One', 1)]" - ] - }, - "execution_count": 40, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a1.items()" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n", - "4\n" - ] - } - ], - "source": [ - "a2 = a1.pop('Four')\n", - "print a1\n", - "print a2" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/.ipynb_checkpoints/05-checkpoint.ipynb b/.ipynb_checkpoints/05-checkpoint.ipynb deleted file mode 100644 index d24f4ac..0000000 --- a/.ipynb_checkpoints/05-checkpoint.ipynb +++ /dev/null @@ -1,589 +0,0 @@ -{ - "cells": [ - { - "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": [ - "#Control Flow Statements" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##If" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "if some_condition:\n", - " \n", - " algorithm" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello\n" - ] - } - ], - "source": [ - "x = 12\n", - "if x >10:\n", - " print \"Hello\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##If-else" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "if some_condition:\n", - " \n", - " algorithm\n", - " \n", - "else:\n", - " \n", - " algorithm" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello\n" - ] - } - ], - "source": [ - "x = 12\n", - "if x > 10:\n", - " print \"hello\"\n", - "else:\n", - " print \"world\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##if-elif" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": false - }, - "source": [ - "if some_condition:\n", - " \n", - " algorithm\n", - "\n", - "elif some_condition:\n", - " \n", - " algorithm\n", - "\n", - "else:\n", - " \n", - " algorithm" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x y:\n", - " print \"x>y\"\n", - "elif x < y:\n", - " print \"x y:\n", - " print \"x>y\"\n", - "elif x < y:\n", - " print \"x=7:\n", - " break" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Continue" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This continues the rest of the loop. Sometimes when a condition is satisfied there are chances of the loop getting terminated. This can be avoided using continue statement. " - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "1\n", - "2\n", - "3\n", - "4\n", - "The end.\n", - "The end.\n", - "The end.\n", - "The end.\n", - "The end.\n" - ] - } - ], - "source": [ - "for i in range(10):\n", - " if i>4:\n", - " print \"The end.\"\n", - " continue\n", - " elif i<7:\n", - " print i" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##List Comprehensions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python makes it simple to generate a required list with a single line of code using list comprehensions. For example If i need to generate multiples of say 27 I write the code using for loop as," - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n" - ] - } - ], - "source": [ - "res = []\n", - "for i in range(1,11):\n", - " x = 27*i\n", - " res.append(x)\n", - "print res" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Since you are generating another list altogether and that is what is required, List comprehensions is a more efficient way to solve this problem." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[27*x for x in range(1,11)]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "That's it!. Only remember to enclose it in square brackets" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Understanding the code, The first bit of the code is always the algorithm and then leave a space and then write the necessary loop. But you might be wondering can nested loops be extended to list comprehensions? Yes you can." - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[27*x for x in range(1,20) if x<=10]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let me add one more loop to make you understand better, " - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[27*z for i in range(50) if i==27 for z in range(1,11)]" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/.ipynb_checkpoints/06-checkpoint.ipynb b/.ipynb_checkpoints/06-checkpoint.ipynb deleted file mode 100644 index a642528..0000000 --- a/.ipynb_checkpoints/06-checkpoint.ipynb +++ /dev/null @@ -1,1075 +0,0 @@ -{ - "cells": [ - { - "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" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This is the basic syntax of a function" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "def funcname(arg1, arg2,... argN):\n", - " \n", - " ''' Document String'''\n", - "\n", - " statements\n", - "\n", - "\n", - " return " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Read the above syntax as, A function by name \"funcname\" is defined, which accepts arguements \"arg1,arg2,....argN\". The function is documented and it is '''Document String'''. The function after executing the statements returns a \"value\"." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hey Rajath!\n", - "Rajath, How do you do?\n" - ] - } - ], - "source": [ - "print \"Hey Rajath!\"\n", - "print \"Rajath, How do you do?\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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. \n", - "\n", - "Defining a function firstfunc()." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def firstfunc():\n", - " print \"Hey Rajath!\"\n", - " print \"Rajath, How do you do?\" " - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hey Rajath!\n", - "Rajath, How do you do?\n" - ] - } - ], - "source": [ - "firstfunc()" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def firstfunc(username):\n", - " print \"Hey\", username + '!'\n", - " print username + ',' ,\"How do you do?\"" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Please enter your name : Guido\n" - ] - } - ], - "source": [ - "name1 = raw_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." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hey Guido!\n", - "Guido, How do you do?\n" - ] - } - ], - "source": [ - "firstfunc(name1)" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def firstfunc(username):\n", - " print \"Hey\", username + '!'\n", - " print username + ',' ,\"How do you do?\"\n", - "def secondfunc():\n", - " name = raw_input(\"Please enter your name : \")\n", - " firstfunc(name)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Please enter your name : karthik\n", - "Hey karthik!\n", - "karthik, How do you do?\n" - ] - } - ], - "source": [ - "secondfunc()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Return Statement" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def times(x,y):\n", - " z = x*y\n", - " return z" - ] - }, - { - "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" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "20\n" - ] - } - ], - "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." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Instead of declaring another variable the entire statement itself can be used in the return statement as shown." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def times(x,y):\n", - " '''This multiplies the two input arguments'''\n", - " return x*y" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "20\n" - ] - } - ], - "source": [ - "c = times(4,5)\n", - "print c" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function times in module __main__:\n", - "\n", - "times(x, y)\n", - " This multiplies the two input arguments\n", - "\n" - ] - } - ], - "source": [ - "help(times)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Multiple variable can also be returned, But keep in mind the order." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "eglist = [10,50,30,12,6,8,100]" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def egfunc(eglist):\n", - " highest = max(eglist)\n", - " lowest = min(eglist)\n", - " first = eglist[0]\n", - " last = eglist[-1]\n", - " return highest,lowest,first,last" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(100, 6, 10, 100)" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "egfunc(eglist)" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " a = 100 \n", - " b = 6 \n", - " c = 10 \n", - " d = 100\n" - ] - } - ], - "source": [ - "a,b,c,d = egfunc(eglist)\n", - "print ' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Implicit 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." - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def implicitadd(x,y=3):\n", - " return x+y" - ] - }, - { - "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." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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 - }, - "outputs": [ - { - "data": { - "text/plain": [ - "7" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "implicitadd(4)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "But if the second argument is specified then this value overrides the implicit value assigned to the argument " - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "implicitadd(4,4)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Any number of arguments" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def add_n(*args):\n", - " res = 0\n", - " reslist = []\n", - " for i in args:\n", - " reslist.append(i)\n", - " print reslist\n", - " return sum(reslist)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4, 5]\n" - ] - }, - { - "data": { - "text/plain": [ - "15" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add_n(1,2,3,4,5)" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3]\n" - ] - }, - { - "data": { - "text/plain": [ - "6" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add_n(1,2,3)" - ] - }, - { - "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]" - ] - }, - { - "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]" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "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" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "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 : [1, 2, 3, 4, 5, 6]\n" - ] - } - ], - "source": [ - "egfunc1()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "##Lambda Functions" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "z = lambda x: x * x" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "64" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "z(8)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###map" - ] - }, - { - "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]" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[3, 4, 5, 6, 7, 8, 9, 10, 11]\n" - ] - } - ], - "source": [ - "eg = map(lambda x:x+2, list1)\n", - "print eg" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You can also add two lists." - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "list2 = [9,8,7,6,5,4,3,2,1]" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 10, 10, 10, 10, 10, 10, 10, 10]\n" - ] - } - ], - "source": [ - "eg2 = map(lambda x,y:x+y, list1,list2)\n", - "print eg2" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": false - }, - "source": [ - "Not only lambda function but also other built in functions can also be used." - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['10', '10', '10', '10', '10', '10', '10', '10', '10']\n" - ] - } - ], - "source": [ - "eg3 = map(str,eg2)\n", - "print eg3" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "###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." - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "list1 = [1,2,3,4,5,6,7,8,9]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To get the elements which are less than 5," - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4]" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "filter(lambda x:x<5,list1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Notice what happens when **map()** is used." - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[True, True, True, True, False, False, False, False, False]" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[4, 8]" - ] - }, - "execution_count": 40, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "filter(lambda x:x%4==0,list1)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/.ipynb_checkpoints/07-checkpoint.ipynb b/.ipynb_checkpoints/07-checkpoint.ipynb deleted file mode 100644 index f233280..0000000 --- a/.ipynb_checkpoints/07-checkpoint.ipynb +++ /dev/null @@ -1,1189 +0,0 @@ -{ - "cells": [ - { - "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": [ - "#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." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "A class is declared as follows" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "class class_name:\n", - "\n", - " Functions" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "class FirstClass:\n", - " pass" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**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\"" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "egclass = FirstClass()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "instance" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(egclass)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "classobj" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(FirstClass)" - ] - }, - { - "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", - "\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. " - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "class FirstClass:\n", - " def __init__(self,name,symbol):\n", - " self.name = name\n", - " self.symbol = symbol" - ] - }, - { - "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. " - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "eg1 = FirstClass('one',1)\n", - "eg2 = FirstClass('two',2)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "one 1\n", - "two 2\n" - ] - } - ], - "source": [ - "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" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__', '__init__', '__module__']" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(FirstClass)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**dir( )** of an instance also shows it's defined attributes." - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__', '__init__', '__module__', 'name', 'symbol']" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(eg1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Changing the FirstClass function a bit," - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, - "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," - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "eg1 = FirstClass('one',1)\n", - "eg2 = FirstClass('two',2)" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "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'" - ] - } - ], - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__', '__init__', '__module__', 'n', 's']" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(eg1)" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "one 1\n", - "two 2\n" - ] - } - ], - "source": [ - "print eg1.n, eg1.s\n", - "print eg2.n, eg2.s" - ] - }, - { - "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." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "class FirstClass:\n", - " def __init__(asdf1234,name,symbol):\n", - " asdf1234.n = name\n", - " asdf1234.s = symbol" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "eg1 = FirstClass('one',1)\n", - "eg2 = FirstClass('two',2)" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "one 1\n", - "two 2\n" - ] - } - ], - "source": [ - "print eg1.n, eg1.s\n", - "print eg2.n, eg2.s" - ] - }, - { - "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" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__', '__init__', '__module__', 'cube', 'n', 's']" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(eg1)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "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", - "\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" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here test is a class attribute and name is a instance attribute." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": true - }, - "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" - ] - }, - { - "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)" - ] - }, - { - "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 - }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 28, - "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)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/00.ipynb b/00.ipynb index 7298166..c6e3ea0 100644 --- a/00.ipynb +++ b/00.ipynb @@ -11,14 +11,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#Python-Lectures" + "# Python-Lectures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##Introduction" + "## Introduction" ] }, { @@ -32,7 +32,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Installation" + "## Installation" ] }, { @@ -52,7 +52,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Installation from unofficial distributions" + "### Installation from unofficial distributions" ] }, { @@ -66,7 +66,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "####Anaconda" + "#### Anaconda" ] }, { @@ -82,7 +82,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "####Canopy" + "#### Canopy" ] }, { @@ -98,7 +98,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Launching IPython Notebook" + "## Launching IPython Notebook" ] }, { @@ -116,7 +116,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##How to learn from this resource?" + "## How to learn from this resource?" ] }, { @@ -136,7 +136,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##License" + "## License" ] }, { @@ -163,7 +163,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.12" } }, "nbformat": 4, diff --git a/01.ipynb b/01.ipynb index 42e738e..3a5e2d4 100644 --- a/01.ipynb +++ b/01.ipynb @@ -11,44 +11,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#The Zen Of Python" + "# The Zen Of Python" ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The Zen of Python, by Tim Peters\n", - "\n", - "Beautiful is better than ugly.\n", - "Explicit is better than implicit.\n", - "Simple is better than complex.\n", - "Complex is better than complicated.\n", - "Flat is better than nested.\n", - "Sparse is better than dense.\n", - "Readability counts.\n", - "Special cases aren't special enough to break the rules.\n", - "Although practicality beats purity.\n", - "Errors should never pass silently.\n", - "Unless explicitly silenced.\n", - "In the face of ambiguity, refuse the temptation to guess.\n", - "There should be one-- and preferably only one --obvious way to do it.\n", - "Although that way may not be obvious at first unless you're Dutch.\n", - "Now is better than never.\n", - "Although never is often better than *right* now.\n", - "If the implementation is hard to explain, it's a bad idea.\n", - "If the implementation is easy to explain, it may be a good idea.\n", - "Namespaces are one honking great idea -- let's do more of those!\n" - ] - } - ], + "outputs": [], "source": [ "import this" ] @@ -69,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": true }, @@ -82,19 +54,11 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7 Hey\n" - ] - } - ], + "outputs": [], "source": [ "print x+y, xy" ] @@ -108,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": true }, @@ -119,19 +83,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 1\n" - ] - } - ], + "outputs": [], "source": [ "print x,y" ] @@ -147,7 +103,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Arithmetic Operators" + "## Arithmetic Operators" ] }, { @@ -167,88 +123,44 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "1+2" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "2-1" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "1*2" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "1/2" ] @@ -262,44 +174,22 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "0.5" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "1/2.0" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "5" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "15%10" ] @@ -313,22 +203,11 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "1.0" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "2.8//2.0" ] @@ -337,7 +216,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Relational Operators" + "## Relational Operators" ] }, { @@ -356,7 +235,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { "collapsed": true }, @@ -367,44 +246,22 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "z == 1" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "z > 1" ] @@ -413,7 +270,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Bitwise Operators" + "## Bitwise Operators" ] }, { @@ -432,7 +289,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": { "collapsed": true }, @@ -444,20 +301,11 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "0b10\n" - ] - } - ], + "outputs": [], "source": [ "print a & b\n", "print bin(a&b)" @@ -465,22 +313,11 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "5 >> 1" ] @@ -498,22 +335,11 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "5 << 1" ] @@ -533,7 +359,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#Built-in Functions" + "# Built-in Functions" ] }, { @@ -547,7 +373,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Conversion from one system to another" + "## Conversion from one system to another" ] }, { @@ -559,88 +385,44 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'0xaa'" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "hex(170)" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "170" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "0xAA" ] }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'010'" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "oct(8)" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "010" ] @@ -654,21 +436,11 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "8\n", - "170\n", - "10\n" - ] - } - ], + "outputs": [], "source": [ "print int('010',8)\n", "print int('0xaa',16)\n", @@ -684,20 +456,11 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "7\n" - ] - } - ], + "outputs": [], "source": [ "print int(7.7)\n", "print int('7')" @@ -712,44 +475,22 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'b'" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "chr(98)" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "98" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "ord('b')" ] @@ -758,7 +499,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Simplifying Arithmetic Operations" + "## Simplifying Arithmetic Operations" ] }, { @@ -770,21 +511,12 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": { "collapsed": false, "scrolled": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "6.0\n", - "4.56\n" - ] - } - ], + "outputs": [], "source": [ "print round(5.6231) \n", "print round(4.55892, 2)" @@ -799,19 +531,11 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5.38516480713\n" - ] - } - ], + "outputs": [], "source": [ "c =complex('5+2j')\n", "print abs(c)" @@ -826,22 +550,11 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(4, 1)" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "divmod(9,2)" ] @@ -855,21 +568,11 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n", - "True\n" - ] - } - ], + "outputs": [], "source": [ "print isinstance(1, int)\n", "print isinstance(1.0,int)\n", @@ -891,21 +594,11 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "-1\n", - "1\n", - "0\n" - ] - } - ], + "outputs": [], "source": [ "print cmp(1,2)\n", "print cmp(2,1)\n", @@ -921,20 +614,11 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "27\n", - "2\n" - ] - } - ], + "outputs": [], "source": [ "print pow(3,3)\n", "print pow(3,3,5)" @@ -949,21 +633,11 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 2]\n", - "[2, 3, 4, 5, 6, 7, 8]\n", - "[2, 10, 18, 26]\n" - ] - } - ], + "outputs": [], "source": [ "print range(3)\n", "print range(2,9)\n", @@ -974,7 +648,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Accepting User Inputs" + "## Accepting User Inputs" ] }, { @@ -986,41 +660,22 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Type something here and it will be stored in variable abc \tHey\n" - ] - } - ], + "outputs": [], "source": [ "abc = raw_input(\"Type something here and it will be stored in variable abc \\t\")" ] }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(abc)" ] @@ -1034,41 +689,22 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Only integer can be stored in variable abc \t275\n" - ] - } - ], + "outputs": [], "source": [ "abc1 = input(\"Only integer can be stored in variable abc \\t\")" ] }, { "cell_type": "code", - "execution_count": 38, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(abc1)" ] @@ -1097,7 +733,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.12" } }, "nbformat": 4, diff --git a/02.ipynb b/02.ipynb index c4cb722..8f92d21 100644 --- a/02.ipynb +++ b/02.ipynb @@ -11,7 +11,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#Print Statement" + "# Print Statement" ] }, { @@ -28,19 +28,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n" - ] - } - ], + "outputs": [], "source": [ "print \"Hello World\"" ] @@ -56,40 +48,22 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hey\n" - ] - } - ], + "outputs": [], "source": [ "print 'Hey'" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "My name is Rajath Kumar M.P.\n", - "\n", - "I love Python.\n" - ] - } - ], + "outputs": [], "source": [ "print \"\"\"My name is Rajath Kumar M.P.\n", "\n", @@ -105,21 +79,12 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n", - "Hello World !\n" - ] - } - ], + "outputs": [], "source": [ "string1 = 'World'\n", "print 'Hello', string1\n", @@ -137,19 +102,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "HelloWorld!\n" - ] - } - ], + "outputs": [], "source": [ "print 'Hello' + string1 + string2" ] @@ -163,19 +120,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World\n" - ] - } - ], + "outputs": [], "source": [ "print \"Hello %s\" % string1" ] @@ -198,23 +147,11 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "collapsed": false }, - "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" - ] - } - ], + "outputs": [], "source": [ "print \"Actual Number = %d\" %18\n", "print \"Float of the number = %f\" %18\n", @@ -232,19 +169,11 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello World !\n" - ] - } - ], + "outputs": [], "source": [ "print \"Hello %s %s\" %(string1,string2)" ] @@ -253,7 +182,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Other Examples" + "## Other Examples" ] }, { @@ -265,107 +194,55 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I want %d to be printed here\n" - ] - } - ], + "outputs": [], "source": [ "print \"I want %%d to be printed %s\" %'here'" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "_A_A_A_A_A_A_A_A_A_A\n" - ] - } - ], + "outputs": [], "source": [ "print '_A'*10" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "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" - ] - } - ], + "outputs": [], "source": [ "print \"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\"" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I want \\n to be printed.\n" - ] - } - ], + "outputs": [], "source": [ "print \"I want \\\\n to be printed.\"" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Routine:\n", - "\t- Eat\n", - "\t- Sleep\n", - "\t- Repeat\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "print \"\"\"\n", "Routine:\n", @@ -378,7 +255,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#PrecisionWidth and FieldWidth" + "# PrecisionWidth and FieldWidth" ] }, { @@ -392,22 +269,11 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'3.121312'" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"%f\" % 3.121312312312" ] @@ -421,22 +287,11 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'3.12131'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"%.5f\" % 3.121312312312" ] @@ -450,22 +305,11 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "' 3.12131'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"%9.5f\" % 3.121312312312" ] @@ -479,22 +323,11 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'00000000000003.12131'" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"%020.5f\" % 3.121312312312" ] @@ -508,20 +341,11 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " 3.121312\n", - "-3.121312\n" - ] - } - ], + "outputs": [], "source": [ "print \"% 9f\" % 3.121312312312\n", "print \"% 9f\" % -3.121312312312" @@ -536,20 +360,11 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "+3.121312\n", - "-3.121312\n" - ] - } - ], + "outputs": [], "source": [ "print \"%+9f\" % 3.121312312312\n", "print \"% 9f\" % -3.121312312312" @@ -564,22 +379,11 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'3.121 '" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"%-9.3f\" % 3.121312312312" ] @@ -601,7 +405,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.12" } }, "nbformat": 4, diff --git a/03.ipynb b/03.ipynb index 7a9de7f..469d364 100644 --- a/03.ipynb +++ b/03.ipynb @@ -11,7 +11,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#Data Structures" + "# Data Structures" ] }, { @@ -25,7 +25,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Lists" + "## Lists" ] }, { @@ -39,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "collapsed": false }, @@ -50,19 +50,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "outputs": [], "source": [ "print type(a)" ] @@ -76,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "collapsed": false }, @@ -101,22 +93,11 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'apple'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "x[0]" ] @@ -130,22 +111,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'orange'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "x[-1]" ] @@ -159,7 +129,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "collapsed": false }, @@ -177,19 +147,11 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[['apple', 'orange'], ['carrot', 'potato']]\n" - ] - } - ], + "outputs": [], "source": [ "z = [x,y]\n", "print z" @@ -207,19 +169,11 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['apple', 'orange']\n" - ] - } - ], + "outputs": [], "source": [ "z1 = z[0]\n", "print z1" @@ -234,22 +188,11 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'apple'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "z1[0]" ] @@ -263,22 +206,11 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'apple'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "z[0][0]" ] @@ -308,7 +240,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "collapsed": true }, @@ -319,20 +251,11 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 2, 3]\n", - "[4, 5, 6, 7, 8, 9]\n" - ] - } - ], + "outputs": [], "source": [ "print num[0:4]\n", "print num[4:]" @@ -347,22 +270,11 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 3, 6]" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "num[:9:3]" ] @@ -371,7 +283,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Built in List Functions" + "### Built in List Functions" ] }, { @@ -383,22 +295,11 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "len(num)" ] @@ -412,44 +313,22 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "min(num)" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "9" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "max(num)" ] @@ -463,22 +342,11 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 5, 4, 7]" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "[1,2,3] + [5,4,7]" ] @@ -492,7 +360,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": { "collapsed": false }, @@ -510,44 +378,22 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "'Fire' in names" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "'Rajath' in names" ] @@ -561,7 +407,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": { "collapsed": true }, @@ -572,20 +418,11 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "z\n", - "az\n" - ] - } - ], + "outputs": [], "source": [ "print max(mlist)\n", "print min(mlist)" @@ -600,7 +437,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": { "collapsed": true }, @@ -611,20 +448,11 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "94\n", - "1\n" - ] - } - ], + "outputs": [], "source": [ "print max(nlist)\n", "print min(nlist)" @@ -646,20 +474,11 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Earth\n", - "Air\n" - ] - } - ], + "outputs": [], "source": [ "print max(names, key=len)\n", "print min(names, key=len)" @@ -678,22 +497,11 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "['h', 'e', 'l', 'l', 'o']" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "list('hello')" ] @@ -707,7 +515,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": { "collapsed": false }, @@ -718,19 +526,11 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1]\n" - ] - } - ], + "outputs": [], "source": [ "lst.append(1)\n", "print lst" @@ -745,22 +545,11 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "lst.count(1)" ] @@ -774,7 +563,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": { "collapsed": true }, @@ -785,19 +574,11 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8]]\n" - ] - } - ], + "outputs": [], "source": [ "lst.append(lst1)\n", "print lst" @@ -814,19 +595,11 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] - } - ], + "outputs": [], "source": [ "lst.extend(lst1)\n", "print lst" @@ -841,22 +614,11 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "lst.index(1)" ] @@ -870,19 +632,11 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 'name', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] - } - ], + "outputs": [], "source": [ "lst.insert(5, 'name')\n", "print lst" @@ -897,19 +651,11 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 4, 8, 7, 'Python', 1, [5, 4, 2, 8], 5, 4, 2, 8]\n" - ] - } - ], + "outputs": [], "source": [ "lst[5] = 'Python'\n", "print lst" @@ -924,22 +670,11 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "lst.pop()" ] @@ -953,22 +688,11 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "lst.pop(0)" ] @@ -982,19 +706,11 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 4, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n" - ] - } - ], + "outputs": [], "source": [ "lst.remove('Python')\n", "print lst" @@ -1009,19 +725,11 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 8, 7, 1, [5, 4, 2, 8], 5, 4, 2]\n" - ] - } - ], + "outputs": [], "source": [ "del lst[1]\n", "print lst" @@ -1038,19 +746,11 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 4, 5, [5, 4, 2, 8], 1, 7, 8, 1]\n" - ] - } - ], + "outputs": [], "source": [ "lst.reverse()\n", "print lst" @@ -1067,19 +767,11 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 2, 4, 5, 7, 8, [5, 4, 2, 8]]\n" - ] - } - ], + "outputs": [], "source": [ "lst.sort()\n", "print lst" @@ -1094,19 +786,11 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[5, 4, 2, 8], 8, 7, 5, 4, 2, 1, 1]\n" - ] - } - ], + "outputs": [], "source": [ "lst.sort(reverse=True)\n", "print lst" @@ -1121,20 +805,11 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Air', 'Earth', 'Fire', 'Water']\n", - "['Water', 'Fire', 'Earth', 'Air']\n" - ] - } - ], + "outputs": [], "source": [ "names.sort()\n", "print names\n", @@ -1151,20 +826,11 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Air', 'Fire', 'Water', 'Earth']\n", - "['Water', 'Earth', 'Fire', 'Air']\n" - ] - } - ], + "outputs": [], "source": [ "names.sort(key=len)\n", "print names\n", @@ -1188,7 +854,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": { "collapsed": false }, @@ -1199,19 +865,11 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] - } - ], + "outputs": [], "source": [ "listb = lista\n", "print listb" @@ -1226,20 +884,11 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4]\n", - "[2, 1, 4, 9]\n" - ] - } - ], + "outputs": [], "source": [ "lista.pop()\n", "print lista\n", @@ -1249,19 +898,11 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 9]\n" - ] - } - ], + "outputs": [], "source": [ "print listb" ] @@ -1277,7 +918,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": null, "metadata": { "collapsed": true }, @@ -1288,19 +929,11 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] - } - ], + "outputs": [], "source": [ "listb = lista[:]\n", "print listb" @@ -1308,20 +941,11 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4]\n", - "[2, 1, 4, 9]\n" - ] - } - ], + "outputs": [], "source": [ "lista.pop()\n", "print lista\n", @@ -1331,19 +955,11 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[2, 1, 4, 3]\n" - ] - } - ], + "outputs": [], "source": [ "print listb" ] @@ -1364,20 +980,11 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(3, 1)\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "xyz = divmod(10,3)\n", "print xyz\n", @@ -1400,7 +1007,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "metadata": { "collapsed": false }, @@ -1419,22 +1026,11 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(27,)" - ] - }, - "execution_count": 55, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "27," ] @@ -1448,22 +1044,11 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(27, 27)" - ] - }, - "execution_count": 56, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "2*(27,)" ] @@ -1477,21 +1062,12 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1, 2, 3)\n", - "('H', 'e', 'l', 'l', 'o')\n" - ] - } - ], + "outputs": [], "source": [ "tup3 = tuple([1,2,3])\n", "print tup3\n", @@ -1508,20 +1084,11 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "('H', 'e', 'l')\n" - ] - } - ], + "outputs": [], "source": [ "print tup3[1]\n", "tup5 = tup4[:3]\n", @@ -1537,7 +1104,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": null, "metadata": { "collapsed": true }, @@ -1548,38 +1115,22 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "alpha beta gamma\n" - ] - } - ], + "outputs": [], "source": [ "print a,b,c" ] }, { "cell_type": "code", - "execution_count": 61, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('R', 'a', 'j', 'a', 't', 'h', 'K', 'u', 'm', 'a', 'r', 'M', 'P')\n" - ] - } - ], + "outputs": [], "source": [ "d = tuple('RajathKumarMP')\n", "print d" @@ -1601,22 +1152,11 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 62, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "d.count('a')" ] @@ -1630,22 +1170,11 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "d.index('a')" ] @@ -1668,19 +1197,11 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "outputs": [], "source": [ "set1 = set()\n", "print type(set1)" @@ -1688,19 +1209,11 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "set([1, 2, 3, 4])\n" - ] - } - ], + "outputs": [], "source": [ "set0 = set([1,2,2,3,3,4])\n", "print set0" @@ -1722,7 +1235,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "metadata": { "collapsed": false }, @@ -1733,7 +1246,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": null, "metadata": { "collapsed": false }, @@ -1751,22 +1264,11 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 2, 3, 4, 5}" - ] - }, - "execution_count": 68, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "set1.union(set2)" ] @@ -1780,22 +1282,11 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{0, 1, 2, 3}" - ] - }, - "execution_count": 69, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "set1.add(0)\n", "set1" @@ -1810,22 +1301,11 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{2, 3}" - ] - }, - "execution_count": 70, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "set1.intersection(set2)" ] @@ -1839,22 +1319,11 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{0, 1}" - ] - }, - "execution_count": 71, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "set1.difference(set2)" ] @@ -1868,22 +1337,11 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{0, 1, 4, 5}" - ] - }, - "execution_count": 72, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "set2.symmetric_difference(set1)" ] @@ -1897,66 +1355,33 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 73, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "set1.issubset(set2)" ] }, { "cell_type": "code", - "execution_count": 74, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 74, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "set2.isdisjoint(set1)" ] }, { "cell_type": "code", - "execution_count": 75, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 75, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "set2.issuperset(set1)" ] @@ -1970,19 +1395,11 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "set([1, 2, 3])\n" - ] - } - ], + "outputs": [], "source": [ "set1.pop()\n", "print set1" @@ -1997,22 +1414,11 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 3}" - ] - }, - "execution_count": 77, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "set1.remove(2)\n", "set1" @@ -2027,22 +1433,11 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "set()" - ] - }, - "execution_count": 78, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "set1.clear()\n", "set1" @@ -2065,7 +1460,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.12" } }, "nbformat": 4, diff --git a/04.ipynb b/04.ipynb index 5a502ea..ce152c4 100644 --- a/04.ipynb +++ b/04.ipynb @@ -11,7 +11,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Strings" + "## Strings" ] }, { @@ -23,7 +23,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "collapsed": false }, @@ -38,23 +38,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Taj Mahal is beautiful \n", - "Taj Mahal is beautiful \n", - "Taj Mahal\n", - "is\n", - "beautiful \n" - ] - } - ], + "outputs": [], "source": [ "print String0 , type(String0)\n", "print String1, type(String1)\n", @@ -70,20 +58,11 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "M\n", - "Mahal is beautiful\n" - ] - } - ], + "outputs": [], "source": [ "print String0[4]\n", "print String0[4:]" @@ -93,7 +72,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Built-in Functions" + "### Built-in Functions" ] }, { @@ -105,20 +84,11 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "-1\n" - ] - } - ], + "outputs": [], "source": [ "print String0.find('al')\n", "print String0.find('am')" @@ -133,19 +103,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "a\n" - ] - } - ], + "outputs": [], "source": [ "print String0[7]" ] @@ -159,20 +121,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "2\n" - ] - } - ], + "outputs": [], "source": [ "print String0.find('j',1)\n", "print String0.find('j',1,3)" @@ -187,19 +140,11 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Observe the first letter in this sentence.\n" - ] - } - ], + "outputs": [], "source": [ "String3 = 'observe the first letter in this sentence.'\n", "print String3.capitalize()" @@ -214,22 +159,11 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "' Taj Mahal is beautiful '" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "String0.center(70)" ] @@ -243,22 +177,11 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'------------------------Taj Mahal is beautiful------------------------'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "String0.center(70,'-')" ] @@ -272,22 +195,11 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'00000000Taj Mahal is beautiful'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "String0.zfill(30)" ] @@ -301,21 +213,11 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "collapsed": false }, - "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" - ] - } - ], + "outputs": [], "source": [ "s = 'h\\te\\tl\\tl\\to'\n", "print s\n", @@ -334,31 +236,11 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "4\n" - ] - }, - { - "ename": "ValueError", - "evalue": "substring not found", - "output_type": "error", - "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;31mValueError\u001b[0m: substring not found" - ] - } - ], + "outputs": [], "source": [ "print String0.index('Taj')\n", "print String0.index('Mahal',0)\n", @@ -376,19 +258,11 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "False\n" - ] - } - ], + "outputs": [], "source": [ "print String0.endswith('y')" ] @@ -402,20 +276,11 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n" - ] - } - ], + "outputs": [], "source": [ "print String0.endswith('l',0)\n", "print String0.endswith('M',0,5)" @@ -430,20 +295,11 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4\n", - "2\n" - ] - } - ], + "outputs": [], "source": [ "print String0.count('a',0)\n", "print String0.count('a',5,10)" @@ -460,22 +316,11 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'*a_a-'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "'a'.join('*_-')" ] @@ -496,20 +341,11 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "collapsed": false }, - "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" - ] - } - ], + "outputs": [], "source": [ "a = list(String0)\n", "print a\n", @@ -526,19 +362,11 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " /i/s/ /b/e/a/u/t/i/f/u/l\n" - ] - } - ], + "outputs": [], "source": [ "c = '/'.join(a)[18:]\n", "print c" @@ -555,19 +383,11 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n" - ] - } - ], + "outputs": [], "source": [ "d = c.split('/')\n", "print d" @@ -582,20 +402,11 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l']\n", - "4\n" - ] - } - ], + "outputs": [], "source": [ "e = c.split('/',3)\n", "print e\n", @@ -611,20 +422,11 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Taj Mahal is beautiful\n", - "taj mahal is beautiful\n" - ] - } - ], + "outputs": [], "source": [ "print String0\n", "print String0.lower()" @@ -639,22 +441,11 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'TAJ MAHAL IS BEAUTIFUL'" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "String0.upper()" ] @@ -668,22 +459,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Bengaluru is beautiful'" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "String0.replace('Taj Mahal','Bengaluru')" ] @@ -697,7 +477,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": { "collapsed": true }, @@ -715,22 +495,11 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello'" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "f.strip()" ] @@ -744,7 +513,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": { "collapsed": true }, @@ -755,22 +524,11 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "' ***----hello---******* '" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "f.strip('*')" ] @@ -784,20 +542,11 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "----hello---\n", - "hello\n" - ] - } - ], + "outputs": [], "source": [ "print f.strip(' *')\n", "print f.strip(' *-')" @@ -814,20 +563,11 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "----hello---******* \n", - " ***----hello---\n" - ] - } - ], + "outputs": [], "source": [ "print f.lstrip(' *')\n", "print f.rstrip(' *')" @@ -856,19 +596,11 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " \n" - ] - } - ], + "outputs": [], "source": [ "d0 = {}\n", "d1 = dict()\n", @@ -884,19 +616,11 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'OneTwo': 12, 'One': 1}\n" - ] - } - ], + "outputs": [], "source": [ "d0['One'] = 1\n", "d0['OneTwo'] = 12 \n", @@ -912,19 +636,11 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n" - ] - } - ], + "outputs": [], "source": [ "print d0['One']" ] @@ -938,7 +654,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": { "collapsed": false }, @@ -957,19 +673,11 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)]\n" - ] - } - ], + "outputs": [], "source": [ "d2 = zip(names,numbers)\n", "print d2" @@ -988,19 +696,11 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n" - ] - } - ], + "outputs": [], "source": [ "a1 = dict(d2)\n", "print a1" @@ -1010,7 +710,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###Built-in Functions" + "### Built-in Functions" ] }, { @@ -1022,19 +722,11 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{}\n" - ] - } - ], + "outputs": [], "source": [ "a1.clear()\n", "print a1" @@ -1049,19 +741,11 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n" - ] - } - ], + "outputs": [], "source": [ "for i in range(len(names)):\n", " a1[names[i]] = numbers[i]\n", @@ -1077,22 +761,11 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[4, 5, 3, 2, 1]" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "a1.values()" ] @@ -1106,22 +779,11 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "['Four', 'Five', 'Three', 'Two', 'One']" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "a1.keys()" ] @@ -1135,22 +797,11 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[('Four', 4), ('Five', 5), ('Three', 3), ('Two', 2), ('One', 1)]" - ] - }, - "execution_count": 40, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "a1.items()" ] @@ -1164,20 +815,11 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n", - "4\n" - ] - } - ], + "outputs": [], "source": [ "a2 = a1.pop('Four')\n", "print a1\n", @@ -1201,7 +843,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.12" } }, "nbformat": 4, diff --git a/05.ipynb b/05.ipynb index d24f4ac..3c83251 100644 --- a/05.ipynb +++ b/05.ipynb @@ -11,14 +11,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#Control Flow Statements" + "# Control Flow Statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "##If" + "## If" ] }, { @@ -32,19 +32,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello\n" - ] - } - ], + "outputs": [], "source": [ "x = 12\n", "if x >10:\n", @@ -55,7 +47,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##If-else" + "## If-else" ] }, { @@ -73,19 +65,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello\n" - ] - } - ], + "outputs": [], "source": [ "x = 12\n", "if x > 10:\n", @@ -98,7 +82,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##if-elif" + "## if-elif" ] }, { @@ -122,19 +106,11 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x4:\n", @@ -437,7 +324,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##List Comprehensions" + "## List Comprehensions" ] }, { @@ -449,19 +336,11 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n" - ] - } - ], + "outputs": [], "source": [ "res = []\n", "for i in range(1,11):\n", @@ -479,22 +358,11 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "[27*x for x in range(1,11)]" ] @@ -515,22 +383,11 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "[27*x for x in range(1,20) if x<=10]" ] @@ -544,22 +401,11 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "[27*z for i in range(50) if i==27 for z in range(1,11)]" ] @@ -581,7 +427,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.12" } }, "nbformat": 4, diff --git a/06.ipynb b/06.ipynb index a642528..16fd554 100644 --- a/06.ipynb +++ b/06.ipynb @@ -11,7 +11,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#Functions" + "# Functions" ] }, { @@ -51,20 +51,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hey Rajath!\n", - "Rajath, How do you do?\n" - ] - } - ], + "outputs": [], "source": [ "print \"Hey Rajath!\"\n", "print \"Rajath, How do you do?\"" @@ -81,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": true }, @@ -94,20 +85,11 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hey Rajath!\n", - "Rajath, How do you do?\n" - ] - } - ], + "outputs": [], "source": [ "firstfunc()" ] @@ -121,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": true }, @@ -134,19 +116,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Please enter your name : Guido\n" - ] - } - ], + "outputs": [], "source": [ "name1 = raw_input('Please enter your name : ')" ] @@ -160,20 +134,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hey Guido!\n", - "Guido, How do you do?\n" - ] - } - ], + "outputs": [], "source": [ "firstfunc(name1)" ] @@ -187,7 +152,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "collapsed": false }, @@ -203,21 +168,11 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Please enter your name : karthik\n", - "Hey karthik!\n", - "karthik, How do you do?\n" - ] - } - ], + "outputs": [], "source": [ "secondfunc()" ] @@ -238,7 +193,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "collapsed": true }, @@ -258,19 +213,11 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "20\n" - ] - } - ], + "outputs": [], "source": [ "c = times(4,5)\n", "print c" @@ -292,7 +239,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "collapsed": false }, @@ -305,19 +252,11 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "20\n" - ] - } - ], + "outputs": [], "source": [ "c = times(4,5)\n", "print c" @@ -332,23 +271,11 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function times in module __main__:\n", - "\n", - "times(x, y)\n", - " This multiplies the two input arguments\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "help(times)" ] @@ -362,7 +289,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "collapsed": true }, @@ -373,7 +300,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": { "collapsed": true }, @@ -396,44 +323,22 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(100, 6, 10, 100)" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "egfunc(eglist)" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " a = 100 \n", - " b = 6 \n", - " c = 10 \n", - " d = 100\n" - ] - } - ], + "outputs": [], "source": [ "a,b,c,d = egfunc(eglist)\n", "print ' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d" @@ -443,7 +348,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Implicit arguments" + "## Implicit arguments" ] }, { @@ -455,7 +360,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": { "collapsed": true }, @@ -481,22 +386,11 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "7" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "implicitadd(4)" ] @@ -510,22 +404,11 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "implicitadd(4,4)" ] @@ -534,7 +417,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Any number of arguments" + "## Any number of arguments" ] }, { @@ -546,7 +429,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": { "collapsed": true }, @@ -570,58 +453,22 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3, 4, 5]\n" - ] - }, - { - "data": { - "text/plain": [ - "15" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "add_n(1,2,3,4,5)" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2, 3]\n" - ] - }, - { - "data": { - "text/plain": [ - "6" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "add_n(1,2,3)" ] @@ -630,7 +477,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Global and Local Variables" + "## Global and Local Variables" ] }, { @@ -642,7 +489,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": { "collapsed": false }, @@ -660,7 +507,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": { "collapsed": true }, @@ -679,34 +526,11 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "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" - ] - } - ], + "outputs": [], "source": [ "egfunc1()" ] @@ -720,26 +544,18 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], + "outputs": [], "source": [ "eg3 = [1,2,3,4,5]" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": { "collapsed": true }, @@ -759,22 +575,11 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "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 : [1, 2, 3, 4, 5, 6]\n" - ] - } - ], + "outputs": [], "source": [ "egfunc1()" ] @@ -783,7 +588,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Lambda Functions" + "## Lambda Functions" ] }, { @@ -795,7 +600,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": { "collapsed": true }, @@ -806,22 +611,11 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "64" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "z(8)" ] @@ -830,7 +624,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###map" + "### map" ] }, { @@ -842,7 +636,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "metadata": { "collapsed": true }, @@ -853,19 +647,11 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[3, 4, 5, 6, 7, 8, 9, 10, 11]\n" - ] - } - ], + "outputs": [], "source": [ "eg = map(lambda x:x+2, list1)\n", "print eg" @@ -880,7 +666,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": { "collapsed": false }, @@ -891,19 +677,11 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[10, 10, 10, 10, 10, 10, 10, 10, 10]\n" - ] - } - ], + "outputs": [], "source": [ "eg2 = map(lambda x,y:x+y, list1,list2)\n", "print eg2" @@ -920,19 +698,11 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['10', '10', '10', '10', '10', '10', '10', '10', '10']\n" - ] - } - ], + "outputs": [], "source": [ "eg3 = map(str,eg2)\n", "print eg3" @@ -942,7 +712,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "###filter" + "### filter" ] }, { @@ -954,7 +724,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "metadata": { "collapsed": true }, @@ -972,22 +742,11 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4]" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "filter(lambda x:x<5,list1)" ] @@ -1001,22 +760,11 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[True, True, True, True, False, False, False, False, False]" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "map(lambda x:x<5, list1)" ] @@ -1030,22 +778,11 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[4, 8]" - ] - }, - "execution_count": 40, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "filter(lambda x:x%4==0,list1)" ] @@ -1067,7 +804,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.12" } }, "nbformat": 4, diff --git a/07.ipynb b/07.ipynb index f233280..0d0cf46 100644 --- a/07.ipynb +++ b/07.ipynb @@ -11,7 +11,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#Classes" + "# Classes" ] }, { @@ -675,7 +675,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##Inheritance" + "## Inheritance" ] }, { @@ -1136,7 +1136,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#Where to go from here?" + "# Where to go from here?" ] }, { @@ -1181,7 +1181,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.12" } }, "nbformat": 4,