py过程中练习

最大公约数最小公倍数

x=eval(input("please enter a num as x:"))
y=eval(input("please enter a num as y:"))
mul=x*y
while x%y!=0:
    x,y=y,x%y
print("max same is:",y)
min=mul//y
print("min same is:",min)
 

统计各种字符个数

s=input("please enter a text:")
alpha,digit,space,other=0,0,0,0
for i in s:
    if i.isalpha():
        alpha+=1
    elif i.isdigit():
        digit+=1
    elif i.isspace():
        space+=1
    else :
        other+=1
print("""字母的个数为:{},数字的个数为:{}
,空格的个数为:{},其它的个数为:{}""".format(alpha,digit,space,other))

函数类型

def add1(a,b):
    return a+b
def add2(a,b,c):
    return a+b+c
def text():
    pass
print(type(text()))
print(type(add1))
print(type(add2(1,2,3)))

函数定义不一定在调用之前:  一般认为函数定义必须在函数调用之前,但如果函数A调用函数B,那么函数B的定义可以放在函数B的调用之后

def add3(n):
    n=add2(n)
    return n+1
def add2(n):
    return n+2
print(add3(1))
 

值传递和地址传递(list列表)

斐波那契数列(递归)

def feibo(n):
    if n==1 or n==2:
        return 1
    else:
        return feibo(n-1)+feibo(n-2)
x=eval(input("please enter the num you want to search:"))
print("the num's number is:",feibo(x))

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值