- Published on
白帽駭客工具實作03 - [基礎知識]Python版本演進、特點、撰寫風格及基礎語法
- Authors
- Name
- Ed Li
- X
Guido van Rossum
Python之父Guido van Rossum
Python’s Creator:Python版本演進
Evolution of Python Versions
項次 | 版本 | 說明 |
---|---|---|
1 | Python 1.0 | 1994 年發布: Python 1.0 正式版在 1994 年 1 月發布,標誌著 Python 作為一種正式的編程語言開始被廣泛使用。 |
2 | Python 2.0 | 2000 年發布: Python 2.0 於 2000 年 10 月發布,這個版本引入記憶體垃圾回收機制和更好的 Unicode 支持等新特性。 |
3 | Python 3.0 | 2008 年發布: Python 3.0 於 2008 年 12 月發布,這是一個不向後兼容的版本,主要修復 Python 2.x 的一些設計缺陷。Python 3.0 引入了許多改進,如統一的字符串類型、改進的整數除法、函式注釋和更多內置函式。過渡期: Python 2.x 和 3.x 系列曾經並行開發和使用了很長一段時間,幫助使用者從 Python 2 過渡到 Python 3。Python 2在2020 年 1 月 1 日正式停止支持。 |
No. | Version | Description |
---|---|---|
1 | Python 1.0 | Released in 1994: Python 1.0 was officially released in January 1994, marking the beginning of Python as a widely used programming language. |
2 | Python 2.0 | Released in 2000: Python 2.0 was released in October 2000, introducing new features such as memory garbage collection and improved Unicode support. |
3 | Python 3.0 | Released in 2008: Python 3.0 was released in December 2008. This version is not backward compatible and mainly addresses some of the design flaws in Python 2.x. Python 3.0 introduced many improvements, such as a unified string type, improved integer division, function annotations, and more built-in functions. Transition period: The Python 2.x and 3.x series were developed and used in parallel for a long time to help users transition from Python 2 to Python 3. Python 2 officially ended support on January 1, 2020. |
Python特點
項次 | 特點 | 說明 |
---|---|---|
1 | 簡潔和可讀性 | Python 語法設計簡潔明了,強調代碼的可讀性,使得程式開發人員可以用更少的程式碼表達更多的功能。 |
2 | 廣泛的應用領域 | Python 被廣泛應用於網絡開發、數據科學、人工智能、機器學習、自動化批次檔、系統管理、科學計算等多個領域。 |
3 | 龐大的標準庫和生態系統 | Python 提供了豐富的標準庫,涵蓋了網絡通信、文件 I/O、數學運算等多個方面。Python 社群還提供了大量的第三方庫和框架,如 Django、Flask、NumPy、Pandas、TensorFlow 等。 |
4 | 跨平台性 | Python 可以在多種操作系統上運行,包括 Windows、macOS、Linux 等,具備跨平台特性。 |
No. | Feature | Description |
---|---|---|
1 | Simplicity and Readability | Python's syntax is designed to be simple and clear, emphasizing code readability, allowing developers to express more functionality with less code. |
2 | Wide Range of Applications | Python is widely used in web development, data science, artificial intelligence, machine learning, automation scripting, system administration, scientific computing, and many other fields. |
3 | Extensive Standard Library and Ecosystem | Python offers a rich standard library that covers many areas such as network communication, file I/O, and mathematical operations. The Python community also provides numerous third-party libraries and frameworks like Django, Flask, NumPy, Pandas, TensorFlow, and more. |
4 | Cross-Platform Capability | Python can run on various operating systems, including Windows, macOS, and Linux, making it a cross-platform language. |
Python撰寫風格PEP8
Python Coding Style: PEP8
在開始說明PEP8前,先說明一下什麼是PEP。PEP(Python Enhancement Proposal,Python 增強提案)用來規範與定義Python的各種強化、功能延伸等。PEP8為Python社群共通的開發風格準則,目的提供開發者寫出一致性的程式碼。很多開源的程式碼如Django等,也是以PEP8作為基準,再添加新風格來進行開發。 Before explaining PEP8, let’s first clarify what PEP is. PEP (Python Enhancement Proposal) is used to standardize and define various enhancements, feature extensions, and other aspects of Python. PEP8 is a common development style guide within the Python community, aimed at helping developers write consistent code. Many open-source projects, such as Django, use PEP8 as a base standard and then add their own styles for development.
- 命名規範:
- Naming Conventions:
項次 | 名稱 | 內容 |
---|---|---|
1 | 類別名稱 | 駝峰命名法 |
2 | 模組名稱 | 以短名稱命名、全小寫的名稱;若為提高可讀性,可在模組名稱中使用底線 _ |
3 | 套件名稱 | 以短名稱命名、全小寫的名稱 |
4 | 函式名稱 | 小寫,若有多個單字時,可以使用底線分隔單字以提高可讀性 |
5 | 變數名稱 | 與函式名稱遵循相同規定 |
6 | 常數名稱 | 常數通常在模組層級定義,並全部使用大寫字母書寫,可用下底線分隔單字。例如: MAX_AGE 和 TOTAL |
No. | Name | Description |
---|---|---|
1 | Class Name | CamelCase naming convention |
2 | Module Name | Short, all-lowercase names; underscores (_) can be used to improve readability if necessary |
3 | Package Name | Short, all-lowercase names |
4 | Function Name | Lowercase; underscores can be used to separate words for readability if there are multiple words |
5 | Variable Name | Follows the same rules as function names |
6 | Constant Name | Constants are usually defined at the module level and written in all-uppercase letters with words separated by underscores, e.g., MAX_AGE and TOTAL |
- 縮排:使用4個空格。
- Indentation: Use 4 spaces.
def test_function():
if True:
print("Hello, edli_01!")
行長度:最大為80個字符後換行。
Line Length: The maximum line length is 80 characters before wrapping.
匯入:
Imports:
#錯誤版本 / Incorrect Version
import os, sys
#正確版本 / Correct Version
import os
import sys
from math import sqrt, pi
程式碼之編碼格式:UTF-8。
Code encoding format: UTF-8.
空行:函式間應有2個空行。
Blank lines: There should be 2 blank lines between functions.
def edli_01():
pass
#空行01 / Blank line 01
#空行02 / Blank line 02
def edli_02():
pass
- 單行文件字串:簡單的函式,可以使用單行文件字串。單行文件字串包含在一對三引號,於開始到結束的引號應在同一行內。
- Single-line docstring: Simple functions can use single-line docstrings. Single-line docstrings are enclosed within a pair of triple quotes, and both the opening and closing quotes should be on the same line.
def read_edli_01_message():
"""回傳「閱讀完畢」字串。"""
"""Returns a string “Reading completed."""
return finish
- 多行文件字串:較複雜的函式、類或模塊等,應使用多行文列字串。多行文件字串的第一行為簡短總結,第二行為空,第三行開始為參數等資訊,詳見下面範例。
- Multi-line docstring: More complex functions, classes, or modules should use multi-line docstrings. The first line of a multi-line docstring is a brief summary, followed by a blank line. Starting from the third line, additional information such as parameters is provided, as shown in the example below.
def read_edli_01_articles(want):
"""
這是一個確認讀者是否想閱讀edli_01文章。
參數:
want (Boolean): 第一個布林值。
回傳:
Boolean: 回傳Ture or False。
"""
"""
This is to confirm whether the reader wants to read the edli_01 article.
Parameters:
want (Boolean): The first boolean value.
Returns:
Boolean: Returns True or False.
"""
return True
- 更詳細資訊請見PEP 8 – Style Guide for Python Code
- For more detailed information, please refer to PEP 8 – Style Guide for Python Code
Python基礎語法
Python官網-Python教學 Python official website - Python tutorial
參考資訊
PEP 8 – Style Guide for Python Code