< Python 역사(버전별 출시일) >
1.0 : 1994
2.0 : 2000
2.7 : 2010 (2020.01 지원 종료)
3.0 : 2008
- 발표후 3년간 지원
3.5 : 2015
3.6 : 2016
3.7 : 2018
3.8 : 2019
3.9 : 2020.10
3.10 : 2021.10 출시 예정
//---------------------
파이썬 3 사용법 정리
//-----------------------------------------------------------------------------
- pip 설치 (pip install)
https://pip.pypa.io/en/stable/installation/
> python -m ensurepip --upgrade
    - 설치 방법2
https://bootstrap.pypa.io/get-pip.py 파일 다운로드 
> python get-pip.py
- pip 자체 업데이트 (pip update, pip self update)
> python -m pip install --upgrade pip
//-----------------------------------------------------------------------------
//--------------------- 
* 주석 
- 한줄 주석 : # 
- 범위 주석 : '''   ,    """
r""" 
This is a multiline 
comment. 
""" 
- 범위 주석에서 다음 에러를 방지하기 위해 """ 앞에 r 을 추가 한다.
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 324-325: truncated \UXXXXXXXX escape
//--------------------- 
* 코드 블럭 
탭위치로 코드블럭이 결정됨 
def loop1( arr ): 
   for i in arr:  
        print(i,1) 
        print(i,2) 
    print(i,3) 
    return 
//--------------------- 
* 메시지 출력 
x = 5 
y = 4 
print("{0} + {1} = {2}".format(x, y, x + y)) 
print(x,y) 
//--------------------- 
* 키입력 받기 
- pause 효과 
input('Press Key...') 
//--------------------- 
* 대기 
import time 
print("대기") 
time.sleep(3.3)  
//--------------------- 
* 파일시간 
import os 
import platform 
t1 = os.path.getmtime("d:\\t1.txt") 
t2 = os.path.getmtime("d:\\t2.txt") 
print(t1, t2, t2-t1) # 연산 가능 
//--------------------- 
* 프로그램 실행 
import os 
import platform 
import subprocess 
import win32api 
os.system( "dir -w" ) #모든 명령을 한개 행으로 
print os.popen("echo Hello, World!").read() #윈도우에서 안됨 
os.startfile(r'd:\t1.txt') #연결 프로그램 실행 
win32api.WinExec( cmd4 ) # 윈도우 API이용 
t1 = r"d:\t1.txt" 
subprocess.run([ 'notepad.exe', t1 ]) # 실행 파라미터 나누기 
subprocess.call(['notepad.exe', t1]) 
subprocess.Popen(['notepad.exe', t1 ]) 
//------------------- 
* 함수 
def fn1( in ): 
    print(in) 
    return 
//--------------------- 
* 조건 
if b > a: 
    print("b is greater than a")
elif b==a:
print("2")
else:
print("3")
if os.path.exists(dest)==True:t2=os.path.getmtime(dest); 
else: t2=0; 
//-------------------- 
* 3항 연산자(ternary) 
is_nice=False 
state = "nice" if is_nice else "not nice" 
print(state) 
//----------------------- 
* 예외 처리 
import sys 
try: 
    f = open('myfile.txt') 
    s = f.readline() 
    i = int(s.strip()) 
except OSError as err: 
    print("OS error: {0}".format(err)) 
//------------------- 
* 루프 - for 
arr = [1,2] 
for i in arr:  
    print(i,1) 
    print(i,2) 
print(i,3)
//------------------- 
* 루프 - while 
i = 1 
while i < 6: 
    print(i) 
    i += 1 
//------------------- 
* 명령행 실행 인자 
- sys.argv 
print ('Number of arguments:', len(sys.argv), 'arguments.') 
print ('Argument List:', str(sys.argv), sys.argv[0] ) 
//-----------------------------------------------------------------------------
외부 모듈 import 방법 
    - 사용자 작성한 라이브러리 로컬에서 절대경로로 로딩하는 방법
    - 방법1 : sys.path.append 이용
sys.path.append("경로") 
import 폴더.파일이름 as lib  # noqa 
    
    - 방법2 : 환경변수 PYTHONPATH를 설정
//----------------------------------------------------------------------------- 
vscode 파이썬 포맷터 autopep8 에서 import 관련 코드 정렬 안하기 
import 코드 뒤에  # noqa 추가 
    - 참고 
python formatter autopep8 prevent import order change 
Allow statements before imports with Visual Studio Code and autopep8 
https://stackoverflow.com/questions/54030320/allow-statements-before-imports-with-visual-studio-code-and-autopep8
//-----------------------------------------------------------------------------
'Code > Python' 카테고리의 다른 글
| python 가상 환경 제어 (0) | 2022.04.21 | 
|---|---|
| python 스크립트를 실행파일로 만들기 (0) | 2022.04.21 | 
| python 명령을 치면 Microsoft Store(윈도우 앱 스토어) 설치가 뜨는 문제 해결 (0) | 2020.02.01 | 
| [파이썬] 셀레늄을 이용한 웹 크롤링 (0) | 2020.02.01 | 
| 아나콘다 설치 에러 해결 방법 (0) | 2020.01.24 | 










