# 運算符與表達式 ###### tags: `人生苦短,我學Python` ## 算數運算符 :::warning | +| - | * | | -------- | -------- | -------- | |/ | % | // | 以上都是常用的算術運算符 ::: - 舉個例子 ```python= python = 5 c = 4 print(python + c) #9 ``` ## 賦值運算符 * 賦與 :::info |x += y | x = x + y | | -------- | -------- | | x -= y | x = x - y | 左右兩邊的意思相等,不過就只是稍微少了點字 ::: ## 比較運算符 * 也稱為關係運算符 :::danger 比如大於(>)、小於(<)、等於(==)...... p.s.記住,==才是等於,=的意思是右邊的數值賦予給左邊 ::: * 舉例 ```python= a = 5 b = 4 print(a == b) #false ``` ## 邏輯運算符 :::danger and(和), or(或), not(不是) ::: * 舉例 ```python= a = 5 b = 5 print(a and b == 5) #true ``` ## 位運算符 :::info 1. 111,100的「&」 2. 101,011的「|」 3. 異或(xor) ::: ## 按位取反 * 舉例 ```python= a = 5 print(~a) #-6 #因為5的2進位是(0000 0101),反過來就變成(1111 1010),代表-6 ``` ## >> 和 << * << 代表 * 2 * >> 代表 / 2 ## 運算符的優先順序  ## 條件表達式 * 也就是所謂的if/else語句 ```python= a = 5 b = 5 if a > b : print('the same') else : print('not the same') #not the same ``` {%hackmd S1DMFioCO %}
×
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