Java and Python basic grammar comparison

Introduction

I compared the basic grammars of Java and Python. I would appreciate it if you could point out if the Java grammar is dirty or if there are any differences!

table of contents#

[1. Output](# 1 output) [2. Variable](# 2 variable) [3. Combining strings and numbers](# 3 Combining strings and numbers) [4.if statement](# 4if statement) [5.for statement](# 5for statement) [6.while statement](# 6while statement) [7. break, continue with for and while](break-continue with # 7 for and while) 8.list [9. Dictionary](# 9 dictionary)

1. Output

python


print('123')
print(123)
>>>123
>>>123

java


public class Main {
    public static void main(String[] args){
        
        System.out.println("123");
        System.out.println(123);
    }
}
>>>123
>>>123

2. Variable

python


mystr = 'hello'
myint = 123
print(mystr)
print(myint)
>>>hello
>>>123

java


public class Main {
    public static void main(String[] args){
        
        String mystr = "hello";
        int myint = 123;
        
        System.out.println(mystr);
        System.out.println(myint);
    }
}
>>>hello
>>>123

3. Combining strings and numbers

python


myint = 123
print('The numbers are' + str(myint))
>>>The number is 123

java


public class Main {
    public static void main(String[] args){
        
        int myint = 123;
        System.out.println("The numbers are" + myint);
    }
}
>>>The number is 123

4. if statement

python


myint = 10
if myint == 10:
    print('myint is 10')
>>>myint is 10

java


public class Main {
    public static void main(String[] args){
        
        int myint = 10;
        if(myint == 10){
            System.out.println("myint is 10");
        }
    }
}
>>>myint is 10

5. for statement

python


for char in 'Hello':
    print(char)
>>>H
>>>e
>>>l
>>>l
>>>o

java


//split and extended for statement
public class Main {
    public static void main(String[] args){
        
        String mystr = "hello";
        String[] strArray = mystr.split("");
        for(String s : strArray) {
	        System.out.println(s);
            }
    }
}

//charAt and for statement
public class Main {
    public static void main(String[] args){
        
        String mystr = "hello";
        for(int i = 0; i < mystr.length(); i++) {
	        System.out.println(mystr.charAt(i));
            }
    }
}

>>>h
>>>e
>>>l
>>>l
>>>o

・ Loop ### for the specified number of times

python


for i in range(3):
    print(i)
>>>0
>>>1
>>>2

java


public class Main {
    public static void main(String[] args){
        
    for(int i = 0; i < 3; i++) {
        System.out.println(i);
        }
    }
}
>>>0
>>>1
>>>2

-List and array loop processing

python


list = ['item1', 'item2', 'item3']
for item in list:
    print(item)
>>>item1
>>>item2
>>>item3

java


public class Main {
    public static void main(String[] args){
        
        String[] list = {"item1", "item2", "item3"};
        for(int i = 0; i <list.length; i++){
            System.out.println(list[i]);
        }
    }
}
>>>item1
>>>item2
>>>item3

6. While statement

python


val = 0
while val < 4:
    val += 1
    print(val)
print('val is' + str(val))
>>>1
>>>2
>>>3
>>>4
>>>val is 4

java


public class Main {
    public static void main(String[] args){
        
        int val = 0;
        while(val < 4){
            val++;
            System.out.println(val);
        }
        System.out.println("val is" + val);
    }
}
>>>1
>>>2
>>>3
>>>4
>>>val is 4

7. break, continue with for and while

・ Break ### in for statement

python


list = [1,2,3,4,5]
for i in list:
    if i == 3:
        break
    print(i)
>>>1
>>>2
>>>Repeat processing ends with break

java


public class Main {
    public static void main(String[] args){
        
        int[] list = {1,2,3,4,5};
        for(int i = 1; i < list.length; i++){
            if(i == 3){
                break;
            }
            System.out.println(i);
        }
    }
}
>>>1
>>>2
>>>Repeat processing ends with break

・ Break ### with while statement

python


val = 1 
while val < 10:
    if val == 3:
        break 
    print(val)
    val += 1
>>>1
>>>2
>>>Repeat processing ends with break

java


public class Main {
    public static void main(String[] args){
        
        int val = 1;
        while(val < 10){
            if(val == 3){
                break;
            }
            System.out.println(val);
            val++;
        }
    }
}
>>>1
>>>2
>>>Repeat processing ends with break

・ Continue ### in the for statement

python


#continue with a for statement
list = [1,2,3,4,5]
for i in list:
    if i == 2:
        continue
    print(i)
>>>1
>>>3
>>>4
>>>5

java


public class Main {
    public static void main(String[] args){
        
        int[] list = {1,2,3,4,5};
        for(int i = 1; i < list.length+1; i++){
            if(i == 2){
                continue;
            }
            System.out.println(i);
        }
    }
}
>>>1
>>>3
>>>4
>>>5

・ Continue ### with a while statement

python


val = 0
while val < 5:
    val += 1
    if val == 2:
        continue
    print(val)
>>>1
>>>3
>>>4
>>>5

java


public class Main {
    public static void main(String[] args){
        
        int val = 1;
        while(val < 6){
            if(val == 2){
                val++;
                continue;
            }
            System.out.println(val);
            val++;
        }
    }
}
>>>1
>>>3
>>>4
>>>5

8.list# In python, lists play the role of arrays.

python


list = ['a','b','c']
print(list)
>>>['a','b','c']

In Java, you can't get all the contents of the list at once, so you need to call them one by one using for.

java


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args){
        
        List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
        
        for(int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}
>>>a
>>>b
>>>c

-Reference the elements in the list

python


list = ['a','b','c']
#First element
print(list[0])
#Second element
print(list[2])
>>>a
>>>c

java


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args){
        
        List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
        //First element
        System.out.println(list.get(0));
        //Second element
        System.out.println(list.get(2));
    }
}
>>>a
>>>c

