Skip to main content

Posts

Showing posts with the label shallow

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...

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...