<jupyter_start><jupyter_text>函数- 函数可以用来定义可重复代码，组织和简化- 一般来说一个函数在实际开发中为一个小功能- 一个类为一个大功能- 同样函数的长度不要超过一屏  定义一个函数def function_name(list of parameters):        do something![](../Photo/69.png)- 以前使用的random 或者range 或者print.. 其实都是函数或者类<jupyter_code>def fun_name():
    pass
def fun_name():
    print('tiancai')
fun_name()    #() 代表调用
def jiou():
    '''
    这是判断奇偶de
    '''
    num_ = eval(input('>>'))
    if num_ % 2 == 0:
        print('ou')
    else:
        print('ji')
jiou()
def su():
    i = eval(input('>>'))
    for i in range(2,100):
        for j in range(2,i):
            if i % j == 0 :
                print(i+'yes')
        else :
            print(i+'no')
su()<jupyter_output>>>3<jupyter_text>调用一个函数- functionName()- "()" 就代表调用 ![](../Photo/70.png)  带返回值和不带返回值的函数- return 返回的内容- return 返回多个值- 一般情况下，在多个函数协同完成一个功能的时候，那么将会有返回值 ![](../Photo/71.png)- 当然也可以自定义返回None  EP：![](../Photo/72.png)  类型和关键字参数- 普通参数- 多个参数- 默认值参数- 不定长参数  普通参数<jupyter_code>def y(x):
    return x ** 2
y_ = y(100)
print(y_)
def name_(name):
    print('{}你好'.format(name))
name_(name = 'haha')    #带参数名传递
name_('haha')    #位置参数   不带参名传递
def yanzheng():
    import random
    i = 0
    while i <3 :
        list_ = ['1','2','3','4','5','6','7','8','9','0','q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','n','m']
        random.shuffle(list_)
        yanzhengma = ''.join(list_[0:4])
        print('验证码是:',yanzhengma)
        input_ = input('>>')

        if yanzhengma == input_:
            print('验证码正确')
            break
        else:
            print('请再输入')
            i +=1
    #     if i ==3:
    #         print('验证码次数超限')
    else:
        print('验证码次数超限')
yanzheng()
def input_():
    num = eval(input('>>'))
    res3 = san(num)
    res2 = liang(num)
    print(res3 - res2)
def san(num):
    return num **3
def liang(num):
    return num **2
input_()<jupyter_output>>>5
100<jupyter_text>多个参数<jupyter_code>def name1_(name1,name2,name3='hehe '):  #如果参数含有默认值，统一放在最后面，不能插
    print('{}{}{}你们好'.format(name1,name2,name3))
name1_('haha ','heihei ')
name_ = '1453121869'
password = '<PASSWORD>'
is_ok_and_y = False
def login(name_login,password_login):
    if name_login == name_ and password_login == password:
        print('ok')
    else:
        print('no')
login(name_login='1453121869',password_login='<PASSWORD>')
def qidong():
    global is_ok_and_y
    if is_ok_and_y == False:
        print('是否七天免登录y/n')
        res = input('>>')
        name_login = input('请输入账号')
        password_login = input('请输入密码')
        if res == 'y':
            login(name_login,password_login)
            is_ok_and_y = True
        else:
            login(name_login,password_login)
    else:
        print('ok')
qidong()<jupyter_output>是否七天免登录y/n
>>n
请输入账号1453121869
请输入密码<PASSWORD>
ok<jupyter_text>默认值参数  强制命名<jupyter_code>def kj(*,name1,name2,name3):                      # *是强制命名，必须带参数传递
    print('{}{}{}你好'.format(name1,name2,name3))
kj('西西','果果','哈哈')
kj(name1='西西',name2='果果',name3='哈哈')<jupyter_output>西西果果哈哈你好<jupyter_text>不定长参数- \*args> - 不定长，来多少装多少，不装也是可以的  - 返回的数据类型是元组  - args 名字是可以修改的，只是我们约定俗成的是args- \**kwargs > - 返回的字典  - 输入的一定要是表达式（键值对）- name,\*args,name2,\**kwargs  使用参数名<jupyter_code>def test(*args):
    print(args)
test(1,2,1,2,1,2,1,2,1,25,4,5,4,5,4,5)
def test1(**kwargs):
    print(kwargs)
test1(a=1,b=2,c=1)<jupyter_output>{'a': 1, 'b': 2, 'c': 1}<jupyter_text>变量的作用域- 局部变量 local- 全局变量 global- globals 函数返回一个全局变量的字典，包括所有导入的变量- locals() 函数会以字典类型返回当前位置的全部局部变量。<jupyter_code>a = 100
b = 200
c = 300
def test2():
    global a,b,c
    a = 1000
    b = 200
    c = True
    print(a,b,c)
