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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 交互式 Python > 原文: [http://zetcode.com/lang/python/interactivepython/](http://zetcode.com/lang/python/interactivepython/) 在 Python 編程教程的這一部分中,我們討論交互式 Python 解釋器。 Python 代碼可以通過兩種基本方式啟動。 作為腳本或在交互式解釋器中。 ```py #!/usr/bin/env python # first.py print("The Python tutorial") ``` 這是一個小型 Python 腳本的示例。 它是從 UNIX Shell 啟動的。 ```py $ ./first.py The Python tutorial ``` ## 交互解釋器 運行 Python 代碼的另一種方法是交互式 Python 解釋器。 Python 解釋器對于我們的探索非常有用。 當我們快速想要測試 Python 語言的一些基本功能并且不想編寫整個腳本時。 為了獲得交互式解釋器,我們在最喜歡的 shell 上執行 Python 命令。 ```py $ python3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ``` 這是 Python 解釋器的歡迎消息。 我們在機器上看到了 Python 版本。 在我們的例子中是 Python 3.5.2。 `">>>"`是在 Python 交互模式下使用的提示。 要離開解釋器并返回外殼,我們可以輸入 `Ctrl + D` 或`quit()`。 鍵入 `Ctrl + L` 將清除 Python 解釋器的屏幕。 現在我們可以查詢一些有用的信息。 ```py >>> credits Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands for supporting Python development. See www.python.org for more information. ``` 如果鍵入`credits`,我們將獲得有關參與 Python 開發的組織的一些信息。 ```py >>> copyright Copyright (c) 2001-2016 Python Software Foundation. All Rights Reserved. Copyright (c) 2000 BeOpen.com. All Rights Reserved. Copyright (c) 1995-2001 Corporation for National Research Initiatives. All Rights Reserved. Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam. All Rights Reserved. ``` `copyright`命令提供 Python 編程語言的版權。 `license()`命令提供了有關 Python 許可證的多個頁面。 ## 獲得幫助 `help`命令提供了有關 Python 的一些幫助。 ```py >>> help Type help() for interactive help, or help(object) for help about object. >>> ``` 我們可以通過兩種方式使用該命令。 我們可以獲取有關特定對象的幫助,或者進入交互式幫助模式。 例如,如果我們鍵入`help(True)`,我們將獲得有關`bool`對象的一些信息。 ```py Help on bool object: class bool(int) | bool(x) -> bool | | Returns True when the argument x is true, False otherwise. | The builtins True and False are the only two instances of the class bool. | The class bool is a subclass of the class int, and cannot be subclassed. | | Method resolution order: | bool | int | object | | Methods defined here: | | __and__(...) | x.__and__(y) <==> x&y ... ``` 如果主題大于一頁,我們可以使用箭頭滾動主題。 如果要退出該主題,請按 `q` 鍵。 如果鍵入`help()`,將獲得解釋器的交互式幫助模式。 ```py >>> help() Welcome to Python 3.5's help utility! If this is your first time using Python, you should definitely check out the tutorial on the Internet at http://docs.python.org/3.5/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". help> ``` 要退出幫助模式并返回解釋器,我們使用`quit`命令。 `keywords`命令提供 Python 編程語言中可用關鍵字的列表。 ```py help> keywords Here is a list of the Python keywords. Enter any keyword to get more help. False def if raise None del import return True elif in try and else is while as except lambda with assert finally nonlocal yield break for not class from or continue global pass ``` 如果我們輸入任何關鍵字,我們都會得到一些幫助。 `modules`命令給出了可用模塊的列表。 同樣,鍵入模塊名稱將提供其他幫助。 最后,我們有`topics`命令。 ```py help> topics Here is a list of available topics. Enter any topic name to get more help. ASSERTION DELETION LOOPING SHIFTING ASSIGNMENT DICTIONARIES MAPPINGMETHODS SLICINGS ATTRIBUTEMETHODS DICTIONARYLITERALS MAPPINGS SPECIALATTRIBUTES ATTRIBUTES DYNAMICFEATURES METHODS SPECIALIDENTIFIERS AUGMENTEDASSIGNMENT ELLIPSIS MODULES SPECIALMETHODS BASICMETHODS EXCEPTIONS NAMESPACES STRINGMETHODS BINARY EXECUTION NONE STRINGS BITWISE EXPRESSIONS NUMBERMETHODS SUBSCRIPTS BOOLEAN FLOAT NUMBERS TRACEBACKS CALLABLEMETHODS FORMATTING OBJECTS TRUTHVALUE CALLS FRAMEOBJECTS OPERATORS TUPLELITERALS CLASSES FRAMES PACKAGES TUPLES CODEOBJECTS FUNCTIONS POWER TYPEOBJECTS COMPARISON IDENTIFIERS PRECEDENCE TYPES COMPLEX IMPORTING PRIVATENAMES UNARY CONDITIONAL INTEGER RETURNING UNICODE CONTEXTMANAGERS LISTLITERALS SCOPING CONVERSIONS LISTS SEQUENCEMETHODS DEBUGGING LITERALS SEQUENCES ``` `topics`命令提供有關 Python 編程語言的主題列表。 在這里我們可以找到一些有用的信息。 ## Python 代碼 接下來,我們將提供一些 Python 解釋器的實際示例。 ```py >>> 2 + 4 6 >>> 5 * 56 280 >>> 5 - 45 -40 >>> ``` Python 解釋器可以用作計算器。 立即執行每個表達式,結果顯示在屏幕上。 ```py >>> a = 3 >>> b = 4 >>> a**b 81 >>> a == b False >>> a < b True >>> ``` 我們可以定義變量并對其執行操作。 ```py >>> import random >>> dir(random) ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'uniform', 'vonmisesvariate', 'weibullvariate'] >>> ``` 在這里,我們導入了一個隨機模塊。 利用`dir()`函數,我們進一步探索了隨機模塊。 借助特殊的`__doc__`字符串,我們可以獲得有關特定功能的幫助。 ```py >>> print random.seed.__doc__ Initialize internal state from hashable object. None or no argument seeds from current time or from an operating system specific randomness source if available. If a is not None or an int or long, hash(a) is used instead. >>> ``` `locals()`命令顯示我們當前的本地名稱空間。 ```py >>> locals() {'random': <module 'random' from '/usr/lib/python3.5/random.py'>, '__spec__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__builtins__': <module 'builtins' (built-in)>, '__doc__': None, '__name__': '__main__'} ``` 我們可以看到我們先前導入的隨機模塊。 ```py >>> class Car: ... pass ... >>> def function(): ... pass ... >>> for i in range(5): ... print(i) ... 0 1 2 3 4 >>> ``` 我們可以定義自己的類,函數或使用控制流結構。 我們一定不要忘記縮進代碼。 要完成所有這些代碼塊,我們鍵入`,輸入兩次`鍵。 ```py >>> import os >>> os.getcwd() '/home/vronskij/programming/python' >>> os.system('ls') command_line_arguments.py read_input.py 0 ``` 在這里,我們導入`os`模塊并與操作系統進行交互。 最后,我們要退出解釋器。 我們可以通過幾種方式退出解釋器: * `Ctrl + D` * `exit()` 我們還可以以編程方式退出解釋器。 ```py >>> raise SystemExit $ ``` 要么 ```py >>> import sys >>> sys.exit() $ ``` 解釋器已退出。 ## Python 之禪 Python 的 Zen 是一套有關如何編寫良好的 Python 代碼的規則。 它以某種方式反映了語言的哲學。 ```py >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! ``` 可以通過啟動`import this`來讀取規則。 在本章中,我們研究了 Python 交互式解釋器。
                  <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>

                              哎呀哎呀视频在线观看