Migrating From Java To Python.

Why ?..

The very first thing comes to mind is Python is an open source programming language, Which gives us an advantage over wide community support.

The second most important concept is Python is dynamically typed programming language, Which saves hell lot of time for you when you are in a hurry. The third concept which most of python programmers like is code structure, Python is Indentation specific rather than semicolon and braces like java and c or c++ which makes python code organised and easily understandable.

The final and important thing is it’s Easy and Powerful. Please do not trust me see for yourself.

What we learn here?..
  • Data types
  • Collections
  • OOPs Concepts
Lets learn woo yaa ..
  1. Data Types :-

Python holds all basic data types provided in java. Int, Float, Boolean, Strings.

Java

int i=5;
float f=5.0;
boolean b=false;
String s="Hello World";
//Type casting
int i= 5;
float f=(float)i; //5.0
String s= "5";
int i=Integer.parseInt(s); //5
float f=Float.parseFloat(s); //5.0
//string manipulations
String s1="hello";
String s2="world";
String s3=s1.concat(s2); //helloworld
String s4 = s1 + s2; //helloworld
s1.charAt(2); //l
s1.length(); //5
s1.contains("ll"); //true
s1.endsWith("lo"); //true
s1.toLowerCase();  //hello
s1.toUpperCase();  //HELLO

Python

i=5 
f=5.0 
b=False 
s="Hello World"
#Type casting
i=5
f=float(i)
s="5"
i=int(s)
f=float(s)
#string manipulations
s1="hello"
s2="world"
s3="{0} {1}".format(s1,s2) #hello world
s4=s1+s2 #helloworld
s1[2] #l
len(s1) #5
"ll" in s1 #True
s1.endswith("lo") #True
s1.lower() #hello
s1.upper() #HELLO

Data Types :-

 

2.Collections:-

Collections in python does not have restriction on the data type which gives us more power while storing multiple data types.

Java

//Lists
List <String> l1= new ArrayList<String>(); 
List <String> l2= new LinkedList<String>();  
List <String> l3= new Stack<String>(); 
 
//Queue
Queue<String> q1 = new PriorityQueue();  
Queue<String> q2 = new ArrayDeque()

//Sets
Set<String> s1 = new HashSet<String>();  
Set<String> s2 = new LinkedHashSet<String>();  
Set<String> s3 = new TreeSet<String>();  

//

Map<int,String> s1 = new HashMap<int,String>();  
Map<int,String> s2 = new LinkedHashMap<int,String>();  
Map<int,String> s3 = new TreeMap<int,String>();

Python

#List
l1=list()
l2=[1,2,"hello",True,["world",12],55.05]

#Tuple
t1=tuple()
t2=(1,2,5,8,7,1,6,12,56)

#Set
s1=set()
s2={1,2,3,"hello",7} #No duplicates

#Dictionary
d1=dict()
d2={1:"hello",2:55,3:[1,2,3]}

3.OOPS:-

 Python oops looks very similar to java with some syntax changes, Major concepts would be constructors, destructors, inheritance.

Java

public Class SomeThing {
  String name;
  String email;
  SomeThing(String name,String email){ //Constructor
    this.name=name;
    this.email=email;
  }
  public void someFunction(){ //Function
    System.out.println(this.name+" "+this.email);
  }
}

SomeThing something = new SomeThing("john","john@abc.com");
something.someFunction();

Python

class SomeThing():
  def __init__(self,name,email): #Constructor
    self.name=name
    self.email=email

  def some_function(self): #Function
    print self.name,self.email

something=SomeThing("john","john@abc.com")
something.some_function()

 

362 total views, 1 views today

Posts created 6

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top