프로그래밍/Python 49

감마함수(gamma function)의 값을 (유효수자 15자리 까지 정밀하게) 계산하는 Python 언어 소스

Lanczos 알고리즘은 Stirlng 공식에 의한 알고리즘 보다 정밀하며, 십진수로 유효숫자 약 15자리 까지는 정확하게 계산해 준다. 단지 exp 함수를 이용하는 부분에서는 exp 함수의 구현에 따라 오차가 더 있을 수 있다. #!/usr/bin/env python # -*- encoding:utf-8 -*- # Filename: testLanczos-01.py # # An approximation for the gamma function by using the Lanczos algorithm # # Execute: python testLanczos-01.py # or # Execute: ./testLanczos-01.py # # See: http://en.wikipedia.org/wiki/Lancz..

Python 2.6, 2.6 및 3.2 로 복소수의 포맷(format) 출력

* 리눅스의 Python 2.6 으로 실행한 결과 Python 2.6.5 (r265:79063, Oct 1 2012, 22:04:36) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> x = complex(1, 2./3) >>> x (1+0.66666666666666663j) >>> "{0.real:.5}+{0.imag:.5}j".format(x) '1.0+0.66667j' >>> "{0.real:.5f}{0.imag:+.5f}j".format(x) '1.00000+0.66667j' >>> print "{0.real:.5f}{0.imag:+.5f}j".format(x) 1.0000..

pyglet 에 한글 사용하는 예제

* pyglet 을 이용하는 간단한 Python 소스 (소스파일명: testpyglet.py) import pyglet window = pyglet.window.Window() label = pyglet.text.Label('Hello, world', font_name='Times New Roman', font_size=36, x=window.width//2, y=window.height//2, anchor_x='center', anchor_y='center') @window.event def on_draw(): window.clear() label.draw() pyglet.app.run() 실행 결과: * pyglet 을 이용하여 한글을 출력하는 Python 소스 (소스파일명: testpyglet2.p..

Python 3.2 를 이용한 간단한 수학식 계산하기

Python 2.x 에서는 print 가 def, for, return 들 처럼 하나의 키워드(예약어)이지만, Python 3.x 에서는 print 가 하나의 함수이므로 반드시 소괄호를 붙여서 print( ..... ) 처럼 써야 한다. * 파이썬 버전 확인: -------------------------- $ python3.2 Python 3.2.1 (v3.2.1:ac1f7e5c0510, Jul 9 2011, 01:03:53) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. Cannot read termcap database; u..

Python 2.7 을 이용한 간단한 수학식 계산하기

* 파이썬 버전 확인: -------------------------- $ python Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. Cannot read termcap database; using dumb terminal settings. >>> * Python 2.7 용 소스 파일: first-sample.py #!/usr/local/bin/python # coding: utf8 from math import * def lg..

조립제법(Horner의 방법) 예제 2 for Python

다항식 p(x) 를 1차 다항식 ax - b 로 나눌 때의 몫과 나머지를 구하는 조립제법을 Python 언어로 구현해 보았다. 조립제법은 일명 Horner의 방법이라고도 불리우는데, 이는 x = a 에서 다항식 p(x)의 값 p(a)을 계산하는 가장 빠른 알고리즘이기도 하다. p(x) = (ax - b)q(x) + r 여기서 r은 나머지이며 r = p(b/a) 이다. 또 q(x)는 몫이다. [참고] * 온라인으로 조립제법 표 만들기 손으로 계산하는 조립제법 표 * 온라인으로 구하는 다항식의 도함수: 조립제법을 이용한 다항식의 도함수 아래의 소스파일은 파일 testSyntheticDivision.py 를 수정한 것이다. python 대신 jython이나 IronPython 으로도 수정 없이 그대로 실행된다..

숫자 맞추기 게임 with Python

Python 언어로 숫자 맞추기 게임을 작성해 보았다.input()  함수가 내부적으로 Python 2.x와 Python3.x에서 다르게 동작한다.    In Python2.*, input([prompt]) was equivalent to eval(raw_input([prompt])).    In Python3.*, input([prompt]) of Python2.* was removed and raw_input([prompt])                         was renamed as input([prompt]). 아래 소스의 16째 줄 guess = int(sbuf) 는 스트링을 정수로 타입변환하는 과정이다.소스 파일명: guessNumber01.py#!/usr/bin/env python..

Python 2.x의 input() 함수와 Python 3.x의 input() 함수의 차이

In Python2.*, input([prompt]) was equivalent to eval(raw_input([prompt])). In Python3.*, input([prompt]) of Python2.* was removed and raw_input([prompt]) was renamed as input([[rompt]). * Python 2.6에서 input() 함수 테스트하기 명령행프롬프트> python Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "icense" for more information. >>> x = ..

스트링 리스트에서 스트링 찾기(find) with Python

[파일명: testStringFindInList.py]------------------------------------------------ # coding=ms949 import sys def find(arr, s): for i in range(0, len(arr)): if arr[i].find(s) >= 0: return i return -1; def printArray(arr): sys.stdout.write("[") for i in range(0, len(arr) - 1): sys.stdout.write(arr[i] + ", ") if len(arr) > 0: sys.stdout.write(arr[len(arr) - 1]) print("]") words = ["하나", "둘", "셋", "넷", ..