# 2022-09-25 第一天 Python入門實作班上課記錄
###### tags: `python`
#### print(): 將資料輸出到畫面上
```python=
print('你好')
print('Hello Python')
print('今天')
print('123')
print('Hello Python')
print('Hello', 'World', 'test', 123)
print(9876 + 1)
print('9876' + '1' + 'Hello')
```
#### input(): 接收使用者輸入
```python=
# input函式輸入的資料存放在a變數裡
a = input('請輸入一個數字:')
# input函式輸入的資料存放在b變數裡
b = input('請再輸入一個數字:')
# a字串轉換成數字後會存放在c
c = int(a)
d = int(b)
print(a + b)
print(c + d)
```
#### type(): 偵測資料型態
```
a = True
b = type(a)
print(b)
```
#### 字串格式化: f-string
```
name = input('請輸入名字:')
age = input('請輸入年齡:')
# 字串格式化
print('你的名字叫', name, ", 年齡為:", age, '歲')
print(f'你的名字叫{name}, 年齡為:{age}歲')
```
#### 讓使用者輸入兩個數字,格式化後輸出
```python=
num1 = int(input('請輸入第一個數字'))
num2 = int(input('請輸入第二個數字'))
print(f'{num1}和{num2}的總合為{num1 + num2}')
```
#### 群集資料型態list
```
a = [1, 2, 3, 1, 5] # list
b = [1, 2, 3, 1, 5] # list
c = [1, 2, 3, 1, 5] # list
print(a[4])
print(a[3])
print(a[2])
print(a[1])
print(a[0])
print(a[0]+a[1]+a[2]+a[3]+a[4])
a.append(9) # 將9增加到a list裡面去
a.insert(2, 'xyz')
b.append('abc')
c.append(True)
# del a[0]
# del b[0]
# del c[0]
print(a)
print(b)
print(c)
# t = sum(a)
# print("t=", t)
```
#### 多重判斷式
```
import random # 亂數模組
import datetime # 日期模組
wday = datetime.date.today().weekday() # 0 = 星期一 ~ 6 = 星期日
print(wday) # 測試用
if wday == 0:
print('今天是星期一')
if wday == 1:
print('今天是星期二')
if wday == 2:
print('今天是星期三')
if wday == 3:
print('今天是星期四')
if wday == 4:
print('今天是星期五')
if wday == 5:
print('今天是星期六')
if wday == 6:
print('今天是星期日')
# 組合
if wday == 0:
print('今天是星期一')
elif wday == 1: # else if
print('今天是星期二')
elif wday == 2:
print('今天是星期三')
elif wday == 3:
print('今天是星期四')
elif wday == 4:
print('今天是星期五')
elif wday == 5:
print('今天是星期六')
elif wday == 6:
print('今天是星期日')
```
#### 一個算命程式
```
import random
result = random.randint(1, 5) # 抽籤
if result == 1:
print('今天會撿到錢')
elif result == 2:
print('明天加薪')
elif result == 3:
print('今天中彩卷')
elif result == 4:
print('今天摸彩抽到iPhone')
else:
print('今天踩到大便')
```
#### 還是判斷式
```
a = 4
b = 9
c = 6
# 判斷式一號
if a > b and b > c:
print('ok1')
else:
print('not ok1') # V
# 判斷式二號
if a > b or b > c:
print('ok2') # V
else:
print('not ok2')
# 判斷式三號
if a > b > c: # if a > b and b > c:
print('ok3')
else:
print('not ok3') # V
# 判斷式四號
# 巢狀判斷式
if a > b:
if b > c:
print('ok4')
else:
print('not ok4') # V
```
#### 判斷奇偶數
```
user = int(input('請輸入一個數字:'))
print('你輸入的是:', end='')
# if user % 2 == 0:
# print('偶數')
# else:
# print('奇數')
print('偶數' if user % 2 == 0 else '奇數')
```
#### 本日最終時戰練習: 找出人生藥局的快篩剩餘量
```python=
import csv
with open('Fstdata.csv', 'r', encoding='utf-8') as my_file:
for line in my_file:
col = line.split(',')
if '人生' in col[1]:
print(col[1], '快篩剩餘:', col[7])
```
說明:
1. 全台快篩示計剩餘量可至政府的OpenData網站下載: https://data.gov.tw/dataset/152408
2. 下載的Fstdata.csv檔案須跟程式碼放在同一個目錄下