-Adding a value to the list

python


list = ['a','b','c']
#Add to the end
list.append('d')
#Add to specified position
list.insert(2, 'd')

print(list)
>>>['a','b','d','c','d']

java


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args){

        List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
        //Add to the end
        list.add("d");
        //Add to specified position
        list.add(2, "d");
    }
}
>>>{"a","b","d","c","d"}

-Remove value from list

python


list = ['a','b','c']
#Remove trailing element
list.pop()
#Delete the element at the specified position
list.pop(1)
print(list)

>>>['a']

java


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args){

        List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
        //Delete the element at the specified position
        list.remove(0);
    }
}
>>>{"b","c"}

・ Update list values

python


list = ['a','b','c']
list[1] = 'x'
print(list)
>>>['a','x','c']

java


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args){

        List<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
        list.set(1,"x");
    }
}
>>>{"a","x","c"}

9. Dictionary, Map

python


dict = { 'apple':1, 'orange':2, 'banana':3 }
print(dict['apple'])
>>>1

java


import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args){

        LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
            
        map.put("apple", 1);
        map.put("orange", 2);
        map.put("banana", 3);
            
        System.out.println(map);
        System.out.println(map.get("apple"));
    }
}
>>>{apple=1, orange=2, banana=3}
>>>1

I used LinkedHashMap this time because I wanted to keep the order.

** Map types and features ** LinkedHashMap: Hold in the order in which the keys are stored. TreeMap: Hold in ascending order of keys. HashMap: Do not keep order.

・ Update dictionary and Map values

python


dict = { 'apple':1, 'orange':2, 'banana':3 }
dict['apple'] = 5
print(dict)
>>>{ 'apple':5, 'orange':2, 'banana':3 }

java


import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args){

        LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
            
        map.put("apple", 1);
        map.put("orange", 2);
        map.put("banana", 3);
        
        map.put("apple",5);
        System.out.println(map);
    }
}
>>>{apple=5, orange=2, banana=3}

-Delete dictionary and Map values

python


dict = { 'apple':1, 'orange':2, 'banana':3 }
del dict['apple'] 
print(dict)
>>>{'orange':2, 'banana':3 }

java


import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args){

        LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
            
        map.put("apple", 1);
        map.put("orange", 2);
        map.put("banana", 3);
        
        map.remove("apple");
        System.out.println(map);
    }
}
>>>{orange=2, banana=3}

・ Confirmation of key existence

python


dict = { 'apple':1, 'orange':2, 'banana':3 }
if 'apple' in dict:
    print('Exists')
else:
    print('do not exist')
>>>Exists

java


import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args){

        LinkedHashMap<String, Integer> map = new LinkedHashMap<>() ;
            
        map.put("apple", 1);
        map.put("orange", 2);
        map.put("banana", 3);
        
        if(map.containsKey("apple")){
            System.out.println("Exists");
        }else{
            System.out.println("do not exist");
        }
    }
}
>>>Exists

Recommended Posts

Java and Python basic grammar comparison
Python installation and basic grammar
Python (Python 3.7.7) installation and basic grammar
Python3 basic grammar
Five languages basic grammar comparison (C #, Java, Python, Ruby, Kotlin)
Learn Python! Comparison with Java (basic function)
Python basic grammar / algorithm
Python basic grammar (miscellaneous)
Python basic grammar note (4)
Python basic grammar note (3)
Python basic grammar memo
Comparison of Python and Ruby (Environment / Grammar / Literal)
Basic Python 3 grammar (some Python iterations)
Python, Java, C ++ speed comparison
Python Basic Grammar Memo (Part 1)
I compared Java and Python!
Python basic grammar (miscellaneous) Memo (3)
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
Python 3 sorted and comparison functions
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Difference between java and python (memo)
Python Basic Course (14 Modules and Packages)
Basic grammar of Python3 system (dictionary)
[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)
Overlapping regular expressions in Python and Java
Closure 4 language comparison (Python, JavaScript, Java, C ++)
(Java, JavaScript, Python) Comparison of string processing
Differences in syntax between Python and Java
[Basic grammar] Differences between Ruby / Python / PHP
Python basic operation 3rd: Object-oriented and class
[Python] I personally summarized the basic grammar.
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
Differences between Ruby and Python (basic syntax)
Basic grammar of Python3 system (included notation)
RF Python Basic_01
First Python 3 ~ First comparison ~
python grammar check
Comparing the basic grammar of Python and Go in an easy-to-understand manner
[Super basic] Compare Python, Java and JavaScript (variable, if statement, while statement, for statement)
Basic Python writing
Python grammar notes
RF Python Basic_02
MessagePack-Try to link Java and Python with RPC
Python3 Engineer Certification Basic Exam-Notes and Problem Trends-
I wrote a class in Python3 and Java
Why I'm a Java shop and start Python
VBA user tried using Python / R: basic grammar
[Python / Chrome] Basic settings and operations for scraping
Comparison of R and Python writing (Euclidean algorithm)
Basic operation of Python Pandas Series and Dataframe (1)
[Go] Basic grammar ① Definition
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
[python] Compress and decompress
Python basic course (12 functions)
Python and numpy tips
Python Basic Course (7 Dictionary)
Python SDP runtime comparison