<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python 數字和字符串 > 原文: [https://javabeginnerstutorial.com/python-tutorial/python-numbers-and-strings-2/](https://javabeginnerstutorial.com/python-tutorial/python-numbers-and-strings-2/) 在本文中,我們將介紹 Python 3 數字和字符串,因為它們是使用 Python 開發的基礎。 ## Python 3 數字 您可能已經想到,數字數據類型存儲數字值,例如整數和浮點數。 盡管您應該了解數字的另一種類型:*復數*。 讓我們簡要看一下這三種數字。 * 整數(也稱為整數)是沒有小數點的整數,可以包含正數或負數。 只要您希望提供良好的計算可能性,整數就可以。 * 浮點數是浮點數,這意味著即使它們是整數,它們也包含一個小數點。 它們涵蓋了一組實數,因此您可以對其進行任何操作。 * 大多數情況下,復數出于科學目的。 但是很高興知道還有更多東西。 您可能從數學上知道復數表示為`+ bi`,其中`i`是表示 -1 的平方根的虛部。 **但是在 Python** 中,虛部表示為`j`,因此,如果要編寫復數,則應執行:`c = 42 + 26j`。 ### 特殊數字 您也可以在 Python 中使用兩個常用的數字(至少在數學上是如此)。 這些數字是`pi`和`e`。 要使用它們,您必須導入數學模塊: ```py >>> import math >>> math.pi 3.141592653589793 >>> math.e 2.718281828459045 ``` ### 隨機數 在一些書籍或教程的幫助下開始開發時,您會遇到隨機數生成的問題。 稍后我們將使用隨機數。 但是,現在是使用 Python 引入隨機數生成的好時機。 為此,我們必須導入一個模塊:`random`。 ```py >>> import random >>> random.choice((1,2,3,4,'a')) # chooses one item of the provided list, tuple or string (it has to be an idexable object) 2 >>> random.random() # returns a random floating point number which is greater or equal to `0` and less than `1` 0.17489302862620337 >>> l = [1,2,3,4,5] >>> >>> random.shuffle(l) # shuffles the list in place, returns `None` >>> l [1, 4, 3, 2, 5] >>> random.randrange(1,10) # selects a random integer between the range of the first and the second parameter 4 >>> random.randrange(1,10,2) # the third parameter is an optional integer, defines the step in the range to select the elements from 5 ``` ## 字符串 字符串在 Python 中定義為引號之間表示的字符列表。 我們可以使用單引號(`'`或單引號)或雙引號(`"`)來表示字符串。 我們可以在一個字符串中使用另一個字符串,但是如果您使用單引號將字符串開頭,則也必須以該字符串結尾。 這些字符串是單行字符串,這意味著您無法將它們分散在多行中,這會導致錯誤。 但是,在討論注釋時,我們討論了以三個引號開頭的字符串(再次使用單引號或雙引號也沒有關系)。 它們可以通過多條線傳播。 但是,它們會在最后轉換為常規字符串,并且在寫文本時僅在換行符(`\n`)處按回車符。 這種字符串最常見的用法是文檔。 當我們到達函數和類時,我們將討論這個主題。 讓我們舉個簡單的示例: ```py >>> "this is a File "<stdin>", line 1 "this is a ^ SyntaxError: EOL while scanning string literal >>> 'this is line " File "<stdin>", line 1 # Starting and ending of String should be done with same type of quote 'this is line " ^ SyntaxError: EOL while scanning string literal >>> 'this will " work " without any isue' 'this will " work " without any isue' >>> ''' this is a ... string''' ' this is a\nstring' ``` ### 訪問和更新字符串 Python 沒有*字符*的顯式類型,它們被視為長度為 1 的字符串。 訪問字符串與訪問任何其他變量相同。 不能就地更新字符串,因為字符串在 Python 中是不可變的類型。 如果您“更新”字符串的值,則僅使用新字符串重新分配該值。 ```py >>> s1 = "Hello World!" >>> id(s1) 4318883504 >>> s1 = "Hello Python!" >>> id(s1) 4318903344 >>> id('Hello World!') 4318883504 ``` ### 特殊字符串運算符 因為字符串是 Python 中的類型,被視為某種列表,所以字符串具有特殊的運算符,我們可以將其用于開發目的。 讓我們用示例來看看它們。 在下一個小節中將對切片進行詳細說明。 * `+`符號將字符串連接起來 * `*`符號創建字符串的多個副本 * `[int]`將字符串中給定位置(`int`)的字符切片 * `[int1:int2]`它獲取給定范圍內的字符(`int1 – int2`) * `[::int]`獲取給定范圍之間的字符,以及字符之間的步長(`int`) * `in`判斷字符串中是否存在子字符串 * `not in`判斷字符串中是否不存在子字符串 ```py >>> 'Hello' + ' ' + "Python" # the + sign concatenates the strings 'Hello Python' >>> 'Hello'+'!' * 5 # the * sign creates multiple copies of the string 'Hello!!!!!' >>> "Python"[2] # slices the character at the given position out of the string 't' >>> "Hello World!"[0:5] # gets the characters between the given range 'Hello' >>> 'Hello World!'[::2] # gets the characters between the given range with the amount of steps between the characters 'HloWrd' >>> 'llo' in "Hello World!" # looks up if a substring is present in the string True >>> "hell" not in 'Hello World!' # looks if a substring is not present in the string True ``` ### 三引號 我已經提到了我對三重引號的看法:它們是多行字符串而不是注釋。 多行字符串表示它們與單行(或普通)字符串的打印方式相同,但保留其顯式的新行并將特殊的轉義字符轉換為其打印版本。 ```py >>> spam = """Spam, Spam, Spam, lovely Spam ... Wonderful Spam, Lovely Spam. ... Spam, Spam, Spam, magnificent Spam, ... \tSuperlative Spam. ... Spam, Spam, Spam, wonderous Spam,\nSurgical Spam, splendiferous Spam. ... Eggs, Bacon, sausage!\rSpam, Spam, Spam, Spaaam!""" >>> spam 'Spam, Spam, Spam, lovely Spam\nWonderful Spam, Lovely Spam.\nSpam, Spam, Spam, magnificent Spam,\n\tSuperlative Spam.\nSpam, Spam, Spam, wonderous Spam,\nSurgical Spam, splendiferous Spam.\nEggs, Bacon, sausage!\rSpam, Spam, Spam, Spaaam!' >>> print(spam) Spam, Spam, Spam, lovely Spam Wonderful Spam, Lovely Spam. Spam, Spam, Spam, magnificent Spam, Superlative Spam. Spam, Spam, Spam, wonderous Spam, Surgical Spam, splendiferous Spam. Spam, Spam, Spam, Spaaam! ``` 如您所見,當不打印字符串時,他的特殊轉義符將保持轉義-甚至在創建多行字符串時,我們在添加換行符的地方 Python 都顯式顯示`\n`。 ### 字符串格式化 字符串格式化在 Python 中很酷。 格式有兩種版本:帶`%`符號(類似于 C 的`printf`方法)和字符串本身的`format()`方法。 但是,在大多數情況下,我將同時介紹這兩種版本,第二種版本更好,更易于使用。 讓我們直接進入細節。 ```py >>> name = 'Bob' >>> age = 31 >>> 'My name is %s and I am %d years young!'%(name, age) 'My name is Bob and I am 31 years young!' >>> 'My name is {} and I am {} years young!'.format(name, age) 'My name is Bob and I am 31 years young!' >>> 'My name is {0} and I am {1} years young!'.format(name, age) 'My name is Bob and I am 31 years young!' ``` 如您所見,兩個格式化選項之間沒有太大區別。 但是,如果您查看第一個版本,則可以看到變量使用的占位符(`%s`和`%d`)與第二個版本中的占位符不同。 關鍵是對于`%`格式,您必須告訴參數的類型以根據結果字符串中的格式啟用 python。 這也意味著如果要多次打印變量,則必須重復變量。 格式函數與參數索引一起使用,并根據類型確定格式。 因此,您不必明確提及參數的類型。 如果您提供的參數數量與占位符相同,則可以保留參數索引。 ```py >>> 'My name is %s and I am %d years young and I have read %d books!'%(name, age, age) 'My name is Bob and I am 31 years young and I have read 31 books!' >>> 'My name is {} and I am {} years young and I have read {} books!'.format(name, age) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: tuple index out of range >>> 'My name is {} and I am {} years young and I have read {} books!'.format(name, age, age) 'My name is Bob and I am 31 years young and I have read 31 books!' >>> 'My name is {0} and I am {1} years young and I have read {1} books!'.format(name, age) 'My name is Bob and I am 31 years young and I have read 31 books!' ``` 取決于您以后使用哪種格式,但是在本系列文章中,我將堅持使用`format`函數。 但是,如果您更喜歡`%`,這里有一些字符以及它們在格式字符串中的作用: * **`%c`**:字符 * **`%s`**:格式化前通過`str()`進行字符串轉換 * **`%i`**:帶符號的十進制整數 * **`%d`**:帶符號的十進制整數 * **`%u`**:無符號十進制整數 * **`%o`**:弱八進制 * **`%x`**:十六進制整數(小寫字母) * **`%X`**:十六進制整數(大寫字母) * **`%e`**:指數符號(小寫的“`e`”) * **`%E`**:指數表示法(大寫的“`E`”) * **`%f`**:浮點實數 * **`%g`**:`%f`和`%e`中的較短者 * **`%G`**:`%F`和`%E`中的較短者 ### 參考文獻 * [官方文檔](https://docs.python.org/3/library/numbers.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看