自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python邏輯操作的三大重要環(huán)節(jié)

開發(fā) 后端
Python邏輯操作需要我們主要的地方有很多,其中有三個最重要的環(huán)節(jié),下面我們就看看如何才能更好的避免這三個環(huán)節(jié)出錯誤。

Python邏輯操作的過程中有三個重要的環(huán)節(jié)。每一個都需要我們十分注意,下面我們就看看如何才能更好的運用Python邏輯操作中三個環(huán)節(jié)來完善我們的應(yīng)用。希望大家在今后的學(xué)習(xí)中有所收獲。舉例:

  1. #coding:utf-8  
  2. test1 = 12 
  3. test2 = 0 
  4. print (test1 > test2) and (test1 > 14) #result = False 
  5. print (test1 < test2) or (test1 > -1) #result = True 
  6. print (not test1) #result = False 
  7. print (not test2) #result = True 

 

 

 

嚴格的說,邏輯操作符的操作數(shù)應(yīng)該為布爾表達式。但Python對此處理的比較靈活。即使操作數(shù)是數(shù)字,解釋器也把他們當成“表達式”。非0的數(shù)字的布爾值為1,0的布爾值為0.

舉例:

  1. #coding:utf-8  
  2. test1 = 12 
  3. test2 = 0 
  4. print (test1 and test2) #result = 0 
  5. print (test1 or test2) #result = 12 
  6. print (not test1) #result = Flase 
  7. print (not test2) #reslut = True 

在Python中,空字符串為假,非空字符串為真。非零的數(shù)為真。
數(shù)字和字符串之間、字符串之間的邏輯操作規(guī)律是:
對于and操作符:只要左邊的表達式為真,整個表達式返回的值是右邊表達式的值,否則,返回左邊表達式的值。對于or操作符:只要兩邊的表達式為真,整個表達式的結(jié)果是左邊表達式的值。如果是一真一假,返回真值表達式的值,如果兩個都是假,比如空值和0,返回的是右邊的值。(空值或0)

舉例:

  1. #coding:utf-8   
  2. test1 = 12   
  3. test2 = 0   
  4. test3 = ''   
  5. test4 = "First"   
  6. print test1 and test3 #result = ''   
  7. print test3 and test1 #result = ''   
  8. print test1 and test4 #result = "First"   
  9. print test4 and test1 #result = 12 
  10. print test1 or test2 #result = 12 
  11. print test1 or test3 #result = 12 
  12. print test3 or test4 #result = "First" 
  13. print test2 or test4 #result = "First" 
  14. print test1 or test4 #result = 12 
  15. print test4 or test1 #result = "First" 
  16. print test2 or test3 #result = '' 
  17. print test3 or test2 #result = 0 

以上就是對Python邏輯操作的相關(guān)介紹。希望大家有所收獲。

【編輯推薦】

  1. Python源碼的三大應(yīng)用技術(shù)
  2. Python循環(huán)如何解決無限循環(huán)
  3. Python編程不同于其他編程語言的優(yōu)點
  4. Python語言開發(fā)中的六大應(yīng)用方式
  5. Python連接數(shù)據(jù)庫如何解決中文亂碼
責(zé)任編輯:張浩 來源: 博客園
相關(guān)推薦

2010-03-11 19:45:09

Python邏輯

2021-03-08 15:24:18

云計算私有云云安全

2023-12-31 13:05:19

pytorch深度學(xué)習(xí)框架

2009-06-19 11:38:15

JavaFX 1.2

2020-09-06 08:26:59

Python 3開發(fā)代碼

2012-11-21 10:45:06

信息安全信息安全事件

2015-11-04 12:02:46

桌面虛擬化

2019-02-26 09:00:00

人工智能AI保險

2010-04-16 09:27:36

2009-10-26 17:07:17

VB.NET UNDO

2013-09-25 17:06:34

Windows Azu微軟CloudOS

2020-05-28 16:38:50

新基建智慧城市重構(gòu)

2010-03-10 13:42:44

Python腳本

2010-03-11 15:01:52

Python源碼

2018-06-14 14:07:57

Pythonweb框架

2010-09-30 16:06:21

J2ME平臺

2024-11-14 11:00:00

Python繼承機制

2017-10-18 23:15:52

物聯(lián)網(wǎng)產(chǎn)業(yè)鏈網(wǎng)絡(luò)

2012-08-24 11:00:31

2009-01-11 10:11:22

Linux操作系統(tǒng)測試
點贊
收藏

51CTO技術(shù)棧公眾號