Python

出自維基百科,自由嘅百科全書
(由Python 程式語言跳轉過嚟)
跳去導覽 跳去搵嘢
Python
Python-logo-notext.svg
編程範式多範型函數式指令式反射式結構化面向對象
設計者Guido van Rossum
編程員Python軟件基金會
第一次出現1991年, 31年之前 (1991)[1]
穩定版本
3.10.5[2] 喺維基數據度改 / 2022年6月6號,29日之前
預覽版本
3.11.0b3[3] 喺維基數據度改 / 2022年6月1號,34日之前
類型系統鴨子類型動態強類型[4]漸進英文gradual typing(自從3.5)[5]
操作系統跨平臺WindowsLinux/UnixmacOS
軟件授權Python軟件基金會許可證
副檔名.py、.pyi、.pyc、.pyd、.pyo(3.5之前)[6]Template:Notatypo、.pyz(自從3.5)[7]
網站Python嘅官方網站
Major implementations
CPythonPyPyIronPythonJythonStackless PythonMicroPythonCircuitPython英文CircuitPython、RustPython[8]
Dialects
CythonRPythonStarlark英文Bazel (software)[9]
啟發語言
ABC[10]Ada[11]ALGOL 68[12]APL[13]C[14]C++[15]CLU[16]Dylan[17]Haskell[18]Icon[19]Java[20]Lisp[21]Modula-3[15]PerlStandard ML[13]
影響語言
BooCobra英文Cobra (programming language)CoffeeScript[22]DF#FalconGenie[23]GoGroovyJavaScript[24][25]Julia[26]Nim、Ring[27]Ruby[28]Swift[29]

Python英國發音:/ˈpaɪθən/ 美國發音:/ˈpaɪθɑːn/),係一種直譯高階程式語言,係1991 年由吉多·范羅蘇姆始創[30],特徵係啲編碼嘅可讀性高,而且會多用空格(即係跟越位法則),支援多種嘅編程範式,包括結構化編程物件導向編程,而且有個全面嘅標準庫[31]。因為呢啲緣故,Python 喺廿一世紀初俾人廣泛採用嚟做好多種唔同嘅編程作業,例如係編寫機械學習程式同電子遊戲程式[32]

語法[編輯]

縮進[編輯]

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

關鍵字[編輯]

Python35個關鍵字或「保留字」;佢哋唔能夠用作識別碼

  • and
  • as
  • assert
  • async
  • await
  • break
  • class
  • continue
  • def
  • del
  • elif
  • else
  • except
  • False
  • finally
  • for
  • from
  • global
  • if
  • import
  • in
  • is
  • lambda
  • None
  • nonlocal
  • not
  • or
  • pass
  • raise
  • return
  • True
  • try
  • while
  • with
  • yield

數據結構[編輯]

  • array[::]
  • zip(,)

控制流程[編輯]

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

  • if... else
  • if...elif...else
  • for
  • while
  • try

I/O[編輯]

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

子程序[編輯]

  • def func1(*args):

運算子[編輯]

  • + 相加
  • - 相減
  • * 相乘
  • /
  • % 取餘數

例子碼[編輯]

以下係 Python 嘅 Hello World 代碼,出名在非常簡單

print("Hello, world!")

