Python basics memorandum

Article summary

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>

Arithmetic operator

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.

Example

java


Math.pow(2, 3)
3 / 2

python


2 ** 3
3 // 2

variable

Python does not require type definition when defining variables. </ strong>

Example

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"]

Join list

In Python, when concatenating multiple lists, you can combine them with "+".

Example

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

Manipulate the list

Delete the element

Example

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]

Sum all the values in the list

Example

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)

Check the number of elements in the 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>

Example

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)

slice

You can retrieve multiple consecutive elements of a list.

Example

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

Repeat (for statement)

Repeat in list

for i in list: Repeated content </ strong>

Example

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

Specify the number of times and repeat

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

Example

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

Conditional branch (if statement)

Java conditional branch with else if Python is conditional with elif

  • Addition We received additional information in the comments regarding string comparisons!

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")

Example

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")

Afterword

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.

References

Jun Shibata (2016) "Minna no Python 4th Edition" SB Creative Co., Ltd. Official Python Reference [https://www.python.org/]

Recommended Posts