# Python 基礎語法
contributed by <[`yanjiun`](https://github.com/yanjiunhaha)>
###### tags: `school`
---
# Python Identifier (標識符)
## 命名規則
----
* 由字母、數字、底線
* 大小寫分明
*底線開頭的慣例:*
1. 單底線開頭 `_foo` 代表不能直接使用的變數。
2. 雙底線開頭 `__foo` 代表不能直接使用的函式。
3. 雙底線開頭結尾 `__foo__` 代表 Python 內建專用標識,如: `__init__()` 預設為建構子。
----
#### Python 保留字
保留字不能用作常數或變數,或任何其他標識符。
已|使|用|保|留|字
-|-|-|-|-|-
and | exec | not | assert | finially | or
break | for | pass | class | from | print
continue | global | raise | def | if | return
del | import | try | elif | in | while
else | is | with | except | lambda | yield
----
#### 換行、縮排
* Python 使用換行與縮排來控制程式流程
* Python 對格式要求嚴格,縮排必須統一。
* 縮排與空格不要混用
```python
if True:
print("True")
else :
print("False")
```
----
#### 引號 (字串)
* 字串可以使用單引號 `'string'`、雙引號 `"string"`
* 多行字串使用三次
```python
word = 'word'
sentence = "this is sentence"
paragraph = '''this is paragraph.
contains multiple statements.'''
paragraph2 = """this is paragraph.
contains multiple statements."""
```
----
#### 註解
* 單行註解使用 `#` 開頭
* 多行註釋使用多行字串使用方法 `'''annotation'''` 或 `"""comment"""`
```python
string = "Hello, Python" # this is a comment
'''
this is multiple comments. using '
this is multiple comments. using '
'''
"""
this is multiple comments. using "
this is multiple comments. using "
""""""
```
---
# Python 變數
變數除存在記憶體中,宣告變數將會在記憶體中規劃一個空間。
根據變數的資料型態,直譯器會自動規劃記憶體空間並除存。
----
#### 變數賦值
* Python 中的變數不需要型態宣告。
* 每個變數在使用前都必須賦值,變數被賦值後才會被創建。
* 使用等號 `=` 來給變數賦值。等號左邊是變數名,右邊是變數的值。
```python
counter = 100 #integer
miles = 1000.0 #float
name = "John" #string
print(counter, miles, name)
```
----
#### 多變數賦值
* Python 允許你同時未多個變量賦值
```python
a = b = c = 1
print(a, b, c)
```
* 你也可以為多變數賦予多個值
```python
a, b, c = 1, 2, "john"
```
----
### 標準資料型態
1. Numbers (數字)
2. String (字串)
3. List (列表)
4. Tuple (元組)
5. Dictionary (字典)
----
#### Python Number (數字)
1. integer
2. float
3. complex
```python
i_a, i_b = 10, -10 # integer by decimal
i_c, i_d = 0x10, -0x10 # integer by hexadecimal
f_a, f_b = 1.23, -4. # float
f_c, f_d = 1.23e+5, -4.E-5
c = 3 + 4j # complex
```
----
#### Python String (字串)

```python
string = "abcdef"
print(string[0], string[1])
print(string[-1], string[-2]) # from back
```
----

```python
print(string[2:5], string[2:])
print(string[-4:5], string[-4:])
print(string[1:5,2])
print(string * 2)
print(string + "ghi")
```
----
#### Python List (列表)
* list 是 Python 使用最頻繁的資料型態。
* 可以完成大多數集合類型的結構。
* 列表使用 `[data1, data2]` 標識,是 Python 最通用的複合數據類型。
* list 內可包含各類資料型態。如:字串、數字、甚至 list
```python=
d1 = 1 # integer
d2 = 2. # float
d3 = "3" # string
l1 = [d1, d2, d3]
l2 = [d1, d2, d3, l1]
print(l2)
```
----

```python=
list = ['hello', 123, 4.56, "789", 0.123]
tinylist = [123, 'john']
print(list)
print(list[0])
print(list[1:3])
print(list[2:])
print(tinylist * 2)
print(list + tinylist)
```
----
#### Python Tuple (元組)
* tuple 是一種類似 list 的資料型態。
* 但 tuple 不能二次賦值,相當唯獨(不可修改)。
* tuple 使用 `(data1, data2, data3)` 標識。
```python=
list = ('hello', 123, 4.56, "789", 0.123)
tinylist = (123, 'john')
print(list)
print(list[0])
print(list[1:3])
print(list[2:])
print(tinylist * 2)
print(list + tinylist)
```
----
* 修改 tuple 是不合法的
```python=
list1 = ('hello', 123, 4.56, "789", 0.123)
list2 = ['hello', 123, 4.56, "789", 0.123]
list1[2] = 1000 # n
list2[2] = 1000 # y
```
----
#### Python Dictionary (字典)
* 字典是一個**無序**的集合資料型態。
* 字典是透過 key (關鍵字)來存取字典內的元素。
* 字典使用 `{key1:data1, key2:data2, key3:data3}`標識。
```python=
dict = {}
dict['one'] = 1
dict[2] = "two"
dict[3.] = [1,2,3]
tinydict = {'name':dict, 'code':123, 'dept':'sales'}
print(dict['one'])
print(dict[2])
print(tinydict)
print(tinydict.key())
print(tinydict.values())
```
----
#### Python 資料類型轉換
function|describe
-|-
int(x[,base])|covert to integer
float(x)|convert to float
str(x)|convert to string
chr(x)|convert to char
ord(x)|convert to integer
hex(x)|convert to hex's string
---
# Python 運算子
{"metaMigratedAt":"2023-06-14T22:53:07.714Z","metaMigratedFrom":"Content","title":"Python 基礎語法","breaks":true,"contributors":"[{\"id\":\"c54ee739-026a-43b2-8eb5-398a1a40b341\",\"add\":4562,\"del\":401}]"}