<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 基礎知識 - 又稱 Hello World 以及如何實現 > 原文: [https://javabeginnerstutorial.com/python-tutorial/python-basics-a-k-a-hello-world-and-how-to-achieve-it-2/](https://javabeginnerstutorial.com/python-tutorial/python-basics-a-k-a-hello-world-and-how-to-achieve-it-2/) 在介紹并安裝 Python 之后,讓我們繼續前進,并使用 Python 3 編寫第一行代碼。 第一步是使用 python3 命令啟動交互式解釋器。 三個箭頭(或更大的符號)指示提示,我們可以在其中鍵入解釋器的命令。 我們將輸入以下文本:“`print('We want a shrubbery!')`”,然后按回車鍵。 以下代碼段顯示了您應該看到的內容: ```py Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 5 2014, 20:42:22) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print('We want a shrubbery!') We want a shrubbery! ``` 這是基本的 Python 應用,它將給定的字符串輸出到控制臺。 基本上,我們將使用`print`函數將所有消息寫入控制臺。 ## 基本語法 Python 的基本語法與其他編程語言非常相似。 但是,有些差異會在開發應用時引起麻煩。 因此,讓我們研究 Python 應用的基本語法。 ### 縮進 這是您可能遇到的最令人不安的事情。 如果您沒有正確配置 IDE 或使用其他格式設置擴展由他人編寫的代碼,這可能會在以后讓您徹夜難眠。 **但是縮進是什么意思?** 在執行分組在一起的語句時,使用最廣泛的編程語言會使用大括號(`{}`)。 Python 利用縮進,因為這樣代碼更易讀。 讓我們用一個小示例來演示一下: **Java 代碼** ```py if(a == 42){a = 25; System.out.println(a);}else{a = 42;} ``` **Python 代碼** ```py if a == 42: a = 25 print(a) else: a = 42 ``` 如您所見,Python 代碼更具可讀性(盡管我也是一個有良好基礎的 Java 開發人員,但我認為這是我的看法,因此我可以覆蓋前面的示例以使其可讀)。 如果您有**縮進行(請仔細查看 python 代碼中的縮進)**,則表示它屬于同一執行組。 例如,在 Java 中,它將不會: ```py if(a == 42) a = 25; System.out.println(a); // Indention will not work here. And it will give compilation error else a = 42; ``` 如果不使用花括號,則會出現`SyntaxError`,因為在上面的情況下`System.out.println(a);`不屬于`if`語句。 **但是**這不是一篇有關 Java 或這兩種編程語言之間的差異的文章,因此我從這件事回到*縮進*的主題。 為什么會導致不眠之夜? 因為您有兩種縮進方式:**空格**或 **TAB**。 **而且您不能將兩者混為一談!** 如果這樣做,則會出現`TabError`: ```py >>> a = 33 >>> if a == 42: ... a = 25 ... print(a) File "<stdin>", line 3 print(a) ^ TabError: inconsistent use of tabs and spaces in indentation ``` 在上面的示例中不可見,但是首先我使用空格,然后使用制表符使行縮進。 這導致異常。 如果我切換線路,則會收到另一個錯誤: ```py File "<stdin>", line 3 a = 25 ^ ``` `IndentationError`:縮進與任何外部縮進級別都不匹配 這就是我不眠之夜的意思。 如果您混淆了所有內容,則必須瀏覽文件,找到在每個可能的地方都用制表符交換空格,反之亦然。 ### 注釋 注釋代碼有時是必要的(盡管一些開發人員說,如果必須編寫注釋來描述代碼,則應重新編寫代碼)。 您可以使用井號(`#`)。 這告訴解釋器,井號后面的同一行上的文字僅是注釋,它們無意執行: ```py >>> print(1+2) # results in 3 : this is comment 3 ``` **注意**與其他編程語言一樣,**沒有**多行注釋。 如果要禁用代碼塊,則必須在每行的開頭放置`#`。 ### 標識符 標識符是用于標識變量,類,函數,模塊或其他對象的名稱。 在 Python 中,您可以以字母(大寫或小寫無關緊要)或下劃線(`_`)開頭,并在其后跟隨零個或多個字母,數字和下劃線。 標識符名稱中不允許使用標點符號(例如`@`,`$`和`%`)。 該語言區分大小寫,這意味著您可以擁有一個名為`my_variable`的變量和另一個名為`My_variable`的變量,它們將被視為不同的變量。 因此**要小心**,因為這在開發時也會引起問題。 ### 保留字 高級編程語言幾乎是自由形式的代碼編寫。 但是,有一些內部字詞是為特殊用例保留的,當解釋器遇到它們時,它們對解釋器具有意義。 這意味著您不能將這些關鍵字用作標識符名稱。 在其他情況下,您會根據關鍵字獲得錯誤。 ```py False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise ``` 當然,它們是區分大小寫的。 這意味著您可以使用`false`作為變量名,但不能使用`False`。 ### 用戶輸入 當您可以編寫交互式應用時,編程會變得很有趣。 交互性意味著用戶可以向應用輸入一些輸入,然后他/她將成為應用的答案。 我現在不會從這里開始編寫帶有用戶輸入的應用,但是我將告訴您如何要求用戶提供一些東西。 它帶有輸入函數,并且使用非常簡單: ```py >>> entered = input("Enter some text: ") Enter some text: 42 >>> entered '42' ``` 如您所見,您可以將參數傳遞給`input`函數,并且該參數將顯示為用戶的命令提示符。 他/她輸入內容后,將其作為字符串存儲在輸入的變量中。 稍后,我們將更全面地檢查`input`函數。 ## 基本運算符 現在是時候看看 Python 中可用的基本運算符了。 我知道在不了解基本類型的情況下并沒有什么用,但是,嘿! 我們已經知道語言中至少有數字和字符串,這對運算符來說是一個很好的起點。 ### 算術運算符 每個人都知道它們,它們是數學的基礎。 因此,Python 也必須了解它們。 如果它們來自正確的類型,它們也可以使用變量。 這意味著您不能在數字和字符串上使用加法。 讓我們快速列舉一些示例。 我希望這些示例能說明自己。 如果不是這種情況,請隨時給我寫一封信息,我還將在示例中添加說明性文字。 ```py >>> 6 + 5 # the + sign adds numbers up 11 >>> 6 - 5 # the - sign subtracts the second number from the firs 1 >>> 6 * 5 # the * sign multiplies the numbers 30 >>> 6.0 / 5 # the / sign divides the numbers 1.2 >>> 6.0 // 5 # the // sign makes an integer division 1.0 >>> 6 % 5 # the % calculates the modulo 1 >>> 6 ** 5 # the ** sign raises the first number to the power of the second 7776 ``` ### 比較運算符 這些運算符比較值(變量或值本身不再重要)。 結果來自布爾類型,可以為`True`或`False`。 ```py >>> 5 == 4 # == is the equality operator, returns True when both sides are equal False >>> 4 == 4 True >>> 5 != 5 # != is the inequality operator, returns False when both sides are equal False >>> 5 > 5 # > is the greater than operator False >>> 5 < 5 # < is the less than operator False >>> 5 >= 5 # >= is the greater or equal operator True >>> 5 <= 5 # is the less or equal operator True ``` ### 賦值運算符 我們已經知道 **Python 中的**賦值運算符,即等號(`=`)。 但是,這些運算符中有相當一部分具有不同的用法,但所有這些運算符都會減少一些編寫所需的代碼。 在下面的示例中,我將一遍又一遍地使用相同的變量來說明這些運算符的工作方式。 同樣,如果您想知道示例的含義,請隨時給我寫郵件,并在這些示例中添加說明。 僅輸入變量名(在本例中為`a`)并按回車鍵,便可以在交互式解釋器中看到變量的值。 ```py >>> a = 5 >>> a += 3 # a has the value of 8 >>> a -= 2 # a has the value of 6 >>> a *= 3 # a has the value of 18 >>> a /= 4 # a has the value of 4.5 >>> a //= 1 # a has the value of 4.0 >>> a %= 6 # a has the value of 4.0 >>> a **= 2 # a has the value of 16.0 >>> a 16.0 ``` ### 邏輯運算符 它們并不多:和,或不。 當我們看看愛是什么時,我們已經在復活節蛋中遇到了它們。 它們是邏輯運算符,因此最好是在使用它們的地方具有布爾值。 但是,這里有一點例外。 0,空對象和`None`被視為`False`,因此您可以在條件表達式中使用它們(稍后將在后面的文章中使用)。 數字和非空對象當然被視為`True`。 現在,讓我們看一些示例: ```py >>> a = True >>> b = False >>> a and b False >>> a or b True >>> not a False >>> not b True >>> not 0 True >>> not 1 False >>> not [] # this is an empty list True ``` ### 成員運算符 當我們想查看某物是否為集合的成員時,它會派上用場。 例如,數字 4 在前 5 個質數的列表中。 成員運算符是`in`關鍵字。 有些書也沒有添加到此列表中,但據我們了解,不是一個邏輯運算符,它否定了所使用的值。 結果是布爾值`True`或`False`。 ```py >>> primes = [2,3,5,7,11] >>> 4 in primes False >>> 4 not in primes True >>> 5 in primes True ```
                  <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>

                              哎呀哎呀视频在线观看