Thursday 13 November 2014

Java To Python.......

Learning a new thing is always a fun... then whatever it is ... you enjoy it...
I am learning Python now...I knew java and use it for building the application as of now....
While learning the Python, it doesn't seems to hard with respect to the syntax..its very easy..
its like plain text sort of thing being learnt...

Python's simple and straight-forward syntax also encourages good programming habbits, especially through its focus on white space indentation, which contributes to the development of neat looking code.

We try to put out some difference between Python and Java...

Before that I am not criticizing any language here, just wanted you to enjoy this as I had...

1. If you want to print the "Hello World" in java, it looks something like this

public class TestJava {
public static void main(String[] args) {

System.out.println("Hello World");
}
}

and the same thing in Python is

print "Hello, Python!";


2. When it comes to some reserved words ..then yes both the languages has some keys reserved. I dint find any difference here. And the view is same that the reserved words not be used as constant or variable or any other identifier names.


3. Next is line and indentation where In Python the fact is that there are no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount.
Both blocks in this example are fine:

if True:
    print "True"
else:
  print "False"

and the below block in this example will generate an error:

if True:
    print "Answer"
    print "True"
else:
    print "Answer"
  print "False"


Thus, in Python all the continuous lines indented with similar number of spaces would form a block.


4. Quotation in Python: Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the string.

Same is not the case with Java while defining the String literals.. :)

5. Commenting the code in Python : A hash sign (#) that is not inside a string literal begins a comment.
All characters after the # and up to the physical line end are part of the comment and the Python interpreter ignores them.

In java Single line commenting is done by "//" and multiple lines commenting is by /* */. Anything that comes between /* */ will be ignored.

6. Values Assigning to Variable : In Python
   count = 100  # integer assignment
   distance = 10.5 # floating
   name = "Steve"  # String

   In Java

   String name = "xyx";
   Integer count = 100;

   Difference is you need to mention the data type along with the variable name.

7. Multiple Assignment:
   Python allows you to assign a single value to several variables simultaneously.
   For example:
a = b = c = 1

   Same thing is not possible in Java.

   You can also assign multiple objects to multiple variables.
   For example:
  a, b, c = 1, 2, "john"
   and if you print it the output is
   print a,b,c
   1 2 john

8. Lists : Lists are the most versatile of Python's compound data types.
   A list contains items separated by commas and enclosed within square brackets ([]).

   In java you have different collections like ArrayList and Hashset etc.

   One difference between them is that all the items belonging to a list in Python can be of different data    type.

   In Java it does too but seems hard when you introduce Generics.

9. Value access from List :
   In Python the values stored in a list can be accessed using the slice operator ( [ ] and [ : ] ) with
   indexes starting at 0 in the beginning of the list and working their way to end -1.
   list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
   
   print list          # Prints complete list
   print list[0]       # Prints first element of the list
   print list[1:3]     # Prints elements starting from 2nd till 3rd

   In Java use of Iterator or new for each would be used to get the data.

10. Defining a Function
    Here are simple rules to define a function in Python.
    Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
    Any input parameters or arguments should be placed within these parentheses. You can also define       parameters inside these parentheses.
    The first statement of a function can be an optional statement - the documentation string of the               function or docstring.
    The code block within every function starts with a colon (:) and is indented.
    The statement return [expression] exits a function, optionally passing back an expression to the caller.
    A return statement with no arguments is the same as return None.
   
    For Example :
    def printme( str ):
    "This prints a passed string into this function"
    print str
    return

    In Java :
    public void test(Integer i){
System.out.println("funtion" + i);
    }


I would appreciate if anyone add more if they knew and correct me if I am wrong at any point...
Till then cheers...

No comments:

Post a Comment