JavaPython:List
Mine navigeerimisribale
Mine otsikasti
Java vs Python |
|
In python, string can reside in a pair of single quotes as well as a pair of double quotes. It supports multiplication: "x"*3 is "xxx".
Näide
Java | Python |
---|---|
<syntaxhighlight lang="java" line="1" >
//arraylist is closest with list in python ArrayList<Integer> aList = new ArrayList<Integer>(); //add aList.add(1); aList.add(3); aList.add(2); aList.add(4); //index int n = aList.get(0); //get sub list List<Integer> subList = aList.subList(0, 2); //1, 3 </syntaxhighlight> |
<syntaxhighlight lang="python" line="2" >
aList = [] aList = [1, 'mike', 'john']
aList.append(2)
aList.extend(["new","list"]) print aList
aList = [0,1,2,3,4,5,6]
print len(aList)
print aList[2]
print aList[0:3]
print aList[2:]
print aList[-2]
aList[0] = 10 print aList
</syntaxhighlight> |