[15:00:22] [## Class Started at Fri Jul 22 15:00:22 2016 ##] [15:00:22] #startclass [15:00:31] roll call please [15:00:33] Shantanu Acharya [15:00:34] yash bhardwaj [15:00:34] Abhishek Shrivastava [15:00:34] Harsh Vardhan [15:00:35] Akhilesh Dhaundiyal [15:00:35] Priyanka Sharma [15:00:35] Vaibhav JAin [15:00:38] sandeep kumar choudhary [15:00:42] Pooja Yadav [15:00:43] Avik Mukherjee [15:00:44] chandu [15:00:44] Shaurya Kalia [15:00:45] Sidhant Gupta [15:00:47] Trishna Guha [15:00:50] sriniketh [15:00:51] Aditya Bayana [15:00:52] hemanth [15:00:53] Anupama Mandal [15:00:53] Priyanka Ambre [15:00:56] mspreddy [15:01:03] surendhar [15:01:05] kalyan [15:01:08] kiran [15:01:08] bhanuteja [15:01:09] Kshitij [15:01:11] Sanyam Khurana [15:01:12] Yajushi Srivastava [15:01:20] Aman Kumar [15:01:23] chaitanya gupta [15:01:27] goutham [15:01:32] Akshay Shipurkar [15:01:33] sairam [15:01:35] Anurag [15:01:43] Avinash Madhukar [15:02:31] Rohan Hazra [15:02:47] Jogender Kota [15:02:55] Before we start today topic, anyone have query regarding about what we learnt yesterday? [15:02:58] Vivek Shelar [15:03:07] Prashant Jamkhande [15:03:09] Deepanshu kapoor [15:03:26] Gobinda Akhuli [15:04:11] Sudeep Mukherjee [15:04:21] Varsha R [15:04:46] Looks like everyone got what we discussed yesterday. Today's topic is Data Structure in python. [15:05:10] What is data structure, anyone know? [15:05:36] Data Structure is a way to organized data in such a way that it can be used efficiently [15:05:37] programming constructs which allow us to store data in various ways [15:05:56] used to store data [15:06:09] Organising data in CS [15:06:10] format for data so that it could be easily used [15:06:15] a way to organize and access data. [15:06:15] A data structure is a specialized format for organizing and storing data. [15:06:21] Right so it is a way to store data and then we can retrieve or manipulate later. [15:06:36] It is a collection of possible data that can be stored in an organized fashion, so that possible operations can be defined, can be carried out efficiently [15:06:51] arrays, stacks and queues are its examples, right? [15:06:58] avik, Yes [15:07:03] Data means information and structure means how it is organized. [15:07:06] Now python different build in data-structures. which we will discuss today. [15:07:23] 1. We will start from List. [15:07:38] different operations to store and retrieve data efficiently. [15:07:49] Everyone is in python terminal if not then open one. [15:08:11] roll call:Pragnavi [15:08:25] python [15:08:41] roll call: himavarshini [15:08:48] roll call:sindhu [15:09:11] Now try to execute >>> a = ['hi', 'welcome', 'to', 'dgplug'] [15:09:45] Roll call: Poonam Jadhav [15:09:51] you can check a object type in python using type method. [15:10:03] Roll call: Revathy Narayankutty [15:10:04] Try >>> type(a) [15:10:20] Roll call : Krishna Biradar [15:10:47] You will find that object type is a list. so we just defined a list. [15:11:11] Roll call: Sarath Chandrika [15:11:29] We can perform lots of different operation on list. As we said earlier we can retrieve the data also so let's try that. [15:12:09] Assume I want to access 'welcome' then I should access it using list index which start from 0. [15:12:20] i got disconnected after praveenkumar said Now python different build in....someone please help me after that. [15:12:55] So to get 'welcome' you should >>> print a[1] [15:13:18] s/print a[1]/print(a[1]) [15:13:29] Everyone got welcome printed? [15:13:38] can we modify this? [15:13:41] yes [15:13:51] yes [15:13:52] yes [15:14:10] Yes [15:14:13] yes [15:14:22] yes [15:14:22] yes [15:14:25] chandu: use ! if you have question. and yes we can modify it, we will discuss that also soon. [15:15:01] Now if I want to get 'dgplug' what should I execute? [15:15:08] Yes [15:15:17] a[3] [15:15:21] A[3] [15:15:22] praveenkumar: I can get result by typing "print a[1] ! [15:15:23] print a]3 [15:15:25] print a[3] [15:15:25] print (a[3]) [15:15:27] print(a[3]) [15:15:27] a[3] [15:15:29] printa[3] [15:15:35] a(3)print [15:15:43] print(a[3]) [15:15:53] print(a[3]) [15:15:59] print a[3] [15:16:04] printa[3] [15:16:05] a[-1] [15:16:09] print(a[-1]) [15:16:15] iKshitij: it should be print(a[1]) [15:16:35] a[-1] [15:17:04] Now what if I want to insert a element at 2nd index? [15:17:44] a[2]="element" [15:17:45] ?? [15:17:50] a[2] = 'element' [15:17:53] a.insert(2, "hi") [15:17:54] a[2]='element' [15:17:55] list have lots of inbuild methods to manipute list according to our need in this situation we execute >>> a.insert(2, "my_element") [15:18:01] a.insert(2,'element') [15:18:02] a[2]=elementname [15:18:16] a[1]='element' [15:18:23] roll call :Himanshu sharma [15:18:36] kiran_: kalyan Your suggession will replace 2nd index item with new one not insert. [15:18:57] Roll Call : Saurav Saha [15:19:03] this means list is dynamic [15:19:04] srvsaha is now known as SRvSaha [15:19:36] List is mutable object in python so you can change it easily. (we will discuss it in a min) [15:20:44] Now everyone know about stack/queue right so how we will implement using list? [15:21:19] a.pop() [15:21:23] yes [15:21:28] a.append() [15:21:33] a.pop() [15:21:35] Stack => It's Last in First out structure and Queue => first come first out structure. [15:21:40] we have methods like a.pop() and a.append() to use list as stack [15:22:09] and a.pop() and a.insert() for queue [15:22:13] Roll call: Rahul Bhattacharjee [15:22:30] ! [15:22:36] So we have pop() and append() method for list which can be used for this. [15:22:40] next [15:22:42] A.append() is not working . [15:23:13] Clear [15:23:16] aman_, use a.append('element') [15:23:28] Got it . :) [15:23:33] aman_: Not working doesn't give me any clue about your problem until you tell me what is the error when you executed. [15:23:39] aman_: Great. [15:23:50] ! [15:23:59] now everyone clear about how basic operation in list working? [15:24:17] yes [15:24:22] Yes [15:24:26] yes [15:24:31] yes [15:24:37] yes [15:24:51] Good, Now we discuss about List Comprehensions [15:24:53] yes [15:24:57] yes [15:25:03] Anyone know what is that? [15:25:07] yes [15:25:23] next [15:25:26] easy way to generate list without for loop [15:25:26] next [15:25:31] pop() and append() are for stack ? [15:26:31] pop()?? [15:26:37] darkside_: to implement stack you can use pop(last_index_of_array) and to append(last_index_of_arrary) [15:26:37] ! [15:26:46] then what will be for queue ? [15:26:52] praveenkumar: pop() not clear [15:27:15] ! [15:27:30] darkside_: For queue you should use pop(0) and append(last_index_of_array) [15:27:53] iKshitij: try in your python terminal. [15:27:55] okay got that [15:27:55] next [15:28:00] next [15:28:32] last in first out means??? [15:28:40] list comprehension way to create list [15:29:12] hemanth: https://en.wikipedia.org/wiki/Stack_(abstract_data_type) [15:29:15] The element that is entered last is the first one to be popped [15:29:29] Roll Call - Pabitra Pati [15:29:37] Alright let's carry with list comprehension. [15:30:48] Assume I have a list >>> a = [1, 2, 3] and I want another list which have elements multiple of a's elements. [15:31:21] List comprehension is a way to do that and it's more pythonic way. [15:31:51] >>> [x ** 2 for x in a] will provide me another list which what I required. [15:32:37] Within single line I can generate another list. [15:32:41] ! [15:32:44] next [15:33:01] can we use a while loop [15:33:30] surendhar: yes you can do but as I said pythonic way to create another list if operation is simple. [15:33:58] ok [15:33:58] ! [15:34:01] next [15:34:05] praveenkumar , I have seen map function in haskell does it work in python for the same task? [15:34:22] surendhar, in while loop you may need 3 lines to do a task but list comphrension can do that for you in 1 line [15:35:18] vbhjain: yes python does have map function but I am not sure how it work with haskell. for python you can check help(map) in python terminal [15:35:47] praveenkumar, ok I will :) [15:36:04] Moving on I suggest you folks to do more practice about list comprehension and other available methods. [15:36:18] ! [15:36:19] 2. Tuples - Another data structure in python [15:36:22] next [15:36:40] in list comprehension do we really create a new list [15:37:00] or we just print the previous pist in multiples [15:37:22] s pist/list [15:37:22] Yash_B, a new list is created, if you wish you can assign it to a variable [15:37:38] Yash_B: No so when you used list comprehension you actucally created a new list which can be assigned to a variable. [15:37:53] ! [15:37:56] next [15:38:00] thanks praveenkumar [15:38:03] [x ** 2 for x in a] give me error [15:38:13] ! [15:38:15] Traceback (most recent call last): [15:38:15] File "", line 1, in [15:38:15] File "", line 1, in [15:38:15] TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' [15:38:34] akshays: always use pastebin for multiple line paste. [15:38:44] you need to use integers for this list [15:38:54] or numbers [15:38:56] akshays, pastebin -> [15:38:56] File "", line 1, in [15:38:58] akshays, power operator cannot be used on strings [15:39:05] sorry https://paste.fedoraproject.org/ [15:39:08] akshays: looks like your variable 'a' is either string or number but not list. [15:39:20] i know but it was just 4 lines so i just pasted it [15:39:32] ! [15:39:43] next [15:39:46] next [15:40:02] is any way to replace element from list [15:40:12] akshays: also make sure each element in list should be integer so you can perform multiply operation. [15:40:38] same problem as akshays [15:40:38] chandu: yes you can replace a element from list using it's index [15:41:21] thank you [15:41:49] ! [15:41:49] ! [15:41:50] thanks praveenkumar [15:41:53] next [15:41:55] got it [15:42:15] how to find index in list [15:43:00] bhanu, a.index [15:43:09] bhanu, a.index(element) [15:43:19] bhanu: you can check index method [15:43:26] next [15:43:29] k=1 ; k*3 #does that create a new variable other than k [15:44:04] Yash_B: new variable no until you assign k*3 to some variable. [15:44:11] ! [15:44:16] a.index(element) will return you the first index of the element [15:44:32] next [15:44:51] if same elements there in list means how can i get index of all [15:45:01] Yash_B, typing k*3 will only make the python interpretor give out the result of the operation . no new variable till you assign it [15:45:52] shaurya_3 is this thing not comparable with the list comprehension technique [15:46:06] yash_B it just same as doing calculations in c or c++ [15:46:15] ! [15:46:19] chandu: http://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list [15:46:22] next [15:46:22] yash_B list is mutable object [15:47:29] if i have a character or string in the list and we perform a comprehension then what is the output [15:48:23] sriniketh: Still will be a list if operation you performed inside comprehension supported by string otherwise you will get nice traceback. [15:48:50] Any more questions or we should move to Tuple now? [15:48:58] sriniketh, comprehensions are usually done on lists that has homogenous objects [15:49:16] ! [15:49:38] next [15:49:44] what do u mean by traceback [15:50:17] sriniketh, what do you mean by u? [15:51:04] sriniketh: type B in your python shell and you will get NameError which is traceback, more you can check https://docs.python.org/3/library/traceback.html [15:51:15] means we will get back the list even the comprehension has done [15:51:59] sriniketh: Big No, check what I said for traceback. [15:52:29] okay ,thank you. [15:52:48] Now let's talk about Tuples. [15:53:04] ! [15:53:24] try to execute >>> a = 'Fedora', 'Debian', 'Kubuntu', 'Pardus' [15:53:28] next [15:54:28] Now to check the object type execute >>> type(a) , what type you get? [15:54:49] tuple [15:54:53] tuple [15:54:54] tuple [15:54:58] Tuple [15:55:01] tuple [15:55:08] tuple [15:55:21] tuple [15:55:23] tuple [15:55:23] tuple [15:55:34] tuple [15:55:56] [15:55:58] what is tuple [15:56:01] Good so we have now tuple obect type, Now try to replace a value from tuple >>> a[2] = 'Replaced' , what you see (only one can tell)? [15:56:02] [15:56:07] tuple [15:56:30] mspreddy: tupple is another data structure in python like list but have different property. [15:57:10] 'tuple' object does not support item assignment [15:57:23] That's right [15:57:51] yes tuples are immutable like strings in java [15:58:03] Because tuple is immutable and it means you can't del/add/edit anything from tuple. [15:58:29] https://www.irccloud.com/pastebin/Ni8gUUS7/ [16:00:03] We need tuple for non-editable data so that if by mistake we try to replace anything in data it will throw a error. [16:00:43] Any questions on Tuple? [16:01:27] Alright so another data structure is Set [16:01:53] 3. Set => data structure which provide no duplicate item. [16:02:21] Now everyone try to execute >>> a = set('abcthabcjwethddda') [16:03:18] Here you can see we put a string to set method which we create a set object for us and assigned to 'a'. Again you can check type of a using >>> type(a) [16:04:23] ! [16:05:07] So if you noticed we provide multiple char in the strings and some are used multiple times but when you check about what 'a' contain you will see it doesn't have any duplicate item. [16:05:09] next [16:05:20] how can we access the separate non-duplicate items in a set? [16:06:46] shauryak_3: set is not list so you can't access the element but you can perform set operations. [16:07:10] Now try to execute >>> b = set('alacazam') [16:07:54] shauryak_3 : you can try e = next(iter(a)) and then print e [16:08:31] And now we can perform set operation like union, intersection ..etc [16:08:32] you can iterate over the elements [16:08:43] praveenkumar, darkside_ thanks [16:09:08] darkside_: yes iteration is possible but you can't accesss set element using index [16:09:36] Now try >>> a - b, what you get? [16:10:35] you will get letters in 'a' but not in 'b' right [16:10:49] which set operation is this? [16:10:49] yes [16:10:58] Difference, yes [16:11:24] like in maths [16:11:45] yes in math we call it set complements operation. [16:12:59] 4. Dictionary another data structure for python [16:14:09] Dictionary is unordered set of key:value pair where key is always unique. And we access item value using that unique key [16:14:57] Try to execute >>> data = {'kushal':'Fedora', 'kart_':'Debian', 'Jace':'Mac'} [16:15:21] And check the type of data variable using >>> type(data) [16:15:28] Funny that this line is still valid :) [16:15:31] dict [16:15:36] All 3 of us use the same distro. [16:15:37] kushal: :) [16:16:01] type dict [16:16:25] Now I would I know what 'Jace' uses ( here I know the key) [16:16:42] s/I would/how would/ [16:17:16] So I can execute >>> data['Jace'] and it will provide me value which associated with this key [16:18:49] How can I know if a key exist or not in a dictionary? [16:19:49] dict.haskey() [16:19:55] 'Keyname' in data [16:20:08] data.haskey() in our case [16:21:06] it's data.has_key() or you can check using 'in' operation. [16:21:51] so >>> data.has_key('Jace') will return True and same >>> 'Jace' in data [16:21:53] Okay, thanks for correction [16:22:43] You can add and modify element in dictionary because it's mutable. [16:23:18] To add a item use >>> data['welcome'] = 'dgplug' here welcome will become key. [16:23:56] If you want to edit then >>> data['welcome'] = 'edited' and it will replace 'welcome' key value from dgplug to replace [16:25:25] Now I will encourage you folks to go through datastructure section in http://pymbook.readthedocs.io/en/latest/datastructure.html and next time when we have session ask your doubts if any. [16:25:41] If you have any questions right now please ask. [16:25:47] ! [16:25:52] next [16:26:02] How to use new line and tab in string [16:26:32] aman_: '\n' => new line and '\t' for tab [16:27:03] ! [16:27:12] next [16:27:25] are there any specific operations in tupple buy not in lists? [16:28:11] chshbh: yes you can't del a element from tuple but can do in list. same with replace and add. [16:28:11] they look same just that tupple are immutable? [16:28:51] Looks same does't mean they are same. They do have different use case. [16:29:06] okay [16:29:16] ! [16:29:18] next [16:29:21] getting error while trying this data.has_key('jace') [16:29:29] link :https://da.gd/y9ORQ [16:30:07] sandeepk: which python version you are using? [16:30:19] data.has_key('jac') in your case [16:30:23] 3.4.3 [16:30:30] sandeepk, ^^ [16:30:59] sandeepk, you have used 'jac' as your key [16:31:13] but in that case return value shoul be false [16:31:30] 'dict' object has no attribute 'has_key' [16:31:43] PrashantJ: , ^^ [16:31:57] sandeepk, yes did not noticed that [16:32:59] sandeepk: it should work try to use a different variable and define dictionary and then access using has_key [16:32:59] sorry sandeepk [16:33:11] sandeepk: see if that work [16:33:22] PrashantJ: it's ok do't say sorry :-) [16:33:33] s/do't/don't [16:34:18] Alright so we are closing today session. [16:34:25] #endclass [16:34:25] [## Class Ended at Fri Jul 22 16:34:25 2016 ##]