A Data Science Central Community
3. Sequence Type
This time, we compare the sequence types of the two languages. By the terminology sequence type, we mean the data structure that is kind of a sequence of data.
In Java, we have: String, array, list(arraylist and linkedlist), set;
In Python, we have: Str, tuple, list, set;
Yes, String is one of the sequence type, because it is a sequence of char, isn’t it? About the Sting type, we only need to know its implementation is the same in both languages, and that’s it.
They are rarely be used in the real world, but they are the very fundamental block. The shot cut to understand them is to notice 3 things:
You can assign a new value to the elements of an array whenever you want, code like:
int[] myArray = {1,2,3};
System.out.print(myArray[0]);
myArray[0]=2;
System.out.print(myArray[0]);
is valid. But in Python, once a tuple is built, then the value of the elements is fixed like constants. You can’t change it. Code like:
myTuple = (1,2,3)
myTuple[0] = 2
is not valid.
Static in size means once an array or a tuple is well built, you can’t change its size, you can’t append a new element to it.
Array elements are bounded by the type you state when you declare the array. Like:
int[] arrayName;
then each of the elements of arrayName must be integer type.
In Python, there is no type bound on the tuple object. You can build a tuple like this:
myTuple = (1,’a’,2.3,”hello “)
List is a very important sequence type in both languages because of its dynamic feature. In java, the mainly difference of list from array is that the size of a list is adjustable. You can adjust the size of a list when necessary. And in Python, the mainly difference of list from tuple is that you can change the value of elements whenever you want.
In java, List is not a primitive type. It is an interface of three classes: ArrayList, LinkedList and Vector. The List is a resizable array(It is actually developed base on array). For more details(for example, difference between ArrayList and LinkedList) you can refer to the following link:
http://www.programcreek.com/2013/03/arraylist-vs-linkedlist-vs-vector/
In Python list is a primitive type, thus it’s very convenient to use. The following link will lead you to the site that shows the detail.
http://effbot.org/zone/python-list.htm
The summary is, the list type in both languages has the same features, adjustable size, mutable elements and the elements could be accessed by index. And again, in Java, the elements of list must be of the same type (primitive type or predefined class) while in Python there is no type bound for the elements of list.
Set is a mathematical concept. It is a collection of distinct elements, with particular set operators: intersection and union. In Python, you can build up a collection with the same features by the type set. The following is a simple tutorial for the type set in Python:
http://greeennotebook.com/2010/06/python-sets-tutorial/
However, in Java, the type set is quite different. The same thing is, duplicating elements is not allowed. But the mathematical set operations are not well supported(compared to Python). Please refer to the following link for more details:
http://www.programcreek.com/2013/03/hashset-vs-treeset-vs-linkedhas...
It seems that the type set in Java focus more on the performance of accessing and mutating elements. And Python focus more on set calculations than Java.
In java, we generally use for loop like this:
for (int i =0; condition on i;i++)
{body}
It also supply a foreach loop for sequence iteration:
for(type element : sequence object)
{body}
The loop will take some actions on each element of the sequence object. If you read ‘:’ as ‘in’, then the syntax is analog to the for loop in Python.
There is one thing about looping in Python is worth to notice. When using range() and xrange() methods for looping, the performance is different:
import time
t1=time.clock()
for i in range(1000): pass
t2=time.clock()
for i in xrange(1000):pass
t3=time.clock()
print t2-t1
print t3-t2
output:
5.89294900688e-05
4.39582142135e-05
Obviously, using xrange() is better. Also, by range(size), you create a list of a determined size. Conversely, xrange(end number) is a generator which generate a number from 0 until the end number, thus xrange() costs less memory. Conclusion, xrange() is better for looping in Python.
When studying Java and Python, always keep in mind the distinction of type bound, namely the static-typed and dynamic-typed feature. In java, all objects are strictly restrained in type; In Python, all objects are always free of type restrict.
Comment
Thanks for your complememt Davide. When I check the Java api, I skipped these two methods because of the boolean return type.
When apply c.retainAll(b) and c.addAll(b), the set c will be modified to be the result(the intersection or the union of the original sets).
I will do the correction to the article. Thanks again Davide!
Nice comparison, it is a good guide to the choice of a programming language.
In Java, set operations can be performed using addAll(.) for the union and retainAll(.) for the intersection
e.g. c <- a intersection b is:
Set<...> c = new HashSet<...>(a);
c.retainAll(b)
e.g. c <- a union b becomes
Set<...> c = new HashSet<...>(a);
c.addAll(b)
© 2021 TechTarget, Inc.
Powered by
Badges | Report an Issue | Privacy Policy | Terms of Service
Most Popular Content on DSC
To not miss this type of content in the future, subscribe to our newsletter.
Other popular resources
Archives: 2008-2014 | 2015-2016 | 2017-2019 | Book 1 | Book 2 | More
Most popular articles
You need to be a member of AnalyticBridge to add comments!
Join AnalyticBridge