Python
(由Python module跳轉過嚟)
提示:呢篇文講嘅唔係蟒蛇。
Python(英國發音:/ˈpaɪθən/ 美國發音:/ˈpaɪθɑːn/),係一種直譯高階程式語言,由荷蘭科研機構 CWI(數學及電腦科學中心)嘅 Guido van Rossum 設計,1991年首次推出[29][30],特徵係啲編碼嘅可讀性高、空格有語義(所謂越位法則)、支援多種嘅編程範式(包括結構化編程同物件導向編程)、而且有個全面嘅標準庫[31]。因為呢啲緣故,Python 喺廿一世紀初俾人廣泛採用嚟做好多種唔同嘅編程作業,例如係編寫機械學習程式同電子遊戲程式[32]。喺工程學同科學界常用;喺財經界亦都成為咗考CFA牌可以考嘅嘢[33]。
編程範式 | 多範型:函數式、指令式、反射式、結構化、物件導向 |
---|---|
設計者 | Guido van Rossum |
編程員 | Python軟件基金會 |
第一次出現 | 1991年[1] |
穩定版本 | 3.13.0[2]
/ 2024年10月7號,44日之前 |
類型系統 | 鴨子類型、動態、強類型[3]、漸進(自從3.5)[4] |
操作系統 | 跨平臺:Windows、Linux/Unix、macOS等 |
軟件授權 | Python軟件基金會許可證 |
副檔名 | .py、.pyi、.pyc、.pyd、.pyo(3.5之前)[5]、Template:Notatypo、.pyz(自從3.5)[6] |
網站 | www |
Major 實作版本 | |
CPython、PyPy、IronPython、Jython、Stackless Python、MicroPython、CircuitPython、RustPython[7] | |
衍生變體 | |
Cython、RPython、Starlark[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]、Perl、Standard ML[12] | |
影響語言 | |
Boo、Cobra、CoffeeScript[21]、D、F#、Falcon、Genie[22]、Go、Groovy、JavaScript[23][24]、Julia[25]、Nim、Ring[26]、Ruby[27]、Swift[28] | |
語法
編輯縮進
編輯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)
睇埋
編輯攷
編輯- ↑ 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.
- ↑ "Python 3.13 released" (英文). 2024年10月7號. 喺2024年10月7號搵到.
- ↑ "Why is Python a dynamic language and also a strongly typed language - Python Wiki". wiki.python.org. 喺2021-01-27搵到.
- ↑ "PEP 483 -- The Theory of Type Hints". Python.org. 原先內容歸檔喺2020-06-14. 喺2020-04-22搵到.
- ↑ File extension .pyo was removed in Python 3.5. See PEP 0488 互聯網檔案館嘅歸檔,歸檔日期2020年6月1號,.
- ↑ Holth, Moore (2014-03-30). "PEP 0441 -- Improving Python ZIP Application Support". 原先內容歸檔喺2018-12-26. 喺2015-11-12搵到.
- ↑ "RustPython".
- ↑ "Starlark Language". 喺25 May 2019搵到.
- ↑ 引用錯誤 無效嘅
<ref>
標籤;無文字提供畀叫做faq-created
嘅參照 - ↑ "Ada 83 Reference Manual (raise statement)". 原著喺2019年10月22號歸檔. 喺2022年4月5號搵到.
- ↑ Kuchling, Andrew M. (22 December 2006). "Interview with Guido van Rossum (July 1998)". amk.ca. 原著喺1 May 2007歸檔. 喺12 March 2012搵到.
- ↑ 12.0 12.1 "itertools — Functions creating iterators for efficient looping — Python 3.7.1 documentation". docs.python.org.
- ↑ 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.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
- ↑ 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.
- ↑ 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) - ↑ Kuchling, A. M. "Functional Programming HOWTO". Python v2.7.2 documentation. Python Software Foundation. 喺9 February 2012搵到.
- ↑ Schemenauer, Neil; Peters, Tim; Hetland, Magnus Lie (18 May 2001). "PEP 255 – Simple Generators". Python Enhancement Proposals. Python Software Foundation. 喺9 February 2012搵到.
- ↑ 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) - ↑ "More Control Flow Tools". Python 3 documentation. Python Software Foundation. 喺24 July 2015搵到.
- ↑ "CoffeeScript". coffeescript.org.
- ↑ "The Genie Programming Language Tutorial". 喺28 February 2020搵到.
- ↑ "Perl and Python influences in JavaScript". www.2ality.com. 24 February 2013. 喺15 May 2015搵到.
- ↑ Rauschmayer, Axel. "Chapter 3: The Nature of JavaScript; Influences". O'Reilly, Speaking JavaScript. 原著喺2018年12月26號歸檔. 喺15 May 2015搵到.
- ↑ 引用錯誤 無效嘅
<ref>
標籤;無文字提供畀叫做Julia
嘅參照 - ↑ Ring Team (4 December 2017). "Ring and other languages". ring-lang.net. ring-lang. 原著喺2018年12月25號歸檔. 喺2022年4月5號搵到.
- ↑ 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.
- ↑ 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.
- ↑ "History and License — Python 3.11.2 documentation" (英文). Python Software Foundation. 喺2023年2月20號搵到.
- ↑ "The History of Python: A Brief Timeline of Python". Blogger. 2009-01-20. 喺2016-03-20搵到.
- ↑ "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."
- ↑ Kuhlman, Dave. "A Python Book: Beginning Python, Advanced Python, and Python Exercises". Section 1.1.
- ↑ 世澤 (2023年3月19號)。《瑞士政府嘗試迫UBS同Credit Suisse合併 投資銀行部分爛到無朋友邊個會接手 大行黃金時代已經過去 古怪事情將會陸續有嚟》。《黃世澤幾分鐘評論》。時間 22:37。喺2023年3月19號搵到。
參考
編輯- 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.
- 免費 Python 網上廣東話教學 (由香港浸會大學提供)