⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node12.html

📁 Pythone Library reference. it is ok and simple.
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0062)http://www.honors.montana.edu/~jjc/easytut/easytut/node12.html -->
<!--Converted with LaTeX2HTML 99.2beta6 (1.42)original version by:  Nikos Drakos, CBLU, University of Leeds* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from:  Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>Boolean Expressions</TITLE>
<META content="Boolean Expressions" name=description>
<META content=easytut name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content=text/css http-equiv=Content-Style-Type><LINK 
href="node12_files/easytut.css" rel=STYLESHEET><LINK href="node13.html" 
rel=next><LINK href="node11.html" rel=previous><LINK href="easytut.html" 
rel=up><LINK href="node13.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node13.html" 
name=tex2html304><IMG align=bottom alt=next border=0 height=24 
src="node12_files/next.png" width=37></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html300><IMG align=bottom alt=up border=0 height=24 
src="node12_files/up.png" width=26></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node11.html" 
name=tex2html294><IMG align=bottom alt=previous border=0 height=24 
src="node12_files/prev.png" width=63></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html302><IMG align=bottom alt=contents border=0 height=24 
src="node12_files/contents.png" width=65></A> <BR><B>Next:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node13.html" 
name=tex2html305>Dictionaries</A> <B>Up:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html301>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node11.html" 
name=tex2html295>For Loops</A> &nbsp; <B><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html303>Contents</A></B> <BR><BR><!--End of Navigation Panel--><!--Table of Child-Links--><A 
name=CHILD_LINKS><STRONG>Subsections</STRONG></A> 
<UL>
  <LI><A 
  href="http://www.honors.montana.edu/~jjc/easytut/easytut/node12.html#SECTION001210000000000000000" 
  name=tex2html306>Examples</A> 
  <LI><A 
  href="http://www.honors.montana.edu/~jjc/easytut/easytut/node12.html#SECTION001220000000000000000" 
  name=tex2html307>Exercises</A> </LI></UL><!--End of Table of Child-Links-->
<HR>

