Learn Python! Comparison with Java (basic function)

Introduction

Now that I've started learning Python, I'd like to compare it with the Java I've learned.

print function

print('Hello')
print("Hello")

Execution result スクリーンショット 2020-11-09 150744.png For Java

System.out.println("Hello");

Comment out

#Comment out
print('No comments are displayed')

For Java

//Comment out
System.out.println("No comments are displayed");

variable

name = 'Sato'
age = '20'
print(name)
print(age)

Execution result スクリーンショット 2020-11-09 151912.png For Java

String name = "Sato";
int age = 20;
System.out.println(name);
System.out.println(age);

input function

name = input('Please enter your name>>')
age = input('Please enter your age>>')
print(name)
print(age)

Execution result スクリーンショット 2020-11-09 154543.png For Java

System.out.println("Please enter your name");
String name = new java.util.Scanner(System.in).nextLine();
System.out.println("Please enter your name");
int age = new java.util.Scanner(System.in).nextInt();
System.out.println(name);
System.out.println(age);

* Numbers entered by the input function are treated as str (character string), so the following data type conversion is required </ font>

Data type conversion

Convert from str type to int type

#Substitute the string "1" for x
x = '1'
#Convert string "1" to integer "1"
y = int(x)
#Calculate between integers
print(y + 1)

Execution result スクリーンショット 2020-11-09 160518.png For Java

//Substitute the string "1" for x
String x = "1";
//Convert string "1" to integer "1"
int y = Integer.parseInt(x);
//Calculation between integers
System.out.println(y + 1);

Convert from int type to str type

#Substitute the integer "1" for x
x = 1
#Convert integer "1" to string "1"
y = str(x)
#Concatenation of strings
print(y + '1')

Execution result スクリーンショット 2020-11-09 161900.png For Java

//Substitute the string "1" for x
int x = 1;
//Convert integer "1" to string "1"
String y = String.valueOf(x);
//Concatenation of strings
System.out.println(y + "1");

typ function

Examine the data type of a value in a variable

a = 'Hello'
b = 1
print(type(a))
print(type(b))

Execution result スクリーンショット 2020-11-09 223333.png

format function

Embed a number in a string

name = 'Sato'
age = 20
print('my name is{}is. Age is{}I'm old.'.format(name, age))

Or

name = 'Sato'
age = 20
print(f'my name is{name}is. Age is{age}I'm old.')

Execution result スクリーンショット 2020-11-09 230636.png For Java

String name = "Sato";
int age = 20;
String str = String.format("my name is%s. Age is%I'm d years old.", name, age);
System.out.println(str);

References

https://book.impress.co.jp/books/1118101169

Next time, I would like to learn about the collection.

Recommended Posts

Learn Python! Comparison with Java (basic function)
Java and Python basic grammar comparison
Getting Started with python3 # 1 Learn Basic Knowledge
Learn Python with ChemTHEATER
Python, Java, C ++ speed comparison
BASIC authentication with Python bottle
[Python] Using OpenCV with Python (Basic)
[Python] [SQLite3] Operate SQLite with Python (Basic)
Learn search with Python # 2bit search, permutation search
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Basic Python operation 2nd: Function (argument)
Basic study of OpenCV with Python
python function ①
[Python] function
python function ②
Closure 4 language comparison (Python, JavaScript, Java, C ++)
(Java, JavaScript, Python) Comparison of string processing
Playing card class in Python (with comparison)
Learn the design pattern "Singleton" with Python
Comparison of matrix transpose speeds with Python
Learn the design pattern "Facade" with Python
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Get started with Python in 30 minutes! Development environment construction & learn basic grammar
FizzBuzz with Python3
Scraping with Python
Library comparison summary to generate PDF with Python
MessagePack-Try to link Java and Python with RPC
RF Python Basic_01
First Python 3 ~ First comparison ~
Statistics with python
python enumerate function
Performance comparison of face detector with Python + OpenCV
Scraping with Python
Achieve Basic Authentication with CloudFront Lambda @ Edge with Python 3.8
Twilio with Python
Python> function> Closure
Integrate with Python
[Python] Generator function
Play with 2016-Python
AES256 with python
I tried function synthesis and curry with python
Basic Python writing
python starts with ()
Note for formatting numbers with python format function
with syntax (Python)
Python3 basic grammar
Bingo with python
Zundokokiyoshi with python
Python> function> Inner function
RF Python Basic_02
ARC037 Baum test politely with Python recursive function
Send HTTP with Basic authentication header in Python
Python function decorator
Excel with Python
Microcomputer with Python
Learn python gestures
Cast with python
I tried to learn the sin function with chainer
Getting Started with python3 # 2 Learn about types and variables
Learn Nim with Python (from the beginning of the year).
[Introduction to Python] How to iterate with the range function?