print(a,b,c)
test2()
print(a,b,c)
globals()
locals()<jupyter_output><empty_output><jupyter_text>注意：- global ：在进行赋值操作的时候需要声明- 官方解释：This is because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope.- ![](../Photo/73.png)  Homework- 1![](../Photo/74.png)<jupyter_code>def getPentagonalNumber(n):
    count = 0
    for n in range(1,101):
        num = n * (3*n-1) / 2
        print(num,end="    ")
        count +=1
        if count%10 ==0:
            print()
getPentagonalNumber(100)<jupyter_output>1.0    5.0    12.0    22.0    35.0    51.0    70.0    92.0    117.0    145.0    
176.0    210.0    247.0    287.0    330.0    376.0    425.0    477.0    532.0    590.0    
651.0    715.0    782.0    852.0    925.0    1001.0    1080.0    1162.0    1247.0    1335.0    
1426.0    1520.0    1617.0    1717.0    1820.0    1926.0    2035.0    2147.0    2262.0    2380.0    
2501.0    2625.0    2752.0    2882.0    3015.0    3151.0    3290.0    3432.0    3577.0    3725.0    
3876.0    4030.0    4187.0    4347.0    4510.0    4676.0    4845.0    5017.0    5192.0    5370.0    
5551.0    5735.0    5922.0    6112.0    6305.0    6501.0    6700.0    6902.0    7107.0    7315.0    
7526.0    7740.0    7957.0    8177.0    8400.0    8626.0    8855.0    9087.0    9322.0    9560.0    
9801.0    10045.0    10292.0    10542.0    10795.0    11051.0    11310.0    11572.0    11837.0    12105.0    
12376.0    12650.0    12927.0    13207.0    13490.0    13776.0    14065.0    14357.0    14652.0    14950.0<jupyter_text>- 2 ![](../Photo/75.png)<jupyter_code>def sumDisgits(n):
    ge = n%10
    shi = (n//10)%10
    bai = n//100
    sum_ = ge + shi + bai
    print(str(sum_)+'('+str(bai)+'+'+str(shi)+'+'+str(ge)+')')
sumDisgits(234)<jupyter_output>9(2+3+4)<jupyter_text>- 3![](../Photo/76.png)<jupyter_code>def displaySortedNumbers(num1,num2,num3):
    num1,num2,num3 = eval(input('Enter three numbers: '))
    print(min(num1,num2,num3),num1+num2+num3-max(num1,num2,num3)-min(num1,num2,num3),max(num1,num2,num3))
displaySortedNumbers(1,2,3)<jupyter_output>Enter three numbers: 3,2,1
1 2 3<jupyter_text>- 4![](../Photo/77.png) - 5![](../Photo/78.png)<jupyter_code>def printChars(ch1,ch2,numberPerLine):
    j = 0
    for i in range(ord(ch1),ord(ch2)+1):
        print(chr(i),end = ' ')
        j += 1
        if j % numberPerLine == 0:
            print('\n')
printChars('1','Z',10)<jupyter_output>1 2 3 4 5 6 7 8 9 : 

; < = > ? @ A B C D 

E F G H I J K L M N 

O P Q R S T U V W X 

Y Z<jupyter_text>- 6![](../Photo/79.png)<jupyter_code>def numberOfDaysInAYear(year):
    for year in range(2010,2021):
        if ((year%4 == 0) and (year%100 !=0)) or (year % 400 == 0):
            print(str(year)+'年有366天。')
        else:
            print(str(year)+'年有365天。。')
numberOfDaysInAYear(2010)<jupyter_output>2010年有365天。。
2011年有365天。。
2012年有366天。
2013年有365天。。
2014年有365天。。
2015年有365天。。
2016年有366天。
2017年有365天。。
2018年有365天。。
2019年有365天。。
2020年有366天。<jupyter_text>- 7![](../Photo/80.png)<jupyter_code>import math
def distance(x1,y1,x2,y2):
    ju = math.sqrt((x2-x1)**2+(y2-y1)**2)
    print('两点之间距离为： '+str(ju))
distance(1,1,2,2)<jupyter_output>两点之间距离为： 1.4142135623730951<jupyter_text>- 8![](../Photo/81.png)<jupyter_code>def meisen():
    for p in range(2,50):
        for j in range(2,p):
            if p % j == 0 :
                break
        else :
            if  2**p-1<=31:
                print(p,'     ',2**p-1)
print('p       2^p-1')
meisen()<jupyter_output>p       2^p-1
2       3
3       7
5       31<jupyter_text>- 9![](../Photo/82.png)![](../Photo/83.png)<jupyter_code>import time
time.asctime( time.localtime(time.time()) )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17949065293958277, 'fraction_numerical': 0.1208344622053644, 'mean_word_length': 2.6676602086438153, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '611313', 'license': 'Apache-2.0', 'n_tokens_mistral': 3902, 'n_tokens_neox': 3559, 'n_words': 563, 'path': '9.14.ipynb', 'repo_name': 'xiayangaa/test', 'size': '7370'}