<jupyter_start><jupyter_text>Python Control Flow Tools  1. if<jupyter_code>x = int(input("Please enter an integer: "))
if x < 0:
    x = 0
    print('Negative changed to zero')
elif x == 0:
    print('Zero')
elif (x == 1):
    print('Single')
else:
    print('More')<jupyter_output>Please enter an integer:  1<jupyter_text>记住几个特点：  1. `elif`是else if缩写2. 注意缩进  2. `for` python的`for`迭代语句和C语言等里面的for不同，它的目的在于按照迭代一个“序列”中的内容，而不是提供自定义的迭代方式，如：<jupyter_code>words = ['cat', 'window', 'defenestrate']
for w in words:
    print(w, len(w))<jupyter_output>cat 3
window 6
defenestrate 12<jupyter_text>如果在迭代时希望改变原来的值，则迭代原来值的拷贝，例如：<jupyter_code>for w in words[:]:  # Loop over a slice copy of the entire list.
    if len(w) > 6:
        words.insert(0, w)
print(words)<jupyter_output>['defenestrate', 'cat', 'window', 'defenestrate']<jupyter_text>3. `while` `while`循环，当条件成立时，继续循环。  注意循环体要缩进<jupyter_code># Fibonacci series:
# the sum of two elements defines the next
a, b = 0, 1
while a < 10:
    print(a)
    a, b = b, a+b<jupyter_output>0
1
1
2
3
5
8<jupyter_text>4. `range()`方法 `range`方法是被在迭代时使用，能够用来加强`for`语句，例如：<jupyter_code>for i in range(-10, -100, -30):
    print(i)<jupyter_output>-10
