Posts

Showing posts from May, 2018

Sets in Python

Sets      A set is unordred collection of items with no duplicate. It can be used to eliminating duplicates or membership testing. Set also supports union, intersection, difference and symmetric difference. Set can be initialized using set().      Set can be initialized using set(). set1 = set() Set has following methods set.add(element)      Add element to set. If element already present nothing happens. set.clear()      Remove all elements and clear the set. set.copy()      Returns shallow copy set. set.difference(another_set[s])      Returns the difference of two or mor e sets as new set. set.difference_update(another_set)      Remove all elements of another from this set. set.discard(element)      Remove an element from set if is a member. set.intersection(another_set[s])   ...

Python Tuples

Tuple      This data structure of python is immutable like array. We can't update it after initialization.      Tuple is initialized using tuple class or (). tuple1 = tuple() tuple2 = () tuple3 = value1, value2,...,valueN Below are some methods of tuple object tuple.count(value)      Returns number of time value appears in tuple. tuple.index(value, [start, [stop]])      Returns first index of value or raises ValueError if value not present. It optionally take two more parameters start and stop and searches value in between start and stop. Lets Understand this by taking Example >>> t=tuple() >>> t () >>> t = (19, 3, 7, 5, 2, 8) >>>  t (19, 3, 7, 5, 2, 8) >>> 19 in t True >>> t[3:5] (5, 2) >>> len(t) 6 >>> t.count(3) 1 >>>  t.index(3) 1 Keep Following, For More....