目錄
- 一.return 邏輯判斷運算式 and
- 二.return 邏輯判斷運算式 or
- 三.return 邏輯判斷運算式 and 和 or 配合使用
- 四.return 邏輯判斷運算式重點總結
- 五.猜你喜歡
零基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門

一.return 邏輯判斷運算式 and
and:遇假則假,所以前面為假就不再執行后面代碼,直接回傳假;前面為真則繼續判斷執行后面代碼直到運算式結束或者出現假為止;
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python return邏輯判斷運算式.py
@Time:2021/04/11 07:57
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
def fun1():
# 所有條件都為真,回傳最后一個值
return "21" and True
def fun2():
# 檢測所有運算式,直到遇到假為止,并回傳假
return 54 and 1 and True and 0
def fun3():
# 遇到真,繼續后面的判斷,直到遇到假為止,如果遇見假直接回傳,不再繼續判斷
return 1 and True and False and 54 and 0
print(fun1())
print(fun2())
print(fun3())
'''
輸出結果:
True
0
False
'''
小敲門:
- 1.如果有假的運算式:回傳值為第一個假運算式的結果;
- 2.如果沒有假的運算式:回傳值為最后一個真運算式的結果;
二.return 邏輯判斷運算式 or
or:遇真則真,所以前面為真就不執行后面的代碼,直接回傳真;前面為假則繼續判斷執行后面直到運算式結束或者出現真為止;
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python return邏輯判斷運算式.py
@Time:2021/04/11 07:57
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
def fun1():
# 所有條件都為真,回傳第一個真的運算式
return "21" or True
def fun2():
# 所有條件都為假,直到遇到真為止,并回傳真,沒有真則回傳最后一個假
return "" or False or 0
def fun3():
# 直到遇到真為止,并回傳真,不在繼續后面的判斷
return 0 or True or False or 54 or 0
print(fun1())
print(fun2())
print(fun3())
'''
輸出結果:
21
0
True
'''
小敲門:
- 1.如果有真的運算式:回傳值為第一個真運算式的結果;
- 2.如果沒有真的運算式:回傳值為最后一個假運算式的結果;
三.return 邏輯判斷運算式 and 和 or 配合使用
and 和 or 配合使用:其實并沒有先后順序,運算式重前往后依次執行,上一個運算式的結果作為下一個運算式的開始;
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python return邏輯判斷運算式.py
@Time:2021/04/11 07:57
@Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累!
"""
def fun1():
'''
動作分解:
第一步:"21" and True 回傳結果 True
第二步:True or 1 回傳結果 True
注意第二步 True or 1 中的 True 是第一步回傳的結果并不是運算式中的True
'''
return "21" and True or 1 #等價:return (("21" and True) or 1)
def fun2():
'''
動作分解:
第一步:"" or False 回傳結果 False
第二步:False and 0 回傳結果 False
注意第二步 False and 0 中的 False 是第一步回傳的結果并不是運算式中的 False
'''
return "" or False and 0 #等價:return (("" or False) and 0)
def fun3():
'''
動作分解:
第一步:0 or True 回傳結果 True
第二步:True and False 回傳結果 False
第三步:False or 54 回傳結果 54
第四步:54 and 0 回傳結果 0
注意:上一步的結果作為下一步的開始
'''
return 0 or True and False or 54 and 0 #等價:return ((((0 or True) and False) or 54) and 0)
def fun4():
'''
動作分解:
第一步:0 and True and False 回傳結果 0
第二步:0 or 54 回傳結果 54
第三步:54 and 0 回傳結果 0
注意:上一步的結果作為下一步的開始
'''
return 0 and True and False or 54 and 0 #等價:return (((0 and True and False) or 54) and 0)
print(fun1())
print(fun2())
print(fun3())
print(fun4())
'''
輸出結果:
True
False
0
0
'''
四.return 邏輯判斷運算式重點總結
其實作為一個普通函式直接回傳字串或者其他資料型別就完了,為何非要這樣費力不討好?
學習學習,學習是一個程序,我想我們應該程序中成長,不然即使寫了一萬次hello world 又有何用?
return 邏輯判斷運算式 / 字典推導式 / 串列推導式 / 條件推導式 都是在各種開源專案中頻繁使用得寫法,這往往也是編程水平的一種提現,

五.猜你喜歡
- Python 字串/串列/元組/字典之間的相互轉換
- Python 區域變數和全域變數
- Python type 函式和 isinstance 函式區別
- Python is 和 == 區別
- Python 可變資料型別和不可變資料型別
- Python 淺拷貝和深拷貝
- Python 遞回函式
- Python sys 模塊
- Python 串列 list
- Python 元組 tuple
- Python 字典 dict
- Python 條件推導式
- Python 串列推導式
- Python 字典推導式
- Python 函式宣告和呼叫
- Python 不定長引數 *argc/**kargcs
未經允許不得轉載:猿說編程 ? Python return 邏輯判斷運算式
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/286528.html
標籤:其他
上一篇:PHP的DBA擴展學習
下一篇:Spi,微內核與插件化
