<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python – 關鍵字 > 原文: [https://howtodoinjava.com/python/python-keywords/](https://howtodoinjava.com/python/python-keywords/) **Python 關鍵字**是 [python](https://howtodoinjava.com/python-tutorial/) 編程語言的保留字。 這些關鍵字不能用于其他目的。 Python 中有 35 個關鍵字 - 下面列出了它們的用法。 `and` A **logical AND** operator. Return `True` if both statements are `True`. ```py x = (5 > 3 and 5 < 10) print(x) # True ``` `or` 邏輯或運算符。 如果兩個語句中的任何一個為`True`,則返回`True`。 如果兩個語句都為假,則返回`False`。 ```py x = (5 > 3 or 5 > 10) print(x) # True ``` `as` 它用于創建別名。 ```py import calendar as c print(c.month_name[1]) #January ``` `assert` 它可以用于調試代碼。 它會測試條件并返回`True`,否則產生`AssertionError`。 ```py x = "hello" assert x == "goodbye", "x should be 'hello'" # AssertionError ``` `async` 它用來聲明一個函數為協程,就像`@asyncio.coroutine`裝飾器所做的一樣。 ```py async def ping_server(ip): ``` `await` 它用于調用`async`協程。 ```py async def ping_local(): return await ping_server('192.168.1.1') ``` `class` 它用于創建一個類。 ```py class User: name = "John" age = 36 ``` `def` 它用于創建或定義函數。 ```py def my_function(): print("Hello world !!") my_function() ``` `del` 它用于刪除對象。 在 Python 中,所有事物都是對象,因此`del`關鍵字也可以用于刪除變量,[列表](https://howtodoinjava.com/python/python-lists/)或列表的一部分,等等。 ```py x = "hello" del x ``` `if` 它用于創建條件語句,該條件語句僅在條件為`True`時才允許我們執行代碼塊。 ```py x = 5 if x > 3: print("it is true") ``` `elif` 它用于條件語句中,是`else if`的縮寫。 ```py i = 5 if i > 0: print("Positive") elif i == 0: print("ZERO") else: print("Negative") ``` `else` 它決定如果`if..else`語句中的條件為`False`時該怎么辦。 ```py i = 5 if i > 0: print("Positive") else: print("Negative") ``` 也可以在`try...except`塊中使用。 ```py x = 5 try: x > 10 except: print("Something went wrong") else: print("Normally execute the code") ``` `try` 如果它包含任何錯誤,它將定義一個測試代碼塊。 `except` 如果`try`塊引發錯誤,它將定義要運行的代碼塊。 ```py try: x > 3 except: print("Something went wrong") ``` `finally` 它定義了一個代碼塊,無論`try`塊是否引發錯誤,該代碼塊都將執行。 ```py try: x > 3 except: print("Something went wrong") finally: print("I will always get executed") ``` `raise` 它用于手動引發異常。 ```py x = "hello" if not type(x) is int: raise TypeError("Only integers are allowed") ``` `False` 它是一個布爾值,與 0 相同。 `True` 它是一個布爾值,與 1 相同。 `for` 它用于創建`for`循環。 `for`循環可用于遍歷序列(如列表,元組等)。 ```py for x in range(1, 9): print(x) ``` `while` 它用于創建`while`循環。 循環繼續進行,直到條件語句為假。 ```py x = 0 while x < 9: print(x) x = x + 1 ``` `break` 它用于中斷`for`循環或`while`循環。 ```py i = 1 while i < 9: print(i) if i == 3: break i += 1 ``` `continue` 它用于在`for`循環(或`while`循環)中結束當前迭代,并繼續進行下一個迭代。 ```py for i in range(9): if i == 3: continue print(i) ``` `import` 它用于導入模塊。 ```py import datetime ``` `from` 它僅用于從模塊中導入指定的節。 ```py from datetime import time ``` `global` 它用于從非全局范圍創建全局變量,例如在函數內部。 ```py def myfunction(): global x x = "hello" ``` `in` 1\. 它用于檢查序列(列表,范圍,字符串等)中是否存在值。 2\. 它也用于在`for`循環中遍歷序列。 ```py fruits = ["apple", "banana", "cherry"] if "banana" in fruits: print("yes") for x in fruits: print(x) ``` `is` 它用于測試兩個變量是否引用同一對象。 ```py a = ["apple", "banana", "cherry"] b = ["apple", "banana", "cherry"] c = a print(a is b) # False print(a is c) # True ``` `lambda` 它用于創建小的匿名函數。 它們可以接受任意數量的參數,但只能有一個表達式。 ```py x = lambda a, b, c : a + b + c print(x(5, 6, 2)) ``` `None` 它用于定義一個“空”值,或者根本沒有值。 `None`與 0,`False`或空字符串不同。 `None`是它自己的數據類型(`NoneType`),并且只有`None`可以是`None`。 ```py x = None if x: print("Do you think None is True") else: print("None is not True...") # Prints this statement ``` `nonlocal` 它用于聲明變量不是局部變量。 它用于在嵌套函數內部使用變量,其中變量不應屬于內部函數。 ```py def myfunc1(): x = "John" def myfunc2(): nonlocal x x = "hello" myfunc2() return x print(myfunc1()) ``` `not` 它是一個邏輯運算符,并反轉`True`或`False`的值。 ```py x = False print(not x) # True ``` `pass` 它用作將來代碼的占位符。 當執行`pass`語句時,什么也不會發生,但是當不允許使用空代碼時,可以避免出現錯誤。循環,函數定義,類定義或`if`語句中不允許使用空代碼。 ```py for x in [0, 1, 2]: pass ``` `return` 它用于退出一個函數并返回一個值。 ```py def myfunction(): return 3+3 ``` `as` 用于簡化異常處理 `yield` 用于結束一個函數,返回一個生成器 學習愉快! 參考: [W3School](https://www.w3schools.com/python/python_ref_keywords.asp)
                  <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>

                              哎呀哎呀视频在线观看