# 資研 10/20 社課講義 [**提問表單**](https://forms.gle/z4dPC1JPV6uA2q9w7) [**上課PPT**](https://www.canva.com/design/DAFxsQgsjUg/zTTQihjCAB5wWg5DMwaTgw/view?utm_content=DAFxsQgsjUg&utm_campaign=designshare&utm_medium=link&utm_source=editor) --- :::warning **運算子 Operators** 1. **算數運算子 Arithmetic Operators** 2. **比較運算子 Comparison Operators** | == | > | < | >= | <= | != | |:----:|:----:|:----:|:---:|:---:|:---:| | 等於 | 大於 | 小於 | 大於(或)等於 | 小於(或)等於 | 不等於 | 3. **邏輯運算子 Logical Operators** | and | or | not | |:----:|:----:|:----:| | 且 | 或 | 否定 | >and:Returns True if both statements are true >or:Returns True if one of the statements is true >not:Reverse the result, returns False if the result is true 4. **身分運算子 Identity Operators** | is | is not | |:----:|:------:| | 是 | 不是 | >is:Returns True if both variables are the same object >is not:Returns True if both variables are not the same object 5. **位元運算子 Bitwise Operators** - 將數字轉為<font color="#CE0000">**2進位**</font>後,再運算 | & | \| | ^ | ~ | << | >> | |:----:|:----:|:----:|:---:|:---:|:---:| | 且 | 或 | 異或 | 非 | 左推 | 右推 | 6. **關係運算子 Membership Operators** | in | not in | |:----:|:------:| | 在...之中 | 不在...之中 | 7. **賦值運算子 Assignment Operators** | x = y | x += y | x -= y | x \*= y | x /= y | x %= y | x //= y | |:----:|:----:|:----:|:---:|:---:|:---:|:---:| | x = y | x = x+y | x = x-y |x = x\*y |x = x/y |x = x%y |x = x//y | | x \*\*= y | x &= y | x \|= y | x ^= y | x >>= y | x <<= y | |:-----:|:----:|:----:|:---:|:---:|:---:| | x = x\*\*y | x = x&y | x = x\|y | x = x^y | x = x>>y |x = x<<y | ```python= a = 12 b = 2 # 相等 print( a == b ) # 輸出:False # 大於或等於 print( a >= b ) # 輸出:True # 大於(字串) print( 'a' > 'b' ) # 輸出:False # 且 print( a==b and a>=b ) # 輸出:False # 或 print( a==b or a>=b ) # 輸出:True # 非(!=) print( a is not b ) # 輸出:True # a = a-b a -= b print( a ) # 輸出:10 # 二進位左移2位(不夠補0) print(bin(a<<2)) # 輸出:0b101000 # a = 1010 # b = 10 #a&b = 0010 print( a & b ) # 輸出:2 ``` ::: :::warning **資料型態-布林值 Booleans** - True / False - 1 / 0 ::: :::warning **二進位制** - 計算 - 十進位制轉二進位制 - 二進位制轉十進位制  >ob以後的才是二進位後的數值 ```python= # 10轉二進位 print(bin(a)) # 輸出:0b1010 # 2轉二進位 print(bin(b)) # 輸出:0b10 ``` ::: :::warning **ASCII碼** [**ASCII碼轉換表**](https://zh.wikipedia.org/zh-tw/ASCII) - 字符轉ASCII:ord() - ASCII轉字符:chr() ```python= # ASCII碼轉換-字符轉ASCII碼 print(ord('a')) # 輸出:97 # ASCII碼轉換-字符轉ASCII碼 print(ord('b')) # 輸出:98 # ASCII碼轉換-ASCII碼轉字符 print(chr(99)) # 輸出:c ``` ::: ## 選擇結構 If...Else Statement  - 架構: ```python= if '決策式A': '結果A' elif '決策式B': '結果B' else: '結果C(或其他)' ``` >elif 部分可以有很多個 >else(建議、可以用於debug)要有否則可能有資料位被歸類 - 例題 計算BMI並輸出分級值 讓使用者輸入年齡、生理性別、體重(kg)和身高(m),計算出BMI值,並依據BMI值顯示其分級 BMI=體重(kg)/身高<sup>2</sup>(m<sup>2</sup>) >年齡限16、17 >測資: >16 >女 >55 >1.6 >輸出:您的體重在正常範圍內 :::spoiler 範例程式碼  ```python= # 例題範例程式碼 A = input('請輸入年齡:') S = input('請輸入生理性別:') W = int(input('請輸入體重(kg):')) H = float(input('請輸入身高(m):')) BMI = W/(H**2) if S == '男': if A == 16: if BMI <= 17.4: print('您的體重過輕') elif BMI <= 23.3: print('您的體重在正常範圍內') elif BMI <= 25.6: print('您的體重過重') else: # BMI > 25.6 print('您的體重屬於肥胖') else: # 17 if BMI <= 17.8: print('您的體重過輕') elif BMI <= 23.5: print('您的體重在正常範圍內') elif BMI <= 25.6: print('您的體重過重') else: # BMI > 25.6 print('您的體重屬於肥胖') else: # 女 if A == 16: if BMI <= 17.1: print('您的體重過輕') elif BMI <= 22.7: print('您的體重在正常範圍內') elif BMI <= 25.3: print('您的體重過重') else: # BMI > 25.3 print('您的體重屬於肥胖') else: # 17 if BMI <= 17.3: print('您的體重過輕') elif BMI <= 22.7: print('您的體重在正常範圍內') elif BMI <= 25.3: print('您的體重過重') else: # BMI > 25.3 print('您的體重屬於肥胖') ``` :::  ## 練習題 **Zerojudge** [**連結**](https://zerojudge.tw/) >注意事項:zerojudge內的輸入不可使用<font color="#CE0000">提示字</font> :::spoiler i786.範例程式碼 ```python= a = int(input()) b = int(input()) print(a+b) print(a-b) print(a*b) print(a//b) print(a%b) ``` ::: :::spoiler i789.範例程式碼 ```python= a=int(input()) b=int(input()) if a>b : print(a,'>',b,sep='') elif a<b : print(a,'<',b,sep='') else: # a == b print(a,'==',b,sep='') ``` ::: --- ## 補充資料 **推薦網站:**[**W3Schools**](https://www.w3schools.com/)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up