관리 메뉴

거니의 velog

231030_파이썬 기초 3 본문

대덕인재개발원/대덕인재개발원_파이썬 프로그래밍

231030_파이썬 기초 3

Unlimited00 2023. 10. 30. 08:28

[day03.mydef01] - 매개변수가 있고, 리턴값도 있는 경우

def add(a, b):
    return a + b

def minus(a, b):
    return a - b

def multiply(a, b):
    return a * b

def divide(a, b):
    return a / b

sum = add(4, 2)
min = minus(4, 2)
mul = multiply(4, 2)
div = divide(4, 2)

print("sum =", sum) # sum = 6
print("min =", min) # min = 2
print("mul =", mul) # mul = 8
print("div =", div) # div = 2.0
print("div =", int(div)) # div = 2

[MyMethod01.java]

package day03;

public class MyMethod01 {

	static int add(int a, int b) {
		return a + b;
	}
	
	static int minus(int a, int b) {
		return a - b;
	}
	
	static int multiply(int a, int b) {
		if(a == 0 || b == 0) {
			System.out.println("인수가 0인 값은 들어올 수 없습니다.");
			return -1;
		}
		return a * b;
	}
	
	static int divide(int a, int b) {
		if(a == 0 || b == 0) {
			System.out.println("인수가 0인 값은 들어올 수 없습니다.");
			return -1;
		}
		return a / b;
	}
	
	public static void main(String[] args) {
		
		int sum = add(4, 2);
		System.out.println("sum : " + sum); // sum : 6
		
		int min = minus(4, 2);
		System.out.println("min : " + min); // min : 2
		
		int mul = multiply(4, 2);
		if(mul == -1) {
			System.out.println("잘못된 곱셈식 입니다.");
		}
		System.out.println("mul : " + mul); // mul : 8
		
		int div = divide(4, 2);
		if(div == -1) {
			System.out.println("잘못된 나눗셈식 입니다.");
		}
		System.out.println("div : " + div); // div : 2
		
	}
	
}


[day03.mydef02] - 매개변수가 있고, 리턴값이 없는 경우

# def showDan(dan):
#     for i in range(1, 9+1):
#         print(str(dan) + "*" + str(i) + "=" + str(dan * i))
#
# showDan(5) # 5단 출력

def showDan(dan):
    for i in range(1, 9+1):
        print(str(dan) + "*" + str(i) + "=" + str(i*dan))
    # print(str(dan) + "*" + str(1) + "=" + str(1*dan))
    # print(str(dan) + "*" + str(2) + "=" + str(2*dan))
    
showDan(7)


[day03.mydef03] - 매개변수가 없고, 리턴값이 있는 경우

from random import random

def getHollJJak():
    rnd = random()
    if(rnd > 0.5):
        return "홀"
    else:
        return "짝"
    
com = getHollJJak()
print("com :",com)

[day03.mydef04] - 매개변수도 없고, 리턴값도 없는 경우

def whatsYourName():
    print("홍길동")
    
whatsYourName() # 홍길동

- 파이썬은 멀티 리턴이 가능. 자바나 자바스크립트는 불가능하다.

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) # sum = 6
print("min =", min) # min = 2
print("mul =", mul) # mul = 8
print("div =", div) # div = 2.0
print("div =", int(div)) # div = 2
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) # 됨

# sum, min, mul = add_min_mul_div(4, 2) # 안 됨
# sum, min = add_min_mul_div(4, 2) # 안 됨

sum = add_min_mul_div(4, 2) # 됨

print("sum =", sum) # sum = (6, 2, 8, 2.0), 튜플
print("sum =", sum[2]) # sum = 8

# print("sum =", sum) # sum = 6
# print("min =", min) # min = 2
# print("mul =", mul) # mul = 8
# print("div =", div) # div = 2.0
# print("div =", int(div)) # div = 2

[Animal.java]

package day03;

public class Animal {

	int full = 1;
	
	void eat(int amount) {
		full += amount;
	}
	
}

[OopTest01.java]

package day03;

public class OopTest01 {

	public static void main(String[] args) {
		Animal ani = new Animal();
		System.out.println("full : " + ani.full); // full : 1
		ani.eat(9);
		System.out.println("full : " + ani.full); // full : 10
	}
	
}

[Human.java]

package day03;

public class Human extends Animal {

	boolean flag_tool = false;
	
	void momstouch() {
		flag_tool = true;
	}
	
}

