[Python] Java 코드를 Python 코드로 바꾸기

조건문 if에서 Java를 사용할 때와 Python을 사용할 때의 코드 길이를 비교했다.

Python을 이용했을 때 약 50%의 코드 길이가 줄여졌다. 생산성이 그만큼 높다는 이야기이다.

물론 Python에서도 클래스를 적용한다면 차이는 줄어들겠지만 어쨌든 Python의 코드 길이가 짧다.




'''
주석 안의 코드가 Java코드
'''
주석 밖의 코드가 Python코드
# 둘의 코드 길이 비교


====================== Java -> Python ======================

########## 자바와의 비교 ##########

'''

package min.a.ifstatement;

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

    int i = 10;

    if (i > 9) {

        System.out.println("if의 조건은 참입니다.");

        } 

    }

}

'''

i = 10

if i > 9:

    print('if의 조건은 참입니다.')

# 자바 9줄, 파이썬 3줄




'''

package min.a.ifstatement.practice;

import java.io.*;

public class Sum { public static void main(String args[ ]) throws IOException {

    InputStreamReader inputStreamReader = new InputStreamReader(System.in);

    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    int su1, su2, tot = 0;

    System.out.print("첫번째 수 = ");

    su1 = Integer.parseInt(bufferedReader.readLine( ));

    System.out.print("두번째 수 = ");

    su2 = Integer.parseInt(bufferedReader.readLine( ));

    if (su1 > su2) {

        int imsi = su1;

        su1 = su2;

        su2 = imsi;

    }

    for (int i = su1; i <= su2; i++) {

        tot += i;

    }

    System.out.println( );

    System.out.print(su1 + "에서 " + su2 + "사이의 합은 "+ tot + "입니다." ); }

}

'''

inputStreamReader1 = input('첫번째 수 = ')

inputStreamReader2 = input('두번재 수 = ')

su1 = inputStreamReader1

su2 = inputStreamReader2

tot = 0

if su1 > su2:

    imsi = su1

    su1 = su2

    su2 = imsi

for i in list(range(int(su1),int(su2))):

    tot += i

print()

print(su1 + ' 에서 ' + su2 + ' 사이의 합은 ' + str(tot) + '입니다.')

# 자바 23줄, 파이썬 13줄




'''

package min.a.ifstatement;

public class OddEven { 

    public static void main(String[ ] args) {

        int i = 9;

        if (i % 2 == 1) {

            System.out.println(i + "는 홀수이다.");

        } else {

            System.out.println(i + "는 짝수이다.");

        } 

    }

}

'''

i = 9

if i % 2 == 1:

    print(str(i) + ' 는 홀수이다.')

else:

    print(str(i) + ' 는 짝수이다.')

# 자바 11줄, 파이썬 5줄




'''

package min.a.ifstatement.practice;

import java.util.Scanner;

public class OddEvenConfirmation { 

    public static void main(String args[ ]) {

        int i;

        Scanner scanner = new Scanner(System.in);

        System.out.print("숫자 입력 : ");

        i = scanner.nextInt( );

        if (i % 2 == 1) {

            System.out.println("홀수를 선택하셨습니다.");

        } else {

            System.out.println("짝수를 선택하셨습니다.");

        }

        scanner.close( ); 

    }

}

'''

i = input('숫자 입력 : ')

i = i

if int(i) % 2 == 1:

    print('홀수를 선택하셨습니다.')

else:

    print('짝수를 선택하셨습니다.')

# 자바 16줄, 파이썬 6줄




'''

package min.a.ifstatement.practice;

import java.util.Scanner;

public class LeapYear { 

    private static Scanner scanner; 

    public static void main(String args[ ]) {

        int year;

        scanner = new Scanner(System.in);

        System.out.print("년도 입력 : ");

        year = scanner.nextInt( );

        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {

            System.out.println("년은 윤년입니다.");

        } else {

            System.out.println("년은 윤년이 아닙니다.");

        } 

    }

}

'''

year = input('년도 입력 : ')

year = int(year)

if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:

    print(str(year) + '년은 윤년입니다.')

else:

    print(str(year) + '년은 윤년이 아닙니다.')

# 자바 16줄, 파이썬 6줄




