Python3.9官方文檔翻譯版python簡介之列表

Lists

自譯:列表

Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.

自譯:Python有很多複雜的數據類型,可以被用作將其他值組合在一起。最常用的是列表,它的語法為用方括號將數據組合,中間使用英文逗號分隔,列表可以包含不同類型的項,但通常項為相同類型。

Python3.9官方文檔翻譯版python簡介之列表

Like strings (and all other built-in sequence types), lists can be indexed and sliced:

自譯:同字符串一樣(和其他的內建序列類型),列表可以使用索引和切片:

Python3.9官方文檔翻譯版python簡介之列表

All slice operations return a new list containing the requested elements. This means that the following slice returns a shallow copy of the list:

自譯:所有的切片操作會返回一個包含請求元素的新列表。這意味著輸出的切片返回的是列表的淺複製。

Python3.9官方文檔翻譯版python簡介之列表

Lists also support operations like concatenation:

自譯:列表還支持連接操作:

Python3.9官方文檔翻譯版python簡介之列表

Unlike strings, which are immutable, lists are a mutable type, i.e. it is possible to change their content:

自譯:與字符串(不可改變)不同的是,列表是可變類型。即可以改變它們的內容:

Python3.9官方文檔翻譯版python簡介之列表

You can also add new items at the end of the list, by using the append() method (we will see more about methods later):

自譯:你可以使用"append()"方法在列表的尾部添加新元素(我們在之後會看到更多方法)

Python3.9官方文檔翻譯版python簡介之列表

Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:

自譯:也可以對切片進行賦值,這甚至可以改變列表的大小或者把列表的元素清除了:

Python3.9官方文檔翻譯版python簡介之列表

The built-in function len() also applies to lists:

自譯:內置函數"len()"也適用於列表

Python3.9官方文檔翻譯版python簡介之列表

It is possible to nest lists (create lists containing other lists), for example:

自譯:列表內可以將列表作為一個元素內嵌使用,例如:

Python3.9官方文檔翻譯版python簡介之列表

First Steps Towards Programming

自譯:編程的第一步

Of course, we can use Python for more complicated tasks than adding two and two together. For instance, we can write an initial sub-sequence of the Fibonacci series as follows:

自譯:當然,我們可以使用python完成難度遠遠大於"2+2"這種任務。例如,我們可以使用python編寫解決下面的斐波那契數列問題。

Python3.9官方文檔翻譯版python簡介之列表

This example introduces several new features.

自譯:這個例子可以說明一下幾個新的特徵。


The first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.

自譯:"a,b = 0,1"第一行包含了多重賦值:變量a和b同時獲得新值0和1.在最後一行"a,b = b,a+b"也同樣使用了這種用法,在計算表達式時,先計算右側的求值"b,a+b",在進行賦值"a,b",右側的求值也是由左至右的順序進行。


The while loop executes as long as the condition (here: a < 10) remains true. In Python, like in C, any non-zero integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false. The test used in the example is a simple comparison. The standard comparison operators are written the same as in C: < (less than), > (greater than), == (equal to), <= (less than or equal to), >= (greater than or equal to) and != (not equal to).

自譯:只要條件為"真"(這裡是"a<10")時,while循環就會一直運行。在python中像C一樣,任何非零數都為真。零是假。條件可以是字符串或列表值,實際上可以是任何序列;任何非零數的長度都是真,空序列為假。示例中的例子是一個簡單的比較。標準的寫法和C語言相同:""大於,"=="等於,"<="小於等於,">="大於等於和"!="不等於。

The body of the loop is indented: indentation is Python's way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.

自譯:循環的主體內容是縮進的:縮進是python對語句進行分組的方式。在交互提示中,你必須使用空格或製表符對每一個縮進行進行操作,實際上你需要使用一個文本編輯器對python進行更復雜的輸入操作。所有優秀的文本編輯器都有自動縮進功能。在交互界面輸入一個複合語句時,你必須使用一個空行已提示操作完成(因為解析器不能判定你輸入的哪一行為最後一行)。需要注意的是在基本模塊中每一行都有相同的縮進。


The print() function writes the value of the argument(s) it is given. It differs from just writing the expression you want to write (as we did earlier in the calculator examples) in the way it handles multiple arguments, floating point quantities, and strings. Strings are printed without quotes, and a space is inserted between items, so you can format things nicely, like this:

自譯:"print"功能是顯示給定的參數值。不同於你想寫表達式的是它可以處理多個把參數、浮點運算和字符串(像我們早期縮寫的計算器的例子一樣)。字符串不帶引號打印輸出,兩個不同元素間用空格連接,這樣你可以很好的設置格式,像下面的例子那樣:

Python3.9官方文檔翻譯版python簡介之列表

The keyword argument end can be used to avoid the newline after the output, or end the output with a different string:

自譯:關鍵詞參數"end"可以被用作在輸出後避免新航的出現,或者用不同的字符串結尾。

Python3.9官方文檔翻譯版python簡介之列表


Footnotes

自譯:腳註

[1] Since ** has higher precedence than -, -3**2 will be interpreted as -(3**2) and thus result in -9. To avoid this and get 9, you can use (-3)**2.

自譯:由於"**"的優先級高於"-",所以"-3**2"的運算結果為"-9"," (-3)**2"的運算結果為"9"。

[2] Unlike other languages, special characters such as \\n have the same meaning with both single ('...') and double ("...") quotes. The only difference between the two is that within single quotes you don't need to escape " (but you have to escape \\') and vice versa.

自譯:與其他語言不同,單引號''和雙引號""具有相同含義,兩者的區別點只是如果包含字符串中包含單引號'那麼你不得不使用雙引號"(否則你只能使用轉移符\\),反之亦然。


Python3.9官方文檔翻譯版python簡介之列表


分享到:


相關文章: