Now that I've started learning Python, I'd like to compare it with the Java I've learned.
print('Hello')
print("Hello")
Execution result For Java
System.out.println("Hello");
#Comment out
print('No comments are displayed')
For Java
//Comment out
System.out.println("No comments are displayed");
name = 'Sato'
age = '20'
print(name)
print(age)
Execution result For Java
String name = "Sato";
int age = 20;
System.out.println(name);
System.out.println(age);
name = input('Please enter your name>>')
age = input('Please enter your age>>')
print(name)
print(age)
Execution result 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>
#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 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);
#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 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");
Examine the data type of a value in a variable
a = 'Hello'
b = 1
print(type(a))
print(type(b))
Execution result
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 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);
https://book.impress.co.jp/books/1118101169
Next time, I would like to learn about the collection.
Recommended Posts