Python

出自維基百科,自由嘅百科全書
跳去導覽 跳去搵嘢
Python 個嘜頭

Python 係一種直譯高階程式語言,係1991 年由 Guido van Rossum始創[1],特徵係啲嘅可讀性高,而且會多用空格(即係跟越位法則),支援多種嘅編程範式,包括結構化編程物件導向編程,而且有個全面嘅標準庫[2]。因為呢啲緣故,Python 俾好多人認為佢簡單又易用,所以喺廿一世紀初俾人廣泛採用嚟做好多種唔同嘅編程作業,例如係編寫機械學習程式同電子遊戲程式呀噉[3]

句法[編輯]

縮進[編輯]

Python 呢種程式語言跟越位法則(off-side rule),即係話縮進會用嚟表示「邊幾行碼屬同一個碼塊」嘅資訊。

數據結構[編輯]

  • array[::]
  • zip(,)

控制流程[編輯]

Python 容許多種控制流程功能,包括:

  • if... else:
  • for
  • while
  • try

I/O[編輯]

  • print(...)
  • input("")
  • with open("...", " ") as ...

子程序[編輯]

  • def func1(*args):

例子碼[編輯]

以下呢段 python 碼會攞一個正整數做 input,再俾嗰個數嘅階乘output

n = int(input('Type a number, and its factorial will be printed: '))

if n < 0:
    raise ValueError('You must enter a positive integer')

fact = 1
i = 2
while i <= n:
    fact *= i
    i += 1

print(fact)

睇埋[編輯]

[編輯]

  1. "The History of Python: A Brief Timeline of Python". Blogger. 2009-01-20. 喺2016-03-20搵到.
  2. "About Python". Python Software Foundation. Retrieved 24 April 2012., second section "Fans of Python use the phrase "batteries included" to describe the standard library, which covers everything from asynchronous processing to zip files."
  3. Kuhlman, Dave. "A Python Book: Beginning Python, Advanced Python, and Python Exercises". Section 1.1.

參考文獻[編輯]

  • Downey, Allen B. (May 2012). Think Python: How to Think Like a Computer Scientist (Version 1.6.6 ed.). ISBN 978-0-521-72596-5.