# 運行 python 程序
> 原文: [https://thepythonguru.com/running-python-programs/](https://thepythonguru.com/running-python-programs/)
* * *
于 2020 年 1 月 7 日更新
* * *
您可以通過兩種方式運行 python 程序,首先通過直接在 python shell 中鍵入命令或運行存儲在文件中的程序。 但是大多數時候您想運行存儲在文件中的程序。
讓我們在記事本目錄中創建一個名為`hello.py`的文件,即使用記事本(或您選擇的任何其他文本編輯器)創建一個`C:\Users\YourUserName\Documents`,記住 python 文件具有`.py`擴展名,然后在文件中編寫以下代碼。
```py
print("Hello World")
```
在 python 中,我們使用`print`函數將字符串顯示到控制臺。 它可以接受多個參數。 當傳遞兩個或多個參數時,`print()`函數顯示每個參數,并用空格分隔。
```py
print("Hello", "World")
```
**預期輸出**:
```py
Hello World
```
現在打開終端,并使用`cd`命令將當前工作目錄更改為`C:\Users\YourUserName\Documents`。

要運行該程序,請鍵入以下命令。
```py
python hello.py
```

如果一切順利,您將獲得以下輸出。
```py
Hello World
```
## 獲得幫助
* * *
使用 python 遲早會遇到一種情況,您想了解更多有關某些方法或函數的信息。 為了幫助您,Python 具有`help()`函數,這是如何使用它。
**語法**:
要查找有關類別的信息:`help(class_name)`
要查找有關方法的更多信息,屬于類別:`help(class_name.method_name)`
假設您想了解更多關于`int`類的信息,請轉到 Python shell 并鍵入以下命令。
```py
>>> help(int)
Help on class int in module builtins:
class int(object)
| int(x=0) -> integer
| int(x, base=10) -> integer
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is a number, return x.__int__(). For floating point
| numbers, this truncates towards zero.
|
| If x is not a number or if base is given, then x must be a string,
| bytes, or bytearray instance representing an integer literal in the
| given base. The literal can be preceded by '+' or '-' and be surrounded
| by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
| Base 0 means to interpret the base from the string as an integer literal.
| >>> int('0b100', base=0)
| 4
|
| Methods defined here:
|
| __abs__(self, /)
| abs(self)
|
| __add__(self, value, /)
| Return self+value.
```
如您所見,`help()`函數使用所有方法吐出整個`int`類,它還在需要的地方包含說明。
現在,假設您想知道`str`類的`index()`方法所需的參數,要找出您需要在 python shell 中鍵入以下命令。
```py
>>> help(str.index)
Help on method_descriptor:
index(...)
S.index(sub[, start[, end]]) -> int
Like S.find() but raise ValueError when the substring is not found.
```
在下一篇文章中,我們將學習 python 中的數據類型和變量。
* * *
* * *
- 初級 Python
- python 入門
- 安裝 Python3
- 運行 python 程序
- 數據類型和變量
- Python 數字
- Python 字符串
- Python 列表
- Python 字典
- Python 元組
- 數據類型轉換
- Python 控制語句
- Python 函數
- Python 循環
- Python 數學函數
- Python 生成隨機數
- Python 文件處理
- Python 對象和類
- Python 運算符重載
- Python 繼承與多態
- Python 異常處理
- Python 模塊
- 高級 Python
- Python *args和**kwargs
- Python 生成器
- Python 正則表達式
- 使用 PIP 在 python 中安裝包
- Python virtualenv指南
- Python 遞歸函數
- __name__ == "__main__"是什么?
- Python Lambda 函數
- Python 字符串格式化
- Python 內置函數和方法
- Python abs()函數
- Python bin()函數
- Python id()函數
- Python map()函數
- Python zip()函數
- Python filter()函數
- Python reduce()函數
- Python sorted()函數
- Python enumerate()函數
- Python reversed()函數
- Python range()函數
- Python sum()函數
- Python max()函數
- Python min()函數
- Python eval()函數
- Python len()函數
- Python ord()函數
- Python chr()函數
- Python any()函數
- Python all()函數
- Python globals()函數
- Python locals()函數
- 數據庫訪問
- 安裝 Python MySQLdb
- 連接到數據庫
- MySQLdb 獲取結果
- 插入行
- 處理錯誤
- 使用fetchone()和fetchmany()獲取記錄
- 常見做法
- Python:如何讀取和寫入文件
- Python:如何讀取和寫入 CSV 文件
- 用 Python 讀寫 JSON
- 用 Python 轉儲對象