<jupyter_start><jupyter_text>函数- 函数可以用来定义可重复代码，组织和简化- 一般来说一个函数在实际开发中为一个小功能- 一个类为一个大功能- 同样函数的长度不要超过一屏 Python中的所有函数实际上都是有返回值(return None),如果你没有设置return,那么Python将不显示None.如果你设置return,那么将返回出return这个值.  定义一个函数def function_name(list of parameters):        do something![](../Photo/69.png)- 以前使用的random 或者range 或者print.. 其实都是函数或者类<jupyter_code>def max_(number1,number2,number3):
    if number1>number2>number3 or number1>number3>number2 :
        result=number1
    elif number2>number3>number1 or number2>number1>number3:
        result=number2
    elif number3>number1>number2 or number3>number2>number1:
        result=number3
        return result
max_(number1=1,number2=2,number3=3)
import random
n=random.randint(0,10)
print(n)

def shu():
    n=random.randint(0,10)
    if number>n:
        result =big
    elif number<n:
        result=small
    elif number==n:
        result=zhenghao
        return result
shu(number=6)
def wwj():
    print('hahaha')
wwj()
def w(name):
    print(name,'矮子')
    
w('qyh')
def panduan(shu):
    if shu % 2==0:
        print('偶数')
    else:
        print('奇数')
panduan(shu=2)
def w(name1,name2):
    print(name1,'矮子')
    print(name2,'胖子')
    
w(name1='kkk',name2='fff')
def w(name1,name2='kkk'):#带默认值的可以不传参，但必须写在最后
    print(name1,'矮子')
    print(name2,'胖子')
w(name1='hhh')<jupyter_output>hhh 矮子
kkk 胖子<jupyter_text>函数的参数如果有默认值的情况,当你调用该函数的时候:可以不给予参数值,那么就会走该参数的默认值否则的话,就走你给予的参数值.  调用一个函数- functionName()- "()" 就代表调用<jupyter_code>def h():
    print('kkk')
def b():
    h()
b()
def a(f):
    f()
a(b)<jupyter_output>kkk<jupyter_text>![](../Photo/70.png)  带返回值和不带返回值的函数- return 返回的内容- return 返回多个值- 一般情况下，在多个函数协同完成一个功能的时候，那么将会有返回值 ![](../Photo/71.png)- 当然也可以自定义返回None  EP：![](../Photo/72.png)<jupyter_code>def main():
    print(min(min))<jupyter_output><empty_output><jupyter_text>类型和关键字参数- 普通参数- 多个参数- 默认值参数- 不定长参数  普通参数  多个参数  默认值参数  强制命名<jupyter_code>def wang(*,name):
    print('hhhh',name)
wang(name='ghj')<jupyter_output>hhhh ghj<jupyter_text>不定长参数- \*args> - 不定长，来多少装多少，不装也是可以的  - 返回的数据类型是元组  - args 名字是可以修改的，只是我们约定俗成的是args- \**kwargs > - 返回的字典  - 输入的一定要是表达式（键值对）- name,\*args,name2,\**kwargs  使用参数名<jupyter_code>def wang(*args):
    print(args)
wang(1,2,3,4,5,6,)
def max_(*args):
    res=0
    for i in args:
        if i>res:
            res =i
    return res
            
        
max_(1,2,3,4,5)
def max_(*args):
    res=0
    for i in args:
        if i>res:
            res =i
    return res
            
def a ():
    num = input('>>')
    n =input('>>')
    res =0
    for i in range(1,int(n)+1):
        print(num * i)
        res +=int(num * i)
    return res
a()
def a():
    res =0
    for j in range(5):  
        for i in range(j+1):   
            res += 2*10**i
    return res
a()
res=0
for i in range(1,21):
    res=1
    for j in range(1,i+1):
        res *=j
    print(res)
i=100
for i in range(1,11):
    for j in range()<jupyter_output><empty_output><jupyter_text>变量的作用域- 局部变量 local- 全局变量 global- globals 函数返回一个全局变量的字典，包括所有导入的变量- locals() 函数会以字典类型返回当前位置的全部局部变量。<jupyter_code>def sum_(*args):
    res =0
    count =1
    chafang=0
    for i in args:
        res+=i
        count+=1
        mean =res/(count-1)
        chafang+=(i-mean)**2
    return res,mean,chafang
sum_(12,1)
def fangcha(*args):
    res =0
    count =1
    chafang=0
    for i in args:
        res+=i
        count+=1
        mean =res/(count-1)
        chafang+=(i-mean)**2
    return chafang
    
fangcha(1,2,3)<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):
        wu=(n*(3*n-1)/2)
        print(wu,end=' ') 
        count+=1
        if count %10==0:
            print()
        
getPentagonalNumber(101)<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) - 3![](../Photo/76.png)<jupyter_code>def displaysortenumbers(num1,num2,num3):
    if num1<num2<num3 :
        result= num1,num2,num3
    elif num1<num3<num2 :
        result= num1,num3,num2
    elif num2<num3<num1:
        result=num2,num3,num1
    elif num2<num1<num3:
        result=num2,num1,num3
    elif num3<num1<num2:
        result=num3,num,num2
    elif num3<num2<num1:
        result=num3,num2,num1
    return result
displaysortenumbers(num1=31,num2=12.4,num3=15)<jupyter_output><empty_output><jupyter_text>- 4![](../Photo/77.png)<jupyter_code>def futureinvestmentvalue(investmentamount,monthlyinteresrate,years):
    ben=10000
    for i in range(1,31):<jupyter_output><empty_output><jupyter_text>- 5![](../Photo/78.png)<jupyter_code>def printchars(ch1,ch2,numberperline):
    numberperline=0
    for i in range(49,91):
        if 48<i<=58:
            print(chr(i),end=' ')
        elif 59<=i<=69:
            print(chr(i),end=' ')
        elif 70<=i<=80:
            print(chr(i),end=' ')
        elif 81<=i<=91:
            print(chr(i),end=' ')
        numberperline+=1
        if numberperline %10==0:
            print()
 printchars(49,91,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,year1):
    for year in range(2010,2021):
        if year % 4==0:
            print('有366天')
        else:
            print('有365天')
numberofdaysinayear(2010,2021)<jupyter_output>有365天
有365天
有366天
有365天
有365天
有365天
有366天
有365天
有365天
有365天
有366天<jupyter_text>- 7![](../Photo/80.png)<jupyter_code>import math
def distance(x1,y1,x2,y2):
    h=math.sqrt((x2-x1)**2+(y2-y1)**2)
    return h
                
distance(2,3,6,5)<jupyter_output><empty_output><jupyter_text>- 8![](../Photo/81.png)<jupyter_code>for i in range(2,32):
    for j in range(2,i-1):
        if i % j==0:
            break
    else:
        print(i,(2**i)-1)<jupyter_output>2 3
3 7
5 31
7 127
11 2047
13 8191
17 131071
19 524287
23 8388607
29 536870911
31 2147483647<jupyter_text>- 9![](../Photo/82.png)![](../Photo/83.png)<jupyter_code>def time.time():<jupyter_output><empty_output><jupyter_text>- 10![](../Photo/84.png)<jupyter_code>for i in range(1,6):
    for j in range(1,6):<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15314675380025572, 'fraction_numerical': 0.12785907089075152, 'mean_word_length': 3.0297653119633656, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 76, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '282710', 'license': 'Apache-2.0', 'n_tokens_mistral': 3808, 'n_tokens_neox': 3352, 'n_words': 584, 'path': '7.20.ipynb', 'repo_name': 'dlxzq/python3', 'size': '7039'}