# MiniBitePython - #1 Create python .py script | 創建Python腳本

in HIVE CN 中文社区last year

### 1. Create .py file ｜ 創建 .py 檔案

First off, we need to open the terminal and type in
`touch sum_num.py` and press the `return` button on your keyboard to create a new .py file named `sum_num`.

`touch sum_num.py`以創建一個名為`sum_num`的新`.py`文件。

Then you will see a `test.py` file you just created appears on the directory you are currently at.

### 2. Edit the blank .py file | 編輯空白的.py文件

To edit the newly created file, you will need a text editor to do so. Here I suggest using VS code from Microsoft. It is an open-source popular text editor.

for the above code,

1. `def main(num):` defined a function called `main` that takes `num` as it parameters for further use.

2. `sum` created a variable that was set to have `0` int value.

3. Here we have a for-loop with a range length of `num` which was supposed to be passed down from the `main` function call from line 8, and it is `100`.

4. In each loop, we add the new value `i` to the `sum` variable.

5. After the for-loop ended, we print the `sum` result to the terminal.

7-8. For the script to be able to run properly in the terminal. We need lines 7-8. And it is a good practice to put code line 7 since we are running THE script. For more info about why: https://stackoverflow.com/questions/28336627/if-name-main-python

The result is 4950. Try it with your calculator!

1.`def main(num):`定義了一個名為`main`的函數，該函數將`num`作為其參數供進一步使用。

2.創建了一個名為`sum`的變量，該變量被設置為`0` int值。

3.在這裡，我們有一個for循環，其範圍長度為`num`，從第8行的`main`函數調用中向下傳遞，在這個 example 中數值為 `100`

4.在每個循環中，我們將新值`i`添加到`sum`變量中。

1. for循環結束後，我們將`sum`使用 `print()`把結果打印到終端。

7-8。 為了使腳本能夠在終端中正常運行。 我們需要 line 7-8。 由於我們正在運行腳本，因此最好在代碼行7中放置。想知道為什麼可以看看這裡: https://stackoverflow.com/questions/28336627/if-name-main-python

Summary:

We learned to create .py file, edit the blank .py file with a text editor and to write a simple python script.

Cheers!