Python 簡介

出自KMU Wiki

(修訂版本間差異)
跳轉到: 導航, 搜索
在2008年12月3日 (三) 17:28所做的修訂版本 (編輯)
Cch (對話 | 貢獻)
(檢查一個數是否為質數)
←上一個
在2008年12月3日 (三) 18:13所做的修訂版本 (編輯) (撤銷)
Cch (對話 | 貢獻)
(檢查一個數是否為質數)
下一個→
第41行: 第41行:
import sys import sys
 +import math
import string import string

在2008年12月3日 (三) 18:13所做的修訂版本

本條目所指的 Python 是 Python 程式語言

目錄

概覽

Python 官方網站對 Python 的介紹

A Byte of Python 對 Python 的簡介(中譯版)

Marr 對 Python 的簡介

根據 2008 年 11 月 TIOBE Programming Community Index,Python 的普及率在全世界排名第 6

免費電子書

Python 官方文件

A Byte of Python

A Byte of Python 中譯版

下載

官方網站下載

校內下載

參考資料

Python 官方網站

Swaroop C.H.'s Python Notes

練習

檢查一個數是否為質數


#!/usr/local/bin/python

# This program tests wether an integer is a prime

import sys
import math
import string

print 'This program tests wether an integer is a prime'
while 1 :
        sys.stdout.write('Please input an integer: ')
        s = raw_input()
        n = string.atol(s)
        ub = math.sqrt(n) + 1
        is_prime = 1
        f = 2
        while f < ub :
                if n % f == 0:
                        is_prime = 0
                        print n, ' has a factor ', f
                        break
                else :
                        f = f + 1
        if is_prime == 1 :
                print n, ' is a prime'
        else :
                print n, ' is not a prime'

附註:

  1. FreeBSD 上有一支程式 primes (/usr/games/primes) 可幫我們列出質數以供測試,如: primes 100 200 可列出 100 到 200 間的質數