zoukankan      html  css  js  c++  java
  • Python基础-----函数、内置函数、递归等练习

    !/usr/bin/env python
    -*- coding:utf-8 -*-

    ##############################################################################
    1、列举布尔值为False的值
    0 False '' [] () {} None

    ##############################################################################
    2、根据范围获取其中3和7整除的所有数的和,并返回调用者;
    符合条件的数字个数以及符合条件数字的总和

    def func(start_num,stop_number):
    res = []
    for i in range(start_num,stop_number):
    if i % 3 ==0 and i % 7 == 0:
    res.append(i)
    return res,sum(res),len(res)

    test = func(1,25)
    print(type(test))

    ##############################################################################
    3、函数的默认返回值是 None

    ##############################################################################
    4、 break:结束当前整个循环
    continue:结束本次循环进入下一次循环
    return:结束函数,并返回结果,默认为None

    ##############################################################################
    5、使用set集合获取两个列表l1 = [11,22,33],l2 = [22,33,44]中的相同元素

    l1 = [11,22,33]
    l2 = [22,33,44]
    set1 = set(l1)
    set2 = set(l2)
    same = set1 & set2
    print(same)

    ##############################################################################
    6、定义函数统计一个字符串中大写字母、小写字母、数字的个数,并以字典为结果返回给调用者

    def func(st):
    dic = {}
    count_num = 0
    count_cap = 0
    count_low = 0
    for i in st:
    if i.isdigit():
    count_num += 1
    elif i.isupper():
    count_cap += 1
    elif i.islower():
    count_low += 1
    dic['数字'] = count_num
    dic['大写字母'] = count_cap
    dic['小写字母'] = count_low
    return dic
    test_str = func('ASD-+xcf123')
    print(test_str)

    ##############################################################################
    7、函数的位置参数、关键字参数、默认参数、可变长参数
    位置参数:按形参的位置传入较位置参数,即普通参数
    关键字参数:传入实参时指定形参的值
    默认参数:形参直接指定默认值得参数
    可变长参数:*args **kwargs,前者只能接收没有位置参数的实参或传入的列表、元组,
    后者可以接收关键字参数,和字典格式

    ##############################################################################
    8、字符串“你好”转为以 utf-8 编码形式转换为字节

    方法1
    s = '你好'
    b_s1 = bytes(s,'utf-8')
    print(b_s1)

    # 方法2
    s = '你好'
    b_s2 = s.encode('utf-8')
    print(b_s2)

    ##############################################################################
    9、利用内置函数zip(),实现如下功能:

    l1 = ['alex',11,22,33]
    l2 = ['is',11,22,33]
    l3 = ['good',11,22,33]
    l4 = ['boy',11,22,33]
    请获取字符串s = 'alex_is_good_boy'

    print('_'.join(list(zip(l1,l2,l3,l4))[0]))

    ##############################################################################
    9、利用递归实现 1*2*3*4*5*6*7 = 5040
    方法1:
    def func(x,y=1):
    if x == 1:
    return y
    y *= x
    return func(x-1,y)
    f = func(7)
    print(f)

    方法2:
    def func1(x):
    if x == 1:
    return 1
    return x * func1(x-1)
    f1 = func1(7)
    print(f1)

    方法3
    from functools import reduce
    res = reduce(lambda x,y:y*x,range(1,8))
    print(res)

    ##############################################################################
    10、利用with实现同时打开两个文件(一读,一写,并将读取的内突写入到写入模式的文件中)

    with open('a','r') as f1,open('b','w') as f2:
    f2.write(f1.read())

    ##############################################################################
    11、有如下两个列表,第一个列表中的数字无序不重复排列,第二个为空列表
    需求:
    取出列表1的最小值放到列表2的首个位置
    取出列表1的最小值(仅大于上次一的最小值)放到列表2的首个位置
    取出列表1的最小值(仅大于上次一的最小值)放到列表2的首个位置...
    以此类推,从而获取一个有序的列表2,并将其返回给函数调用者

    l1 = [2,1,6,4,9,0,22,5]
    l2 = []
    def sort_l(x,y):
    for i in range(len(x)):
    min_num = min(x)
    x.remove(min_num)
    y.insert(0,min_num)
    return y

    s_l = sort_l(l1,l2)
    print(l2)

    ##############################################################################
    12、猴子吃桃:猴子第一天摘下若干个桃子,当即吃了一半,不过瘾就多吃了一个。第二天又将剩下
    的桃子吃了一半,不过瘾多吃了一个。以后每天都吃前一天剩下的一般再加一个。到第10天刚好剩下
    一个。问猴子第一天摘了多少个桃子?

    def f(x,day):
    day -= 1
    if day == 0:
    return x
    x = (x+1) * 2
    return f(x,day)
    res = f(1,10)
    print(res)

    ##############################################################################
    13、 a.利用filter函数、自定义函数获取l1中元素大于33的所有元素,l1 = [11,22,33,44,55]
    l1 = [11,22,33,44,55]
    def f(x):
    return x > 33
    new_list = list(filter(f,l1))
    print(new_list)

    # 利用匿名函数
    new_list = list(filter(lambda x:x>33,l1))
    print(new_list)

    b. 利用map、自定义函数将所有是奇数的元素加 100
    l1 = [11,22,33,44,55]
    def f(x):
    if x % 2:
    return x+100
    else:
    return x
    new_list = list(map(f,l1))
    print(new_list)

    利用匿名函数(和三元表达式)
    new_list = list(map(lambda x:(x+100 if x % 2 else x),l1))
    print(new_list)
  • 相关阅读:
    Educational Codeforces Round 88 (Rated for Div. 2) D. Yet Another Yet Another Task(枚举/最大连续子序列)
    Educational Codeforces Round 88 (Rated for Div. 2) A. Berland Poker(数学)
    Educational Codeforces Round 88 (Rated for Div. 2) E. Modular Stability(数论)
    Educational Codeforces Round 88 (Rated for Div. 2) C. Mixing Water(数学/二分)
    Codeforces Round #644 (Div. 3)
    Educational Codeforces Round 76 (Rated for Div. 2)
    Educational Codeforces Round 77 (Rated for Div. 2)
    Educational Codeforces Round 87 (Rated for Div. 2)
    AtCoder Beginner Contest 168
    Codeforces Round #643 (Div. 2)
  • 原文地址:https://www.cnblogs.com/Meanwey/p/9741277.html
Copyright © 2011-2022 走看看