-40
-70<jupyter_text>尽管`range`方法看起来很像是产生了一个真的list，但并不是这样的。  它并不是返回一个真的序列，而是一个可以返回连续项的对象。  >In many ways the object returned by `range()` behaves as if it is a list, but in fact it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space.<jupyter_code>print(range(10))<jupyter_output>range(0, 10)<jupyter_text>使用`list`可以真的产生一个list，如：<jupyter_code>list(range(5))<jupyter_output><empty_output><jupyter_text>5. break & continue & 循环中的else语句 `break`&`continue`与其它语言一种，用来跳出与继续循环在python当中，`for`和`while`这些循环语句可以后接一个`else`子句。  当循环结束时，else子句执行。  但是如果循环语句是被`break`语句打断跳出的，则`else`子句不会执行。<jupyter_code>for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print(n, 'equals', x, '*', n//x)
            break
    else:
        # loop fell through without finding a factor
        print(n, 'is a prime number')<jupyter_output>2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3<jupyter_text>6. `pass` python中的`pass`语句在语句上需要，但是程序要求无行为。  有以下几个用途：  - 创建最小情况的类- 在写新代码时，作为一个占位符使程序可以运行，但是不引起副作用<jupyter_code>class MyEmptyClass:
    pass

def initlog(*args):
    pass   # Remember to implement this!<jupyter_output><empty_output><jupyter_text>7. function  7.1. 函数定义 使用`def`关键字定义新的function。  **注意**：  1. function总会有返回值，如果没有显示的`return`语句，会隐式的返回一个内部值`None`。2. 在function内部第一行可以写string，作为对于函数的说明3. 可以引用全局变量，但不要在函数体内部对全局变量进行赋值，这样会导致创建一个新的同名局部变量<jupyter_code>def fib(n):  # return Fibonacci series up to n
    """Return a list containing the Fibonacci series up to n."""
    result = []
    a, b = 0, 1
    while a < n:
        result.append(a)    # see below
        a, b = b, a+b
    return result
fib(1000)<jupyter_output><empty_output><jupyter_text>7.2. 函数参数   7.2.1. 默认函数参数可以为函数参数通过`=`提供默认值，但需要注意在定义时，参数指向的默认值就会被计算。  之后调用该函数时不会重新计算默认值。  这就导致了如果参数的默认值是可改变类型（list等），并且在函数体内部直接修改了参数的默认值，会导致参数一直保存之前的值。例如：<jupyter_code>def f(a, L=[]):
    L.append(a)
    return L

print(f(1))
print(f(2))
print(f(3))<jupyter_output>[1]
[1, 2]
[1, 2, 3]<jupyter_text>如果想改变这种参数值在不同调用中共享的情况，就不要直接修改参数值，而是赋给参数新的值。例如：<jupyter_code>def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L
print(f(1))
print(f(2))<jupyter_output>[1]
[2]<jupyter_text>7.2.2. 关键字参数在python函数定义当中，可以使用`keyarg=value`的形式定义关键字参数。  **注意**在函数调用时，必须保证关键字参数在位置参数之后出现，例如：<jupyter_code>def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
    print("-- This parrot wouldn't", action, end=' ')
    print("if you put", voltage, "volts through it.")
    print("-- Lovely plumage, the", type)
    print("-- It's", state, "!")

parrot('a thousand', state='pushing up the daisies')
# 错误的函数调用方式
parrot(voltage=5.0, 'dead')<jupyter_output><empty_output><jupyter_text>在函数定义的最后经常出现`*arg`和`**keywords`，前者是一个数组，接受多个输入，后者是一个字典，接受所有没有对应关键字参数key-word输入。  两者的顺序不能相反。例如：<jupyter_code>def cheeseshop(kind, *arguments, **keywords):
    print("-- Do you have any", kind, "?")
    print("-- I'm sorry, we're all out of", kind)
    for arg in arguments:
        print(arg)
    print("-" * 40)
    for kw in keywords:
        print(kw, ":", keywords[kw])

cheeseshop("Limburger", 
           "It's very runny, sir.",
           "It's really very, VERY runny, sir.",
           shopkeeper="<NAME>",
           client="<NAME>",
           sketch="Cheese Shop Sketch")<jupyter_output>-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
shopkeeper : <NAME>
client : <NAME>
sketch : Cheese Shop Sketch<jupyter_text>通常情况下可变数量参数`*arg`出现在参数列表的末尾，但是如果不是的话，所有出现在`*arg`之后的参数都必须是key-word的形式，例如：<jupyter_code>def concat(*args, sep="/"):
    return sep.join(args)

concat("earth", "mars", "venus")<jupyter_output><empty_output><jupyter_text>7.2.3. 解构参数list输入 有时候，在调用函数时，传入函数的值需要从list或者字典当中取出。  使用`*`解构list，使用`**`解构字典，例如：<jupyter_code>d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
parrot(**d)<jupyter_output>-- This parrot wouldn't VOOM if you put four million volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's bleedin' demised !<jupyter_text>7.2.4. 使用lambda创建简单function在python中使用`lambda`关键字创建简单的function，返回function object。例如：<jupyter_code>pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
pairs<jupyter_output><empty_output><jupyter_text>7.2.5. 使用function documentation strings在函数体第一行使用字符串来描述function。  该string第一行是对于函数目的的总结，应该是简短的。  如果希望有更多的描述，则空一行，在第三行进行描述。  描述的第一行决定了之后文本段落的缩进量。<jupyter_code>def my_function():
    """Do nothing, but document it.

          No, really, it doesn't do anything.
    second describing line;
  third describing line;
    """
    pass

print(my_function.__doc__)<jupyter_output>Do nothing, but document it.

          No, really, it doesn't do anything.
    second describing line;
  third describing line;<jupyter_text>7.2.6. function annotations> Function annotations are completely optional metadata information about the types used by user-defined functions.  函数的注解只是描述函数参数、返回信息的原数据。它不会有其它的影响，可以通过function的`__annotations__`属性查看，例如：<jupyter_code>def f(ham: str, eggs: str = 'eggs') -> str:
    print("Annotations:", f.__annotations__)

f('spam')<jupyter_output>Annotations: {'ham': <class 'str'>, 'eggs': <class 'str'>, 'return': <class 'str'>}
{'fraction_non_alphanumeric': 0.14984894259818732, 'fraction_numerical': 0.018731117824773415, 'mean_word_length': 4.823218997361478, 'pattern_counts': {'":': 4, '<': 74, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '231025', 'license': 'MIT', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3280, 'n_words': 573, 'path': 'python-notebook/2-Control Flow Tools.ipynb', 'repo_name': 'liuxiyang641/notebooks-641', 'size': '6644'}