or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
DAY 02 變數和簡單的資料型別
變數的使用
「變數」是指一個包含部分已知或未知數值或資訊(即一個值)之儲存位址
那我們來實際操作一下 :
設定一個變數名稱叫s,s等於一個字串,然後再將s print出來
印出的結果如下,因為s等於hello,所以將s印出就等於是在印hello
接下來我們把上述的程式碼擴充成以下形式 :
印出後我們會發現結果如下:
為甚麼會從hello變成hahaha呢 ? 因為python會記錄它最新的值,也就是說變數我們能隨時更改,它只會印出最新的那個值 !
變數命名的注意事項:字串(String)的基本運用
在python3裡面只要是引號括起來的都是字串,所以單引號雙引號是不影響的 !
現在來說一些字串的常見用法:
上面程式碼的說明 :
所以印出的東西分別是:
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →當你的print是用兩個單引號括起來,但你要印的字串裡也有單引號的時候,你就得用\這個符號隔在內容的單引號前面,不然就是得換成兩個雙引號,如下 :
這樣才能不讓程式碼出錯喔 ~
字串的增加空白和刪除空白
1.增加空白
增加空白有兩種方式 \n (換行) 和 \t (空格)
印出來就是 :
印出來就是 :
2.刪除空白
刪除空白有三種方法可以使用,lstrip()刪除左邊空白、rstrip()刪除右邊空白,或是strip()刪除兩邊空白
從輸出結果來看,我們可以發現lstrip()方法只是暫時將字串的空白刪除,如果想要永久刪除空白,我們應該要把刪除空白的字串再存回變數中 !
p.s 順便說一下要寫注釋的方法,在python中是用#這個符號
學會了刪除左邊空白後,刪除右邊空白和刪除兩邊空白也是同樣的做法 !
好了今天就講到這了,各位明天見
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →資料來源:<<python程式設計的樂趣>>-Eric Matthes著/H&C譯