python练习题9

这篇博客包含Python的基础和进阶练习题。基础部分包括理解不同参数类型、字符串统计、长度判断、数字求和与阶乘计算以及不定长参数的处理。进阶题目涉及检查字符串是否符合Python变量命名规则,以及验证日期的合法性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

''' 基础题 '''

1.简述必需参数、关键字参数、默认参数、不定长参数的区别

必需参数:调用函数的时候必需以正确的顺序传参,实参和形参的数量保持一致
关键字参数:允许函数调用的时候,形参和实参的顺序不一致,使用关键字自动匹配
默认参数:在定义函数的时候,直接给形式参数赋值。
不定长参数:*args:用于接收多个位置参数,得到的形式是元组;**kwargs:用于接收多个关键字参数,得到的形式是字典

2.封装函数,计算传入字符串中单个【数字】、【字母】、【空格] 以及 【其他字符】的个数

老师的办法:
str = "123ASDsdvg4334 &^$# 323d天冷了sds 765&^$ 212你好"
​
​
def test(str1):
    count_digit = 0  # 统计数字
    count_alpha = 0  # 统计字母
    count_space = 0  # 统计空格
    count_other = 0  # 统计其他字符
    for i in str1:
        if i.isdigit():
            count_digit += 1
        elif i.isalpha():
            count_alpha += 1
        elif i.isspace():
            count_space += 1
        else:
            count_other += 1
    print(f"数字有{count_digit}个")
    print(f"字母有{count_alpha}个")
    print(f"空格有{count_space}个")
    print(f"字符有{count_other}个")
​
​
test(str)
​
自己的办法:
def fn(p):
    shuzi = 0 #数字
    zimu = 0 #字母
    kongge = 0 #空格
    qita = 0 #其他
    for i in p:
        if i.isdigit():
            shuzi +=1
        elif i.isalpha():
            zimu += 1
        elif i.isspace():
            kongge +=1
        else:
            qita += 1
    return shuzi,zimu,kongge,qita
​
test = fn(' as5')
print(test)

3.封装函数,判断用户传入的参数(字符串、列表、元组其中之一)长度是否大于5, len()>5

老师的办法
def demo(str2):
    return len(str2)>5
​
自己的办法
def fn(data):
    if len(data) > 5:
        print("该参数长度大于5")
    else:
        print("该参数长度不大于5")
​
​
str = "xiao ran"
list = [12, 34, 56, 78, 90]
tuple = {"长风破浪会有时", "静观其变"}
fn(str)
fn(list)
fn(tuple)

4.封装函数,计算1到n的和, 并返回结果打印出来; (n为函数参数)

def fn4(n): pass

老师的办法:
def fn(n):
    return sum(range(1,n+1))
​
自己的办法:
def fn4(n):
    sum1 = 0
    for i in range(1, n+1):
        sum1 += i
    return sum1
​
​
sum1 = fn4(6)
print(sum1)

5.封装函数,计算n的阶乘, 并返回结果打印出来

def fn5(n): pass

import math
​
​
def fn5(n):
    print(math.factorial(n))
num = fn5(4)
print()
​

6.封装函数,传入不定个数的数字,返回所有数字的和, 提示: *args

def fn(*args):
    print(sum(args))
sum1=fn(12,34,54)
print(sum1)

7.封装函数,判断一个年份是不是闰年

def isRunYear(year):
    return (year % 4==0 and year % 100 !=0) or year % 400==0

''' 进阶题 '''

1.写一个函数,识别字符串是否符合python语法的变量名

1.数字字母下划线,且不能以数字开头. 2.不能使用关键字 ​ import keyword ​ print(keyword.kwlist)

import keyword
​
​
def checkStr(name):
    if name in keyword.kwlist:  # 判断变量名是不是关键字
        return False
    if name[0].isdigit():  # 判断变量名是否是数字开头
        return False
    for i in name:
        if i.isalpha() or i == "_":
            continue
        return False
    return True
​2.写一个函数计算两个数的最小公倍数; 并返回结果打印出来

def func2(a, b): pass

def fn2(a,b):
    for i in range(max(a,b),a*b+1):
        if i % a==0 and i % b==0:
            return i
print(fn2(4,18))
  1. 年月日分别为自定义函数的参数,判断某一个日期是否为合法的日期; (选做)

如: 2020年12月33日不是合法的日期

2021年2月29日是不合法的日期

def fn3(year, month, day): pass

def checkTime(year, month, day):
    if month < 1 or month > 12:
        return False
    if month in [1, 3, 5, 7, 8, 10, 12]:
        return 1 <= day <= 31
    if month == 2:
        if isRunYear(year):   # 引用上面判断闰年
            return 1 <= day <= 29
        return 1 <= day <= 28
    return 1 <= day <= 30
print(checkTime(2021,10,32))
​
​
详细代码:
def isRunYear(year):
    return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
​
​
def checkTime(year, month, day):
    if month < 1 or month > 12:
        return False
    if month in [1, 3, 5, 7, 8, 10, 12]:
        return 1 <= day <= 31
    if month == 2:
        if isRunYear(year):
            return 1 <= day <= 29
        else:
            return 1 <= day <= 28
    return 1 <= day <= 30
print(checkTime(2021, 2, 28))
​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值