[OopTest01.java]

package day03;

public class OopTest01 {

	public static void main(String[] args) {
		
//		Animal ani = new Animal();
//		System.out.println("full : " + ani.full); // full : 1
//		ani.eat(9);
//		System.out.println("full : " + ani.full); // full : 10
		
		Human hum = new Human();
		System.out.println("full : " + hum.full); // full : 1
		System.out.println("flag_tool : " + hum.flag_tool); // flag_tool : false
		hum.eat(9);
		hum.momstouch();
		System.out.println("full : " + hum.full); // full : 10
		System.out.println("flag_tool : " + hum.flag_tool); // flag_tool : true
		
	}
	
}

[day03.myoop01]

class Animal:
    def __init__(self):
        self.full = 1
    
    def eat(self, amount):
        self.full += amount

ani = Animal()

print("ani.full :", ani.full) # ani.full : 1
ani.eat(9)
print("ani.full :", ani.full) # ani.full : 10

[day03.myoop02]

from day03.myoop01 import Animal

ani = Animal()

print("ani.full :", ani.full) # ani.full : 1
ani.eat(9)
print("ani.full :", ani.full) # ani.full : 10

- 파이썬에서는 왜 2번이나 실행되었을까?


[OopTest02.java]

package day03;

public class OopTest02 {

	public static void main(String[] args) {

		Animal ani = new Animal();
		System.out.println("ani.full : " + ani.full);
		ani.eat(9);
		System.out.println("ani.full : " + ani.full);
		
	}

}

[Animal.java]

package day03;

public class Animal {

	int full = 1;
	
	void eat(int amount) {
		full += amount;
	}
	
	public static void main(String[] args) {
		Animal ani = new Animal();
		System.out.println("ani.full : " + ani.full); // ani.full : 1
		ani.eat(9);
		System.out.println("ani.full : " + ani.full); // ani.full : 10
	}
	
}

- 자바에서는 둘 다 같은 결과가 나옴.


[두 소스의 차이점?]

- main() 메서드의 차이임.

- 파이썬에서 main() 메서드의 역할을 하는 것이 있음
  if __name__ == '__main__':

[day03.myoop01]

class Animal:
    def __init__(self):
        self.full = 1
    
    def eat(self, amount):
        self.full += amount

if __name__ == '__main__':
    ani = Animal()
    print("ani.full :", ani.full) # ani.full : 1
    ani.eat(9)
    print("ani.full :", ani.full) # ani.full : 10

[day03.myoop02]

from day03.myoop01 import Animal

ani = Animal()

print("ani.full2 :", ani.full) # ani.full : 1
ani.eat(9)
print("ani.full2 :", ani.full) # ani.full : 10

[day03.myoop01] : 소멸자 생성하는 방법

class Animal:
    def __init__(self):
        self.full = 1
        print("생성자")
    
    def eat(self, amount):
        self.full += amount
        
    # 소멸자 : destroyer
    def __del__(self):
        print("소멸자")


if __name__ == '__main__':
    
    ani = Animal() # 생성자
    print("ani.full :", ani.full) # ani.full : 1
    ani.eat(9)
    print("ani.full :", ani.full) # ani.full : 10
    # 소멸자

[day03.myoop01] : 상속 관계 설정하는 방법

class Animal:
    def __init__(self):
        self.full = 1
        print("생성자")
    
    def eat(self, amount):
        self.full += amount
        
    # 소멸자 : destroyer
    def __del__(self):
        print("소멸자")
        
class Human(Animal):
    def __init__(self):
        super().__init__()
        self.flag_tool = False
        
    def momstouch(self):
        self.flag_tool = True

if __name__ == '__main__':
    
    # ani = Animal() # 생성자
    # print("ani.full :", ani.full) # ani.full : 1
    # ani.eat(9)
    # print("ani.full :", ani.full) # ani.full : 10
    # # 소멸자
    
    hum = Human() # 생성자
    print("hum.full :", hum.full) # hum.full : 1
    print("hum.flag_tool :", hum.flag_tool) # hum.flag_tool : False
    hum.eat(9)
    hum.momstouch()
    print("hum.full :", hum.full) # hum.full : 10
    print("hum.flag_tool :", hum.flag_tool) # hum.flag_tool : True
    # 소멸자

* 멀티 상속

- 자바는 단일 상속 체계

- 파이썬은 멀티 상속이 가능하다.

package day03;

