It is a memorandum that I learned according to References with the intention of becoming able to read Python as much as possible. This is a summary of the points that I was interested in and the points that I found useful compared to Java. * This is a fairly basic content. </ strong>
The four arithmetic operations (+-* /
) and surplus calculation (%) are the same as in Java, but exponentiation can be convenient.
However, Python's /
becomes a floating decimal number when it is not divisible. Use the //
operator to get an integer value (rounded down to the nearest whole number for floating point numbers).
In Python, you can use "**
" to raise power.
java
Math.pow(2, 3)
3 / 2
python
2 ** 3
3 // 2
Python does not require type definition when defining variables. </ strong>
java
//String
String str = "AIUEO";
//Numbers
int index = 100;
//list
List<String> list = new ArrayList<String>(Arrays.asList("Ah", "I", "U", "e", "O"));
python
#String
str = "AIUEO"
#Numbers
index = 100
#list
list = ["Ah", "I", "U", "e", "O"]
In Python, when concatenating multiple lists, you can combine them with "+".
java
List<String> list1 = new ArrayList<String>(Arrays.asList("Ah", "I", "U", "e", "O"));
List<String> list2 = new ArrayList<String>(Arrays.asList("Or", "Ki", "Ku", "Ke", "This"));
List<String> list3 = new ArrayList<>();
list3 = Stream
.concat(list1.stream(), list2.stream())
.collect(Collectors.toList());
python
list1 = ["Ah", "I", "U", "e", "O"]
list2 = ["Or", "Ki", "Ku", "Ke", "This"]
list3 = list1 + list2
java
List<String> list = new ArrayList<String>(Arrays.asList("Ah", "I", "U", "e", "O"));
//Delete "A"
list.remove(0);
python
list = ["Ah", "I", "U", "e", "O"]
#Delete "A"
del list[0]
java
List<int> list = new ArrayList<int>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Arrays.stream(list).sum();
python
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum(list)
Java is the size method Python is a len method Java's length method is a method that returns the length of a string, so be careful not to confuse it </ strong>
java
List<int> list = new ArrayList<int>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
list.size()
python
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
len(list)
You can retrieve multiple consecutive elements of a list.
java
List<int> list = new ArrayList<int>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
sList = list.subLisy(2, 4)
//sList[3, 4]Enter
python
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sList1 = list[2:4]
#sList1[3, 4]Enter
sList2 = list[:5]
#sList2[1, 2, 3, 4, 5]Enter
sList3 = list[7:]
#sList3[8, 9, 10]Enter
for i in list: Repeated content </ strong>
java
List<String> list = new ArrayList<String>(Arrays.asList("Ah", "I", "U", "e", "O"));
for(String str : list) {
println(str);
}
/*
Ah
I
U
e
O
*/
python
list = ["Ah", "I", "U", "e", "O"]
for str in list:
print(str)
#Ah
#I
#U
#e
#O
Conditions for repeating can be specified by arguments.
When repeating from 0 to n times </ strong> for i in range(n): Repeated sentence
When repeating from m to n </ strong> for i in range(m, n): Repeated sentence
When repeating from m to n by increasing l </ strong> for i in range(m, n, l): Repeated sentence
When repeating n times from m </ strong> for i in range(m, m + n): Repeated sentence
When repeating n times from m </ strong> for i in range(m, m + n, l): Repeated sentence
java
int sum = 0;
for(int i = 0; i < 10; i++) {
sum += i;
print(sum);
}
/*
0
1
3
...
45
*/
python
sum = 0
for i in range(10):
sum += i
print(sum)
#0
#1
#3
#...
#45
Java conditional branch with else if Python is conditional with elif
Comparison (without else) </ strong> if 1 == 1: print("True") Comparison (with else) </ strong> if 1 == 1: print("True") else: print("False") Comparison (with conditional branching) </ strong> if a == "ABC": print ("ABC") elif a == "DEF" print ("DEF") else: print ("not ABC or DEF")
Search </ strong> if 5 in [5, 10, 15]: print("True")
java
int a = 0;
if(a == 1) {
print("a is 1");
} else if (a == 2) {
print("a is 2");
} else {
print("a is neither 1 nor 2");
}
python
a = 0
if a == 1:
print("a is 1")
elif a == 2:
print("a is 2")
else:
print("a is neither 1 nor 2")
I realized firsthand that learning one language makes it easier to understand other languages. I felt that the basics of Java were solidified again, so I may put together a memorandum about functions and class definitions.
Jun Shibata (2016) "Minna no Python 4th Edition" SB Creative Co., Ltd. Official Python Reference [https://www.python.org/]
Recommended Posts