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

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

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


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



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

'''

package min.c.forarrangemet;

public class Sum { 

    public static void main(String[ ] args) {

        int total = 0;

        for (int i = 1; i <= 10; i++) {

            total += i;

        }

        System.out.println("1에서 10까지 총 합계는 " + total + "입니다."); 

    }

}

'''

total = 0

for i in range(1,11):

    total += i

print(total)

# 자바 10줄, 파이썬 4줄


'''

package min.c.forstatement.practice;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class LeapYear { 

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

        System.out.println("입력 년도 이후 2020년까지 윤년과 평년을 확인합니다. ");

        InputStreamReader inputStreamReader = new InputStreamReader(System.in);

        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

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

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

        for (int i = j; i <= 2020; i++) {

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

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

            } else {

                System.out.println(i + "년은 평년입니다.");

            }

        }

    }

}

'''

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

year = int(year)

for i in range(year, 2021):

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

        print('%i년은 윤년입니다.' %i)

    else:

        print('%i년은 평년입니다.' %i)

# 자바 20줄, 파이썬 7줄


'''

package min.c.forstatement;

public class ReiterationOutput { 

    public static void main(String[ ] args) {

        for (int i = 1; i <= 2; i++) {

            for (int j = 1; j <= 3; j++) {

                System.out.println("밖의 for 문 출력: " + i + " 안의 for 문 출력 = " + j); 

            }

        } 

    }

}

'''

for i in range(1,3):

    for j in range(1,4):

        print('밖의 for 문 출력:', i, ' 안의 for 문 출력 = ', j)

# 자바 10줄, 파이썬 3줄


'''

package min.c.forstatement;

public class Pentagram { 

    public static void main(String[ ] args) {

        for (int i = 1; i <= 5; i++) {

            for (int j = 1; j <= i; j++) {

                System.out.print("*");

            }

            System.out.println( );

        }   

    }

}

'''

for i in range(1,6):

    for j in range(1, i+1):

        print('*',end='')

    print()

# 자바 11줄, 파이썬 4줄


'''

package min.d.whilestatement;

public class MiddleValue { 

    public static void main(String args[ ]) {

        int i = 10, j = 20;

        while (++i < --j) {

        } 

        System.out.println("10에서 20사이의 중간값은 " + i + " 입니다."); 

    }

}

'''

i, j = 10, 20

while i < j:

    i += 1

    j -= 1

else:

    print('10에서 20사이의 중간값은 ', i, '입니다.')

# 자바 9줄, 파이썬 6줄


'''

package min.d.whilestatement;

import java.io.*;

public class MultiplicationTable { 

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

        System.out.println("확인하고 싶은 구구단의 단을 입력하세요. ");

        InputStreamReader inputStreamReader = new InputStreamReader(System.in);

        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        System.out.print("단 입력: ");

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

        if (dan >= 2 && dan <= 9) {

            int num = 1;

            int result = 0;

            while (num <= 9) {

                result = dan * num;

                System.out.println(dan + " * " + num + " = " + result);

                num++;

            }

        } else {

            System.out.println("단 값이 잘못되었습니다.");

        } 

    }

}

'''

dan = input('단 입력: ')

dan = int(dan)

if 2 <= dan <= 9:

    num = 1

    while num <= 9:

        result = dan * num

        print(dan, ' * ', num, ' = ', result)

        num += 1

else:

    print('단 값이 잘못되었습니다.')

# 자바 22줄, 파이썬 10줄


'''

package min.d.whilestatement;

public class WhileCompareDowhile { 

    public static void main(String[ ] args) {

        int i = 0;

        while (i >= 1) {

            System.out.println("조건이 맞으면 실행한다.");

        }

        do {

            System.out.println("조건이 맞지 않아도 실행한다.");

        } while (i >= 1); 

    }

}

'''

i = 0

while i >= 1:

    print('조건이 맞으면 실행한다.')

else:

    print('조건이 맞지 않아도 실행한다.')

# 자바 12줄, 파이썬 5줄


'''

package min.d.whilestatement;

public class ThreeMultiplicationTable { 

    public static void main(String args[ ]) {

        int n = 1;

        System.out.println("구구단 3단");

        do {

            System.out.println(" " + 3 + "*" + n + "=" + (3 * n));

            n++;

        } while (n < 10); 

    }

}

'''

n = 1

print('구구단 3단')

while True:

    if n < 10:

        print(" " , 3 , "*" , n , "=" , (3 * n))

        n += 1

    else:

        break;

# do-while은 파이썬에 없는 문법이라 if와 break를 이용하여 프로그램 작성

# 자바 11줄, 파이썬 8줄


'''

package min.d.whilestatement.practice;

import java.util.Scanner;

public class Combinate { 

    static Scanner scanner; 

    public static void main(String[ ] args) {

        scanner = new Scanner(System.in);

        int i = (int) (Math.random( ) * 100) + 1;

        int num = 0;

        do {

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

            num = scanner.nextInt( );

            if (num == i) {

                System.out.println("맞혔습니다.");

                break;

            } else if (num < i) {

                System.out.println("맞출 숫자보다 작습니다.");

            } else {

                System.out.println("맞출 숫자보다 큽니다.");

            }

        } while (true); 

    }

}

'''

import random

i = int(random.random() * 10 + 1)

while 1:

    num = input('입력한 숫자 : ')

    if num == i:

        print('맞혔습니다.')

        break

    elif num > i:

        print('맞출 숫자보다 큽니다.')

    else:

        print('맞출 숫자보다 작습니다.')

# 자바 22줄, 파이썬 11줄


'''

package min.d.whilestatement.practice;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class AverageCredit { 

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

        InputStreamReader inputStreamReader = new InputStreamReader(System.in);

        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        int korean, english, mathematics, total;

        char credit;

        float average = 0.0f;

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

        do {

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

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

        } while (korean < 0 || korean > 100);

        do {

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

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

        } while (english < 0 || english > 100);

        do {

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

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

        } while (mathematics < 0 || mathematics > 100);

        total = korean + english + mathematics;

        average = total / 3.0f;

        switch ((int) (average / 10)) {

        case 10:

        case 9:

            credit = 'A';

            break;

        case 8:

            credit = 'B';

            break;

        case 7:

            credit = 'C';

            break;

        case 6:

            credit = 'D';

            break;

        default:

            credit = 'F';

        }

        System.out.println( );

        System.out.println("총점 = " + total);

        System.out.printf("평균 = %.2f\n", average);

        System.out.println("학점 = " + credit + " 학점"); 

    }

}

'''

kor = 0

eng = 0

math = 0

grade = ''

while 1:

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

    if 0 <= kor <= 100:

        break;

while 1:

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

    if 0 <= eng <= 100:

        break;

while 1:

    math = int(input('수학 점수: '))

    if 0 <= math <= 100:

        break;

total = kor + eng + math

average = round((kor + eng + math)/3, 3)

average_sub_10 = int(round((kor + eng + math)/3, 3)/10)

if average_sub_10 == 10 or average_sub_10 == 9:

    grade = 'A'

elif average_sub_10 == 8:

    grade = 'B'

elif average_sub_10 == 7:

    grade = 'C'

elif average_sub_10 == 6:

    grade = 'D'

else:

    grade = 'F'

print('총점 = ', total)

print('평균 = ', average)

print('학점 = ', grade)

# 자바 49줄, 파이썬 32줄

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

+ Recent posts