DAY 4
3
Data Technology

## [Day04]Python的基本運算！（下）

[Day03] Python的基本運算！（上）

## 布林值

``````mybool = True
print(mybool)
``````

### 比較運算子

``````== 等於
!= 不等於
> 大於
< 小於
>= 大於等於
<= 小於等於
``````

``````print('1',8 == 8)
print('2',8 != 8)
print('3',2 < 0)
print('4',2 <= 2)
print('5','hello' == 'hello')
print('6','hello'== 'Hello')
``````

``````1 True
2 False
3 False
4 True
5 True
6 False
``````

### 布林運算子

and、or、not三個布林運算子是用來比較布林值的，回傳的值也是`True``False`

• and: 兩者都成立才為true
• or: 其中一個成立就為true
• not: 反向

``````print('1',True and True)
print('2',True and False)
print('3',True or False)
print('4',False or False)
print('5',not True)
print('6',not False)
``````

``````1 True
2 False
3 True
4 False
5 False
6 True
``````

### 混合使用

``````(4 > 5) and (1 == 1)
``````

## 流程控制

### if else

``````name = 'apple'
if name == 'apple':
print('hello apple')
else:
print('you are not apple')
``````

### if elif else

``````name = 'apple'
if name == 'banana':
print('hello banana')
elif name == 'apple':
print('hello apple')
elif name == 'pinapple':
print('hello pinapple')
else:
print('you are not apple')
``````

## while 迴圈陳述句

``````count = 0
while count < 5:
print(count)
count = count +1
``````

``````count = 0
if count < 5:
print(count)
count = count +1
``````

### 陷入無窮迴圈？

``````while True:
print('hello!')
``````

### for 迴圈

``````for i in range(3,5):
print(i)
``````

``````for i in range(3,9,2):
print(i)
``````

## 函式

``````def my_first_func():
print('Call this function!')
my_first_func()
``````

def 後面加上函式的名稱，在上面我們將我們的函式取名叫做`my_first_func`（記得縮排）接著我們在程式區塊外呼叫它。

``````Call this function!
``````

## 總結

``````import random
scrnum = random.randint(1,10)
print('guess a number between 1 and 10')
for geuss_num in range(1,7):
print('guess!')
guess = int(input())
if guess < scrnum:
print('too low')
elif guess > scrnum:
print('too high')
else:
print('good job!')
break
``````