'''

package min.a.ifstatement;

public class Credit { 

    public static void main(String[ ] args) {

        int score = 99;

        char grade;

        if (score >= 90) {

            grade = 'A';

        } else if (score >= 80) {

            grade = 'B';

        } else if (score >= 70) {

            grade = 'C';

        } else {

            grade = 'D';

        }

        System.out.println("학점은 " + grade + "이다." ); 

    }

}

'''

score = 99

if score >= 90:

    grade = 'A'

elif score >= 80:

    grade = 'B'

elif score >= 70:

    grade = 'C'

else:

    grade = 'D'

print('학점은 ' + grade + ' 이다.')

# 자바 17줄, 파이썬 10줄





'''

package min.a.ifstatement.practice;

import java.io.IOException;

public class GreateSmallLetter { 

    public static void main(String args[ ]) throws IOException {

        System.out.print("영어 문자를 입력하세요.: ");

        char c = (char) System.in.read( );

        if (c >= 'a' && c <= 'z') {

            System.out.println(c + "는 소문자 입니다.");

        } else if (c >= 'A' && c <= 'Z') {

            System.out.println(c + "는 대문자 입니다.");

        } else {

            System.out.println("대소문자 외의 문자를 입력하였습니다."); 

        }

    }

}

'''

c = input('영어 문자를 입력하세요.:')

if 'a' <= c <= 'z':

    print(c + ' 는 소문자 입니다.')

elif 'A' <= c <= 'Z':

    print(c + ' 는 대문자 입니다.')

else:

    print('영어 대소문자 외의 문자를 입력하였습니다.')

# 자바 15줄, 파이썬 7줄




'''

package min.a.ifstatement.practice;

import java.io.*;

public class Login { 

    static String id = "java"; 

    static String pass = "1234"; 

    public static void main(String args[ ]) throws IOException {

        InputStreamReader inputStreamReader = new InputStreamReader(System.in);

        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        System.out.println("아이디와 패스워드를 입력하세요. ");

        System.out.print("아이디 : ");

        id = bufferedReader.readLine( );

        System.out.print("패스워드 : ");

        pass = bufferedReader.readLine( );

        if (!id.equals("java")) {

            System.out.println("회원 정보가 없습니다.");

        } else if (!pass.equals("1234")) {

            System.out.println("비밀번호가 일치하지 않습니다.");

        } else {

            System.out.println("환영합니다.");

        } 

    }

}

'''

id = 'java'

pw = '1234'

print('아이디와 패스워드를 입력하세요.')

input_id = input('아이디 :')

input_pw = input('패스워드 : ')

if id != input_id:

    print('회원 정보가 없습니다.')

elif pw != input_pw:

    print('비밀번호가 일치하지 않습니다.')

else:

    print('환영합니다.')

# 자바 22줄, 파이썬 11줄




'''

package min.a.ifstatement;

public class NumberComparison { 

    public static void main(String[ ] args) {

        int x = 50;

        int y = 60;

        int z = 70;

        if (y > x) {

            if (y < z) {

                System.out.println("y는 x보다 크고, y는 z보다 작다.");

            } else {

            System.out.println("y는 x보다 크고, y는 z보다 크다.");

            }

        } else {

            System.out.println("y는 x보다 작다");

        } 

    }

}

'''

x = 50

y = 60

z = 70

if y > x:

    if y < z:

        print('y는 x보다 크고, y는 z보다 작다.')

    else:

        print('y는 x보다 크고, y는 z보다 크다.')

else:

    print('y는 x보다 작다.')

# 자바 17줄, 파이썬 10줄




'''

package min.a.ifstatement.practice;

import java.io.*;

public class SuccessOrFailure { 

    static BufferedReader bufferedReader; 

    public static void main(String[ ] args) throws IOException {

        System.out.println("점수를 입력하세요."); 

        InputStreamReader inputStreamReader = new InputStreamReader(System.in);

        bufferedReader = new BufferedReader(inputStreamReader);

        System.out.print("국어 점수 = ");

        int kor = Integer.parseInt(bufferedReader.readLine( ));

        System.out.print("수학 점수 = ");

        int mat = Integer.parseInt(bufferedReader.readLine( ));

        System.out.print("영어 점수 = ");

        int eng = Integer.parseInt(bufferedReader.readLine( ));

        int total = 0;

        total = kor + mat + eng;

        if (total >= 180) {

            if (kor < 40 || mat < 40 || eng < 40) {

                System.out.println("과락이므로 불합격입니다.");

            } else {

                System.out.println("합격입니다.");

            }

        } else {

            System.out.println("총점이 부족하므로 불합격입니다.");

        } 

    }

}

'''

