[Python] 3일차

애옹·2024년 6월 26일

Python

목록 보기
4/13
  1. 메소드
  • 인터프리터 언어이기 때문에 아래 코드의 메소드 순서를 바꾸면 오류 발생, 반면 자바는 컴파일 언어이기 때문에 무관
def add(a, b):
    return a + b

sum = add(4, 2)

print("sum:", sum)

==

package day03;

public class Mydef01 {
	public static int add(int a, int b) {
		return a + b;
	}
	
	public static void main(String[] args) {
		int sum = add(4, 2);
		System.out.println("sum: " + sum);
	}
	
}
  1. 메소드를 이용한 홀짝 출력

mydef02.py

from random import random
def getHJ():
    rnd = random()
    
    if(rnd > 0.5):
        return "홀"
    else:
        return "짝"

com = getHJ()
print("com:", com)

==

from random import random
def getHJ():
    ret = "홀"
    rnd = random()
    if(rnd > 0.5):
        ret = "짝"
    
    return ret

com = getHJ()
print("com:", com)
  1. 메소드를 이용한 구구단 출력

mydef03.py

def showDan(dan):
    for i in range(1, 9 + 1):
        print("{} * {} = {}".format(dan, i, dan * i))

showDan(6)

==
mydef03.java

package day03;

public class Mydef03 {

	static void showDan(int dan) {
		for(int i = 1; i <= 9; i++) {
			System.out.println(dan + " * " + i  + " = " + 6*i);
		}
	}
	
	public static void main(String[] args) {
		showDan(6);
	}
	
}
  1. 파이썬만의 유일한 문법
def add_min_mul_div(a, b):
    return a+b, a-b, a*b, a/b

sum, min, mul, div = add_min_mul_div(4, 2)

print("sum:", sum)
print("min:", min)
print("mul:", mul)
print("div:", div)

∴ 출력값
sum: 6
min: 2
mul: 8
div: 2.0

  1. java로 로또 번호 생성
    5-1) 배열 사용
    MyLotto.java
package day03;

import java.util.Random;

public class MyLotto {
	Random random = new Random();
	public static void main(String[] args) {
		int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
				11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
				21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
				31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
				41, 42, 43, 44, 45};
		
		for(int i = 0; i < 1000; i++) {
			int rnd = (int)(Math.random() * 45);
			int a = arr[0];
			arr[0] = arr[rnd];
			arr[rnd] = a;
		}
		
		System.out.print(arr[0] + " ");
		System.out.print(arr[1] + " ");
		System.out.print(arr[2] + " ");
		System.out.print(arr[3] + " ");
		System.out.print(arr[4] + " ");
		System.out.print(arr[5] + " ");
	}
}

5-2) ArrayList 사용
MyLotto2.java

package day03;

import java.util.ArrayList;
import java.util.Random;

public class MyLotto2 {
	Random random = new Random();
	public static void main(String[] args) {
		ArrayList<Integer> arr = new ArrayList<>();

		for(int i = 1; i <= 45; i++) {
			arr.add(i);
		}

		for(int i = 0; i < 1000; i++) {	
			int rnd = (int) (Math.random() * 45);
			int a = arr.get(0);
			arr.set(0, arr.get(rnd));
			arr.set(rnd, a);
		}

		System.out.println(arr.get(0));
		System.out.println(arr.get(1));
		System.out.println(arr.get(2));
		System.out.println(arr.get(3));
		System.out.println(arr.get(4));
		System.out.println(arr.get(5));

	}
}
  1. OOP(Object Oriented Programming)란?
  • '상속'이라는 개념 필수 :: extends

day04-mytest01.py

#가위/바위/보를 선택하세요 가위
#나: 가위
#컴: 바위
#결과: 짐 (비김, 이김)
from random import random

user = input("가위/바위/보를 선택하세요")
rnd = random()
com = rnd

if(rnd > 0.33):
    com = "가위"
elif(rnd > 0.66):
    com = "바위"
else:
    com = "보"
    
if(user == com):
    print("user:", user, "com:", com, "/ 비김")
elif((user == "가위" and com == "바위") or (user == "바위" and com == "보")
     or (user == "보" and com == "가위")):
    print("user:", user, "com:", com, "/ 짐")
else:
    print("user:", user, "com:", com, "/ 이김")
profile
괴발개발

0개의 댓글