Beginning of Phtyon programming for Java acquirers ①

Self-introduction

Financial system SE for the 4th year since joining the company. I usually make web applications on a framework that uses similar Struts in the Java language. I'm getting tired of the Java language, so I'll study Python, which is popular now. (It's too late now ... with self-discipline)

So, I'm going to study Python from the perspective of a Java master. I will summarize the main points, so please keep in touch if you like!

Python grammar

Basic key

① Comment statement

For comments, add "#" at the beginning of the sentence. Multi-line comments are often treated as comments by enclosing them in "'''" or "" "", which display a multi-line string literal described later.

##It is a comment

'''
How to write multi-line comments
XXX
YYY
'''

② Index specification

In Java language, when you want to extract a part of a character string or a part of a list, you specify it as [n]. This is the same for Python. The specification of n also starts from 0. However, in the case of the character string type, when extracting the final element (end), it was described as "str (str.Length () -1)", but in Python it can be described as [-1]. This is called a "negative index". (From the right end, -1, -2, -3 ...)

③ Processing block is judged by indentation

In Java, the processing block was recognized by the compiler by explicitly enclosing a certain range with "{}". This is expressed only by indentation in Python.

④ ":" is used instead of "{}" in the conditional statement.

In Python, for if statement, for statement, and while statement, write ":" in the place where Java "{" is written. Specific examples are as described below.

Data type

Click here for basic data types.

Data type Contents Concrete example Data types in Java
Numeric type (int type) Integer literal 100 int
Numeric type (float type) Floating point literal 3.141592 double, float
Character string type (str type) String literal Hello, Hello String
Boolean type (bool type) True,Two values of False True, False boolean

Both single and double quotes are OK for string literals. Even if there is a line break in the character string within the enclosed range when surrounded by three'or', it is treated as one character string. (The line feed of the source code is processed as the line feed code \ n)

'Test'
"Test"

# 'aaa¥nbbb¥nccc'Is displayed
'''aaa
bbb
ccc'''

Also, use "None" to indicate that the value itself does not exist. It seems that it can be understood as almost the same concept as Java null. Reference: [Introduction to Python] What is the null object "None"

Objects and variables

Variable assignment is basically the same as Java language. However, in the Java language, for primitive type variable assignment, the variable value itself is assigned (= reference is not assigned), but it should be noted that in Python, it is a reference assignment operation.

num1 = 123
num2 = num1
#In this case, the reference of num1 is copied to num2.
# num1,If num2 is an int type, the value itself of 123 is set for num2 in Java language.

#Now set the variable num2 to a new value
num2 = "Hello"
#num2 will have a new object that will reference it
#In Java language, you can not set a character string in numeric type num2, but in Python it is possible

Reference: Primitive type (basic type) and reference type (class type)

Conditional statement

Similar to Java, Python uses if to describe conditional statements, but The following are different, so you only need to understand here.

#Since the for statement is explained immediately after, it is repeated in the block
#I just want you to understand that you are making a condition judgment
for tmpNum in range(10):
    # end = ""Eliminates automatic line breaks after standard output with
    #Java language print()And println()Same as the difference
    print(str(tmpNum) + "--> ", end = "")
    
    if tmpNum % 2 == 0:
        print("2N")
    elif tmpNum % 3 == 0:
        print("3N")
    else:
        print("not 2N, 3N")

Repeated sentence

In Java language, for statement, extended for statement, while statement, etc. are used. Basically the same for Python. Looking at each while comparing them with the Java language, it looks like this.

for statement

It looks like this in the Java language.

//grammar
for (Initialization formula;Conditional expression;Change formula) {
    //Processing to be repeated when the conditional expression is true
    //Break when escaping;
    //Continue when skipping;
}
//Description example
for (int i = 0; i < list.size(); i++) {
    System.out.println(i + " : " + list.get(i));

    if (i > 2) {
        break;
    }
    if (i % 4 == 0) {
        continue;
    }
}

If you write these in Python, it will be like this.

#grammar
for counter variable in range(Number of repetitions):
Iterative processing

#Description example
# 0,1,2,3,4 is output
for count in range(5):
    print(count)

#If you do this 3,4 is likely to be output, but the same 0 as above,1,2,3,4 is output.
#That is, the counter variables used in the for statement are initialized at run time.(0)Will be done.
#So, if you want to achieve something other than 0 start, range(3, 5)Describe as. (3,4 is output)
count2 = 3
print(count2) #3 is output
for count2 in range(5):
    print(count2)

for count3 in range(3, 5):
    print(count3)

Click here for how to use break and contitue.

strings = ['ruby', 'python', 'perl', 'java', 'c']
for string in strings:
    if string == 'python':
        print('HIT')
        break  #Since they match, break out
    print(string)
#Execution result → ruby, HIT
#continue is the same as break.

while statement

There is a while statement as well as the Java language. The description example looks like this.

count4 = 0
# while (Conditional statement):
#Processing you want to repeat
#* Conditional statements are repeated only as long as they are satisfied (similar to Java)
while (count4 < 10):
    print(str(count4))
    count4 += 1

Plus α: for-else sentence

You can use else as well as the while statement.

#If the break condition is not met in the for statement, the else block is executed.
#* Note that else is always executed if the loop never occurs.
scores = [100, 71, 80, 99, 75]  #Passed because there is no less than 70 points
for score in scores:
    if score <= 70:
        break
else:
    print('Pass')
#Execution result → Pass

Thank you for your relationship so far ^^ The basic edition ends here. Next, we will describe lists, dictionaries, sets, and functions.

Recommended Posts

Beginning of Phtyon programming for Java acquirers ①
Check for the existence of BigQuery tables in Java
The beginning of cif2cell
Recruitment of programming masters
[For beginners] Basics of Python explained by Java Gold Part 2
[For beginners] A word summary of popular programming languages (2018 version)
[For beginners] Basics of Python explained by Java Gold Part 1