kor = int(input('국어 점수 = '))

mat = int(input('수학 점수 = '))

eng = int(input('영어 점수 = '))

total = kor + mat + eng

if total >= 180:

    if kor < 40 or mat < 40 or eng < 40:

        print('과락이므로 불합격입니다.')

    else:

        print('합격입니다.')

else:

    print('총점이 부족하므로 불합격입니다.')

# 자바 26줄, 파이썬 11줄





'''

package min.b.switchstatement;

public class Credit { 

    public static void main(String[ ] args) {

        int score = 99;

        char grade;

        switch (score / 10) {

        case 10:

            grade = 'A';

            break;

        case 9:

            grade = 'A';

            break;

        case 8:

            grade = 'B';

            break;

        case 7:

            grade = 'C';

            break;

        case 6:

            grade = 'D';

            break;

        default:

            grade = 'F';

        }

        System.out.println("학점은 " + grade + "이다." ); 

    }

}

'''

score = 99

if score // 10 == 10:

    grade = 'A'

elif score // 10 == 9:

    grade = 'B'

elif score // 10 == 8:

    grade = 'C'

elif score // 10 == 7:

    grade = 'D'

else:

    grade = 'F'

print('학점은 ' + grade + ' 이다.')

# 자바 27줄, 파이썬 12줄




'''

package min.b.switchstatement;

public class StringType { 

    public static void main(String[ ] args) {

        String day = "Sunday";

        switch (day) {

        case "Monday":

            System.out.println("오늘은 월요일입니다.");

            break;

        case "Tuesday":

            System.out.println("오늘은 화요일입니다.");

            break;

        case "Wednesday":

            System.out.println("오늘은 수요일입니다.");

            break;

        case "Thursday":

            System.out.println("오늘은 목요일입니다.");

            break;

        case "Friday":

            System.out.println("오늘은 금요일입니다.");

            break;

        case "Saturday":

            System.out.println("오늘은 토요일입니다.");

            break;

        case "Sunday":

            System.out.println("오늘은 일요일입니다.");

            break;

        }

    }

}

'''

day = 'Sunday'

if day == 'Monday':

    print('오늘은 월요일입니다.')

if day == 'Tuesday':

    print('오늘은 화요일입니다.')

if day == 'Wednesday':

    print('오늘은 수요일입니다.')

if day == 'Thursday':

    print('오늘은 목요일입니다.')

if day == 'Friday':

    print('오늘은 금요일입니다.')

if day == 'Saturday':

    print('오늘은 토요일입니다.')

if day == 'Sunday':

    print('오늘은 일요일입니다.')

# 자바 29줄, 파이썬 15줄




'''

package min.b.switchstatement.practice;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Salary { 

    static String rank; 

    public static void main(String[ ] args) throws IOException {

        InputStreamReader inputStreamReader = new InputStreamReader(System.in);

        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        System.out.print("직급 입력: ");

        rank = bufferedReader.readLine( );

        int salary = 0;

        switch (rank) {

        case "사원":

            salary = 20000000;

            break;

        case "대리":

            salary = 35000000;

            break;

        case "과장":

            salary = 5000000;

            break;

        case "부장":

            salary = 8000000;

            break;

        }

        System.out.println("연봉은 " + salary + "원입니다."); 

    }

}

'''

rank = input('직급 입력:')

if rank == '사원':

    salary = 20000000

if rank == '대리':

    salary = 35000000

if rank == '과장':

    salary = 50000000

if rank == '부장':

    salary = 80000000

print('연봉은 ' + str(salary) + ' 원입니다.')

# 자바 29줄, 파이썬 10


====================== Java -> Python ======================

+ Recent posts