<jupyter_start><jupyter_text>函数- 函数可以用来定义可重复代码，组织和简化- 一般来说一个函数在实际开发中为一个小功能- 一个类为一个大功能- 同样函数的长度不要超过一屏  定义一个函数def function_name(list of parameters):        do something![](../Photo/69.png)- 以前使用的random 或者range 或者print.. 其实都是函数或者类  调用一个函数- functionName()- "()" 就代表调用<jupyter_code>import os
os.system('say 你是谁？？？')
def fun_name():
    print("还有谁？？？")
fun_name()   #()代表调用一个函数
def fun_jiou():
    '''
    这是小小写的判断奇数还是偶数
    
    '''
    a = eval(input(">>>"))
    if a%2==0:
        print("是偶数")
    else:
        print("奇数")
  

fun_jiou()

def y(x):
    return x**2

y(100)
def fun_sushu():
    number = eval(input(">>"))
    if number > 1:
        for i in range(2,number):
            if (number % i)==0:
                print(str(number),"不是素数")
                break
        else:
            print(str(number),"是素数")
    else:
        print(str(number),"不是素数")
fun_sushu()<jupyter_output>>>9999
9999 不是素数<jupyter_text>![](../Photo/70.png)  带返回值和不带返回值的函数- return 返回的内容- return 返回多个值- 一般情况下，在多个函数协同完成一个功能的时候，那么将会有返回值 ![](../Photo/71.png)- 当然也可以自定义返回None  EP：![](../Photo/72.png)  类型和关键字参数- 普通参数- 多个参数- 默认值参数- 不定长参数<jupyter_code>def san(num):
  #  num = eval(input(">>"))
    return num**3
def liang(num):
   # mun = eval(input(">>"))
    return num**2
def input_():
    num  = eval(input(">>"))
    res3 = san(num)
    res2 = liang(num)
    print(res3 - res2)
input_()<jupyter_output>>>10
900<jupyter_text>普通参数<jupyter_code>acount = '1172304538'
password  = '<PASSWORD>'


def login(acount_,password_):
    
    if acount == acount_ and password == password_:
            print("登陆成功")
    else:
            print("登录失败")

login('1172304538','<PASSWORD>')
def yanzheng():
    print("是否七天内免登陆？y/n")
    res = input(">>")
    a = input("请输入账户：")
    b = input("请输入密码：")
    if res == 'y':
        login(a,b)<jupyter_output><empty_output><jupyter_text>多个参数<jupyter_code>def test(*agrs,**kwagrs):
    print(agrs)
    print(kwagrs)
test(11,22,33,s=100,a=200)<jupyter_output>(11, 22, 33)
{'s': 100, 'a': 200}<jupyter_text>默认值参数  强制命名  不定长参数- \*args> - 不定长，来多少装多少，不装也是可以的  - 返回的数据类型是元组  - args 名字是可以修改的，只是我们约定俗成的是args- \**kwargs > - 返回的字典  - 输入的一定要是表达式（键值对）- name,\*args,name2,\**kwargs  使用参数名  变量的作用域- 局部变量 local- 全局变量 global- globals 函数返回一个全局变量的字典，包括所有导入的变量- locals() 函数会以字典类型返回当前位置的全部局部变量。  注意：- 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):
    i=1
    for n in range(1,n):
        if i <100:
            m=(n*(3*n-1))/2
            i+=1
            print(int(m),end=" ")
            if i%10==0:
                print("\n")
        else:
            break
        
getPentagonalNumber(100)<jupyter_output>1 5 12 22 35 51 70 92 117 

145 176 210 247 287 330 376 425 477 532 

590 651 715 782 852 925 1001 1080 1162 1247 

1335 1426 1520 1617 1717 1820 1926 2035 2147 2262 

2380 2501 2625 2752 2882 3015 3151 3290 3432 3577 

3725 3876 4030 4187 4347 4510 4676 4845 5017 5192 

5370 5551 5735 5922 6112 6305 6501 6700 6902 7107 

7315 7526 7740 7957 8177 8400 8626 8855 9087 9322 

9560 9801 10045 10292 10542 10795 11051 11310 11572 11837 

12105 12376 12650 12927 13207 13490 13776 14065 14357 14652<jupyter_text>- 2 ![](../Photo/75.png)<jupyter_code>def sumDigits(n):
        gewei = n%10
        shiwei = (n//10)%10
        baiwei = n//100
        m = gewei + shiwei + baiwei
        print(str(m)+"(",str(baiwei)+"+"+str(shiwei)+"+"+str(gewei),")")
        
sumDigits(234)<jupyter_output>9( 2+3+4 )<jupyter_text>- 3![](../Photo/76.png)<jupyter_code>def ddd(a,b,c):
    if a>b:
        a,b=b,a
    if b>c:
        b,c=c,b
    if a>b:
        a,b=b,a
        return a,b,c
ddd(3,5,1)
def displaySortedNumbers():
    a,b,c=eval(input("Enter three numbers:"))
    d = [a,b,c]
    d.sort()
    print("The sorted numbers are:",d)
displaySortedNumbers()
displaySortedNumbers()<jupyter_output>Enter three numbers:3,2.4,5
The sorted numbers are: [2.4, 3, 5]<jupyter_text>- 4![](../Photo/77.png) - 5![](../Photo/78.png)<jupyter_code>def printChars(ch1,ch2,numberPerLine):<jupyter_output><empty_output><jupyter_text>- 6![](../Photo/79.png) - 7![](../Photo/80.png)<jupyter_code>def distance(x1,y1,x2,y2):
    import math
    d=math.sqrt(math.pow(x1-x2,2)+math.pow(y1-y2,2))
    print(d)
distance(3,5,2,1)<jupyter_output>4.123105625617661<jupyter_text>- 8![](../Photo/81.png)<jupyter_code>def msh():
    if p<=31:
    for i in range(2,7):<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup