python3(函数、生成式、生成器)

1.python内置数据结构总结
数值:
bool
str
list tuple set dict

可变数据类型 不可变数据类型
可变数据类型:list set dict(是否可以增删改查)

有序数据类型和无序数据类型
有序:str list tuple
无序:数值 bool set
python2:dict无序 python3中:dict有序

一般情况下:有序数据类型可以索引,切片,连接,重复 但是字典除外

可以for循环数据结构
2.函数
1.函数的理解和定义
#定义函数时的变量称为形式参数,变量名可以任意的
#调用函数时的参数称为实参,该参数必须是实际存在的

def xiaoming():
    print('is a man')
    def xiaohong():
        print('is a woman')
    xiaohong()
xiaoming()
def funcl(a):
    print('hello %s' %(a))
funcl('world')
is a man
is a woman
hello world

2.函数练习1
#编写一个名为collatz()的函数,它有一个名为number的参数。
#如果参数是偶数,那么collatz()就打印出number//2
#如果number是奇数,collatz()就打印3number+1
def collatz(number):
if number%2 == 0:
print(’%s’ %(number//2))
else:
print(’%s’ %(3
number+1))
collatz(5)

16
def collatz(number):
    print(number//2 if number%2==0 else 3*number+1)
collatz(5)
16

3.函数动态添加成员

def fun():
    print(fun.x)
    fun()

#动态添加

fun.x = 3
fun()

#动态删除

del fun.x
fun()

4.函数的返回值
#函数调用时一般有返回值,没有定义返回值的时候,python中默认返回None
1)return返回单个值

import random
def level(score):
    if 90 < score <= 100:
        return 'A'
    elif 80 < score <= 90:
        return 'B'
    else:
        return 'C'
def main():
    for i in range(20):
        score = random.randint(1,100)
        print('成绩为%s,等级为%s' %(score,level(score)))
main()
成绩为87,等级为B
成绩为61,等级为C
成绩为12,等级为C
成绩为24,等级为C
成绩为74,等级为C
成绩为18,等级为C

2)return返回多个值
# 接收一个列表,求这个列表的最大值,平均值,最小值
# python函数中,只能返回一个值
# 如果非要返回多个值,会把返回的值封装为一个元组数据类型

def fun(a):
    max_num = max(a)
    min_num = min(a)
    avg_num = sum(a)/len(a)
    return max_num,min_num,avg_num
variables = fun([1,2,3,4,5,100])
print(variables,type(variables))
(100, 1, 19.166666666666668) <class 'tuple'>

5.函数形参之四大参数类型
参数:形参 实参
形参的分类:位置参数 默认参数 可变参数 关键字参数
1)位置参数:形参和实参必须保持一致

def getInfo(name, age):  # 按照位置传递参数
    print(name, age)
getInfo(age=18, name='name')
name 18

2)默认参数:形参和实参可以不一致

def mypow(x,y=2):
    print(x**y)
mypow(3)
mypow(3,4)
9
81

3.可变参数
需求:用户可以传递任意的值,计算数值的和
*args:是一个可变参数
args:是元组数据类型

def mySum(*args):
    sum = 0
    for item in args:
        sum += item
    print(sum)
mySum(1,2,3,4,5,6,7,8)
36

4.关键字参数
#如果要对字典进行解包,一个星号:获取key值

def getStuInfo(name,age,**kwargs):
    print(name,age)
    print(kwargs)
getStuInfo('westos',18,score=[11,23])
d = dict(a=1,b=2)
print(*d)
getStuInfo('westos',100,**d)
westos 18
{'score': [11, 23]}
a b
westos 100
{'a': 1, 'b': 2}
nums = [1,2,3,4,5,6]
nums1 = (1,2,3,4,5,6,7)
nums2 ={1,2,3,4,5,6,7,8}
mySum(*nums)
mySum(*nums1)
mySum(*nums2)
21
28
36
score = (12,23,56,78,90)
minscore,*middlescore,maxscore = score
print(*middlescore)
print(middlescore)
23 56 78
[23, 56, 78]

6.函数练习2
题目需求:
对于一个十进制的正整数, 定义f(n)为其各位数字的平方和,如:

f(13) = 1**2 + 3**2 = 10
f(207) = 2**2 + 0**2 + 7*2 = 53
下面给出三个正整数k,a, b,你需要计算有多少个正整数n满足a<=n<=b,
且k
f(n)=n
输入:
第一行包含3个正整数k,a, b, k>=1, a,b<=10**18, a<=b;
输出:
输出对应的答案;
范例:
输入: 51 5000 10000
输出: 3

方法1:

def f(n):
    n = str(n)
    sum = 0
    for item in n:
        sum += int(item)**2
    return sum
s = input('请输入(k,a,b):')
li = []
for item in s.split():
    li.append(int(item))
k,a,b = li
count = 0
for i in range(a,b+1):
    if k*f(i) == i:
        count += 1
print(count)
请输入(k,a,b):51 5000 10000
3

方法2

def f(n):
    n = str(n)
    sum = 0
    for item in n:
        sum += int(item)**2
    return sum
def isok(k,n):
    if k * f(n) == n:
        return True
    else:
        return False
def main():
    k = 51
    a = 5000
    b = 10000
    count = 0
    for i in range(a,b+1):
        if isok(k,i):
            count += 1
    print(count)
main()

7.函数的作用域
1)局部作用域

num = 10
print('out fun:id=',id(num))
def fun():
    global num
    num = 2
     #局部作用域,在函数运行时生效,函数运行结束则释放
    print('in fun:id=',id(num))
    print('in fun:num = %s' %(num))
fun()
print('in fun:num = %s' %(num))
out fun:id= 9322752
in fun:id= 9322496
in fun:num = 2
in fun:num = 2

2)全局作用域

num = 10
def fun():
    # 通过global关键字声明局部变量为全局变量
    # 函数执行完毕,变量依然生效
    global num
    num = 2
fun()
print(num)
2

8.函数练习3
1.Collatz序列
编写一个名为collatz()的函数,它有一个名为number的参数。
如果参数是偶数,那么collatz()就打印出number//2,并返回
该值。如果number是奇数,collatz()就打印并返回3*number+1。
然后编写一个程序,让用户输入一个整数,并不断对这个数
调用collatz(),直到函数返回值1(令人惊奇的是,这个序列
对于任何整数都有效,利用这个序列,你迟早会得到1!既使数学
家也不能确定为什么。你的程序在研究所谓的“Collatz序列”,
它有时候被称为“最简单的、不可能的数学问题”)。

  • 输入:
    3

  • 输出:
    10
    5
    16
    8
    4
    2
    1

def collatz(number):
    if number == 1:
        exit()
    elif number%2 == 0:
        return number//2
    else:
        return 3*number+1
num = int(input('请输入一个整数:'))
while True:
    num = collatz(num)
    print(num)
请输入一个整数:3
10
5
16
8
4
2
1

9.参数检测

def add(x,y):
    if isinstance(x,(int,float)) and isinstance(y,(int,float)):
        return x + y
    else:
        print('Error')
res = add(2.5,2)
print(res)
4.5

10.函数练习4
编写函数,计算字符串匹配的准确率

def isok(s1,s2):
    if not isinstance(s1,str) and isinstance(s2,(str)):
        print('被匹配字符必须是字符串')
        return
    if len(s1) < len(s2):
        print('Error')
        return
    right = 0
    for a,b in zip(s1, s2):
        if a == b:
            right += 1
    return right/len(s1)
a = 'aaa'
b = 'Aaa'
print(isok(a,b))
0.6666666666666666

11.可变参数练习
案例1:
编写一个函数cacluate, 可以接收任意多个数,返回的是一个元组.
元组的第一个值为所有参数的平均值, 第二个值是大于平均值的所有数

def cacluate(*kwargs):
    avg = sum(kwargs)/len(kwargs)
    li = []
    for i in kwargs:
        if i > avg:
            li.append(i)
    return avg,li
print(cacluate(1,2,3,4,5,6))
(3.5, [4, 5, 6])

案例2:
编写一个函数, 接收字符串参数, 返回一个元组,‘ehllo WROLD’
元组的第一个值为大写字母的个数, 第二个值为小写字母个数.

def isok(s):
    if not isinstance(s,str):
        print('Error')
        return
    else:
        num1=0
        num2=0
        for i in s:
            if i.isupper():
                num1 += 1
            elif i.islower():
                num2 += 1
            else:
                pass
    return num1,num2
w = 'ehllo WROLD'
print(isok(w))
(5, 5)

案例3:
编写函数, 接收一个列表(包含30个整形数)和一个整形数k, 返回一个新列表.
函数需求:
- 将列对应(不包含k)表下标k之前的元素逆序;
- 将下标k之后的元素逆序

def isok(li,k):
    if isinstance(li,list) and isinstance(k,int):
        li1 = li[:k-1]
        li2 = li[k:]
        li[:k-1] = li1[::-1]
        li[k:] = li2[::-1]
        return li
    else:
        print('Error')
        return
print(isok([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],5))
[4, 3, 2, 1, 5, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6]

12.函数练习5
模拟轮盘抽奖游戏
#轮盘分为三部分: 一等奖, 二等奖和三等奖;
#轮盘转的时候是随机的,
#如果范围在[0,0.08)之间,代表一等奖,
#如果范围在[0.08,0.3)之间,代表2等奖,
#如果范围在[0, 1.0)之间,代表3等奖,
#模拟本次活动1000人参加, 模拟游戏时需要准备各等级奖品的个数.

import random
dirc1 = {
    '一等奖':(0,0.08),
    '二等奖':(0.08,0.3),
    '三等奖':(0.3,1),
}
def fun():
    number = random.random()
    for k,v in dirc1.items():
        if v[0] <= number < v[1]:
            return k
# print(fun())
dirc2 = {}
for i in range(1000):
    res = fun()
    if res not in dirc2:
        dirc2[res] = 1
    else:
        dirc2[res] += 1
for k,v in dirc2.items():
    print(k,'--->',v)
三等奖 ---> 704
一等奖 ---> 78
二等奖 ---> 218

3.列表生成式
1.列表生成式
1)接收变量k,a,b

s = '51 5000 10000'
li = []
for i in s.split():
    li.append(int(i))
k,a,b = li
print(k,a,b)
s = '51 5000 10000'
li = [int(i) for i in s.split()]
k,a,b = li
print(k,a,b)
51 5000 10000

2)生成一个列表,列表元素分别为[11,22,33,44…n*n]

li = []
for i in range(1,8):
    li.append(i**i)
print(li)
li = [i**i for i in range(1,8)]
print(li)
[1, 4, 27, 256, 3125, 46656, 823543]

3)找出1~10之间的所有的偶数

print([i for i in range(1, 11) if i % 2 == 0])
[2, 4, 6, 8, 10]

4)找出1~1000之间的所有质数

def isPrime(num):
    for i in range(2,num):
        if num % i == 0:
            return False
    else:
        return True
print([i for i in range(2,1001) if isPrime(i)])
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

2.for循环嵌套

s1='ABC'
s2='123'
'A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3'
print([i + j for i in 'ABC' for j in '123'])
li =[i + j for i in 'ABC' for j in '123']
print(li)
['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3']

需求:讲3x3的矩阵转换成一堆数组
[
[1,2,3],
[4,5,6],
[7,8,9]
]
[1,2,3,4,5,6,7,8,9]

li = [
[1,2,3],
[4,5,6],
[7,8,9]
]
print([item2 for item1 in li for item2 in item1])
from itertools import chain
print(list(chain(*li)))
li1 = []
for item1 in li:
    for item2 in item1:
        li1.append(item2)
print(li1)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

3.列表生成式练习
1)将列表中所有内容都变成小写

li = ['asdaDDs' 'DSAda']
print([i.lower() for i in li])
['asdaddsdsada']

2)找出/var/log目录中,所有以.log结尾的文件名或者目录名

import os
print([filename for filename in os.listdir('/var/log') if filename.endswith('.log')])
['yum.log', 'wpa_supplicant.log', 'Xorg.0.log', 'boot.log']

3)找出1~10之间所有偶数, 并且返回一个列表,(包含以这个偶数为半径的圆的面积)
方法一:

import math
li = []
for i in range(2,12,2):
    square = math.pi*i*i
    li.append(square)
print(li)
12.566370614359172, 50.26548245743669, 113.09733552923255, 201.06192982974676, 314.1592653589793]

方法二:

import math
print([math.pi*i*i for i in range(2,11,2)])
[12.566370614359172, 50.26548245743669, 113.09733552923255, 201.06192982974676, 314.1592653589793]

4)-题目描述:

给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。输
入值小于1000。
如,输入为10, 程序应该输出结果为2。(共有两对质数的和为10,分别为(5,5),(3,7))
#[2,3,5,7]
-输入描述:
输入包括一个整数n,(3 ≤ n < 1000)
-输出描述:
输出对数
-示例1 :
输入:
10
输出:
2

n = int(input('请输入一个整数(3<=n<1000):'))
def isPrime(num):
    for i in range(2,num):
        if num % i == 0:
            return False
    else:
        return True
li = ([i for i in range(2,n+1) if isPrime(i)])
count = 0
for i in li:
    if (n-i) in li and i<= n-i:
        count += 1
print(count)
请输入一个整数(3<=n<1000):10
2

4.字典生成式
1.假设有20个学生,学生分数在60~100之间,筛选出成绩在90分以上的学生

import random
student = {}
for i in range(20):
    name = 'westos' + str(i)
    score = random.randint(60,100)
    student[name] = score
print(student)
highscore = {}
for name,score in student.items():
    if score > 90:
        highscore[name] = score
print(highscore)
student2 = {'westos' + str(i):random.randint(60,100) for i in range(20)}
print(student2)
highscore2 = {name:score for name,score in student2.items() if score>90}
print(highscore2)
{'westos0': 75, 'westos1': 90, 'westos2': 87, 'westos3': 67, 'westos4': 78, 'westos5': 60, 'westos6': 89, 'westos7': 63, 'westos8': 63, 'westos9': 78, 'westos10': 70, 'westos11': 84, 'westos12': 76, 'westos13': 83, 'westos14': 93, 'westos15': 67, 'westos16': 90, 'westos17': 70, 'westos18': 87, 'westos19': 63}
{'westos14': 93}
{'westos0': 84, 'westos1': 80, 'westos2': 62, 'westos3': 75, 'westos4': 85, 'westos5': 87, 'westos6': 85, 'westos7': 67, 'westos8': 91, 'westos9': 94, 'westos10': 79, 'westos11': 97, 'westos12': 85, 'westos13': 64, 'westos14': 71, 'westos15': 69, 'westos16': 65, 'westos17': 81, 'westos18': 70, 'westos19': 60}
{'westos8': 91, 'westos9': 94, 'westos11': 97}

2.将所有的key值变为大写

d = dict(a=1,b=2)
d2 = {}
for i in d:
    d2[i.upper()] = d[i]
print(d2)
print({k.upper():v for k,v in d.items()})
{'A': 1, 'B': 2}
{'A': 1, 'B': 2}

3.大小写key值合并,统一以小写输出

