<jupyter_start><jupyter_text>函数- 函数可以用来定义可重复代码，组织和简化- 一般来说一个函数在实际开发中为一个小功能- 一个类为一个大功能- 同样函数的长度不要超过一屏  定义一个函数def function_name(list of parameters):        do something![](../Python/Photo/69.png)- 以前使用的random 或者range 或者print.. 其实都是函数或者类<jupyter_code>def fun_name():
    print('函数')
fun_name()#()表示函数调用<jupyter_output>函数<jupyter_text>调用一个函数- functionName()- "()" 就代表调用<jupyter_code>def jiou_name():
    '''
    这是注释，加以说明，例：这是czg写的判断奇数偶数的函数。
    '''
    num = int(input('请输入一个数字：'))
    if num%2==0:
        print('这个数是偶数')
    else:
        print('这个数是奇数')
jiou_name()<jupyter_output>请输入一个数字：3
这个数是奇数<jupyter_text>![](../Python/Photo/70.png)  带返回值和不带返回值的函数- return 返回的内容- return 返回多个值- 一般情况下，在多个函数协同完成一个功能的时候，那么将会有返回值 ![](../Python/Photo/71.png)- 当然也可以自定义返回None<jupyter_code>def sushu_name():
    '''
    这是判断一数是不是素数的函数
    '''
    num = eval(input('请输入一个数字：' ))
    for i in range(2,num): 
        if num % i == 0:
            print('不是素数')
            break
    else:
        print('素数')
    return num
a=sushu_name()
print(a)<jupyter_output>请输入一个数字：5
素数
5<jupyter_text>EP：![](../Python/Photo/72.png)  类型和关键字参数- 普通参数- 多个参数- 默认值参数- 不定长参数<jupyter_code>import os
def kuajiang(name):
    os.system('say {}天才'.format(name))
kuajiang('星期五')
kuajiang(name = '常志国')
def y(x):
    return x**2
y_ = y(100)
print(y_)
def san(num):
    return num**3
def liang(num):
    return num**2

def input_():
    num = eval(input('>>'))
    res3 = san(num)
    res2 = liang(num)
    print(res3-res2)

input_()<jupyter_output>>>100
990000<jupyter_text>普通参数  多个参数<jupyter_code>import os
def kuajiang1(name1,name2,name3 ):
    os.system('say {}{}{}天才'.format(name1,name2,name3))
kuajiang1(name1 = '常志国',name2 = '国志常',name3 = '志国常')
import os
#如果参数有信心，都放在最后面。
def kuajiang2(name1,name2,name3= '志国常'):
    os.system('say {}{}{}天才'.format(name1,name2,name3))
kuajiang2(name1 = '常志国',name2 = '国志常')
import 
user = '<EMAIL>'
passward = '<PASSWORD>'
is_ok_and_y = False
def login(user_login,passward_login):
    if user_login ==user and passward_login==passward:
        print('成功')
    else:
        print('失败')
   
    
def qitian():
    global is_ok_and_y
    if is_ok_and_y == False:
        print('是否7天免登陆？y/n')
        res = input('>>')
        a=input('请输入账号：')
        b=input('请输入密码：')
        if res=='y':
            login(user_login=a,passward_login=b)
            is_ok_and_y = True
        else:
            login(user_login=a,passward_login=b)
    else:
        print('成功')
    
    
    
qitian()<jupyter_output>是否7天免登陆？y/n
>>n<jupyter_text>默认值参数  强制命名<jupyter_code>import os
#如果参数有信心，都放在最后面。
def kuajiang2(*,name1,name2,name3= '志国常'):#   *强制命名
    os.system('say {}{}{}天才'.format(name1,name2,name3))
kuajiang2(name1 = '常志国',name2 = '国志常')<jupyter_output><empty_output><jupyter_text>不定长参数- \*args> - 不定长，来多少装多少，不装也是可以的  - 返回的数据类型是元组  - args 名字是可以修改的，只是我们约定俗成的是args- \**kwargs > - 返回的字典  - 输入的一定要是表达式（键值对）- name,\*args,name2,\**kwargs  使用参数名  变量的作用域- 局部变量 local- 全局变量 global- globals 函数返回一个全局变量的字典，包括所有导入的变量- locals() 函数会以字典类型返回当前位置的全部局部变量。<jupyter_code>def  test(*args):
    print(args)
test(1,2,3,3,4)<jupyter_output>(1, 2, 3, 3, 4)<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.- ![](../Python/Photo/73.png)  Homework- 1![](../Python/Photo/74.png)<jupyter_code>def getPentagonalNumber():
    i=1
    
    while i<=100:
        num=(i*(3*i-1))/2
        print(num,end='\t')
        i+=1
        if (i-1)%10==0:
             print()
getPentagonalNumber()<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 ![](../Python/Photo/75.png)<jupyter_code>def sumDigits(num):
   # num = eval(input('请输入一个数字：'))
    sum_=0
    while True:
        number=num%10
        sum_+=number
        num=num//10
        if num==0:
            print(sum_)
            break
