{ "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 }