[Super basic] Compare Python, Java and JavaScript (variable, if statement, while statement, for statement)

Purpose

――It was this assignment from this week ――I ended up studying Java ――Let's summarize the differences between Java and Python while my memory is new. ――By the way, let's pull out and compare JavaScript that I have studied a little.

What to compare in this article

--Variables --if statement --while statement --for statement

variable

Python --No need to declare data type --The type is specified by the contents of the variable. --No need for a semicolon

asobi.py


py_str = "Python"
py_int = 23
py_float = 23.5

#Data type confirmation
print(type(py_str))
print(type(py_int))
print(type(py_float))

console


<class 'str'>
<class 'int'>
<class 'float'>

--You can also rewrite the data type of the variable

asobi.py


py_str = "Python"
py_int = 23

print(type(py_str))

py_str = 10
print(type(py_str))

py_cal = py_str * py_int
print(py_cal)

console


<class 'str'>
<class 'int'>
230

Java --You must declare the data type --There is also a float type, but it seems to mainly use the double type. --I couldn't find a way to check the data type directly (it seems that there is a method called instanceof, but it can't be used for int or double) --For Java data types, the site here is easy to understand. --Don't forget the semicolon

asobi.java


public class asobi {
	public static void main(String[] args) {
		String javaStr = "Java";
		int javaInt = 23;
		double javaFloat = 23.5;
	}
}

JavaScript --No data type declaration is required, but var must be prepended to the variable name ――Both are number type, not int type and float type ... --I also need a semicolon

asobi.html


<html>
<script>
var jsStr = "JavaScript";
var jsInt = 23;
var jsFloat = 23.5;

//Data type confirmation
console.log(typeof(jsStr));
console.log(typeof(jsInt));
console.log(typeof(jsFloat));
</script>
</html>

console


string
number
number

if statement

Python --if, elif, else are used --Add: to the end of the condition --Indentation of the statement to be executed is required

asobi.py


age = 23

if age >= 65:
    print("Retirement age")
elif age >= 20:
    print("I'm an adult")
else:
    print("I'm a minor")

Java --if, else if, else are used --Enclose each condition in () and the statement to be executed in {}

asobi.java


public class asobi {
	public static void main(String[] args) {
		int age = 23;

		if (age >= 65) {
			System.out.println("Retirement age");
		}else if (age >= 20) {
			System.out.println("I'm an adult");
		}else{
			System.out.println("I'm a minor");
		}
	}
}

JavaScript --if, else if, else are used --Enclose each condition in () and the statement to be executed in {} ――The form of the if statement itself is the same as Java.

asobi.html


<html>
<script>
var age = 23;

if (age >= 65) {
    console.log("Retirement age");
}else if (age >= 20){
    console.log("I'm an adult");
}else{
    console.log("I'm a minor");
}
</script>
</html>

while statement

――Let's display the console in each language as follows

console


0
Is 1
2
3
4

Python --Since it is not possible to connect i, which is an int type, and a character string as it is, make i a str type.

asobi.py


i = 0

while i < 5:
    print(str(i) + "is")
    i += 1

Java --As usual, enclose the condition in () and the executable statement in {}. --You can use the form i ++ in increments

asobi.java


public class asobi {
	public static void main(String[] args) {
		int i = 0;
		while (i < 5){
			System.out.println(i + "is");
			i++;
		}
	}
}

JavaScript --As usual, the form of the while statement itself is the same as Java.

asobi.html


<html>
<script>
var i = 0;

while (i < 5){
	console.log(i + "is");
	i++;
}
</script>
</html>

for statement

――Let's display the console in each language as follows

console


0
Is 1
2
3
4

Python --i is repeated in the range of 0 to 4 5 times

asobi.py


for i in range(5):
    print(str(i) + "is")

Java --The repeat condition of the for statement is represented by (initial value; range; increase / decrease). --Variables that store initial values must declare a data type

asobi.java


public class asobi {
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++){
			System.out.println(i + "is");
		}
	}
}

JavaScript --As usual, the form of the for statement itself is the same as Java. --Var must be prepended to the variable name that stores the initial value

asobi.html


<html>
<script>
for (var i = 0; i < 5; i++){
    console.log(i + "is");
}
</script>
</html>

Impressions

――When studying multiple languages, it is easier to remember if you study while comparing ――It's said that Java and JavaScript are different from India and Indonesia, but the writing style is quite similar. ――After all Python is the easiest to write!

Recommended Posts

[Super basic] Compare Python, Java and JavaScript (variable, if statement, while statement, for statement)
A Java programmer studied Python. (for, if, while statement)
Python basic if statement
Java and Python basic grammar comparison
Python Exercise for Beginners # 2 [for Statement / While Statement]
Compare Python and JavaScript array loops
[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)
[Python of Hikari-] Chapter 05-09 Control syntax (use of for statement and while statement properly)
Python if statement
[Python / Chrome] Basic settings and operations for scraping
[Python] if statement
Benchmark for C, Java and Python with prime factorization
[Python] Output battles and combinations (nesting for statements and if statements)
Python Exercise for Beginners # 1 [Basic Data Types / If Statements]
Python # How to check type and type for super beginners
Python basics ② for statement
python memorandum super basic
[Python] for statement error
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
Remove leading and trailing whitespace in Python, JavaScript, or Java
Python list, for statement, dictionary
Python installation and basic grammar
I compared Java and Python!
Addition with Python if statement
Python #function 2 for super beginners
When using if and when using while
Basic Python grammar for beginners
About 2-variable, 4-branch if statement
Python for super beginners Python #functions 1
[Python / PyQ] 4. list, for statement
Python #list for super beginners
Introduction to Python For, While
Python (Python 3.7.7) installation and basic grammar
[Python] Iterative processing (for, while)
Difference in how to write if statement between ruby ​​and python