DAY 2
0

## 變數

### 類型

str `x = 'hello'`
int `x = 1`
float `x = 0.1`
bool `x = True`
list `x = [1, 2, 3]`
dict `x = {"A": "apple", "B", "ball"}`
tuple `x = (1, 2)`

### 命名原則

• 請都使用英文來命名，雖然中文也可以使用，但是非常不推薦
• 開頭用英文、或底線，不要用數字開頭
• pythob 保留關鍵字不要使用
• 大小寫有差異，會變成不同變數，請自己注意
• 基本上強烈建議命名有意義的名稱，免得自己未來看不懂(或是害到你的專案成員不懂)

### 查看型態

``````type('hello')
>>> str
``````

``````isinstance('hello', str)
>>> True
``````

### 變更型態

``````a = int('1')
print(a)
>>> 1
``````
``````a = float('1')
print(a)
>>> 1.0
``````

## 資料運算

### 四則運算

python 有運算的順序，依序為次方、乘法、除法、加法、減法等等。但是我覺得大家可以不用記得這些，記得小時候有學括號，就括起來就對囉～

``````# 加法
x = 1 + 2
print(x)
# 減法
x = 8 - 2
print(x)
# 乘法
x = 9 * 2
print(x)
# 除法
x = 10 / 5
print(x)
# 餘數
x = 15 % 7
print(x)
# 除法求商
x = 15 // 7
print(x)
# 次方
x = 8 ** 2
print(x)
``````

• == 是數學符號的等於
``````1 == 2
>>> False
``````
• ＝ 是賦予的值的概念
``````x = 1
``````

### 不同資料型態運算的坑

``````a = 5.0
b = 6
c = a + b
print(c, type(c))
>>> 11.0 <class 'float'>
``````

## 註解

``````# 我不是程式碼

x = '我是程式碼'

"""

"""
``````

## 斷行

``````# Bad
x = 1 ; y = 1
# Good
x = 1
y = 1
``````

``````a = b = c = 50
y = a + \
b + \
c
``````

## 字串

``````# Bad
string = ' We're the world. '

# Good
string = " We're the world. "
``````

``````a = 'My'
b = 'name'
c = 'is'
print( a + ' ' + b + ' ' + c + '...')
``````

``````name = input('Your name is:')