Boolean Operations
•
We can do calculations with boolean variables just like with integer
variables
•
The boolean operations are: and or not
•
Comparison operators < > <= >= == != return boolean (True or
False)
Boolean Operators
( x == 4 ) and (y ==2)
True if both expressions
are true.
(x == 4) or (y == 2)
Evaluates to true if either
expression is true.
not ( x == 4)
Not “flips” the logic
- True becomes
False and False
becomes True.
P Q P and Q
T T T
F T F
T F F
F F F
P Q P or Q
T T T
F T T
T F T
F F F
P not P
T F
F T
One-Way Decisions
x = 5
print 'Before 5'
if x == 5 :
print 'Is 5'
print 'Is Still 5'
print 'Third 5'
print 'Afterwards 5'
print 'Before 6'
if x == 6 :
print 'Is 6'
print 'Is Still 6'
print 'Third 6'
print 'Afterwards 6'
Before 5
Is 5
Is Still 5
Third 5
Afterwards 5
Before 6
Afterwards 6
X == 5 ?
print 'Is 5'
Yes
print 'Still 5'
print 'Third 5'
No
Indentation
•
Increase indent indent after an if statement or for statement (after : )
•
Maintain indent to indicate the scope of the block (which lines are
affected by the if/for)
•
Reduce indent to back to the level of the if statement or for statement
to indicate the end of the block
•
Blank lines are ignored - they do not affect indentation
•
Comments on a line by themselves are ignored w.r.t. indentation
Warning
: Turn Off Tabs
•
Most text editors can turn tabs into spaces - make sure to enable this
feature
•
NotePad++: Settings -> Preferences -> Language Menu/Tab Settings
•
TextWrangler: TextWrangler -> Preferences -> Editor Defaults
•
Python cares a *lot* about how far line is indented. If you mix tabs
and spaces, you may get “indentation errors” even if everything looks
fine
Please do this now while you are thinking about it so we can all stay sane
x = 5
if x > 2 :
print 'Bigger than 2'
print 'Still bigger'
print 'Done with 2'
for i in range(5) :
print i
if i > 2 :
print 'Bigger than 2'
print 'Done with i', i
x = 5
if x > 2 :
# comments
print 'Bigger than 2'
# don’t matter
print 'Still bigger'
# but can confuse you
print 'Done with 2'
# if you don’t line
# them up
increase / maintain after if or for
decrease to indicate end of block
blank lines and comment lines ignored
Mental begin/end squares
x = 5
if x > 2 :
print 'Bigger than 2'
print 'Still bigger'
print 'Done with 2'
for i in range(5) :
print i
if i > 2 :
print 'Bigger than 2'
print 'Done with i', i
x = 5
if x > 2 :
# comments
print 'Bigger than 2'
# don’t matter
print 'Still bigger'
# but can confuse you
print 'Done with 2'
# if you don’t line
# them up
x > 1
print 'More than one'
x < 100
print 'Less than 100'
print 'All Done'
yes
yes
no
no
x = 42
if x > 1 :
print 'More than one'
if x < 100 :
print 'Less than 100'
print 'All done'
Nested
Decisions
x > 1
print 'More than one'
x < 100
print 'Less than 100'
print 'All Done'
yes
yes
no
no
x = 42
if x > 1 :
print 'More than one'
if x < 100 :
print 'Less than 100'
print 'All done'
Nested
Decisions
x > 1
print 'More than one'
x < 100
print 'Less than 100'
print 'All Done'
yes
yes
no
no
x = 42
if x > 1 :
print 'More than one'
if x < 100 :
print 'Less than 100'
print 'All done'
Nested
Decisions
Không có nhận xét nào:
Đăng nhận xét