Skip to main content

Posts

Showing posts with the label list

Python Hunter, Deep copy and Shallow copy by pran sukh

In any programming language the copy concept is assigning the value of variable to other variable. But python shows strange behavior when this concept is implemented, follow the examples below.       1 2 3 4 5 6 7 8 9 list1 = list () list1 = [ "Python" , "Hunter" ] list2 = list1 print (list1) print (list2) list2 [ 1 ] = "Lover" print (list1) print (list2) In the above code the list1 created and initialized and list1 is passed to list2 . but when when we change any element in list2 it will will affect the list1 too. execute the script and observe the output. Explanation:-  Because there was no new assignment to list2 , list2 was pointing to the list1 . let's see with id() 1 2 print ( id (list1)) print ( id (list2)) The output value of memory location for the both list1 and list2 will be same, that's why change in any list will reflect to other list object too. But we can re...

Dictionary on Python Hunter by Pran Sukh

We have learned about list in previous blogs, list can contain dictionary object vice versa. Both has the ability to change their size at run time. list and dictionary can grow and shrink according to elements at run time. But what is the actual difference b/w these two? Read below.  One is that elements from list object contains elements in linear manner but the dictionary object contains elements as key and value pair.   Elements from list object are fetched with element's location but in dictionary the elements is fetched with key. Last but not the least that list is an ordered object means when the list object is printed it shows the elements as these were inserted but dictionary sort the keys and print the values.      Examples:-  1 2 3 dict_ = dict () dict_ = { "Python" : "Hunter" , "Is" : "Better" } print (dict_) There are lot of methods to manipulate the dictionary object but some of the met...

Python Hunter, Slicing, traversing, copy and pit falling of list and tuple in python. By Pran Sukh

Slicing is a technique in python to pick up data from list,tuple and set from desired location. lets make python a bit more beautiful with slicing technique.  Caution :- No animal is hurt during the development of this tutorial :)   it can be done with three arguments.  e.g dataString [ begin : end : step ] argument begin and end are giving lucid  view, these arguments are to instruct the script that from where the slicing will begin and where it will end, but the tricky argument is the last one ( " step ") . it indicates that how many  index or indices  the code have to   hops or skip to pick up the next slice. In the below example the slicing is starting form 6th index or indices and go till 21th  index and after every slice it will skip the 3 index or indices between the begin and end index.  let's take an example :- 1 25 str = "1abcdefghijklmnopqrstuvwxyz" print ( str [ 6 : 21 : 3 ]) list_1 = [ "T...