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

2020-12-22 老王說侃

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

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

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

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

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

Lists also support operations like concatenation:

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

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

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

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()」方法在列表的尾部添加新元素(我們在之後會看到更多方法)

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

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

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

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

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

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

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編寫解決下面的斐波那契數列問題。

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

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

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

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簡介之字符串

Python3.9官方文檔翻譯版python簡介之數字

Python3.9官方文檔翻譯版之篇首語

Python3.9官方文檔翻譯版之摘要部分

相關焦點

  • 慢步python,你苦苦找尋的python中文使用手冊在哪裡?這裡有答案
    #學習難度大python對大家來說,應該算是相對新的程式語言。從裡面我們學會怎麼連接線材,怎麼開機,怎麼設置……python使用手冊,這是我們急需的存在。遇到哪裡不懂就查哪裡。python使用手冊使用手冊都是官方出版的。所以我們要到python的官方去找。
  • Python自動化辦公(內容)
    、列數、坐標7)獲取一系列格子3、python如何向excel中寫入某些內容?1、python-docx庫介紹2、Python讀取Word文檔內容1)word文檔結構介紹2)python-docx提取文字和文字塊兒3)利用Python向Word文檔寫入內容
  • python-docx段落設置
    筆者將從段落的對齊方式、縮進、間距等3個部分進行敘述,最後製作了本篇文章的思維導圖。01段落的對齊方式在python-docx包中對WORD文檔段落對齊方式的設置主要用到了paragraph. alignmen
  • Python 30 個技巧
    存儲列表元素到新的變量中我們可以使用列表來初始化多個變量,在解析列表時,變量的數目不應該超過列表中的元素個數:【譯者註:元素個數與列表長度應該嚴格相同,不然會報錯】testList = [1,2,3]x, y, z = testList print(x, y, z) #-> 1 2 3
  • 「python學習手冊-筆記」003.數值類型
    003.數值類型本系列文章是我個人學習《python學習手冊(第五版)》的學習筆記,其中大部分內容為該書的總結和個人理解,小部分內容為相關知識點的擴展。非商業用途轉載請註明作者和出處;商業用途請聯繫本人(gaoyang1019@hotmail.com)獲取許可。
  • Python2 已終結,入手Python 3,你需要這30個技巧
    機器之心選自medium作者:Erik-Jan van Baaren機器之心編譯參與:王子嘉、一鳴Python2 在今年和我們說拜拜了,Python3 有哪些有趣而又實用的技巧呢
  • 四種高性能數據類型,Python collections助你優化代碼、簡潔任務
    Counter官方文檔:https://docs.python.org/2/library/collections.html#collections.CounterCounter 是 dictionary 對象的子類。
  • Python字符串函數用法大全
    即:返回用於str.translate()函數翻譯的的轉換表。xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6
  • 如何自學成 Python 大神?這裡有些建議
    同時需要對 Python 中的列表、元組、集合和字典( https://coolpythoncodes.com/python-dictionary ) 等不同的數據結構足夠熟悉,能夠理解循環和列表。O'Reilly 出版的《Learning Python 第 5 版》和 Google 的 Python 在線教程( https://developers.google.com/edu/python/introduction?csw=1 ) 可以幫助你對 Python 基礎進行足夠的了解。在看書的同時,你還應該跟隨你所學的教程進行編程練習。
  • Python實戰 | 只需 「4步」 入門網絡爬蟲
    Beautiful Soup庫是解析、遍歷、維護「標籤樹」的功能庫,對應一個HTML/XML文檔的全部內容。JSON在python中分別由list和dict組成。Python官方json網址是  https://docs.python.org/3/library/json.html?
  • PythonGuru 中文系列教程·翻譯完成
    在線閱讀 ApacheCN 學習資源 目錄 初級 Python python 入門 安裝 Python3 運行 python 程序 數據類型和變量 Python 數字 Python 字符串 Python 列表 Python 字典 Python 元組 數據類型轉換 Python 控制語句 Python 函數 Python 循環 Python 數學函數 Python
  • 聊聊python 辦公自動化之 Word(中)
    作者:星安果 來源:AirPython(公眾號)上一篇文章,對 Word 寫入數據的一些常見操作進行了總結,詳情請看聊聊python 辦公自動化之 Word(上)。相比寫入數據,讀取數據同樣很實用!本篇文章,將談談如何全面讀取一個 Word 文檔中的數據,並會指出一些要注意的點。
  • 【必讀】Python的22個編程技巧
    下面的語句是說「如果 y 是 9,給 x 賦值 10,不然賦值為 20」。如果需要的話我們也可以延長這條操作鏈。,在解析列表時,變量的數目不應該超過列表中的元素個數:【譯者註:元素個數與列表長度應該嚴格相同,不然會報錯】testList= [1,2,3]x,y,z= testListprint(x,y,z)#-> 1 2 3如果你想知道引用到代碼中模塊的絕對路徑,可以使用下面的技巧:import threadingimport socketprint(threading
  • Python中的雙端隊列:collections.deque
    關於deque起因是我想做一個「手氣不錯」的功能,為了提高性能,打算用隊列實現,偶然在Stack Overflow看到一個討論「Efficiency of using a Python list as a queue」python
  • Python 初學者進階的九大技能
    如果想要文件名中的文件擴展名,很容易假設你需要的是最後3個字母。你也可以使用標準庫 `os.path.splitext `,點擊這裡查看:os.path.splitext:https://www.geeksforgeeks.org/python-os-path-splitext-method/。3.
  • Python 程序報錯崩潰後,如何倒回到崩潰的位置?
    一直讀到Redis 列表為空。我們運行一下看看:報錯了,說明Redis 中的某一條數據有問題。你想看看這條有問題的數據,但是現在程序已經崩潰了,進程結束了,這條有問題的數據也就永久丟失了。你再也不可能知道它長什麼樣了。
  • 大神總結的22個Python編程小技巧,請收下!
    存儲列表元素到新的變量中我們可以使用列表來初始化多個變量,在解析列表時,變量的數目不應該超過列表中的元素個數:【譯者註:元素個數與列表長度應該嚴格相同,不然會報錯】testList= [1,2,3]x,y,z= testListprint(x,y,z)#-> 1 2 3
  • 小叮噹機器學習:Python3.6配置TensorFlow的GPU版詳細安裝教程
    然而網上的大多數都是圍繞python2.7版本的tensorflow教程,下面我們就來看看在Centos7 +pyton3.6的環境下,我們怎麼安裝使用這個功能強大的開源庫的GPU版本。Step1.環境確認想要使用GPU版的TesnorFlow來加速我們的神經網絡運算,首先要確保,我們的GPU依賴環境已經搭好。
  • 重磅:包郵寄送《Python數據可視化之美》
    《python數據可視化之美》主要介紹如何使用python中的matplotlib、seaborn、plotnine、basemap等包繪製專業圖表。本書先介紹了python語言編程基礎知識,以及使用numpy和pandas兩個包的數據操作方法;再對比了matplotlib、seaborn和plotnine三個包的圖形語法。
  • 這些Python代碼技巧,你肯定還不知道
    列表推導式(List comprehensions)我最喜歡 Python 編程的原因之一是它的列表推導式(https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions)。