Jan 27, 2015

Textual description of firstImageUrl

Python is interpreted language or compiled language

Lets start this discussion with definition of interpreted or compiled language followed by understand the same in context of python.
Compiled languages are those languages where source code is converted into native/machine one and that native piece of code is executed at run time and software/program unit we use for such languages are called compiler. Example: C,C++.
Interpreted languages are those one where source code is executed line by line at run time and software/program unit we use for such languages are called interpreter.Example: JavaScript, PHP. 
Interpreted languages are very popular in web application because same web page will open in Linux and windows.Imagine, c++ code in web application it will create a havoc and it may not work due to compilation failure, platform dependent. Here comes interpreted language for rescue and it just follows the instruction and execute line by line source code at all platform.
  • Compiled language or interpreted language are not well defined. Any language can be made complied or interpreted one, it depends on the design and implementation of that language. Design of the languages make them unsuitable for native code generation. 
  • It is very possible to create an interpreter for every compiled language, but the reverse is impossible.
There exist a third category where languages are neither purely compiled or purely interpreted. These languages generate an intermediate representation (called bytecode) and later that bytecode is interpreted by interpreter.These languages take advantages of compiled language (machine instructions, faster execution) and interpreted language(platform independence and suitable for scientific work). Languages like Java, Python,Ruby falls under this category.
In general terminology, these languages are termed as interpreted languages just because intermediate representation is not compiled machine/native code,it's just group of byte code instructions which executes faster than source code and slower than machine/native code.
As concluding remarks, Python(Cpython) is neither a true compiled time nor pure interpreted language but it is called interpreted language.  

Lets understand this intricacy of intermediate representation in context of python(Cpython) :
When python program say sonething.py is executed it generates something.pyc as intermediate representation called bytecode(it is platform independent) and in turn python interpretor executes something.pyc file. Above processes can be depicted in following diagram:
Source file to bytecode generation followed by its interpretation  in python(Cpython)

  1. CPython has been designed to compile as fast as possible performing very little error checking and optimization, so it can run fast and in small amounts of memory. 
  2. User is unaware of the intermediate .pyc generation. It is generated automatically and transparently whenever needed, without the user intervention.
    If .pyc file is not there it will be generated automatically.
    else, modification timestamp(magic number) of .py and .pyc is matched, if it varies it will create new .pyc file else older one will be executed. 
Similarly, other implementation of IronPython which compiles to CLR codes, Jython to JVM codes and executed by appropriate "virtual machines" also known as "interpreters" -  the .Net runtime and the Java VM respectively.
How does python is different from java in terms of intermediate code generation?
Java does not perform automatic compilation.During compilation java does more complex work in order to check errors more thoroughly and perform more optimizations.
Reference : Compiled and interpreted languages pros and cons

Jan 17, 2015

Python Control Flow (If-Else, While, For Loop)

Control flow in python:-  

Python supports various decision making contrl structures like If-elif-else, While /For Loops.
The colon character is used to delimit the beginning of a block of code and python uses concept of indented block to group block statements. In other words, python relies on the indentation
level to designate the extent of that block of code, or any nested blocks of code within. It is common for all control structures in python along with body of function and class.

What is indented block ?
: Below code lines print("I am Indented for if a==2") and print("All the best") are after a tab space(highlighted in yellow) with respect to if a==2 so it is termed as indented, however print("I am not Indented for if a==2") is at same level with respect to if a==2
    if a==2:
        print("I am Indented for if a==2")
        print("all the best")
    print("I am not Indented for if a==2")
Conditional statement - if/elif:- Write a sample code to check whether input number is divisible by 2/divisible by 5 or some other case.
    input_number = 28   # input is hard coded
    if input_number% 2 ==0: # Please note :(Colon here) 
        print "Even number, divisible by 2"
    elif input_number% 5== 0 and input_number% 2 != 0:
       print "Divisible by 5 but odd number"
       print "It is some thing different case"
Here pay attention to colon after each conditional statement and indentation after each of that.It is beauty of python which abstain developer from putting opening and closing braces(and avoid verbosity).

While loop: Write a sample code to print 1 to 10. Here J is initialized to 1 and on each iteration j is compared with 1, if it is less it prints value of j and increment j.
    j = 1
    while j < 11: # Please note :(Colon here) 
        print j
        j= j+1
For loop:- Python's For loop is mainly used for iterating through series of elements like List, set, dict, etc. Consider the syntax of for loop for the time being, we will use For loop in detail when we learn these data structures.
    for element in <iterable>:  #<Iterable> = i in range(0,5) or xrange(6) , etc
Python also support Break and Continue Statements. As all programming suggest to use these two control structures sparingly.
Python does not support switch/case as found in other languages. However we can simulate the same using dictionary and separate method call

# define the function blocks
def zero():
    print "You typed zero.\n"

def sqr():
    print "n is a perfect square\n"

def even():
    print "n is an even number\n"

def prime():
    print "n is a prime number\n"

# map the inputs to the function blocks
options = {0 : zero,
           1 : sqr,
           4 : sqr,
           9 : sqr,
           2 : even,
           3 : prime,
           5 : prime,
           7 : prime,

#Then the equivalent switch block is invoked:


Sample example using control statements: 

1. Convert decimal to Binary.
def decToBinary(n):
    k = [];
    x= n
    while (n>0):
    for j in k[::-1]:
    print('The binary no. for %d is %s'%(x, string))
2. Find factorial of a number
def factorial (num):
        return 1;
        return num*factorial (num-1)
n = int(raw_input().strip())
CutCount = factorial (n)
print CutCount
3. Check even and odd number, read input from stdin
def evenCall(str1):
    s = ""
    for i in range(0,str1.__len__()):
        if i%2 == 0:
    return s

def oddCall(str1):
    s = ""
    for i in range(0,str1.__len__()):
        if i%2 != 0:
    return s
#Read number on input to be tested 
input = int(raw_input())
for i in range(0,input):
    #Read each input from stdin
    strInput = raw_input()
    even1 = evenCall(strInput)
    odd1 = oddCall(strInput)
    print(even1 + " "+ odd1)
Previous : Python Data types                                 Next: Python data structure
Textual description of firstImageUrl

Python fundamentals - Data types and In-built Objects

In previous section we installed python and run hello world program. Now we will delve into building block of python language and will write sample program to understand the same.
As we know that python is both procedural and object oriented language. We will focus on Object oriented aspect of python.
Python is an object-oriented language and classes form the basis for all data types. Python has various built-In Classes like int for integers , float for floating point values and str for character string. i,e: Every thing in Python is class, even primitive data type.
Consider following statement in python :
    student_cgp  =  7.65
How do we interpret this in python - student_cgp is an identifier and it references a floating point object with value 7.65. Two important point need to be noted here is :
1. Identifiers in python are case-sensitive.In Python, identifier is similar to a reference variable in Java or a pointer variable in C++. Each identifier is implicitly associated with the memory address of the object to which it refers.
2. Unlike Java and C++, python is a dynamically typed language, there is no advance declaration associating an identifier with a particular data type. Although an identifier has no declared type, the object to which it refers has a definite type.
Below is the list of built in class in python. It can be classified broadly mutable and immutable classes. Object of immutable classes cannot be changed subsequently once it has been instantiated.
Commonly used built-in classes for Python
Note : 
1. Bool class instances are expressed as the literals True and False.
2. Numbers are mainly of two types - integers and floats. In Python, int and float classes are the primary numeric types. Default value from constructor int() is 0 and float() is 0.0.
3. In Java and c++, we have different int type based on precision. However,python automatically chooses the internal representation for an integer based upon the magnitude of its value.

Before moving ahead we will execute some code in python interpreter and predict outcome:
    >>> int(3.14)
    >>> int(-3.99)
    >>> int('137')

Operators in python:- 

Almost all operators are present in Python as in other languages, some of them needs special mention are as follows :

Comments in python:-

In python single line comment is done using # (pound).Below code uses # for comment: 
 # I am comment- it will print Hello World
  >>>print 'hello world'
  hello world
Note:  It is recommended not to use triple-quotes for block comments as it is for documentation strings not block comments, although it has a similar effect.Always use # in for single or multiple statements. 

Previous : Introduction and installation                                 Next: Python Control Flow (If-Else, While, For Loop)

Jan 9, 2015

Textual description of firstImageUrl

Python learning

Introduction and installation 

Python is a programming language with very long and rich history and proclaimed to be simple and powerful. Indeed, it's simplicity and its usefulness in scientific community is the main reason behind its  popularity and of course, role of MIT and Google to make it popular among developer community cannot be denied. Initially, python was known for accomplishing automation task, however with advancement of time various notable applications written in Python like : Original version of the Google crawler, installer for RedHat, named "anaconda". History of python can be read here (Wikipedia). In summary features of python (From byte of python by Swaroop C H) :
  1. Simple and Easy to Learn.
  2. Free and Open Source
  3. High-level Language 
  4. Interpreted - It does not need compilation into binary, program is directly from the source code. Internally, Python converts the source code into an intermediate form called byte-codes and then translates this into the native language of your computer and then runs it.
  5. Portable 
  6. Procedure-oriented and object-oriented programming support
  7. Extensible and embeddable - It can be used along with c/c++, java , etc. 
  8. Diverse and growing community - It is one of the most important feature of python from developer perspective. Python Standard Library is huge which support to various things involving regular expressions,documentation generation, unit testing, threading,databases, web browsers, CGI, FTP, email, XML, XML-RPC, HTML, WAV files,cryptography, GUI (graphical user interfaces), and other system-dependent stuff.
  9. Its important, python has various implementation like Cpython(C), Jpython(Java), IronPython(C#), etc. Refer this for more detail.
    Default implementation is Cpython (it is developed in C language) and it is interpreted language and program is executed on CPython Virtual Machine. In further post and when we say python, we actually mean Cpython. Languages like java, C# have extended default implementation and developed IronPython and Jpython respectively.  

Installation of Python in windows:-

The very first step which is required to start learning python is to install a stable version of python.
1. Download python executable : Python for windows can be downloaded from here. Suppose we have installed python in C drive and it will be available at "C:\Python27"(Assume you have installed python 2.7 version).
2. Update environment variable (Optional) : It is good practice to add python installed directory in environment variable under path. Here "C:\Python27" should be added as system variable under path.For reference see below diagram:

Add "C:\Python27" under system variables section in path.
Test Python installation:- Python is formally an interpreted language, in order to execute python program we need to open python interpreter.Python interpreter can be launched in a number of ways, let's see two most common ways to do the same:
1. Open command prompt -> type python and hit enter.
 If you see a few lines of information followed by the Python prompt >>> , it indicates python has        been installed successfully and  you are ready to GO!! and the window which is appearing now is        python interpreter window. Now, write your first sample program in python. IN python interpreter      just type print "Hello World!!" and it will respond with Hello World!!.
    >>>  print "Hello World!!"
    Hello World!!
2. Go to All programs -> Python 2.7-> Python (command line). Once you click Python (command line), python interpreter appears and here you can write python program. Lets do sum of two number and display result.
    >>>  print 12+23
We have installed python successfully and wrote Hello world, sum program. Its time to move on with fundamentals of python and built-in data structure in python and its uses.It will be discussed in next section.

Read also: Why python is called interpreted language?
  Next: Fundamentals of Python

Jan 3, 2015

Textual description of firstImageUrl

NoSQL database - Different types of NoSQL database

Relational databases have a long-standing position in small and medium-sized enterprises. As volume of data is increasing industries are looking for some alternative which can serve very high request rates at very low latency. Relational database require vertical up-gradation of system(highly efficient cpu/processor) and it is a costly affair and their is an upper limit too, up to what value cpu/processing unit can be upgraded.
Since relational databases(oracle, Mysql) are SQL/table oriented so the alternative databases named NoSQL(initially abbreviated as no sql later it became not only SQL) databases provides a mechanism for storage and retrieval of data that is modelled in means other than the tabular relations used in relational databases. NoSQL broadly cover to all non-relational databases.

NoSQL databases follows BASE(Basically Available, Soft state, Eventual consistency) properties instead of ACID(Atomicity, Consistency, Isolation,Durability) properties. There is trade-off of preferring BASE over ACID when we are dealing with large volume of  data and shifting gear from transaction processing to dealing with real time application or analytic operations. NoSQL database systems has flexible data model, higher scalability, and superior performance, however most of these NoSQL databases does not include the very foundation - expressive query language, secondary indexes and strong consistency,  that has made relational databases so useful for generations of applications . Refer this article for detailed explanation about BASE vs ACID.

Motivation behind inclination towards nosql databases can be summarized as :
1. Need to handle multi-structured data types(structured,semi-structured, unstructured and
    polymorphic data) or scale beyond the capacity constraints of existing systems. NoSQL
    support "horizontal" scaling to clusters of machine and used extensively in big
    data and real-time web applications.
2. Desire to identify viable alternatives to expensive proprietary database software and hardware.
    Commodity hardware can be used to build database ecosystem instead monolithic server and
    strong infrastructure.
3. Agility or speed of software development.
There are long list of databases which comes under category of NoSQL ecosystem. Based on the data structure internally these database uses, it can be broadly classified in three category, it is called data model in NoSQL terminology.
  • Document model - Document databases store data in documents.These documents typically use JSON (JavaScript Object Notation) structure. It is most commonly used due to its flexibility and ability to query on any field. It is used for semi-structured data. Document-oriented system relies on internal structure of the document in order to extract metadata that the database engine uses for further optimization. Document DB get their type information from the data itself and each instance of it is different from each other. Each instance contains relevant information together(combining employee details and its address). MongoDB and  CouchDB are two popular document oriented nosql database. Below diagram shows document sample that contains info from two different source /relational table.

    Document storing data as key value pairs
  • Graph model - Graph databases use graph data structure to store data. It is being used when traversing relationships are core to the application like social network connections.
    Neo4j and Giraph are commonly known graph database.Below diagram shows how graph model is created. 
    Image courtesy: wikipedia 
  • key-value model :-  key-value stores are the most basic type of non-relational database. It is schema less database and every item in the database is stored as an attribute name/key together with its value. It is commonly used for representing unstructured data. Data is considered to be inherently opaque to the database because based on that key data is accessed. Following diagram represent typical key-value store database. Example of Key/Value model database : Riak and Redis 
  •  Wide column model(Columnar NO SQL database ) :- Wide column model is similar to traditional relational database(consisting of table and columns) with one most significant difference : number of columns is not fixed for each record.
    i.e : columns are created for each row rather than being predefined by the table structure.

    Data retrieval only supported using primary key per column family
    Example of Wide Column model database : HBase and Cassandra
Here record 2 and 3 having different columns, Email is added for 3rd record. Consider Wide column database as key value store with value is stored as key-value subset.
Below diagram summarise various data model and corresponding databases with strength and weakness.(Diagram courtesy: Edureka)
Note : 
  1. Document databases provide the ability to query on any field within a document. 
  2. The document data model is the most natural and most productive because it maps directly to objects in modern object-oriented languages.
  3. Key-value stores and wide column stores provide a single means of accessing data: by primary key.  
  4. Document databases and graph databases can be consistent or eventually consistent. MongoDB provide tunable consistency.By default, data is consistent — all writes and reads access the primary copy of the data. However, read operation against secondary copies. 
  5. Eventually consistent systems provide some advantages for inserts at the cost of making reads, updates and deletes more complex, In eventually consistent systems, there is a period of time in which all copies of the data are not synchronized.

Summary of types of NOSQL  

  • Wide Row - Also known as wide-column stores, these databases store data in rows and users are able to perform some query operations via column-based access. A wide-row store offers very high performance and a highly scalable architecture. Examples include: Cassandra, HBase, and Google BigTable.
  • Columnar - Also known as column oriented store. Here the columns of all the rowsare stored together on disk. A great fit for analytical queries because it reduces diskseek and encourages array like processing. Amazon Redshift, Google BigQuery,Teradata (with column partitioning), Druid.
  • Key/Value - These NoSQL databases are some of the least complex as all of the data within consists of an indexed key and a value. Examples include Amazon DynamoDB, Riak, and Oracle NoSQL database
  • Document - Expands on the basic idea of key-value stores where "documents" are more complex, in that they contain data and each document is assigned a unique key, which is used to retrieve the document. These are designed for storing, retrieving, and managing document-oriented information, also known as semistructured data. Examples include MongoDB and CouchDB
  •  Graph - Designed for data whose relationships are well represented as a graph structure and has elements that are interconnected; with an undetermined number of relationships between them. Examples include: Neo4J, OrientDB and TitanDB

References :