1.算數運算符
1.1.什麼是運算符?
運算符用於執行程序代碼運算,會針對一個以上操作上來進行運算。
例如:2 + 3 ,其操作數是2和3,二運算符則是"+"
1.2算數運算符表現形式有+ - * / //
** %
1.如果是兩個字符串相加,則會進行拼串操作
x = 'Logic'+ ' '+'Python'
print('x = ',x)
>>>x = Logic Python
2.如果兩個字符串相乘,則會對字符串進行復制操作,將字符串重置制定次數
3.除法運算符總會返回一個類型的值
4.//取整除運算 只會保留計算機的整數位,總會返回一個整型
取整除運算就是取商的值 6 / 4 = 1.。。。。。。2(取值爲1)
5 . 取模運算 % 求兩個數相除的餘數
取模運算就是取餘數 6 / 4=1.。。。。。。2(取值爲2)
6.冪運算 ** 求一個值的幾次冪(幾次方)
7.開平方運算 也會返回一個浮點型(開0.5次冪)
x = 25**0.5
2.賦值運算符
a = 10
30 = b(不行) 3 = 4(不行)
+= >>> x += 3——>相當於 x = x + 3
-= >>> x -= 3——>相當於 x = x - 3
*= >>> x *= 3 ——>相當於 x = x + 3
/=>>> x /= 3 ——>相當於 x = x / 3
//= >>> x //=3 ——>相當於 x = x // 3
%=>>> x %=3——>相當於 x = x % 3
** = >>> x **=3——>相當於 x = x **3
x = 2
x += 3 #5
x -= 3 #2
x *= 3 #6
x /= 3 #2.0
x //= 3 #0.0
x = 5
x %= 2 #1
print('x = ',x)
3.比較運算符
比較運算符就是用來比較兩個值之間的關係,總會返回一個布爾值
比較左側值是否大於右側值
= 比較左側值是否大於 或者等於右側值
< 比較左側值是否小於左側值
<= 左側值是否小於或者等於右側值
# 比較運算符
r = 10 > 20 # False
r = 30 > 20 # True
r = 30 < 20 # False
r = 20 >= 20 # True
r = 3 > True # 3 > 1
r = '2' > '1' # True
r = '2' > '11' # True
r = 'a' > 'b' #False
r = 'c' < 'd' #True
r = 'ab' > 'b' #False #只用比較 a 和 b的大小
#如果出現多個字符那麼我們比較的方式是對位比較
如果我們想比較id 怎麼辦?
可以用到 is 和 is not
is :比較兩個對象是否是一個對象, 比較的是對象的id
is not :比較兩個對象是否不是同一個對象,比較的是對象的id
r = 3 == 3 #True
r = 'Python' == 'Python' #True
r = 'bcd' == 'abc' #False
r = 1 == True #True
r = 0 is False # False
r = 1 is not True #False
>>> a = 'Jerry Python'
>>> max(a)
'y'
>>> min(a)
' '
>>> ord('y'),ord(' ')
(121, 32)
>>> 1 == True
True
>>> id(1),id(True)
(1831431184, 1830940896)
>>> 0 == False
True
>>> 1 = False
SyntaxError: can't assign to literal
>>> 0 = False
SyntaxError: can't assign to literal
>>> 1 = True
SyntaxError: can't assign to literal
>>> 0 == False
True
>>> 1 == True
True
>>> r = 'bcd' == 'abc'
>>> r
False
>>> r = 0 is False
>>> r
False
>>> r = 1 is not True
>>> r
True
>>> r = 9 is not True
>>> r
True
>>> r = 11 is not False
>>> r
True
>>> False is not True
True
>>> False is True
False
>>> 'abc' is not 'def'
True
>>> 'abc' is 'def'
False
>>> 0 == True
False
4.邏輯運算符
x > 1 and x > 2
not :邏輯非
可以對符號右側的值進行非運算
對於布爾值,非運算會對其進行取反運算 True ——> False, False——>True
對於非布爾值,會先將其轉換爲布爾值,然後再取反
例如: 0 空串 None 還有一些其他是空性的值會轉換成False,剩下的都是True
a = True
a = not a
a = None # False #對於非布爾值,會先將其轉換爲布爾值,然後再取反
a = not a # True
and : 邏輯與(找False,找到了就返回False,沒找到就輸出第二個字符串)
1.(看第一個字符串是否是False,如果是就直接返回第一個值,如果不是就直接返回第二個值)
2.可以對符號兩側的值進行與運算
3.只有在符號兩側都是True的情況下才會返回True, 只要有一個False就會返回False(說白了與運算就是找False)
r = True and True # True
r = True and False # False
r = False and True # False
r = False and False # False
True and print('Hello') # Hello (找False,第一個不是False,就直接返回第二個值)
False and print('Hello') # 空值 (找False,第一個值是False,就直接返回第一個值)
or :邏輯或(找True,找到了就返回True,沒找到就輸出第二個字符串)
1.(看第一個字符串是否是False,如果是就直接返回第一個值,如果不是就直接返回第二個值)
2.or 可以對符號兩側的值進行或運算
3.或運算兩個值只要有一個True,就會返回True,或運算就是找True(返回True,打印出來就是什麼都沒有)
r = True or True # True
r = True or False # True
r = False or True # True
r = False or False # False
True or print('Hello') # 空值
False or print('Hello') # Hello
非布爾值的與或運算
當我們對非布爾值進行或運算的時候,Python會將其當作布爾值進行運算,最終會返回原值
# True True
r = 1 and 2 # 2
# True True
r = 1 and 2 # 2
r = 1 and 0 # 0
r = 0 and None # 0
與運算的規則
與運算找False,如果第一個值是False,就直接返回第一個值,不看第二個值
如果第一個值不是False,則直接返回第二個值。
或運算的規則
或運算就是找True,如果第一個值是True,就直接返回第一個值,則不看第二個值,如果第二個值是False,則返回第二個值。
r = 1 or 2 # 1
r = 1 or 0 # 1
r = 0 or 1 # 1
r = None or 0 # 0
r = 1 < 2 < 3 # 1 < 2 and 2 < 3
5.條件運算符
條件運算符(三元運算符)
語法:語句1 if 條件表達式 else 語句2
print('Python') if True else print('Java')
>>>Python
a = 30
b = 40
print('a的值大') if a > b else print('b的值大')
>>>b的值大
a = 60
b = 40
max = a if a > b else b
print(max)
>>>60
# 現在有 a b c三個變量 請通過條件運算符獲取三個值當中的最大值
a = 10
b = 20
c = 30
max = a if a > b else b
max = max if max > c else c
print(max)
>>>30
# 一次性比較出來大小
a = 10
b = 50
c = 30
max = a if (a > b and a > c) else (b if b > c else c)
print(max)
6. 運算符的優先級
在Python中and的優先級大於or
運算符的優先級可以根據表格來查詢,規則:優先級越高越優先計算,如果優先級一樣,就從左往右依次計算:Python官網——>Documentation——>Python 3.X Docs——>Language Reference ——>Expressions——>Operator precedence
a = 2 or 3 and 4 # 結果爲2(在Python中and的優先級大於or)
# or 和 and 優先級一樣 從左往右運算 結果 4
# or 優先級高 結果 4
# and 優先級高 結果 2
a = not 4 > 2 and 5 > 6 or 3 < 4 # True ——>not T and F or T——>F or T——>T
a = not (4 > 2 and 5 > 6 or 3 < 4) # False——>not(T and F or T)——>not(F or T)
# ——>not T——>False
#優先級大小: not > and > or
#4 > 2——>True not True——>False ; 5 > 6——>False ;3 < 4——>True ; False and False or True ——>True
print(a)