Some not so known facts about Python

  • Type “import this” in your python console. This will print a poem  The Zen of Python, by Tim Peters on your screen.
  • The Zen of Python, by Tim Peters
    
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    
    
    
  • You can use else  with a for loop in Python. It executes only if the for loop exits naturally with out the execution of break statement.
    def func(array):
         for num in array:
            if num%2==0:
                print(num)
                break # Case1: Break is called, so 'else' wouldn't be executed.
         else: # Case 2: 'else' executed since break is not called
            print("No call for Break. Else is executed")
    print("1st Case:")
    a = [2]
    func(a)
    print("2nd Case:")
    a = [1]
    func(a)
  • In Python, everything is done by reference. It doesn’t support pointers.
  • Function Argument Unpacking is another awesome feature of Python. One can unpack a list or a dictionary as function arguments using * and ** respectively. This is commonly known as the Splat operator. Example here
    defpoint(x, y):
        print(x,y)
    foo_list =(3, 4)
    bar_dict ={'y': 3, 'x': 2}
    point(*foo_list) # Unpacking Lists
    point(**bar_dict) # Unpacking Dictionaries

     

  • We can’t define Infinities right? But wait! Not for Python. See this amazing example
    # Positive Infinity
    p_infinity = float('Inf')
    if 99999999999999 > p_infinity:
        print("The number is greater than Infinity!")
    else:
        print("Infinity is greatest")
    # Negetive Infinity
    n_infinity = float('-Inf')
    if -99999999999999 < n_infinity:
        print("The number is lesser than Negative Infinity!")
    else:
        print("Negative Infinity is least")

    Output:

    Infinity is greatest
    Negative Infinity is least
  • Function Argument Unpacking is another awesome feature of Python. One can unpack a list or a dictionary as function arguments using * and ** respectively. This is commonly known as the Splat operator. Example here
    defpoint(x, y):
        print(x,y)
    foo_list =(3, 4)
    bar_dict ={'y': 3, 'x': 2}
    point(*foo_list) # Unpacking Lists
    point(**bar_dict) # Unpacking Dictionaries

    Source – source

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s