제로베이스데이터스쿨_리뷰노트_23/04/04

김지태·2023년 4월 5일
0
post-thumbnail

git 주소 - DennyKim111

05 012 실행(메인)파일

def add(n1, n2):
return n1 + n2

def sub(n1, n2):
return n1 - n2

def mul(n1, n2):
return n1*n2

def div(n1, n2):
return n1 / n2

import add

import sub

import mul

import div

print(add.add(10, 20))
print(sub.sub(14, 2))
print(mul.mul(3, 4))
print(div.div(99, 3))

05 013 name 전역변수

모듈 파일 - unitConversion

def cmToMm(n):
return round(n 10, 3)
def cmToInch(n):
return round(n
0.393, 3)
def cmToM(n):
return round(n 0.01, 3)
def cmToFt(n):
return round(n
0.032, 3)

print(f'10cm: {cmToMm(10)}mm')
print(f'10cm: {cmToInch(10)}inch')
print(f'10cm: {cmToM(10)}m')
print(f'10cm: {cmToFt(10)}ft')

import unitConversion as uc

if name == 'main':
inputNumber = int(input('길이(cm)입력: '))

returnValue = uc.cmToMm(inputNumber)
print(f'returnValue: {returnValue}mm')

returnValue = uc.cmToInch(inputNumber)
print(f'returnValue: {returnValue}inch')

returnValue = uc.cmToM(inputNumber)
print(f'returnValue: {returnValue}m')

returnValue = uc.cmToFt(inputNumber)
print(f'returnValue: {returnValue}ft')

05 014 패키지 - 모듈을 묶어서 관리

from CalculatorForInt import addCal
from CalculatorForInt import subCal
from CalculatorForInt import mulCal
from CalculatorForInt import divCal

print(addCal.add(20, 30))
print(subCal.sub(20, 30))
print(mulCal.mul(20, 30))
print(divCal.div(20, 30))

from CalculatorForFloat import addCal
from CalculatorForFloat import subCal
from CalculatorForFloat import mulCal
from CalculatorForFloat import divCal

print(addCal.add(10, 20))
print(subCal.sub(10, 20))
print(mulCal.mul(10, 20))
print(divCal.div(10, 20))

05 015 site - package / 어디서나 접근 가능한 패키지를 만들자

import sys

for path in sys.path:
print(path)

from Calculator import Cal

print(Cal.add(10, 20))

Calculator 다른 폴더로 옮겼더니 실행 안됨, vnew 로 옮기면 그래도 실행됨 / 가상황경

05 016 자주 쓰는 모듈들

listVar = [2, 3, 4, 5,6 , 11, 33, 4]
print(f'sum(listVar) = {sum(listVar)}')

#최대값

print(f'max(listVar) = {max(listVar)}')

최소값

print(f'min(listVar) = {min(listVar)}')

거듭제곱

print(f'거듭제곱(밑, 지수) {pow(13, 11)}')

반올림

print(f'round(소수, 몇째 자리){round(3.142434243, 3)}')

import math

#절댓값
print(math.fabs(-10))
print(math.fabs(-1.534235235))

올림

print(f'올림 {math.ceil(6.66)}')
print(f'올림 {math.ceil(-6.66)}')

내림

print(f'내림 {math.floor(5.59)}')
print(f'내림 {math.floor(-5.59)}')

버림

print(f'버림 {math.trunc(5.21)}')
print(f'버림 {math.trunc(-5.21)}')

최대공약수

print(f'최대공약수 {math.gcd(144, 36)}')

팩토리얼

print(f'팩토리얼 {math.factorial(10)}')

제곱근

print(f'제곱근 {math.sqrt(121)}')

import time

now = time.localtime()

print(f'{now}')

#연도만
print(f'{now.tm_year}')
#월
print(f'{now.tm_mon}')
#일
print(f'{now.tm_mday}')
#시간
print(f'{now.tm_hour}')
#분
print(f'{now.tm_min}')
#초
print(f'{now.tm_sec}')

05 017 객체 지향 프로그램 / 객체를 이용한 프로그램으로, 객체는 속성과 기능으로 구성된다.

#객체를 이용한 프로그램 -> 객체 = 속성 & 기능

class Car:
def init(self, color, length):
self.color = color
self.length = length

def doStop(self):
print('Stop!!')

def doStart(self):
print('START!!!')

def printCarInfo(self):
print(f'self.color = {self.color}')
print(f'self.length = {self.length}')

car1 = Car('red', 55)
car2 = Car('black', 666)

car1.printCarInfo()
car2.printCarInfo()

car1.doStart()

car2.doStop()

05 018

class Airplane:

def init(self, color, length, weight):
self.color = color
self.length = length
self.weight = weight

def doFly(self):
print('FLY!!!')

def doLand(self):
print('LAND!!!')

def printAirplaneInfo(self):
print(f'self.color = {self.color}')
print(f'self.length = {self.length}')
print(f'self.weight = {self.weight}')

airplane1 = Airplane('black', 999, 9990)
airplane2 = Airplane('blue', 1043, 15000)
airplane3 = Airplane('gold', 1800, 20000)
airplane3 = Airplane('silver', 4444, 20000)
airplane3 = Airplane('Grey', 5000, 20000)

airplane1.doLand()

airplane3.printAirplaneInfo()

airplane2.doFly()

05 019 객체 속성 변경

class NewGenerationPC:
def init(self, name, cpu, memory, ssd):
self.name = name
self.cpu = cpu
self.memory = memory
self.ssd = ssd

def doExcel(self):
print('EXCEL RUN!!')

def doPhotoshop(self):
print('Photoshop Run!!')

