def GuGu():
n = int(input("몇 단을 출력하시겠습니까? "))
for i in range(1, 10):
print("%d * %d = %d" % (n, i, n*i))
GuGu()
몇 단을 출력하시겠습니까? 3 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27
GuGu()
몇 단을 출력하시겠습니까? 9 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
- 10 미만의 자연수에서 3과 5의 배수를 구하면 3, 5, 6, 9이다. 이들의 총합은 23이다.
- 1000 미만의 자연수에서 3의 배수와 5의 배수의 총합을 구하라.
sum = 0
for n in range(1, 1000):
if n%3 == 0 or n%5 == 0:
sum += n
print(sum)
233168
def getTotalPage(m, n):
if m % n == 0:
return m // n
else:
return m // n + 1
print(getTotalPage(5, 10))
print(getTotalPage(15, 10))
print(getTotalPage(25, 10))
print(getTotalPage(30, 10))
1 2 3 3
원하는 메모를 파일에 저장하고 추가 및 조회가 가능한 간단한 메모장을 만들어 보자.
필요한 기능은? 메모 추가하기, 메모 조회하기
입력 받는 값은? 메모 내용, 프로그램 실행 옵션
출력하는 값은? memo.txt
다음과 같은 명령을 실행했을 때 메모를 추가할 수 있도록 만들어 보자.
python memo.py -a "Life is too short"
%%writefile memo.py
import sys
option = sys.argv[1]
memo = sys.argv[2]
if option == "-a":
f = open("memo.txt", "a")
f.write(memo)
f.write("\n")
f.close()
elif option == "-v":
f = open("memo.txt", "r")
memo = f.read()
f.close()
print(memo)
Writing memo.py
%run memo.py -a "Life is too short"
!more memo.py
import sys option = sys.argv[1] memo = sys.argv[2] if option == "-a": f = open("memo.txt", "a") f.write(memo) f.write("\n") f.close() elif option == "-v": f = open("memo.txt", "r") memo = f.read() f.close() print(memo)
!more memo.txt
Life is too short
%run memo.py -a "You need python"
!more memo.txt
Life is too short You need python
%run memo.py -v
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) /workspace/myNotebook/점프 투 파이썬/memo.py in <module>() 2 3 option = sys.argv[1] ----> 4 memo = sys.argv[2] 5 6 if option == "-a": IndexError: list index out of range
%%writefile memo.py
import sys
option = sys.argv[1]
if option == "-a":
memo = sys.argv[2]
f = open("memo.txt", "a")
f.write(memo)
f.write("\n")
f.close()
elif option == "-v":
f = open("memo.txt", "r")
memo = f.read()
f.close()
print(memo)
Overwriting memo.py
%run memo.py -v
Life is too short You need python
문서 파일을 읽어서 그 문서 파일 내에 있는 탭(tab)을 공백(space) 4개로 바꾸어주는 스크립트를 작성해 보자.
필요한 기능은? 문서 파일 읽어 들이기, 문자열 변경하기
입력 받는 값은? 탭을 포함한 문서 파일
출력하는 값은? 탭이 공백으로 수정된 문서 파일
다음과 같은 형식으로 프로그램이 수행되도록 만들 것이다.
python tabto4.py src dst
%%writefile a.txt
Life is too short
You need Python
Writing a.txt
!more a.txt
Life is too short You need Python
%%writefile tabto2.py
import sys
src = sys.argv[1]
dst = sys.argv[2]
f = open(src, "r")
tab = f.read()
f.close()
f = open(dst, "w")
f.write(tab.replace("\t", " "*2))
f.close()
Writing tabto2.py
%run tabto2.py a.txt b.txt
!more b.txt
Life is too short You need Python
import os
print(os.listdir("./"))
['04-sys2.py', 'a.txt', '__pycache__', '06장 파이썬 프로그래밍, 어떻게 시작해야 할까?.ipynb', '05장 파이썬 날개달기.ipynb', '04장 프로그램의 입력과 출력은 어떻게 해야 할까?.ipynb', 'argv_test.py', '03장 프로그램의 구조를 쌓는다! 제어문.ipynb', 'foo.txt', 'argv_test2.py', 'memo.txt', 'mod1.py', 'memo.py', 'test.txt', 'tabto2.py', '.ipynb_checkpoints', '04-sys1.py', 'b.txt', 'test', '02장 파이썬 프로그래밍의 기초, 자료형.ipynb', '04-새파일.txt', 'game', '01장 파이썬이란 무엇인가?.ipynb']
filenames = os.listdir("./")
for filename in filenames:
fullname = os.path.join("./", filename)
print(fullname)
./04-sys2.py ./a.txt ./__pycache__ ./06장 파이썬 프로그래밍, 어떻게 시작해야 할까?.ipynb ./05장 파이썬 날개달기.ipynb ./04장 프로그램의 입력과 출력은 어떻게 해야 할까?.ipynb ./argv_test.py ./03장 프로그램의 구조를 쌓는다! 제어문.ipynb ./foo.txt ./argv_test2.py ./memo.txt ./mod1.py ./memo.py ./test.txt ./tabto2.py ./.ipynb_checkpoints ./04-sys1.py ./b.txt ./test ./02장 파이썬 프로그래밍의 기초, 자료형.ipynb ./04-새파일.txt ./game ./01장 파이썬이란 무엇인가?.ipynb
def search(dirname):
filenames = os.listdir(dirname)
for filename in filenames:
fullname = os.path.join(dirname, filename)
ext = os.path.splitext(fullname)[-1]
if ext == '.py':
print(fullname)
search('./')
./04-sys2.py ./argv_test.py ./argv_test2.py ./mod1.py ./memo.py ./tabto2.py ./04-sys1.py
def search(dirname):
try:
filenames = os.listdir(dirname)
for filename in filenames:
fullname = os.path.join(dirname, filename)
if os.path.isdir(fullname):
search(fullname)
else:
ext = os.path.splitext(fullname)[-1]
if ext == '.py':
print(fullname)
except PermissionError:
pass
search("./")
./04-sys2.py ./argv_test.py ./argv_test2.py ./mod1.py ./memo.py ./tabto2.py ./04-sys1.py ./game/graphic/render.py ./game/graphic/__init__.py ./game/__init__.py ./game/sound/echo.py ./game/sound/__init__.py
import os
for (path, dir, files) in os.walk("./"):
for filename in files:
ext = os.path.splitext(filename)[-1]
if ext == '.py':
print("%s/%s" % (path, filename))
.//04-sys2.py .//argv_test.py .//argv_test2.py .//mod1.py .//memo.py .//tabto2.py .//04-sys1.py ./game/__init__.py ./game/graphic/render.py ./game/graphic/__init__.py ./game/sound/echo.py ./game/sound/__init__.py