Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 15th, 2004, 12:42 PM
my_venus my_venus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 9 my_venus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Python vs Java ?????

Hello !

I'm a studient and i have been researching on Python. My project is releated to programing web/internet with open source . At the beginning , i had decided to choose java - OOP language. Java has been famous as a power programing language , everyone knows this. But recently i have heard about Python-OOP language ,and i have read some articles about Python , i like it. But i really don't know how it is more power than Java (Some companies have said that :" Python is more power than java "). Can you show me some main ideas about this compare ( Python VS Java , how Python is
more power than java ?) . Or you can show me some advantages of Python .
If you think Python is'nt more power than Java , can you show me your reasons ? Please , help me.
Thanks so much.

Reply With Quote
  #2  
Old September 15th, 2004, 01:55 PM
Boki's Avatar
Boki Boki is offline
A wanna-be guru of some sort
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Either online or offline
Posts: 624 Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 4 h 7 m 13 sec
Reputation Power: 14
Well I know Java programmers who, after workimg with Java on real-world projecvts say that Python's standard library is better than Java's, but I am not sure about that. Both Java and Python are slow, but Python is only interpreted, whereas in Java you need to compile code to get the bytecodes and then interpret the bytecodes, so that process is a bit more complicated. On the other hand, Java is closed source and Python is open source.

Here's a link to look at:http://www.ferg.org/projects/python...de-by-side.html

Reply With Quote
  #3  
Old September 15th, 2004, 05:12 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,254 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 9 m
Reputation Power: 265
Firstly to correct Boki, both Java and Python compile down to a bytecode which is then interpreted, but in Java it has to be done as a separate step, while with Python it is done automatically.

There are several reasons why Python is considered more powerful than Java.

Firstly it has a higher level of abstraction, with many data structures as built in types, and many operations built into the language that would need to be done through a library in Java.

For example here is a simple hello world program in Java and Python

Code:
#Python:
print "hello world"


Code:
//Java:

public class Hello {
   public static void main(String [] args) {
      System.out.println("hello world");
   }
}


(My Java is a bit rusty and I have not tested the code, so there may be errors in it).

And here is something a bit more complex, creating a dictionary and then printing out the key/value pairs in it:

Code:
#Python
myDict = { 1: 'one', 2: 'two', 3: 'three' }

for key, val in myDict.items():
  print key, val


Code:
//java

import java.util.*;

public class Dict 
{
    public static void main(String [] args) 
    {
            Hashmap myDict = new Hashmap();
            myDict.put(new Integer(1), "one");
            myDict.put(new Integer(2), "two");
            myDict.put(new Integer(3), "three");
            
            Iterator keyIterator = myDict.keySet().iterator();
            while ( keyIterator.hasNext() )
            {
                Integer key = keyIterator.next();
                String val = myDict.get(key);
                System.out.println( key.ToString() + " " + val );
            }
    }
}


Secondly, while Python fully supports object oriented programming, it does not force you to use it. Notice the lack of class declarations in the previous examples, while Java insists on you creating classes, even if they are never used. Python on the other hand also supports functional and procedural programming, so you can use the best tool for the job.

Thirdly, Python is dynamically typed. Notice in the previous Java examples that you have to declare the types of every variable. In Python a variable can take any type of object, so you do not have to declare it. Python objects are also dynamic - you can add and remove members on the fly.

Fourthly, list comprehensions and generators. I do not have space here to explain them, but they enable you to do in a few lines things that Java programmers would not even consider attempting.

Fifthly, the Python interactive environment. This enables you to try out bits of Python code on the fly and immediately see the results.

Sixthly, in Python functions and methods are first class objects - you can pass a function to another function, or store it in a variable to be called later. In Java you have to muck about writing interfaces and creating anonymous inner classes - complex code to do something very simple. In Python you can also create a function inside another function and return it, with simple closures. e.g.

Code:
>>> # python code run at the interactive console
>>> # create a function that returns a function that adds the number that you passed 
>>> #to the first function to whatever you pass to the second function.  Phew!
>>> def makeAdder(x):
... 	def add(n):
... 		return n+x
... 	return add
... 
>>> add10 = makeAdder(10)
>>> add10
<function add at 0x00DDAEB0>
>>> add10(3)
13


Code:
//I have no idea how you would do this in Java!


So in summary, Python allows you to create programs that are much shorter and more readable than in Java, and so development is much quicker.

Dave - The Developers' Coach

Last edited by DevCoach : September 15th, 2004 at 05:15 PM.

Reply With Quote
  #4  
Old September 15th, 2004, 06:28 PM
Boki's Avatar
Boki Boki is offline
A wanna-be guru of some sort
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Either online or offline
Posts: 624 Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 4 h 7 m 13 sec
Reputation Power: 14
Quote:
Originally Posted by DevCoach
Firstly to correct Boki, both Java and Python compile down to a bytecode which is then interpreted, but in Java it has to be done as a separate step, while with Python it is done automatically.

Yep, I overlooked that, however my point was that in Python all that at least appears to be simpler, less work for the programmer to do.
As for the shortness of Python programs, Python's core is small, but powerfull. Even in Jython, an implementation of Python on Java platform, coding is sometimes 30-40% less than the one for the same applications in Java. Python is great because it allows you to focus on writing programs right away, without having to learn too much syntax, That's why IMO it is a great language to start with.

Reply With Quote
  #5  
Old September 19th, 2004, 10:31 AM
my_venus my_venus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 9 my_venus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thank you so much ,special thank DevCoach.

Reply With Quote
  #6  
Old October 2nd, 2004, 01:34 PM
my_venus my_venus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 9 my_venus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Fourthly, list comprehensions and generators. I do not have space here to explain them, but they enable you to do in a few lines things that Java programmers would not even consider attempting.



Can you tell me more , thanks.

Reply With Quote
  #7  
Old October 2nd, 2004, 05:49 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,254 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 9 m
Reputation Power: 265
Quote:
Originally Posted by my_venus
Fourthly, list comprehensions and generators. I do not have space here to explain them, but they enable you to do in a few lines things that Java programmers would not even consider attempting.



Can you tell me more , thanks.


Sure.

List comprehensions enable you to create a list directly from an expression. the syntax is an expression using 'for' and 'if' statements inside square brackets.

For example, here is a list comprehension that creates a list of the squares of all the numbers from 0 to 20 that are divisible by 3:

Code:
>>> [x*x for x in range(20) if (x%3) == 0]
[0, 9, 36, 81, 144, 225, 324]
>>> 


This is a shortcut for

Code:
list =[]
for x in range(20):
  if (x % 3) == 0:
    list.append(x)


While list comprehensions are just a shorthand way of expressing a loop that creates a list, generators are a new kind of function that create an iterator. A generator can be thought of as a function that can return a value, then carry on from where it left off. It uses the 'yield' keyword instead of 'return' to return a value.

Here is a generator that returns a sequence of squares of numbers that are a multiple of the starting parameter:

Code:
>>> def squareMultiples(n):
... 	current = 0
... 	while 1:
... 		yield current*current
... 		current += n
... 		



So squareMultiples(3) will return an iterator that creates the squares of the multiples of three. However unlike the list comprehension above the generator creates an iterator into an infinite sequence, so the code that uses it will need to limit how often it iterates:

Code:
>>> for x in squareMultiples(3):
... 	if x > 1000: break
... 	print x,
... 
 0 9 36 81 144 225 324 441 576 729 900


Of course generators and list comprehensions are not limited to mathematical expressions. Here is a generator that acts as a filter on a sequence of strings, only returning strings that contain the given matchString:

Code:
>>> def findStrings(matchStr, sequence):
... 	for s in sequence:
... 		if matchStr in s:
... 			yield s
... 


And since file objects are iterable in Python, we can use this as a simple grep:

Code:
>>> classes = [ line for line in findStrings('class', open('myfile.py') ]


this will create a list of all the lines in 'myfile.py' that contain the string 'class'.

In Python 2.4 (currently in alpha) you can also have generator expressions, which are similar to list comprehensions but create a generator instead of a list. So for example to caculate the sum of the squares of the odd numbers from 1 to 100:

Code:
>>> sum(x*x for x in range(100) if x%2 == 1)
166650


Dave - The Developers' Coach

Last edited by DevCoach : October 2nd, 2004 at 05:54 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python vs Java ?????


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT