[JAVA] Python basic memorandum part 2

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. Part 1 is here * This is a fairly basic content. </ strong>

Function definition

python can set default arguments instead of overloading methods.

No arguments </ strong> def doFunc(): Function processing

with arguments </ strong> def doFunc(a, b, c, ...): Function processing

Example

java


//No arguments
void doFunc1() {
    print("The function has been called.");
}

//With arguments
void doFunc2(a, b) {
    print("a + b = " + (a + b));
}

//with return
int doFunc3(a, b, c) {
    retrun a + b + c;
}

//Overload
void overFunc() {
    print("There is no string.")
}

void overFunc(String str) {
    print("The string is" + str + "is.")
}

python


#No arguments
def doFunc1():
    print("The function has been called.")

#No arguments
def doFunc2(a, b):
    print("a + b = " + str(a + b))

#with return
def doFunc3(a, b, c):
    return a + b + c

#Use default arguments
def overFunc(str = None):
    if str is None:
        print("There is no string")
    else:
        print("The string is" + str + "is.")

dictionary

Something like a Java Map (?) Store keys and values.

Example

java


Map<int, String> maps = new HashMap<>();
maps.put(1, "one");
maps.put(2, "two");
maps.put(3, "three");
maps.put(4, "four");
maps.put(5, "five");

//Take out five
maps.get(5);

python


maps = {
    1: "one",
    2: "two",
    3: "three",
    4: "four",
    5: "five"
}

#Take out five
maps[5]

set

A feature implemented to work with sets. A list with unique elements. ~~ List elements cannot be added or retrieved. ~~ Lists and dictionaries cannot be added as elements. ~~ Elements cannot be retrieved. ~~ Index reference is not possible

  • Functions not found in Java!
The set you want to find operator
Union
Difference set -
Logical AND &
Exclusive OR ^
Subset >=Or<=

Example

python


set1 = {1, 2, 3, 4, 5}
set2 = {3, 5, 7}

#Union
wa = set1 | set2
#Difference set
sa = set1 - set2
#Logical AND
seki = set1 & set2
#Exclusive OR
haita = set1 ^ set2
#Subset
bubun = set1 <= set2

Tuple

You cannot change the element. Elements can be added and taken out.

Example

python


tuple1 = (1, 2, 3, 4, 5)
tuple2 = (6, 7, 8, 9, 10)

tuple2[2]
>>8

tuple3 = tuple1 + tuple2

tuple3[9]
>>10

Built-in data type

I've written so far, and if I don't organize the data types that appeared in Part 1 and Part 2, I'm likely to get confused and remember, so I'll write it out once. Looking at the references, I have extracted and left the ones that I found useful and useful.

#Use the type method to find out the data type
type(object)
  • bool(immutable)

Handle only True or False

  • int(immutable)

Handle integers

  • float(immutable)

Handle numbers with a decimal point

  • str(immutable)

Handle multiple characters

  • list(mutable)

Handle multiple elements together

You can add, remove, sort, search for elements, get specific elements </ strong>

list = [Element 1,Element 2, ...]
  • tuple(immutable)

Handle multiple elements together

・ You can add elements and get specific elements </ strong> ・ Advantages ⇒ Can be used as a dictionary key or set element

tuple = (Element 1,Element 2, ...)
  • set(mutable)

Handle sets

・ Store unique elements ・ No order ・ The element cannot be accessed ・ Elements can be changed ・ Set operation is possible </ strong>

set = {Element 1,Element 2, ...}
  • dict(mutable)

Handle multiple elements together Linking keys and values </ strong>

dict = {
Element 1 key:Element 1,
Element 2 key:Element 2,
    ...
}
  • bytes(immutable)

Handle multiple characters (unencoded)

Character strings imported from files and the Internet are imported as bytes type

Delve into bytes type conversion

I thought that bytes type conversion would be essential when creating a website or exchanging data, so I digged a little deeper.

encode ([encoding name [, error handling method]]) decode ([encoding name [, error handling method]])

python


str = "Hello"

#Character string ⇒ byte type
enStr = str.encode("shift-jis", "strict")

#Byte type ⇒ character string
deStr = enStr.decode("shift-jis", "strict")

Afterword

I was told in the comment of Part 1,

Python has no primitive types

I think you have to remember that point without misunderstanding. I would like to advance my understanding so that I can understand and use the differences in data types that handle multiple elements together. I haven't touched on object-oriented programming so far, so I'd like to summarize object-oriented programming as well.

References

Jun Shibata (2016) "Minna no Python 4th Edition" SB Creative Co., Ltd. Python Official Reference Articles that I referred to about built-in data types

Recommended Posts