以下呢段 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. Guttag, John V. (12 August 2016). Introduction to Computation and Programming Using Python: With Application to Understanding Data. MIT Press. ISBN 978-0-262-52962-4.
  2. "Python 3.10.5 is available" (英文). 2022年6月6號. 喺2022年6月6號搵到.
  3. "Expedited release of Python3.11.0b3" (英文). 2022年6月1號. 喺2022年6月1號搵到.
  4. "Why is Python a dynamic language and also a strongly typed language - Python Wiki". wiki.python.org. 喺2021-01-27搵到.
  5. "PEP 483 -- The Theory of Type Hints". Python.org. 原先內容歸檔喺2020-06-14. 喺2020-04-22搵到.
  6. File extension .pyo was removed in Python 3.5. See PEP 0488 互聯網檔案館歸檔,歸檔日期2020年6月1號,.
  7. Holth, Moore (2014-03-30). "PEP 0441 -- Improving Python ZIP Application Support". 原先內容歸檔喺2018-12-26. 喺2015-11-12搵到.
  8. "RustPython".
  9. "Starlark Language". 喺25 May 2019搵到.
  10. 引用錯誤 無效嘅<ref>標籤;無文字提供畀叫做faq-created嘅參照
  11. "Ada 83 Reference Manual (raise statement)".
  12. Kuchling, Andrew M. (22 December 2006). "Interview with Guido van Rossum (July 1998)". amk.ca. 原著喺1 May 2007歸檔. 喺12 March 2012搵到.
  13. 13.0 13.1 "itertools — Functions creating iterators for efficient looping — Python 3.7.1 documentation". docs.python.org.
  14. van Rossum, Guido (1993). 1&type=pdf "An Introduction to Python for UNIX/C Programmers" Check |url= value (help). Proceedings of the NLUUG Najaarsconferentie (Dutch UNIX Users Group). even though the design of C is far from ideal, its influence on Python is considerable.
  15. 15.0 15.1 "Classes". The Python Tutorial. Python Software Foundation. 喺20 February 2012搵到. It is a mixture of the class mechanisms found in C++ and Modula-3
  16. Lundh, Fredrik. "Call By Object". effbot.org. 原著喺2019年11月23號歸檔. 喺21 November 2017搵到. replace "CLU" with "Python", "record" with "instance", and "procedure" with "function or method", and you get a pretty accurate description of Python's object model.
  17. Simionato, Michele. ses/2.3/mro/ "The Python 2.3 Method Resolution Order" Check |url= value (help). Python Software Foundation. The C3 method itself has nothing to do with Python, since it was invented by people working on Dylan and it is described in a paper intended for lispers
  18. Kuchling, A. M. "Functional Programming HOWTO". Python v2.7.2 documentation. Python Software Foundation. 喺9 February 2012搵到.
  19. Schemenauer, Neil; Peters, Tim; Hetland, Magnus Lie (18 May 2001). "PEP 255 – Simple Generators". Python Enhancement Proposals. Python Software Foundation. 喺9 February 2012搵到.
  20. Smith, Kevin D.; Jewett, Jim J.; Montanaro, Skip; Baxter, Anthony (2 September 2004). 18/ "PEP 318 – Decorators for Functions and Methods" Check |url= value (help). Python Enhancement Proposals. Python Software Foundation. 喺24 February 2012搵到.
  21. "More Control Flow Tools". Python 3 documentation. Python Software Foundation. 喺24 July 2015搵到.
  22. "CoffeeScript". coffeescript.org.
  23. "The Genie Programming Language Tutorial". 喺28 February 2020搵到.
  24. "Perl and Python influences in JavaScript". www.2ality.com. 24 February 2013. 喺15 May 2015搵到.
  25. Rauschmayer, Axel. "Chapter 3: The Nature of JavaScript; Influences". O'Reilly, Speaking JavaScript. 喺15 May 2015搵到.
  26. 引用錯誤 無效嘅<ref>標籤;無文字提供畀叫做Julia嘅參照
  27. Ring Team (4 December 2017). "Ring and other languages". ring-lang.net. ring-lang.
  28. Bini, Ola (2007). Practical JRuby on Rails Web 2.0 Projects: bringing Ruby on Rails to the Java platform. Berkeley: APress. p. 3. ISBN 978-1-59059-881-8.
  29. Lattner, Chris (3 June 2014). "Chris Lattner's Homepage". Chris Lattner. 喺3 June 2014搵到. The Swift la nguage is the product of tireless effort from a team of language experts, documentation gurus, compiler optimization ninjas, and an incredibly important internal dogfooding group who provided feedback to help refine and battle-test ideas. Of course, it also greatly benefited from the experiences hard-won by many other languages in the field, drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list.
  30. "The History of Python: A Brief Timeline of Python". Blogger. 2009-01-20. 喺2016-03-20搵到.
  31. "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."
  32. Kuhlman, Dave. "A Python Book: Beginning Python, Advanced Python, and Python Exercises". Section 1.1.

參考[編輯]