Posts

Calendar In Python

Image
  Code Example Example 1: import calendar yy = 2000 mm = 10 print(calendar.month(yy, mm)) Output:    October 2000 Mo Tu We Th Fr Sa Su                    1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Example 2: import calendar print(calendar.calendar(2017)) Output: 2017       January                   February                   March Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su                    1             1  2  3  4  5             1  2  3  4  5  2  3  4  5  6  7  8  ...

Set Data Type in Python

Image
Concept Set a collection which is unordered, unindex. Set does not allow duplicate data. Code Example Example 1: >>> s = {'A', 'B', 'C', 'D'} >>> type(s) <class 'set'> >>>  s = {'A', 'B', 'C', 'D'} {'B', 'D', 'A', 'C'} >>>  s.add('New') >>> s {'B', 'HH', 'D', 'A', 'C'} >>> s.update(['XX', 'YY']) >>> s {'YY', 'XX', 'B', 'HH', 'D', 'A', 'C'} Quiz Level 1 Quiz Level 2 Request Me  

Tuple Data Type in Python

Image
  Concept Tuple  is a collection which is  immutable,  ordered and unchangeable. Allows duplicate members Code Example Example 1: >>> t = () >>> t () >>> type(t) <class 'tuple'> >>> t = (10, 20.35, 'Hello', [1, 2]) >>> t (10, 20.35, 'Hello', [1, 2]) Example 2: >>> t = (10, 20.35, 'Hello', [1, 2]) >>> len(t) 4 Get Help

Dict Comprehension in Python

Image
  Code Example Example 1: d = {i:i for i in range(1, 6)} print(d) Output: {1: 1, 2: 2, 3: 3, 4: 4, 5: 5}   Example 2: d = {i:[i, i+1] for i in range(6)} print(d) Output: {0: [0, 1], 1: [1, 2], 2: [2, 3], 3: [3, 4], 4: [4, 5], 5: [5, 6]}   Example 3: d = {i:i**2 for i in range(6)} print(d) Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25} Get Help

OrderedDict in Python

Image
   Concept An  OrderedDict  is a dictionary subclass that remembers the order that keys were first inserted. OrderedDict  preserves the order  in which the keys are inserted. A regular dict doesn’t track the insertion order and iterating it gives the values in an arbitrary order. Code from collections import OrderedDict od = OrderedDict() od['1'] = 5 od['2'] = 4 od['3'] = 3 od['4'] = 2 od['5'] = 1 Get Help

Dict Data Type in Python

Image
Concept Dictionary  is a collection which is unordered, changeable and indexed. No duplicate members Code Example >>> d = {} >>> type(d) <class 'dict'> >>> d = {'key': 'value'} >>> d {'key': 'value'} >>> d = {'1': 1, '2': 2} >>> d {'1': 1, '2': 2} >>> l = [10, 20] >>> d.update({'l1': l}) >>> d {'1': 1, '2': 2, 'l1': [10, 20]} # data within dict [Tested on IDLE ] >>> l = [1, 2, 3, 4] >>> d1 = {1: 'A', 2: 'a'} >>> t = (1, 2) >>> s = {1, 2}  >>> d = {'1': 1, '2': l, '3': d1, '4': t, '5': s} >>> d {'1': 1, '2': [1, 2, 3, 4], '3': {1: 'A', 2: 'a'}, '4': (1, 2), '5': {1, 2}}  >>> d['3'][1] 'A'  >>> d = {i: i...

Convert Nested List To Flat List

Image
  Code Suppose you have a list like : [[1], [2,3], [4, [5, 6]], [7], [8], [9], [10]] Now if I want to make it a flat list like : [1,2,3,4,5,6,7,8,9] How can we do that?  😕  Don't worry dear, there are many ways to do so. You are going to take help one builtin lib of python 😍 # Example from functools import reduce >>> l = [[1], [2,3], [4, 5, 6], [7], [8], [9], [10]] >>> flat_list = reduce(lambda x, y: x+y, l) >>> flat_list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Enjoy Edge-cutting Technology 😉 Get Help