Python 기본
대상: python을 해본 적이 없는 분
파이썬으로 할 수 있는 일
- 시스템 유틸리티 제작
- GUI 프로그래밍
- C/C++ 와의 결합
- 웹 프로그래밍
- 수치 연산 프로그래밍
- 데이터베이스 프로그래밍
- 데이터 분석, 사물 인터넷
파이썬으로 할 수 없는 일
- 시스템과 밀접한 프로그래밍 영역
- 모바일 프로그래밍
http://repl.it/
실습은 이곳에서 진행합니다.
REPL : Read-Eval-Print Loop의 약자로 스칼라, 파이썬 등 대화형 환경
콘솔에서 컴파일 없이 코드를 입력하고 출력 결과를 확인 할 수 있는 환경
10 미만의 자연수에서 3과 5의 배수를 구하면 3, 5, 6, 9이다. 이들의 총합은 23이다.
1000 미만의 자연수에서 3의 배수와 5의 배수의 총합을 구하라.
- snippet.python
result = 0 for n in range(1, 1000): if n % 3 == 0: result += n if n % 5 == 0: result += n print(result)
- snippet.python
result = 0 for n in range(1, 1000): if n % 3 == 0: result += n if n % 5 == 0: result += n print(result)
- snippet.python
result = 0 for n in range(1, 1000): if n % 3 == 0: result += n if n % 5 == 0: result += n print(result)
변수에 타입을 지정하지 않아요.
- snippet.python
result = 0
for, if 에서 소괄호를 사용하지 않아요.
- snippet.python
if n % 3 == 0:
중괄호{} 대신, :(콜론)을 사용하죠
- snippet.python
if n % 5 == 0: result += n
들여쓰기를 맞춰야 해요.
- snippet.python
for n in range(1, 1000): if n % 3 == 0: result += n
주민등록번호를 포함하고 있는 텍스트가 있다.
이 텍스트에 포함된 모든
주민등록번호의 뒷자리를 * 문자로 변경하시오.
- snippet.python
data = """ park 800905-1049118 kim 700905-1059119 """ result = [] for line in data.split("\n"): word_result = [] for word in line.split(" "): if len(word) == 14 and word[:6].isdigit() and word[7:].isdigit(): word = word[:6] + "-" + "*******" word_result.append(word) result.append(" ".join(word_result)) print("\n".join(result))
주민등록번호를 포함하고 있는 텍스트가 있다.
이 텍스트에 포함된 모든
주민등록번호의 뒷자리를 * 문자로 변경하시오.
정규표현식을 사용하면
- snippet.python
import re data = """ park 800905-1049118 kim 700905-1059119 """ pat = re.compile("(\d{6})[-]\d{7}") print(pat.sub("\g<1>-*******", data))
