목차
1. 설명문(주석, Comments) 및 줄변경(Lines)
2. 산술 연산자 (Arithmetic Operators)
3. 관계 연산자 (Relational Operators)
4. 논리 연산자 (Logical Operators)
5. 콜론 연산자 (Colon Operators)
6. 데이터 타입 (Data Types)
7. 반복문 (Repeat statements)
8. 조건문 (Conditional statements)
9. 배열 (Array)
10. 행렬 연산 (Matrix Operation)
1. 설명문(주석, Comments) 및 줄변경(Lines)
[Note] Notepad++에서 Python 주석 사용하기
주석: Ctrl + K
주석 토글: # 주석에서 Ctrl + K
주석 해제: Ctrl + Shift + K
긴 주석: Ctrl + Shift + Q
긴 주석 해제: Ctrl + Shift + K
| 언어 | 구문 | 줄바꿈 기호 | 줄연속 기호 |
|---|---|---|---|
| C | /* C 주석 (여러 줄) */ |
; |
\ (백슬래시) |
| C++ | // C++ 주석/* C++ 주석 (여러 줄) */ |
; |
\ (백슬래시) |
| CSS | /* CSS 주석 (여러 줄) */ |
; |
없음 (자동 연속) |
| F90 | ! 포트란 주석 |
줄바꿈 자체 | & (and 기호) |
| F77 | * 포트란 주석 |
줄바꿈 자체 | 6열에 + (플러스) |
| HTML | <!-- HTML 주석 (여러 줄) --> |
태그 닫힘 | 없음 |
| JS | // JS 주석/* JS 주석 (여러 줄) */ |
; |
\ (백슬래시) |
| JSP | <%-- JSP 주석 (여러 줄) --%> |
; |
없음 |
| MATLAB | % MATLAB 주석 |
줄바꿈 자체 | ... (마침표 3개) |
| PYTHON | # 파이썬 주석""" 여러 줄 주석 """ |
줄바꿈 자체 | \ (백슬래시) |
2. 산술 연산자 (Arithmetic Operators)
| 기능 | MATLAB(1) | PYTHON | C++ | F77 F90/95(2) |
|---|---|---|---|---|
| 덧셈 | + | + | + | + |
| 뺄셈 | - | - | - | - |
| 곱셈 | * 및 .* | * | * | * |
| 나눗셈 | / 및 ./ | / | / | / |
| 지수 | ^ 및 .^ | ** | pow(3) | ** |
| 나머지 | % | % | ||
| 증가분 | i+=1 | i++ | ||
| 감소분 | i-=1 | i-- | ||
| 그룹 구분 | ( ) | ( ) | ( ) | ( ) |
(1) MATLAB에서 연산자 앞의 ‘.’는 요소별(또는 스칼라) 연산이 필요할 경우 사용한다.
(2) F90/95에서는 필요한 경우 새로운 연산자를 정의하여 사용할 수 있다.
(3) xy는 함수를 사용하여, pow(x, y)로 나타낸다.
3. 관계 연산자 (Relational Operators)
| 기능 | MATLAB | PYTHON | C++ | F90/95 | F77 |
|---|---|---|---|---|---|
| Equal to | == | == | == | == | .EQ. |
| Not equal to | ~= | != | != | /= | .NE. |
| Less than | < | < | < | < | .LT. |
| Less than or equal to | <= | <= | <= | <= | .LE. |
| Greater than | > | > | > | > | .GT. |
| Greater than or equal to | >= | >= | >= | >= | .GE. |
| Object identity | is | ||||
| Negated object identity | is not |
4. 논리 연산자 (Logical Operators)
| 기능 | MATLAB | PYTHON | C++ | F90/95 | F77 |
|---|---|---|---|---|---|
| Logical NOT | ~ | not | ! | .NOT. | .NOT. |
| Logical AND | && | and | && | .AND. | .AND. |
| Logical inclusive OR | || | or | || | .OR. | .OR. |
| Logical exclusive OR | xor | ^ | .XOR. | .XOR. | |
| Logical equivalent | == | == | .EQV. | .EQV. | |
| Logical not equivalent | ~= | != | .NEQV. | .NEQV. |
(1) Bit logical AND, exclusive OR, OR, NOT에 대해서 &, ^, |, ~를 사용한다.
5. 콜론 연산자 (Colon Operators)
| 구문 | F90/95 | MATLAB/PYTHON | 용법 | F90/95 | MATLAB/PYTHON |
|---|---|---|---|---|---|
| 기본 형식 | B:E:I(1) | B:I:E | 1. 배열 요소 위치 | 사용가능 | 사용가능 |
| ≥B | B: | B: | 2. 문자열에서 문자위치 | 사용가능 | 사용가능 |
| ≤E | :E | :E | 3. 반복문 제어 | 사용불가 | 사용가능 |
| 전체 영역 | : | : 또는 ...(생략 기호)(2) | 4. 배열 요소 생성 | 사용불가 | 사용가능 |
| 마지막 인덱스 | -1(3) |
(1) B = 시작값(beginning), E = 끝값(ending), I = 증분값(increment)
(2) 생략 기호(ELLIPSIS): 배열의 모두를 나타낼 때 Python에서는 '...'를 사용할 수 있다.
# 예)
c = array([[1, 2, 3], [4, 5, 6]])
>>> c[...]
Out[13]: array([[1, 2, 3], [4, 5, 6]])
>>> c[-1,:]
Out[14]: array([4, 5, 6])
>>> c[:,-1]
Out[15]: array([3, 6])
(3) 배열의 마지막 인덱스: Python에서는 '-1'를 사용할 수 있다.
6. 데이터 타입 (Data Types)
| 변수(저장) | MATLAB | PYTHON | C++ | F90/95 | F77 |
|---|---|---|---|---|---|
| byte | 실수/복소수(1) | 정수/실수/복소수 | char | character:: | character |
| 정수 | 문자 | 문자열: "", 리스트: [], 튜플: () | int | integer:: | integer |
| 실수 | dictionary: {:, ...} | float | real:: | real | |
| 배정도 실수 | set: {} | double | real*8:: | double precision | |
| 복소수 | Boolean: True, False | complex:: | complex | ||
| 논리자(Boolean) | Binary: bytes, bytearray, memoryview | bool | logical:: | logical | |
| Parameter | parameter:: | parameter | |||
| 포인터 | * | pointer:: | |||
| 구조문 | struct | type:: |
(1) C++에는 복소수형 변수에 대한 특별한 내장형 변수형태가 없다.
7. 반복문 (Repeat statements)
| 기능 | MATLAB | PYTHON | C++ | F90/95 | F77 |
|---|---|---|---|---|---|
| 유한 반복문 | for k=1:n | for i in range(n): | for k=1:n { } | do k=1,n | do # k=1,n |
| 무한 반복문 | while | while True: | while { } | do while | - |
| 반복문 종료 및 탈출 | break | break | break | exit | go to |
| 반복문 1회 건너뜀 | - | continue | continue | cycle | go to |
| 메시지 출력 후 멈춤 | error | except | error() | stop | stop |
| 호출함수로 환원 | return | return | return | return | return |
| 조건부 반복문 | initialize test while_Lexpress true group change test end | initialize test while True: key = input() print(key) | initialize test while (Lexpress) { true group change test } | initialize test do while (Lexpress) true group change test end do | initialize test # continue if (Lexpress) then true group change test go to # end if |
| 기능 | MATLAB | PYTHON | C++ | F90/95 |
|---|---|---|---|---|
| 색인 반복문 | for index=matrix statements end | for i in range(10): statements xdata = [0.1,4,3] for x in xdata: | for (init;test;inc) { statements } | do index=b,e,i statements end do |
| Pretest 반복문 | while (test) statements end | while (test) statements | while (test) { statements } | do while (test) statements end do |
| Posttest 반복문 | items = [9,5,4,10] for idx, val in enumerate(items): | do { statements } while (test) | do statements if (test) exit end do |
8. 조건문 (Conditional statements)
| 기능 | MATLAB | PYTHON | C++ | F90/95 | F77 | JavaScript |
|---|---|---|---|---|---|---|
| 조건부 실행 | if end |
if a > 0: | if { } | if then end if |
if then end if |
if (a > 0) { } |
| 조건부 교체 | else | else: | else | else | else | else { } |
| elseif | elif: | else if | elseif then | elseif then | else if (b > 0) { } | |
| 조건부 선택 | switch/case 문 | 없으므로, if 문 사용 | switch { } | select case end select |
if then end if |
switch (x) { case 1: break; case 2: break; default: break; } |
| 기능 | MATLAB | PYTHON | C++ | F90/95 |
|---|---|---|---|---|
| 논리형 IF문 | if L_express true group end | if (L_express): true group | if (L_express) { true group } | if (L_express) then true group end if |
| 기능 | MATLAB | PYTHON | C++ | F90/95 |
|---|---|---|---|---|
| 내부 IF문 | if L_express1 true group A if L_express2 true group B end true group C end statement group D | if L_express1: true group A if L_express2: true group B true group C statement group D | if (L_express1) { true group A if (L_express2) { true group B } true group C } statement group D | if (L_express1) then true group A if (L_express2) then true group B end if true group C end if statement group D |
| 기능 | MATLAB | PYTHON | C++ | F90/95 |
|---|---|---|---|---|
| IF-ELSE문 | if L_express true group A else true group B end | if L_express: true group A else: true group B | if (L_express) { true group A } else { true group B } | if (L_express) then true group A else true group B end if |
| 기능 | MATLAB | PYTHON | C++ | F90/95 |
|---|---|---|---|---|
| IF-ELSE-IF문 | if L_express1 true group A elseif L_express2 true group B else default group C end | if L_express1: true group A elif L_express2: true group B else: default group C | if (L_express1) { true group A } else if (L_express2) { true group B } else { default group C } | if (L_express1) then true group A else if (L_express2) then true group B else default group C end if |
9. 배열 (Array)
| 기능 | MATLAB | C++ | F90/95 | F77 |
|---|---|---|---|---|
| 조건부 배열 제어 | if | - | where | - |
| 조건부 배열 교체 | else | - | elsewhere | - |
| elseif | - | - | - |
| 기능 | MATLAB | F90/95 |
|---|---|---|
| 배열이름 생성 | () | () |
| 성분들 구분 | , | , |
| 성분들 자동 생성 | : | : |
| 구분 | ; | ; |
| 성분들 생성 | [] | (/ /) |
| 줄간 연결 | ... | & |
| 출력 금지 | ; | default |
10. 행렬 연산 (Matrix Operation)
- 행렬 연산 비교
| 기능 | 수식 | MATLAB | F90/95 | 前 크기 | 後 크기 |
|---|---|---|---|---|---|
| 스칼라+스칼라 | c = a ± b | c = a ± b; | c = a ± b | 1×1 | 1×1 |
| 요소+스칼라 | cjk = ajk ± b | c = a ± b; | c = a ± b | m×n, 1×1 | m×n |
| 요소+요소 | cjk = ajk ± bjk | c = a ± b; | c = a ± b | m×n, m×n | m×n |
| 스칼라×스칼라 | c = a * b | c = a * b; | c = a * b | 1×1 | 1×1 |
| 요소×스칼라 | cjk = ajk × b | c = a * b; | c = a * b | m×n, 1×1 | m×n |
| 요소×요소 | cjk = ajk × bjk | c = a .* b; | c = a * b | m×n, m×n | m×n |
| 스칼라/스칼라 | c = a / b | c = a / b; | c = a / b | 1×1 | 1×1 |
| 요소/스칼라 | cjk = ajk / b | c = a / b; | c = a / b | m×n, 1×1 | m×n |
| 요소/요소 | cjk = ajk / bjk | c = a ./ b; | c = a / b | m×n, m×n | m×n |
| 스칼라 멱승 | c = ab | c = a ^ b; | c = a ** b | 1×1 | 1×1 |
| 요소 멱승 | cjk = ajkb | c = a ^ b; | c = a ** b | m×n, 1×1 | m×n |
| 요소 멱승 | cjk = ajkbjk | c = a .^ b; | c = a ** b | m×n, m×n | m×n |
| 전체배열 | Ckj = Ajk | C = A'; | C = transpose(A); | m×n | n×m |
| 배열×배열 | Cij = ∑kAikBkj | C = A * B; | C = matmul(A,B); | m×r, r×n | m×n |
| 스칼라곱 | c = ∑kAkBk | c = sum(A .* B); | c = sum(A * B); | m×1, 1×m | 1×1 |
| 스칼라곱 | c = A' * B'; | c = dot_product(A,B); | m×1, 1×1 | 1×1 |
- 배열 및 행렬 연산 비교
| 기능 | MATLAB | PYTHON | C++ | F90/95 |
|---|---|---|---|---|
| 1차원 배열 초기화 | A(100)=0 for j=1:100 A(j)=12 end 또는 A=12*ones(1,100) | np.ones([3,3]) np.ones([3,3],dtype='int32') np.zeros([3,3]) np.empty([3,3]) mx1 = np.array([[5,10], [15,20]]) | int A[100]; for (j=0; j<100; j++) A[j]=12; | integer A(100) A=12 |
| 2차원 배열 초기화 | A=ones(10,10) | int A[10][10] | integer A(10,10) | |
| 배열 초기화 및 할당 | A=zeros(2,3) A=[1,7,2; 3,4,6] | int A[2][3]; int A[2][3] = { {1,7,2}, {3,4,6} }; | integer, dimension(2,3):: A A(1,:) = (/1,7,2/) A(2,:) = (/3,4,6/) |
행렬 연산
| 기능 | MATLAB | PYTHON | C++ | F90/95 |
|---|---|---|---|---|
| 곱셈 (C=AB) | C=A*B | C1=np.dot(A,B) C2=np.matmul(A,B) C3=A@B | for (i=0; i<10; i++) { for (j=0; j<10; j++) { C[i][j]=0; for (k=0; k<10; k++) { C[i][j] += A[i][k] * B[k][j]; }}} | C=matmul(A,B) |
| 스칼라곱 | C=a*B | Z = X.copy() # deep copy Y = X # shallow copy | for (i=0; i<10; i++) { for (j=0; j<10; j++) { C[i][j]=a*B[i][j]; }} | C=a*B |
| 역행렬 | B=inv(A) | import scipy.linalg as linalg Ainv = linalg.inv(A) |
11. 부프로그램 (Sub-programs)
| 기능 | MATLAB | PYTHON | C++ | F90/95 |
|---|---|---|---|---|
| 프로그램 | statements [y1⋯yn]=f(a1,⋯,am) [end of file] | statements y1,⋯,yn = f(a1,⋯,am) | main(ac, char **av) { statements y = f(a1,⋯,am); } | program main type y type a1,⋯,type am statements y=f(a1,⋯,am) call s(a1,⋯,am) end program |
| 서브루틴 | void f (type a1,⋯,type am) { statements } | subroutine s (a1,⋯,am) type a1,⋯,type am statements end | ||
| 함수 | function [r1⋯m] =f(a1,⋯,am) statements | def computeProps(n,p,q): a,b,c=n,p,q return (a,b,c) | type f (type a1,⋯,type am) { statements } | function f(a1,⋯,am) type f type a1,⋯,type am statements end |
| 단일 인수 | function out = name (in) | name (in, out) | function name (in) function name (in) result (out) | |
| 다중 인수 | function [inout, out2] = name (in1,in2,inout) | name (in1, in2, inout, out2) | subroutine name (in1, in2, inout, out2) |
12. 전역 변수 (Global variables)
| 기능 | MATLAB | PYTHON | C++ | F90/95 | F77 |
|---|---|---|---|---|---|
| 전역변수 선언 | global variables | global variables | external variables | module name save type(t):: variables end module name | common /name/ variables |
| 전역변수 호출 | global variables | global variables | external variables | use name | common /name/ variables |