Skip to main content

Posts

Showing posts from March, 2018

Set in python on Python hunter blog by pran sukh

The  set can contain  elements or members of  anything: numbers, characters, words, names, letters of the alphabet, even other sets and tuples. Sets are usually denoted with capital letters. Set is a data type in python and can hold any number of elements and any type of data in it. Even set data type is similar to other data types in python like list and tuple still there are some differences. Only unique object will be inserted as set element, duplicate or repeated elements will be removed from set object. Set object can not have list and dictionary object as element but we can pass a single list or dictionary object to Set() method and turn the list or dictionary into individual elements. This is the shortest method to remove the duplicates from list object. No need to filter the duplicate from dictionary object as it is already taken in care by dictionary. It is wise and error free to have tuple object as set's elements. Even the set contain the immutable obje

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