모듈



main1.py
import Square1 as sq
#
# print(Square1.base)
# print(Square1.square(10))
print(sq.base) #별칭을 줘서 사용 할 수 있다.
print(sq.square(10))
print()
from Square1 import base,square # 다른방법
print(base)
print(square(10))


person
class Person:
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
def greeting(self):
print('안녕하세요 저는 {} 입니다. 나이는 {} 입니다.'.format(self.name, self.age))
main2
from person import Person
obj = Person('kim', 20, 'seoul')
obj.greeting()
from person import Person as ps
obj2 =ps('lee', 22, 'busan')
obj2.greeting()
person2
class Person:
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
def greeting(self):
print('안녕하세요 저는 {} 입니다. 나이는 {} 입니다.'.format(self.name, self.age))
main2
from modulePart.person2 import Person
obj3 = Person('choi', 10, 'suwon')
obj3.greeting()

hello
print('hello 모듈 시작')
print('hello.py __name__:', __name__)
print('hello 모듈 종료')
if __name__ == '__main__':
print()
print('main으로 실행 할 때만 출력')


main3
import hello

시작하는 파일이 main

calc.py
def add(n1, n2):
return n1 + n2
def mul(n1, n2):
return n1 * n2
if __name__ == '__main__':
print(add(10, 20))
print(mul(10, 20))
main4.py
import calc
print(calc.add(20, 30))
print(calc.mul(20, 30))
package

operation.py
def add(n1, n2):
return n1 + n2
def mul(n1, n2):
return n1 * n2
geometry.py
def triangle_area(base, height):
return base * height / 2
def rectangle_area(width, height):
return width * height
main5
from calpkg import operation
from calpkg import geometry
print(operation.add(10, 20))
print(operation.mul(10, 20))
print()
print(geometry.triangle_area(10, 20))
print(geometry.rectangle_area(50, 70))

__init__.py
from. import operation
from. import geometry
main6
import calpkg
print(calpkg.operation.add(10, 30))
print(calpkg.geometry.rectangle_area(10, 30))
print()
#__init__.py 에 import를 하지 않으면 불러오지 못한다 따라서
#__init__.py 에 from. import 를 사용해 불러온 다음 다른 파일에서 사용 해야 한다.
from calpkg import * # * 이게 그 파일에 모든걸 가져온다는 뜻
print(operation.mul(10, 20))
print(geometry.rectangle_area(10, 20))
print()
# or
from calpkg.operation import *
print(mul(10, 50))
print(add(10, 50))
from calpkg.geometry import *
print(rectangle_area(10, 50))
print(triangle_area(10, 50))

Numpy
- 수치연산에 특화 시킨것
- 링크드리스트는 순서대로 되어 있는게 아닌 링크를 통해 연속되게 만들어놓은것
- 배열 = 1. 연속된 저장, 2. 같은파일(같은 타입에 같은 크기를 쓰기 위해)
- 주소값 [0] = 0x200 +4*0 , [1] = 0x200 +4*1

가상폴더 만들기




파이참에 적용하기





numpyEx1
import numpy as np
ldata1 = [10,20,30,40]
print(ldata1)
print(type(ldata1))
print()
arr1 = np.array(ldata1)
print(arr1)
print(type(arr1))
print()
ldata2 = [[1,2,3,4], [5,6,7,8]] #리스트객체 2개가 들어가 있는것 뿐
print(ldata2)
arr2 = np.array(ldata2)
print(arr2)
print(arr2.shape)
print(arr2.dtype)
print()
ldata3 = [10.4, 23, 5233.12, 11]
print(ldata3)
arr3 = np.array(ldata3, dtype=np.int32)
print(arr3) #다 실수로 바뀜 why? 같은타입이여야 해서
print(arr3.dtype)
print()
ldata4 = ['1.23', 33.23, 431]
print(ldata4)
arr4 = np.array(ldata4, dtype=np.float64)
print(arr4)


numpyEx2
arange
import numpy as np
arr1 = np.arange(0, 10, 1)
print(arr1)
print(type(arr1))
print()
arr1 = np.arange(0, 10)
print(arr1)
print(type(arr1))
print()
arr1 = np.arange(10)
print(arr1)
print(type(arr1))
print()
#결과는 다 같음

numpyEx2
linspace
arr4 = np.linspace(-3, 3, 5)
print(arr4)
print(type(arr4))

ones
arr5 = np.ones([3,4]) # 안에 리스트 써도 되고 튜플 써도 됨, 크기를 구분한다
print(arr5)

zeros
arr6 = np.zeros([3,4])
print(arr6)

empty
arr7 = np.empty([3,4]) #가장 최근에 할당된 값을 가져옴 / 권장하지 않음
print(arr6)

full
arr8 = np.full([2,3], 100) #특정값을 써서 초기화하고 싶을때
print(arr8)

ones_like
arr9 = np.ones_like(arr8) #가져온다
print(arr9)
arr10 = np.zeros_like(arr8)
print(arr10)
arr11 = np.full_like(arr8, 50)
print(arr11)

'Pyhton > numpy' 카테고리의 다른 글
| Day30_Python(8)_Am (0) | 2025.09.24 |
|---|---|
| Day29_Python(7)_Pm (0) | 2025.09.23 |