# DAY 02 - 串列 & For迴圈
###### tags: `python教學` `for loop` `range()` `split`
串列
```python=
student = ["alan" , "jack" , "rose" , "bonny", "stan"]
```
串列+For迴圈
```python=
student = ["alan" , "jack" , "rose" , "bonny", "stan"]
for name in student:
print(name)
```
輸出結果
```python=
alan
jack
rose
bonny
stan
```
---
for in range
```python=
for number in range(1,10):# 1<=number<10 1~9
print(number)
```
輸出結果
```python=
1
2
3
4
5
6
7
8
9
```
---
奇數
```python=
for number in range(1,10,2):
print(number)
```
輸出結果
```python=
1
3
5
7
9
```
---
## range()建立數字串列
因為range()是一個函數 所以要用list括號起來 轉換成串列
```python=
odd_number = list(range(1,10,2))
print(odd_number)
```
輸出結果
```python=
[1,3,5,7,9]
```
## 擷取串列中的某部分
i <= x < j
```python=
print(student[0:3]) #印出索引足標0,1,2對應的元素
print(student[1:4]) #印出索引足標1,2,3對應的元素
print(studnet[:2]) #印出從索引足標0到1的元素
print(student[2:]) #印出從索引足標2到最後的元素
print(student[-2:]) #印出從索引足標-2(也就是倒數第二個)到最後的元素
```
## 切片的迴圈應用
```python=
student = ["alan" , "jack" , "rose" , "bonny", "stan"]
```
## 串列複製
```python=
student = ["alan" , "jack" , "rose" , "bonny", "stan"]
duty = student[0:2]
for name in duty:
print(name)
```
## 補充 - 「方法」
split
對字串做分割(以空格做切割)
5 10
```python=
test = input("test")
test.split
```
```python=
in = input("輸入數字") #A B
num1 = in.split()[1]
```
簡單來說
test = 5 10
test.split() = ['5' , '10']
test.split() [0] = 5
### zerojudge 解題 a002 簡單加法
```python=
#split
#方法 vs 函數
math = "5 10"
ans = 0
for i in math.split():
#print(n)
ans = ans + int(i)
print(ans)
```