Tutorial Python untuk bukan pengatur cara/Ungkapan Boolean: Perbezaan antara semakan

Kandungan dihapus Kandungan ditambah
Aurora (bincang | sumb.)
Algazel (bincang | sumb.)
Tiada ringkasan suntingan
Baris 74:
Perhatikan bahawa sekiranya ungkapan pertama itu palsu, Python tidak akan memeriksa ungkapan kedua kerana ia tahu bahawa seluruh ungkapan itu palsu.
 
TheBaris next lineberikut, <code>print 5, not a == 7 and b == 7</code>, usesmengguna theoperator <code>not</code>. operator. Hasil <code>not</code> justialah giveslawan theungkapan opposite of the expressiontersebut. (TheUngkapan expressionboleh couldditulis besemula rewritten assebagai <code> print 5, a != 7 and b == 7</code>). Here is theIni tablerajahnya:
 
{| class="wikitable"
!ungkapan
!expression
!hasil
!result
|-
|<code>not</code> true
Baris 87:
|}
 
TheDua twobaris followingberikutan linesitu, <code>print 6, a == 7 or b == 7</code> anddan <code>print 7, a == 7 or b == 6</code>, usemengguna theoperator <code>or</code> operator. TheOperator <code>or</code> operator returnsmemulangkan <tt>true</tt> ifjika theungkapan firstpertama expression is truebenar, ordan ifjika theungkapan secondkedua expressionbenar isatau truekedua-duanya orbenar. bothJika arekedua-duanya true.tidak benar If(yakni, neitherpalsu), areia trueakan it returnsmemulangkan <tt>false</tt>. Ini Here's the tablerajahnya:
 
{| class="wikitable"
!ungkapan
!expression
!hasil
!result
|-
|true <code>or</code> true
Baris 106:
|}
 
Perhatikan bahawa sekiranya ungkapan pertama itu <tt>true</tt> Python tidak akan memeriksa ungkapan kedua oleh sebab ia tahu bahawa seluruh ungkapan itu benar. Ini dapat dilakukan kerana <code>or</code> itu benar jika sekurang-kurangnya separuh ungkapan itu benar. Bahagian pertama itu benar, jadi bahagian kedua mungkin palsu atau benar, akan tetapi seluruh ungkapan itu tetap benar.
Notice that if the first expression is true Python doesn't check the second expression since it knows the whole expression is true. This works since <code>or</code> is true if at least one half of the expression is true. The first part is true so the second part could be either false or true, but the whole expression is still true.
 
TheDua nextbaris two linesberikutnya, <code>print 8, not (a == 7 and b == 6)</code> anddan <code>print 9, not a == 7 and b == 6</code>, showmenunjukkan thatbahawa parenthesestanda cankurungan beboleh useddigunakan tountuk groupmengumpul expressionsungkapan and force one partdan tomemaksa besebahagiannya evaluateddinilai firstdahulu. Perhatikan Noticebahawa thattanda thekurungan parenthesesmenukar changedungkapan the expression fromdaripada <tt>false</tt> toke <tt.true</tt>. This occurredIni sinceberlaku thekerana parenthesestanda forcedkurungan thememaksa <code>not</code> todikenakan applyterhadap toseluruh theungkapan wholedan expressiontidak insteadhanya ofpada just thebahagian <code>a == 7</code> portion.
 
HereIni issatu ancontoh examplepenggunaan of using aungkapan boolean expression:
<source lang="python">
list = ["LifeHayat", "The UniverseAlam", "EverythingSemua", "Jack", "Jill", "LifeHayat", "Jill"]
 
# makebuat a copy of thesalinan list. SeeLihat thebab MoreTambahan ontentang ListsList chapteruntuk tomendapatkan explainpenerangan whattentang [:] means.
copysalinan = list[:]
# isih salinan
# sort the copy
copysalinan.sort()
prevsebelum = copysalinan[0]
del copysalinan[0]
 
count = 0
 
# hurai list untuk mencari padanan
# go through the list searching for a match
while count < len(copysalinan) and copysalinan[count] != prevsebelum:
prevsebelum = copysalinan[count]
count = count + 1
 
# IfJika apadanan matchtidak wasditemu, nottak found thenmungkin count can't be < len
# sinceoleh thesebab whilegelung loopberterusan continues whileselagi count is < len
# dan tiada padanan ditemui
# and no match is found
 
if count < len(copysalinan):
print "FirstPadanan MatchPertama:", prevsebelum
</source>
 
Dan ini outputnya:
And here is the output:
 
FirstPadanan MatchPertama: Jill
 
ThisProgram programini worksterlaksana bydengan continuingia tosentiasa checkmencari forpadanan matchselagi <code>while count < len(copysalinan) anddan copysalinan[count] is nottidak equalsama todengan prevsebelum</code>. WhenApabila eithersama ada <code>count</code> islebih greaterdaripada thanindeks the last index ofterakhir <code>copysalinan</code> orataupun apadanan matchditemui has been found thejusteru <code>and</code> istidak nolagi longerbenar truedan sogelung theberhenti loopatau exitskeluar. TheOperator <code>if</code> simplyhanya checksmemeriksa toagar makememastikan sure that theyang <code>while</code> exitedkeluar becausekerana atiada matchpadanan was foundditemui.
 
The other "trick" of <code>and</code> is used in this example. If you look at the table for <code>and</code> notice that the third entry is "false and won't check". If <code>count >= len(copy)</code> (in other words <code>count < len(copy)</code> is false) then <code>copy[count]</code> is never looked at. This is because Python knows that if the first is false then they can't both be true. This is known as a short circuit and is useful if the second half of the <code>and</code> will cause an error if something is wrong. I used the first expression (<code>count < len(copy)</code>) to check and see if <code>count</code> was a valid index for <code>copy</code>. (If you don't believe me remove the matches "Jill" and "Life", check that it still works and then reverse the order of <code>count < len(copy) and copy[count] != prev</code> to <code>copy[count] != prev and count < len(copy)</code>.)