### **2020電算資訊營**
## **Python語法教學**
Gino
---
# 條件判斷
- 比較運算子
- 條件運算子
- if-else
----
## 比較運算子
----
### 常見的:
- $<$
- $>$
- $<$
- $>=$
- $==$ (注意不是一個等於!)
- $!=$
----
### $=$ v.s. $==$
```python=
a = 3 # 將變數a設為3
a == 3 # 檢查a是否等於3
```
----
## 條件運算子
----
not
and
or
(優先度:not > and > or)
----
### **not**
```python=
print(3 == 5) # False
print(not 3 == 5) # True
```
----
### **and**
```python=
print(5 < 3 and 10 > 6) # False
print(5 > 3 and 10 > 6) # True
```
----
### **or**
```python=
print(5 < 3 or 10 < 6) # False
print(5 > 3 or 10 < 6) # True
```
----
## if-else
----
### **if**
```python=
if 條件:
(條件成立要做的事)
# ^ 這裡記得要縮排(鍵盤左上角tab)
```

----
### **巢狀if - if裡面還有if**
```python=
if 條件A:
if 條件B:
(要做的事)
if 條件A and 條件B:
(要做的事)
```

----
### **if else**
```python=
if 條件:
...
else:
(條件不成立要做的事)
```

----
### **elif (否則如果)**
```python=
if 條件A:
...
elif 條件B: # A不成立才會判斷B
...
elif 條件C: # AB不成立才會判斷C
...
else: # ABC都不成立
...
```
---
# 迴圈
- while
- for
----
## while
----
### **while 迴圈**
```python=
while 條件:
... # 只要條件成立就會一直執行
```
----
### **無窮迴圈**
```python=
while True:
...
```

----
### **break**
```python=
while ...:
...
if 某件事情發生了:
break # 直接離開while迴圈
...
```
----
### **continue**
```python=
while ...:
...
if 某件事情發生了:
continue # 跳到下一次迴圈
...
```
----
## for
----
### **計數的迴圈**
```python=
for i in range(10):
...
```

----
### **巢狀迴圈 - 迴圈裡面的迴圈**
```python=
for i in range(10):
for j in range(10):
...
```

----
### **讀取字串**
```python=
s = "CRC is the best"
for i in s: # i是s裡面的字
print(i, end=" ")
# C R C i s t h e b e s t
for i in range(len(s)): # i是數字
print(s[i], end=" ") # s[i] -> s的第i個字(由0開始數)
```
---
# 函式
- 介紹
- 參數
- return
----
### **工具人**
只要你好好告訴他要幹嘛,他使命必達
```python=
def greet():
print("Hello")
greet() # 呼叫工具人
```
----
### **參數**
```python=
def greet(name):
print("Hello,", name)
greet("Gino") # Hello, Gino
```
----
### **return**
函式結束後回傳的東西(任何東西都可以)
----
### **國中學的函數**
```python=
def f(x):
return 5 * x + 3
print(f(4)) # 19
```
#### $f(x) = 5x + 3$
----
### **比較x, y的大小**
```python=
def compare(x, y):
if x > y:
return True
else:
return False
print(compare(5, 3)) # True
print(compare(3, 5)) # False
```
---
# **總結**
----
### 複習一下我們剛才提到的東西
- 比較、條件運算子(大於小於、not, and, or)
- **if elif else**
- **while迴圈**
- **for迴圈** (計數、巢狀迴圈、讀字串)
- 函式 (聽我們使喚的工具人)
- 函式參數
- 函式回傳值
----
### 開始練習吧!耶伊!
- 0001 Hello OJ!
- 0004 長方形愛好者
- 0009 社課不能當人
- 0010 計時
{"metaMigratedAt":"2023-06-15T11:37:10.102Z","metaMigratedFrom":"YAML","title":"2020電算資訊營 - Python語法教學","breaks":true,"slideOptions":"{\"transition\":\"fade\",\"spotlight\":{\"enabled\":false}}","contributors":"[{\"id\":\"ac1507e0-f05c-4708-bdd2-c56d13fb0dbb\",\"add\":3153,\"del\":189}]"}