● Branching
making a decision to take one path or another.
● Conditions
Simple Condition: only one condition
Compound Condition: the combination of two or more conditions
● Logical Operator
◎ " not " Logical Operator
not True = False
not False = True
◎ " and " Logical Operator
True and True = True
True and False = False
False and True = False
False and False = False
Note: " and " can connect as much as
conditions we need,
but as long as there's one False condition,
the compound condition is False
◎ " or " Logical Operator
True or True = True
True or False = True
False or True = True
False or False = False
Note: " or " can connect as much as
conditions we need,
but as long as there's one True condition,
the compound condition is True
● Using " in " Operator
Uses in conditon statements
Input:
print "e" in "this is a test message"
print "ess" in "this is a test message"
Output:
True
True
● If, elif and else
if <condition> :
<statement>
elif <condition> :
<statement>
...
elif <condition> :
<statement>
else:
<statement>
Note:
Each consecutive lines of code is called a " BLOCK ".
Only one block will be executed.
If there's more then one condition is TRUE,
the block of the first true condition will be executed.
● while Loop
while <condition> :
<statement>
Note: While the condition is TRUE, the code will be executed repeatedly
● Comparison Operators
== equal to
!= not equal to
> greater then
< less then
>= greater then or equal to
<= less then or equal to
● International Infinite Loop, break and continue
◎ Input:
count = 0
while True:
count += 1
# end loop if count is greater then 7.
if count > 7:
break
# skip 2
if count == 2:
continue
print count
◎ Output:
1
3
4
5
6
7
Note:
The condition is always true, so this is a international infinite loop.
When the code hits " continue ", the loop skip the rest of the codes
and starts from the first line of the block.
When the code hits " break ", the loop ended.
● for Loop
for < i > in <main squence> :
<statement>
Note: A for loop repeats its loop body
for each element of the sequesce.
In this case, < i > is the varible
that represents the elements in the sequence
and <main squence> is the sequence.
留言列表