sumDigits(num=1245)<jupyter_output>12<jupyter_text>- 3![](../Python/Photo/76.png)<jupyter_code>def displaySortedNumbers(num1,num2,num3):
    num = [num1,num2,num3]
    num.sort()
    print(num)
displaySortedNumbers(num1=3,num2=4,num3=1)<jupyter_output>[1, 3, 4]<jupyter_text>- 4![](../Python/Photo/77.png)<jupyter_code>def future():
    in_ = eval (input("输入本金"))
    ratr = eval (input("输入年利率"))
    for i in range(1,31):
        years = i
        future = in_ * (1 + ratr / 100/12) ** (years * 12)
        print(str(years)+'    '+str(future))
future()<jupyter_output>输入本金1000
输入年利率9
1    1093.8068976709837
2    1196.4135293926222
3    1308.6453709165366
4    1431.4053333137108
5    1565.6810269415707
6    1712.5527068212796
7    1873.2019633462298
8    2048.921228238936
9    2241.124172232252
10    2451.3570781248113
11    2681.3112807075076
12    2932.8367736408914
13    3207.957092751521
14    3508.8855954841697
15    3838.0432674789427
16    4198.078199528149
17    4591.886891606074
18    5022.637555363697
19    5493.79560255814
20    6009.151524472612
21    6572.851386618251
22    7189.430184049334
23    7863.848325637133
24    8601.531540820313
25    9408.414529883785
26    10290.98870893479
27    11256.354433687085
28    12312.278122196296
29    13467.254736101859
30    14730.576123040439<jupyter_text>- 5![](../Python/Photo/78.png)<jupyter_code>def printChars(ch1, ch2, numberPerLine):
    count = 1
    for i in range(ord(ch1), ord(ch2) + 1):
        if count % numberPerLine == 0:
            print(chr(i))        
        else:
            print(chr(i), end = "")    
        count += 1
printChars(ch1='1', ch2='Z', numberPerLine=10)<jupyter_output>123456789:
;<=>?@ABCD
EFGHIJKLMN
OPQRSTUVWX
YZ<jupyter_text>- 6![](../Python/Photo/79.png)<jupyter_code>def numberOfDaysInAYear(year):
    if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
      return 366
    else:
      return 365
for year in range(2010, 2021):
    numberOfDaysInAYear(year)
    print(str(year) + '年有'+ str(numberOfDaysInAYear(year))+'天')<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![](../Python/Photo/80.png)<jupyter_code>def distance(x1,y1,x2,y2):
    s=((x2-x1)**2+(y2-y1)**2)**0.5
    print('两点的距离是：'+str(s))
distance(x1=eval(input('请输入x1  :')),y1=eval(input('请输入y1  :')),x2=eval(input('请输入x2  :')),y2=eval(input('请输入y2  :')))<jupyter_output>请输入x1  :1
请输入y1  :3
请输入x2  :4
请输入y2  :5
两点的距离是：3.605551275463989<jupyter_text>- 8![](../Python/Photo/81.png)<jupyter_code>def meisushu():
    for p in range(2,32): 
        num=2**(p)-1
        p+=1
        for i in range(2,num): 
            if num % i == 0:
                break
        else:
            print(num)
            

meisushu()<jupyter_output>3
7
31
127
8191
131071
524287<jupyter_text>- 9![](../Python/Photo/82.png)![](../Python/Photo/83.png)<jupyter_code>import time

print('当前时间是：'+time.strftime("%a %b %d %Y %H:%M:%S", time.localtime()))<jupyter_output>当前时间是：Sun Sep 16 2018 21:01:44<jupyter_text>- 10![](../Python/Photo/84.png)<jupyter_code>import random
import sys

def getDice():
    i1 = random.randint(1, 6)
    i2 = random.randint(1, 6)

    print("和值为", i1, "+", i2, "=", i1 + i2)
    dice=i1 + i2
    if dice == 7 or dice == 11:
        print("你赢了")
        sys.exit()
    elif dice == 2 or dice == 3 or dice == 12:
        print("你输了")
        sys.exit()

    point = dice
    print("无效数字：", point)
    
   
    while dice != 7 and dice != point:
        i1 = random.randint(1, 6)
        i2 = random.randint(1, 6)
        print("和值为", i1, "+", i2, "=", i1 + i2)
        dice=i1 + i2
    
    if dice == 7:
        print("你输了")
    else:
        print("你赢了")

getDice()<jupyter_output>和值为 1 + 5 = 6
无效数字： 6
你赢了
{'fraction_non_alphanumeric': 0.15952750268464383, 'fraction_numerical': 0.1724137931034483, 'mean_word_length': 3.1868131868131866, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 66, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '13068', 'license': 'Apache-2.0', 'n_tokens_mistral': 4885, 'n_tokens_neox': 4200, 'n_words': 651, 'path': '9.14.ipynb', 'repo_name': 'old-chang/test', 'size': '8380'}