def printPCInfo(self):
print(self.name, self.cpu, self.memory, self.ssd)

com1 = NewGenerationPC('apple', 'i5', '256G', '256G')
com2 = NewGenerationPC('apple', 'i7', '512G', '512G')

com1.printPCInfo()
com2.printPCInfo()
com1.doExcel()

com1.cpu = 'i9'
com1.ssd = '5000G'
com1.memory = '999G'

com1.printPCInfo()

class Calculator:
def init(self):
self.number1 = 0
self.number2 = 0
self.result = 0

def add(self):
self.result = self.number1 + self.number2
return self.result

def sub(self):
self.result = self.number1 - self.number2
return self.result

def mul(self):
self.result = self.number1 * self.number2
return self.result

def div(self):
self.result = self.number1 / self.number2
return self.result

calculator = Calculator()
calculator.number1 = 10
calculator.number2 = 20

print(f'calculator.add(): {calculator.add()}')
print(f'calculator.sub(): {calculator.sub()}')
print(f'calculator.mul(): {calculator.mul()}')
print(f'calculator.div(): {calculator.div()}')

calculator.number1 = 88
calculator.number2 = 45

print(f'calculator.add(): {calculator.add()}')
print(f'calculator.sub(): {calculator.sub()}')
print(f'calculator.mul(): {calculator.mul()}')
print(f'calculator.div(): {calculator.div()}')

05 020 객체와 메모리

class Robot:

def init(self, color, height, weight):
self.color = color
self.height = height
self.weight = weight

def printRobotInfo(self):
print(self.color, self.height, self.weight)

rb1 = Robot('red', 200, 80)

rb2 = Robot('blue', 300, 120)
rb3 = rb1

rb1.printRobotInfo()
rb2.printRobotInfo()
rb3.printRobotInfo()

rb1.color = 'black'
rb1.height = 111
rb1.weight = 111

rb1.printRobotInfo()
rb2.printRobotInfo()
rb3.printRobotInfo()

scores = [int(input('국어 점수 입력: ')), int(input('영어 점수 입력: ')), int(input('수학 점수 입력: '))]

print(scores)

copyScores = scores.copy()

for idx, score in enumerate(copyScores):
result = score * 1.1
copyScores[idx] = 100 if result > 100 else result

print(f'이전 평균: {sum(scores) / len(scores)}')
print(f'이후 평균: {sum(copyScores) / len(copyScores)}')

05 021 얕은 복사, 깊은 복사

class TemCls:
def init(self, n, s):
self.number = n
self.str = s

def printClsInfo(self):
print(f'self.number: {self.number}')
print(f'self.str: {self.str}')

tc1 = TemCls(10, 'Hello')
tc2 = tc1

tc1.printClsInfo()
tc2.printClsInfo()

tc2.number = 3.14
tc2.str = 'bye'

tc1.printClsInfo()
tc2.printClsInfo()

깊은 복사

import copy

tc1 = TemCls(10, 'Hello')
tc = copy.copy(tc1)

tc1.printClsInfo()
tc2.printClsInfo()

tc2.number = 3.14
tc2.str = 'no'

tc1.printClsInfo()
tc2.printClsInfo()

import copy

scores = [9, 8, 5, 7, 6, 10]

scoresCopy = []

scoresCopy = scores
print(f'id(scores): {id(scores)}')
print(f'id(scoresCopy): {id(scoresCopy)}')

for s in scores:
scoresCopy.append(s)
print(f'id(scores): {id(scores)}')
print(f'id(scoresCopy): {id(scoresCopy)}')

scoresCopy.extend(scores)
print(f'id(scores): {id(scores)}')
print(f'id(scoresCopy): {id(scoresCopy)}')

scoresCopy = scores.copy()
print(f'id(scores): {id(scores)}')
print(f'id(scoresCopy): {id(scoresCopy)}')

scoresCopy = scores[:]
print(f'id(scores): {id(scores)}')
print(f'id(scoresCopy): {id(scoresCopy)}')

plaOriSco = [8.7, 9.1, 8.9, 9.0, 7.9, 9.5, 8.8, 8.3]
plaCopSco = plaOriSco.copy()

plaOriSco.sort()

plaCopSco.sort()
plaCopSco.pop(0)
plaCopSco.pop()

print(f'plaOriSco: {plaOriSco}')
print(f'plaCopSco: {plaCopSco}')

oriTot = round(sum(plaOriSco), 2)
oriAvg = round(oriTot / len(plaOriSco), 2)
print(f'Original Total: {oriTot}')
print(f'Original Average: {oriAvg}')

copTot = round(sum(plaCopSco), 2)
copAvg = round(copTot / len(plaCopSco), 2 )
print(f'Copy Total: {copTot}')
print(f'Copy Average: {copAvg}')

print(f'oriAvg - copAvg = {oriAvg - copAvg}')

클래스 관련 코딩을 볼 때, 처음에는 변수로 계속해서 설정해주는 것이 헷갈렸다.
이미 지정을 한 것 같은 느낌이 드는데도 한 번, 또 한 번(내 느낌상) 계속 변수를 지정해준다.

객체 관련 개념 또한 매우 어려웠다.
내가 강의를 대충 들은건가? 라는 생각이 들어, 다시 돌려봐도 잘 이해가 안갔다.
이럴 때는 계속 코딩을 타이핑 해가면 언젠가 이해가 되겠지?

클래스가 쉬울 것 같으면서도 막상 만들어보려고 하면 코드가 잘 나오지 않는다.
그저 많이 해보는 것이 답일까?

강의 듣느라 연습 시간 확보가 잘 안되는 것 같다.
조급해하지 말자.

profile
데이터 분석가

0개의 댓글