모듈: 파이썬 정의와 문장들을 담고 있는 파일import 모듈 __name__을 통해 모듈 이름 확인 import sys print(sys.__name__)모듈 안에 있는 이름들을 직접 임포트 할 수 있음특정 기능들만 임포트 가능 from sys import copyright print(copyright)as임포트한 모듈을 특정 이름으로 바꿔 사용가능 import sys as system print(system.copyright) Reference: https://docs.python.org/ko/3.6/tutorial/modules.html
함수의 정의만을 포함하는 함수 파일에서 정의 가능함수 파일의 이름은 첫번째 함수의 이름과 같아야 함스크립트 파일에서는 맨 뒤에 함수 정의 1. Function with One Output function y = sumAB(x1, x2) y = x1 + x2; end 2. Function with Multiple Output function [add, substract] = OperatorXY(x, y) add = x + y; substract = x - y; end Reference: https://www.mathworks.com/help/matlab/ref/function.html
1. flipud(array) 상하 반전 flip up to down a = [1 2 3; 4 5 6; 7 8 9; 10 11 12] b = flipud(a) 2. fliplr(array)좌우 반전 flip left to right a = [1 2 3; 4 5 6; 7 8 9; 10 11 12] c = fliplr(a) 3. reshape(array, column, row)행렬의 형태를 변경 1 * x 형태의 배열로 만든 후 해당 모양으로 잘라 만듦열부터 합쳐서 1 * x를 만듦ex) [1 2; 3 4; 5 6] ☞ [1 3 5 2 4 6] a = [1 2 3; 4 5 6; 7 8 9; 10 11 12] % 행렬 형태 변환 b = reshape(a,1, 12); c = reshape(a, 3, 4); R..
1. Array Creation 배열 생성열은 "," 또는 "스페이스" 로 구분행은 ";" 으로 구분 a = [1 2 3; 4 5 6; 7 8 9]2. Matrix and Array Operation더하기, 빼기각각의 원소에 연산을 적용 a = [1 2 3; 4 5 6; 7 8 9] a + 10 b = [10 11 12; 13 14 15; 16 17 18] a + b 곱하기 행렬끼리의 곱은 행렬의 곱을 반환각각의 원소에 연산을 적용하려면 ".*"을 사용 a = [1 2 3; 4 5 6; 7 8 9] a * 10 b = [10 11 12; 13 14 15; 16 17 18] a * b a .* b 전치 행과 열을 바꾸는 것 a = [1 2 3; 4 5 6; 7 8 9] a' 3. Indexing인덱싱행렬의..
1. Class Definition Syntax클래스: 데이터와 기능을 묶을 수 있음 인스턴스: 클래스로 찍어낸 객체123456class MyClass: """A simple example class""" i = 12345 def f(self): return 'hello world' 2. Class Objectsattribute 참조12345678910class MyClass: """A simple example class""" i = 12345 def f(self): return 'hello world' print(MyClass.i)print(MyClass.f)print(MyClass.__doc__) 인스턴스 생성 12345678class MyClass: """A simple example class"..
1. Handling Exceptions 선택한 예외를 처리하는 프로그램 가능 먼저, try 부분이 실행됨예외가 발생하지 않으면 except 부분을 건너 뛰고 try 구문 종료예외가 발생하면 except 부분으로 건너 뛰고 try 구문 종료except 부분에 매치되는 에러가 없다면 상위 try 구문으로 에러 전달123456while True: try: x = int(input("Please enter a number: ")) break except ValueError: print("Oops! That was no valid number. Try again...") 마지막 except 절은 예외 이름 생략 가능와일드 카드 역할 기능12345678910111213import sys try: f = open(..
1. Tuples and Sequences 출력되는 튜플은 항상 괄호로 둘러싸임12345678910111213141516t = 12345, 54321, 'hello!'t[0] t # Tuples may be nested:u = t, (1, 2, 3, 4, 5)u # Tuples are immutable:t[0] = 88888 # but they can contain mutable objects:v = ([1, 2, 3], [3, 2, 1])vColored by Color Scriptercs 빈 튜플은 빈 괄호쌍으로 만들어짐1234567empty = ()singleton = 'hello', #
1. Dictionary 키와 값으로 이루어짐 키는 중복될 수 없음숫자 대신에 키로 인덱싱됨1234tel = {'jack': 4098, 'sape': 4139}tel['guido'] = 4127tel Colored by Color Scriptercs dict()키-값 쌍들의 시퀀스로 딕셔러니 생성 가능1dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) 삭제123del tel['sape']tel['irv'] = 4127tel Reference: https://docs.python.org/ko/3.6/tutorial/datastructures.html#dictionaries
1. Sets 중복요소가 없는 순서 없는 컬렉션 집합을 만들 경우 중괄호나 set() 함수를 사용빈 집합을 만들 경우 set() 함수를 사용1234567891011121314151617181920basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}print(basket) # show that duplicates have been removed 'orange' in basket # fast membership testing 'crabgrass' in basket # Demonstrate set operations on unique letters from two words a = set('abracadabra')b = set('alacazam'..
1. Defining Functions def 키워드는 함수의 정의의 시작을 의미파라미터의 값들은 값에 의한 호출(Call by Value)로 전달123456789101112131415161718def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.""" a, b = 0, 1 while a None 값fib(0)print(fib(0))Colored by Color Scriptercs 2. Default Argument Values 필수 인자와 선택 인자를 사용하여 정의 가능 함수 호출시 선택 인자는 입력하지 않아도 무방1234567891011def ask_ok(prompt, retries=4, reminder='P..