<H1><A name=SECTION001200000000000000000>Boolean Expressions</A> </H1>Here is a 
little example of boolean expressions (you don't have to type it in): <PRE>a = 6
b = 7
c = 42
print 1, a == 6
print 2, a == 7
print 3,a == 6 and b == 7
print 4,a == 7 and b == 7
print 5,not a == 7 and b == 7
print 6,a == 7 or b == 7
print 7,a == 7 or b == 6
print 8,not (a == 7 and b == 6)
print 9,not a == 7 and b == 6
</PRE>With the output being: <PRE>1 1
2 0
3 1
4 0
5 1
6 1
7 0
8 1
9 0
</PRE>What is going on? The program consists of a bunch of funny looking 
<TT>print</TT> statements. Each <TT>print</TT> statement prints a number and a 
expression. The number is to help keep track of which statement I am dealing 
with. Notice how each expression ends up being either 0 or 1. In Python false is 
written as 0 and true is written as 1. The lines: <PRE>print 1, a == 6
print 2, a == 7
</PRE>print out a 1 and a 0 respectively just as expected since the first is 
true and the second is false. The third print, <CODE>print 3,a == 6 and b == 
7</CODE>, is a little different. The operator <TT>and</TT> means if both the 
statement before and the statement after are true then the whole expression is 
true otherwise the whole expression is false. The next line, <CODE>print 4,a == 
7 and b == 7</CODE>, shows how if part of an <TT>and</TT> expression is false, 
the whole thing is false. The behavior of <TT>and</TT> can be summarized as 
follows: 
<P>
<TABLE border=1 cellPadding=3>
  <TBODY>
  <TR>
    <TD align=left>expression</TD>
    <TD align=left>result</TD></TR>
  <TR>
    <TD align=left>true and true</TD>
    <TD align=left>true</TD></TR>
  <TR>
    <TD align=left>true and false</TD>
    <TD align=left>false</TD></TR>
  <TR>
    <TD align=left>false and true</TD>
    <TD align=left>false</TD></TR>
  <TR>
    <TD align=left>false and false</TD>
    <TD align=left>false</TD></TR></TBODY></TABLE>
<P>Notice that if the first expression is false Python does not check the second 
expression since it knows the whole expression is false. 
<P>The next line, <CODE>print 5,not a == 7 and b == 7</CODE>, uses the 
<TT>not</TT> operator. <TT>not</TT> just gives the opposite of the expression 
(The expression could be rewritten as <TT>print 5,a != 7 and b == 7</TT>). Heres 
the table: 
<P>
<TABLE border=1 cellPadding=3>
  <TBODY>
  <TR>
    <TD align=left>expression</TD>
    <TD align=left>result</TD></TR>
  <TR>
    <TD align=left>not true</TD>
    <TD align=left>false</TD></TR>
  <TR>
    <TD align=left>not false</TD>
    <TD align=left>true</TD></TR></TBODY></TABLE>
<P>The two following lines, <CODE>print 6,a == 7 or b == 7</CODE> and 
<CODE>print 7,a == 7 or b == 6</CODE>, use the <TT>or</TT> operator. The 
<TT>or</TT> operator returns true if the first expression is true, or if the 
second expression is true or both are true. If neither are true it returns 
false. Here's the table: 
<P>
<TABLE border=1 cellPadding=3>
  <TBODY>
  <TR>
    <TD align=left>expression</TD>
    <TD align=left>result</TD></TR>
  <TR>
    <TD align=left>true or true</TD>
    <TD align=left>true</TD></TR>
  <TR>
    <TD align=left>true or false</TD>
    <TD align=left>true</TD></TR>
  <TR>
    <TD align=left>false or true</TD>
    <TD align=left>true</TD></TR>
  <TR>
    <TD align=left>false or false</TD>
    <TD align=left>false</TD></TR></TBODY></TABLE>
<P>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 
<TT>or</TT> 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. 
<P>The next two lines, <CODE>print 8,not (a == 7 and b == 6)</CODE> and 
<CODE>print 9,not a == 7 and b == 6</CODE>, show that parentheses can be used to 
group expressions and force one part to be evaluated first. Notice that the 
parentheses changed the expression from false to true. This occurred since the 
parentheses forced the <TT>not</TT> to apply to the whole expression instead of 
just the <TT>a == 7</TT> portion. 
<P>Here is an example of using a boolean expression: <PRE>list = ["Life","The Universe","Everything","Jack","Jill","Life","Jill"]

#make a copy of the list
copy = list[:]
#sort the copy
copy.sort()
prev = copy[0]
del copy[0]

count = 0

#go through the list searching for a match
while count &lt; len(copy) and copy[count] != prev:
    prev = copy[count]
    count = count + 1

#If a match was not found then count can't be &lt; len
#since the while loop continues while count is &lt; len
#and no match is found
if count &lt; len(copy):
    print "First Match: ",prev
</PRE>
<P>And here is the output: <PRE>First Match:  Jill
</PRE>
<P>This program works by continuing to check for match <TT>while count &lt; 
len(copy and copy[count]</TT>. When either <TT>count</TT> is greater than the 
last index of <TT>copy</TT> or a match has been found the <TT>and</TT> is no 
longer true so the loop exits. The <TT>if</TT> simply checks to make sure that 
the <TT>while</TT> exited because a match was found. 
<P>The other `trick' of <TT>and</TT> is used in this example. If you look at the 
table for <TT>and</TT> notice that the third entry is ``false and won't check''. 
If <TT>count &gt;= len(copy)</TT> (in other words <TT>count &lt; len(copy)</TT> 
is false) then copy[count] is never looked at. This is because Python knows that 
if the first is false then they both can't be true. This is known as a short 
circuit and is useful if the second half of the <TT>and</TT> will cause an error 
if something is wrong. I used the first expression (<TT>count &lt; 
len(copy)</TT>) to check and see if <TT>count</TT> was a valid index for 
<TT>copy</TT>. (If you don't believe me remove the matches `Jill' and `Life', 
check that it still works and then reverse the order of <TT>count &lt; len(copy) 
and copy[count] != prev</TT> to <TT>copy[count] != prev and count &lt; 
len(copy)</TT>.) 
<P>Boolean expressions can be used when you need to check two or more different 
things at once. 
<P>
<H1><A name=SECTION001210000000000000000>Examples</A> </H1>
<P>password1.py <PRE>## This programs asks a user for a name and a password.
# It then checks them to make sure the the user is allowed in.

name = raw_input("What is your name? ")
password = raw_input("What is the password? ")
if name == "Josh" and password == "Friday":
    print "Welcome Josh"
elif name == "Fred" and password == "Rock":
    print "Welcome Fred"
else:
    print "I don't know you."
</PRE>
<P>Sample runs <PRE>What is your name? Josh
What is the password? Friday
Welcome Josh

What is your name? Bill
What is the password? Money
I don't know you.
</PRE>
<P>
<H1><A name=SECTION001220000000000000000>Exercises</A> </H1>
<P>Write a program that has a user guess your name, but they only get 3 chances 
to do so until the program quits. 
<P>
<HR>
<!--Navigation Panel--><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node13.html" 
name=tex2html304><IMG align=bottom alt=next border=0 height=24 
src="node12_files/next.png" width=37></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html300><IMG align=bottom alt=up border=0 height=24 
src="node12_files/up.png" width=26></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node11.html" 
name=tex2html294><IMG align=bottom alt=previous border=0 height=24 
src="node12_files/prev.png" width=63></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html302><IMG align=bottom alt=contents border=0 height=24 
src="node12_files/contents.png" width=65></A> <BR><B>Next:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node13.html" 
name=tex2html305>Dictionaries</A> <B>Up:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html301>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node11.html" 
name=tex2html295>For Loops</A> &nbsp; <B><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html303>Contents</A></B> <!--End of Navigation Panel-->
<ADDRESS>Josh Cogliati <A 
href="mailto:jjc@honors.montana.edu">jjc@honors.montana.edu</A> 
</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -