【python】入门笔记二 :基本函数运用

本文是Python入门系列的第二篇,主要介绍Python的基本函数应用,包括常见的内置函数和一些实用技巧,帮助初学者掌握Python函数的使用。
摘要由CSDN通过智能技术生成

1

def recursion(n):
    if n==1:
        return 1
    else:
        return n+recursion(n-1)
recursion(100)

 

2

ch=0
num=0
space=0
other=0
def count(str):
    global ch,num,space,other
    for i in str:
        if i.isalpha():
            ch+=1
        elif i.isdigit():
            num+=1
        elif i.isspace():
            space+=1
        else:
            other+=1
a=input()
count(a)
print('ch=%d,num=%d,space=%d,other=%d'%(ch,num,space,other))

3

def LeapYear(year):
    if year%400==0 or year%100!=0 and year%4==0:
        print('Yes')
    else:
        print('No')
year=int(input())
LeapYear(year)

4

def primeNum(num):
    if num==1 or num==2 or num==3:
        return True
    for i in range(2,int(num/2)):
        if num%i==0:
            return False
    return True
a=int(input())
primeNum(a)

5

def Fibonacci(num):
    if num==1 or num==2:
        return 1
    return Fibonacci(num-1)+Fibonacci(num-2)
Fibonacci(10)

6

c=lambda a,b=1:a*b
print(c(2))
print(c(2,2))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值