Python

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

Python英國發音:/ˈpaɪθən/ 美國發音:/ˈpaɪθɑːn/),係一種直譯高階程式語言,由荷蘭科研機構 CWI(數學及電腦科學中心)嘅 Guido van Rossum 設計,1991年首次推出[29][30],特徵係啲編碼嘅可讀性高、空格有語義(所謂越位法則)、支援多種嘅編程範式(包括結構化編程物件導向編程)、而且有個全面嘅標準庫[31]。因為呢啲緣故,Python 喺廿一世紀初俾人廣泛採用嚟做好多種唔同嘅編程作業,例如係編寫機械學習程式同電子遊戲程式[32]。喺工程學科學界常用;喺財經界亦都成為咗考CFA牌可以考嘅嘢[33]

語法[編輯]

縮進[編輯]

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

關鍵字[編輯]

Python 有35個關鍵字(又叫保留字,即係唔可以當識別碼用):

  • 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.12.2 and 3.11.8 are now available" (英文). 2024年2月7號. 喺2024年2月7號搵到.
  3. "Why is Python a dynamic language and also a strongly typed language - Python Wiki". wiki.python.org. 喺2021-01-27搵到.
  4. "PEP 483 -- The Theory of Type Hints". Python.org. 原先內容歸檔喺2020-06-14. 喺2020-04-22搵到.
  5. File extension .pyo was removed in Python 3.5. See PEP 0488 互聯網檔案館歸檔,歸檔日期2020年6月1號,.
  6. Holth, Moore (2014-03-30). "PEP 0441 -- Improving Python ZIP Application Support". 原先內容歸檔喺2018-12-26. 喺2015-11-12搵到.
  7. "RustPython".
  8. "Starlark Language". 喺25 May 2019搵到.
  9. 引用錯誤 無效嘅<ref>標籤;無文字提供畀叫做faq-created嘅參照
  10. "Ada 83 Reference Manual (raise statement)".
  11. Kuchling, Andrew M. (22 December 2006). "Interview with Guido van Rossum (July 1998)". amk.ca. 原著喺1 May 2007歸檔. 喺12 March 2012搵到.
  12. 12.0 12.1 "itertools — Functions creating iterators for efficient looping — Python 3.7.1 documentation". docs.python.org.
  13. van Rossum, Guido (1993). 1&type=pdf "An Introduction to Python for UNIX/C Programmers". 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. {{cite journal}}: Check |url= value (help)
  14. 14.0 14.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
  15. 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.
  16. Simionato, Michele. ses/2.3/mro/ "The Python 2.3 Method Resolution Order". 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 {{cite web}}: Check |url= value (help)CS1 maint: url-status (link)
  17. Kuchling, A. M. "Functional Programming HOWTO". Python v2.7.2 documentation. Python Software Foundation. 喺9 February 2012搵到.
  18. Schemenauer, Neil; Peters, Tim; Hetland, Magnus Lie (18 May 2001). "PEP 255 – Simple Generators". Python Enhancement Proposals. Python Software Foundation. 喺9 February 2012搵到. {{cite web}}: no-break space character in |title= at position 8 (help)
  19. Smith, Kevin D.; Jewett, Jim J.; Montanaro, Skip; Baxter, Anthony (2 September 2004). 18/ "PEP 318 – Decorators for Functions and Methods". Python Enhancement Proposals. Python Software Foundation. 喺24 February 2012搵到. {{cite web}}: Check |url= value (help); no-break space character in |title= at position 8 (help)
  20. "More Control Flow Tools". Python 3 documentation. Python Software Foundation. 喺24 July 2015搵到.
  21. "CoffeeScript". coffeescript.org.
  22. "The Genie Programming Language Tutorial". 喺28 February 2020搵到.
  23. "Perl and Python influences in JavaScript". www.2ality.com. 24 February 2013. 喺15 May 2015搵到.
  24. Rauschmayer, Axel. "Chapter 3: The Nature of JavaScript; Influences". O'Reilly, Speaking JavaScript. 原著喺2018年12月26號歸檔. 喺15 May 2015搵到.
  25. 引用錯誤 無效嘅<ref>標籤;無文字提供畀叫做Julia嘅參照
  26. Ring Team (4 December 2017). "Ring and other languages". ring-lang.net. ring-lang.
  27. 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.
  28. 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.
  29. "History and License — Python 3.11.2 documentation" (英文). Python Software Foundation. 喺2023年2月20號搵到.
  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.
  33. 世澤 (2023年3月19號). 士政府嘗試迫UBS同Credit Suisse合併 投資銀行部分爛到無朋友邊個會接手 大行黃金時代已經過去 古怪事情將會陸續有嚟. . 時間 22:37. 喺2023年3月19號搵到.

參考[編輯]