public class Xi {

	int cnt_nuclear = 1000;
	
	void dieDeveloper(int count) {
		cnt_nuclear += count;
	}
	
}
package day03;

public class LeeJY {

	int asset = 400;
	int factory = 20;
	
	void buildFactory() {
		asset -= 4;
		factory += 1;
	}
	
}
package day03;

public class Musk {

	int cnt_sat = 20000;
	
	void shootRocket() {
		cnt_sat += 100;
	}
	
}

[day03.myoop03]

class Xi:
    def __init__(self):
        self.cnt_nuclear = 1000
        
    def dieDeveloper(self, count):
        self.cnt_nuclear += count
        
class LeeJY(Xi):
    def __init__(self):
        super(LeeJY, self).__init__()
        self.asset = 400
        self.factory = 20
        
    def buildFactory(self):
        self.asset -= 4
        self.factory += 1
        
class Musk(LeeJY):
    def __init__(self):
        super(Musk, self).__init__()
        self.cnt_sat = 20000
        
    def shootRocket(self):
        self.cnt_sat += 100
        
class LeeGunJung(Musk):
    def __init__(self):
        super(LeeGunJung, self).__init__()
        
if __name__ == '__main__':
    lgj = LeeGunJung()
    print("lgj.cnt_nuclear :", lgj.cnt_nuclear)
    print("lgj.asset :", lgj.asset)
    print("lgj.factory :", lgj.factory)
    print("lgj.cnt_sat :", lgj.cnt_sat)
    lgj.dieDeveloper(1000)
    lgj.buildFactory()
    lgj.shootRocket()
    print("===메서드 호출 후 변환된 값===")
    print("lgj.cnt_nuclear :", lgj.cnt_nuclear)
    print("lgj.asset :", lgj.asset)
    print("lgj.factory :", lgj.factory)
    print("lgj.cnt_sat :", lgj.cnt_sat)


[선생님 풀이]

class Xi:
    def __init__(self):
        self.cnt_nuclear = 1000
        
    def dieDeveloper(self, count):
        self.cnt_nuclear += count
        
class LeeJY:
    def __init__(self):
        self.asset = 400
        self.factory = 20
        
    def buildFactory(self):
        self.asset -= 4
        self.factory += 1
        
class Musk:
    def __init__(self):
        self.cnt_sat = 20000
        
    def shootRocket(self):
        self.cnt_sat += 100
        
class LeeGunJung(Xi, LeeJY, Musk):
    def __init__(self):
        Xi.__init__(self)
        LeeJY.__init__(self)
        Musk.__init__(self)
        
if __name__ == '__main__':
    lgj = LeeGunJung()
    print("lgj.cnt_nuclear :", lgj.cnt_nuclear)
    print("lgj.asset :", lgj.asset)
    print("lgj.factory :", lgj.factory)
    print("lgj.cnt_sat :", lgj.cnt_sat)
    lgj.dieDeveloper(1000)
    lgj.buildFactory()
    lgj.shootRocket()
    print("===메서드 호출 후 변환된 값===")
    print("lgj.cnt_nuclear :", lgj.cnt_nuclear)
    print("lgj.asset :", lgj.asset)
    print("lgj.factory :", lgj.factory)
    print("lgj.cnt_sat :", lgj.cnt_sat)


[day03.mytest01]

# 첫별수를 입력하세요 1
# 끝별수를 입력하세요 3
# *
# **
# ***

start = input("첫별수를 입력하세요 ")
end = input("끝별수를 입력하세요 ")

istart = int(start)
iend = int(end)

for i in range(istart, iend+1):
    print("*" * i)
    
#####################################

def getStar(cnt):
    txt = ""
    for i in range(cnt):
        txt += "*"
    return txt

start = input("첫별수를 입력하세요 ")
end = input("끝별수를 입력하세요 ")

istart = int(start)
iend = int(end)

for i in range(istart, iend + 1):
    print(getStar(i))

# print(getStar(1))
# print(getStar(2))
# print(getStar(3))

'대덕인재개발원 > 대덕인재개발원_파이썬 프로그래밍' 카테고리의 다른 글

231102_파이썬 기초 6  (0) 2023.11.02
231101_파이썬 기초 5  (0) 2023.11.01
231031_파이썬 기초 4  (0) 2023.10.31
231027_파이썬 기초 2  (0) 2023.10.27
231026_파이썬 기초 1  (0) 2023.10.26