Python 2
一. Conditional execution
x = 5
if x < 10
print 'smaller'
if x < 20
print 'bigger'
print 'finis'
1. Comparison Operators
"=" is used for assignment(用来附值)
"==" is representative of "Equal to"
“!=” is representative of "Not equal"
Boolean expressions ask a question and produce a Yes or No result which we use to control program flow.
Boolean expressions using comparison operators evaluate to - True/False - Yes/No.
Comparison operators look at variables but do not change the variables.
2. One-Way Decisions
3. Indentation
Increase 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 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 with regard to indentation.
4. Nested Decisions(在if statement下再套一个condition, 即套的condition是基于if statement的condition一起使用),如:
x = 42
if x > 1 :
print 'more than one'
if x < 100 :
print 'less than 100'
print 'all done'
5. Two-way Decisions
Sometimes we want to do one thing if a logical expression is true and something else if the expression is false.
It is like a fork in the road- we must choose one or the other path but not both.
如:x = 4
if x > 2 :
print ('bigger')
else :
print ('smaller')
print ('all done')
6. Multi-way Decisions 如下所示
if x < 2 :
print ('small')
elif x < 10 :
print ('medium')
else :
print ('large')
print ('all done')
7. The try/except Structure (对code试错)
You surround a dangerous section of code with try and except.
If the code in the try works- the except is skipped ; If the code in the try fails- it jumps to the except section. 如:
astr = 'Bob'
try:
print ('hello')
istr = int (astr)
print ('there')
except:
istr = -1
print ('done', istr)
该代码在istr=int(astr)会报错,因此python会跳过该行代码,继续执行except的命令。
二. Loop execution (循环语句执行)
1. Repeated Steps- 用 ‘while’,要确保在while中的指令能最终跳出loop,否则会一直不停循环,直到报错
Loops (repeated steps) have iteration variables that change each time through a loop. Often these iteration variables go through a sequence of numbers. 如:
n = 5
while n > 0 :
print (n)
n = n - 1
print ('blastoff!')
print (n)
2. Breaking Out of a Loop (跳出loop,执行以后的指令)
The break statement ends the current loop and jumps to the statement immediately following the loop. It is like a loop test that can happen anywhere in the body of the loop (跳出循环指令结束). 如:
while True :
line = input ('> ')
if line == 'done' :
break
print (line)
print ('Done!')
3. Finishing an Iteration with continue (不会跳出loop,跳回重新开始loop,不会再执行以后的指令)
The continue statement ends the current iteration and jumps to the top of the loop and starts the next iteration. (continue结束当前loop,从头开始loop)如:
while True :
line = input ('> ')
if line [0] == '#' :
continue
if line == 'done' :
break
print (line)
print ('Done!')
4. Indefinite Loops
While loops are called "indefinite loops" because they keep going until a logical condition becomes False.
The loops we have seen so far are pretty easy to examine to see if they will terminate or if they will be "infinite loops".
Sometimes it is a little harder to be sure if a loop will terminate.
5. Definite Loops
Quite often we have a list (list()或者[])of items of the lines in a file- effectively a finite set of things.
We can write a loop to run the loop once for each of the items in a set using the Python for construct.
These loops are called "definite loops" because they execute an exact number of times.
We say that "definite loops iterate through the members of a set". 如:
for i in [5, 4, 3, 2, 1] :
print (i)
print ('Blastoff!')
another example:
friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
print ('Happy New Year: ', friend)
print ('Done!')
Definite loops (for loops) have explicit iteration variables that change each time through a loop. These iteration variables move through the sequence or set.
for i in [5, 4, 3, 2, 1] :
print (i)
i是iteration variable,中括号中的数字是five-element sequence, print部分是block(body).
The iteration variable "iterates" through the sequence (ordered set).
The block (body) of code is executed once for each value in the sequence.
The iteration variable moves through all of the values in the sequence.
What we do in loops? The trick is "knowing" something about the whole loop when you are stuck writing code that only sees one entry at a time.
Set some variables to initial values
for thing in data :
Look for something or do something to each entry separately, updating a variable
Look at the variables(跳出loop后variable执行的指令)
Finding the largest value(for...in 应用,见google colab)
6. The "is" and "is not" Operators (类似SQL)
7. Counting in a Loop- To count how many times we execute a loop, we introduce a counter variable that starts at 0 and we add one to it each time through the loop. 如:
zork = 0
print ('Before', zork)
for thing in [9, 41, 12, 3, 74, 15] :
zork = zork + 1 (或zork += 1)
print (zork, thing)
print ('After', zork)
8. Summing in a Loop- To add up a value we encounter in a loop, we introduce a sum variable that starts at 0 and we add the value to the sum each time through the loop. 如:
zork = 0
print ('Before', zork)
for thing in [9, 41, 12, 3, 74, 15]
zork = zork + thing
print (zork, thing)
print ('After', zork)
9. Filtering in a Loop- We use an if statement in the loop to catch/filter the values we are looking for. 如:
print ('Before')
for value in [9, 41, 12, 3, 74, 15] :
if value > 20 :
print ('large number', value)
print ('After')
10. Search Using a Boolean Variable- If we just want to search and know if a value was found, we use a variable that starts at False and is set to True as soon as we find what we are looking for. 如:
found = False
print ('Before', found)
for value in [9, 41, 12, 3, 74, 15] :
if value == 3 :
found = True
print (found, value)
print ('After', found)