---
# System prepended metadata

title: 2023-04-08 Python入門
tags: [已轉移]

---

# 2023-04-08 Python入門
###### tags: `python`
#### 使用者輸入兩個數字後，相加再輸出
```python=
num1 = input()
num2 = input()

num1 = int(num1)  # 資料轉型
num2 = int(num2)  # 資料轉型

print('總和是:', num1 + num2)
```

#### 輸入一小數，只取整數並顯示
```
a = input('請輸入一個小數:')
a = float(a)
a = int(a)
print('整數為:', a)
```

#### 運算子 
```
print(3 + 4)
print(3 - 4)
print(3 * 4)
print(3 ** 4) # 指數運算
print(3 / 4)

print(3 % 4)   # 餘數運算
print(3 // 4)  # 除法取整數

print('3' + '4')
print('3' * 4)
print('3' + str(4))
print(int('3') + 4)

# >, >= ,< , <= , == , =!

print(3 > 4)
print(3 == 4) # 比較兩邊的植有沒有相等
print(3 != 4) # 比較兩邊的植有沒有不相等

print('我' > '你')

print(ord('我'))
print(ord('你'))

print('True and False =', True and False)
```

#### list
```
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

print(type(a))
print(a)
print(a[0])
print(a[1])

print('a有:', len(a), '筆資料')

# print(a[1:5])
# print(a[:5])

print(a[3:1:-1])
```

#### 函式與方法
```
a = 'abc'
print(a.upper())

b = 'DEF'
print(b.lower())

c = 'abcde,ert,899'
print(c.split(','))
```

#### for-in迴圈
```
a = [1, 2, 3]

for temp in a:
    print(temp)
```

#### 請使用for-in迴圈將下面list內數字加總後輸出到畫面上
```a = [9, 8, 123, 77, 143]```

參考答案:
```
a = [9, 8, 123, 77, 143]

s = 0

for t in a:
    s += t

print('總合為:', s)
```

#### UBike
```python=
import requests
import csv

url = 'http://data.tycg.gov.tw/api/v1/rest/datastore/a1b4714b-3b75-4ff8-a8f2-cc377e4eaa0f?format=csv'

response = requests.get(url)

rows = response.text.splitlines()

rows = list(  csv.reader(rows) )

user = input('請輸入站關鍵字:')

for row in rows:
    if user in row[3]:
        print('站名: ', row[3], ', 地址:', row[6])
        print(' - 可借:', row[12])
        print(' - 可還:', row[5])
        print()
```

#### f-string
```python=
a = input('請輸入一個數字:')
b = input('請輸入一個數字:')

print('你輸入了', a, '和', b, ', 總和為:', int(a) + int(b))
print(f'你輸入了 {a} 和 {b} , 總和為: {int(a) + int(b)}')
```