d3 = dict(a=1,b=2,c=3,A=4,B=5)
print({k.lower():d3.get(k.upper(),0)+d3.get(k.lower(),0) for k,v in d3.items()})
d4 = {}
for k,v in d3.items():
    key = k.lower()
    if key not in d4:
        d4[key] = v
    else:
        d4[key] += v
print(d4)
{'a': 5, 'b': 7, 'c': 3}
{'a': 5, 'b': 7, 'c': 3}

5.集合生成式

print({i ** 2 for i in {1, 2, 3}})
print({i ** 2 for i in {1, 2, 3, 9, 12} if i % 3 == 0})
{1, 4, 9}
{81, 9, 144}

6.生成器
1.创建生成器
1)创建生成器的方法1

In [7]: nums = (x*2 for x in range(10))

In [8]: nums
Out[8]: <generator object <genexpr> at 0x7fb2a6137fc0>

In [9]: for num in nums:
   ...:     print(num)
   ...:

2)创建生成器的方法2(定义一个函数,让这个函数变成生成器)

def fib(num):
    a,b,count = 0,1,1
    while count <= num:
        print(b)
        a,b = b,a+b
        count +=1

fib(1000)
1
1
2
3
5
8
13
21
34

如果在调用函数的时候,发现这个函数中有yeild
那么此时,也就不是调用函数了,而是创建了一个生成器对象

def creat_num(num):
    print('~~~1~~~')
    a,b,count = 0,1,0
    while count < num:
        print('~~~2~~~')
        # print(b)
        yield b
        print('~~~3~~~')
        a,b = b,b+a
        count += 1
        print('~~~4~~~')
obj = creat_num(3)
print(obj)
ret = next(obj)
print(ret)
ret1 = next(obj)
print(ret1)
ret2 = next(obj)
print(ret2)
ret3 = next(obj)
print(ret3)

解决方法:

def creat_num(num):
    print('~~~1~~~')
    a,b,count = 0,1,0
    while count < num:
        print('~~~2~~~')
        # print(b)
        yield b
        print('~~~3~~~')
        a,b = b,b+a
        count += 1
        print('~~~4~~~')
obj = creat_num(3)
while True:
    try:
        ret = next(obj)
        print('obj:',ret)
    except Exception as red:
        print(red.value)
        break
~~~1~~~
~~~2~~~
n2: 1
~~~3~~~
~~~4~~~
~~~2~~~
n2: 1
~~~3~~~
~~~4~~~
~~~2~~~
n2: 2
~~~3~~~
~~~4~~~
None

#两个生成器对象之前并没有任何关系

def creat_num(num):
    print('~~~1~~~')
    a,b,count = 0,1,0
    while count < num:
        print('~~~2~~~')
        # print(b)
        yield b
        print('~~~3~~~')
        a,b = b,b+a
        count += 1
        print('~~~4~~~')
obj = creat_num(3)
print(obj)
ret = next(obj)
print(ret)
ret1 = next(obj)
print(ret1)
ret2 = next(obj)
print(ret2)
obj2 = creat_num(100)
print(obj2)
ret4 = next(obj2)
print(ret4)
<generator object creat_num at 0x7f27fa6897d8>
~~~1~~~
~~~2~~~
1
~~~3~~~
~~~4~~~
~~~2~~~
1
~~~3~~~
~~~4~~~
~~~2~~~
2
<generator object creat_num at 0x7f27fa6d82b0>
~~~1~~~
~~~2~~~
1

2.生成器send

def creat_num(num):
    a,b,count = 0,1,0
    while count < num:
        ret = yield b
        print('>>>>>ret>>>>>',ret)
        a,b =b,a+b
        count += 1
obj = creat_num(100)
red = next(obj)
print(red)
red1 = next(obj)
print(red1)
red2 = next(obj)
print(red2)
red = obj.send('hello')
print(red)
1
>>>>>ret>>>>> None
1
>>>>>ret>>>>> None
2
>>>>>ret>>>>> hello
3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值