python练手一

1.统计字符串字符个数,空格字符个数,数字字符个数,其他字符个数 1星

# -*- coding: utf-8 -*-  
s = input("please input string:")
print(s)
letters = 0
space = 0
digit = 0
others = 0
for ch in s:
    #是否为字母
    if ch.isalpha():
        letters +=1
    #是否为空格
    elif ch.isspace():
        space +=1
    #是否为数字
    elif ch.isdigit():
        digit += 1
    else:
        others += 1
print(letters, space, digit, others)

在这里插入图片描述
PS:在输入时如果不想带“”,可以用raw_input(),它将所有输入视为字符串。

2.把每个合数分解成几个质数相乘的形式,例如:100=225*5,并把代码封装成函数的形式。`````

# -*- coding: utf-8 -*-  

def f(n):
    result = []
    i = 2
    str1 = str(n) + '='
    while(n>1):
        if(n%i==0):
            n/=i
            result.append(str(i))
            i -= 1
        i += 1
    str1 += '*'.join(result)
    return str1

if __name__ == '__main__':
    for i in range(100,120):
        print(f(i))

在这里插入图片描述

3.编写函数,获得目录下所有的文件名。

# -*- coding: utf-8 -*-  
import os

#要检查的文件类型
dict_filetype = ["cpp", "c", "h"]

#brief:递归获得所有文件名
#param:path起始目录,要检查的根目录
#param:allfile天空即可
#result:列表所有与dictionary——filetype对应的文件名

def get_filename(path, allfile):
    
    filelist = os.listdir(path)
    for filename in filelist:
        filepath = os.path.join(path, filename)
        #判断文件夹
        if(os.path.isdir(filepath)):
            #文件夹递归
            get_filename(filepath,allfile)
        else:
            #文件,判断文件类型
            for filetype in dict_filetype:
                temp_file_type = filepath.split(".")
                if(filetype==temp_file_type[-1]):
                    allfile.append(filepath)
                    break
                else:
                    print("the file is not code:%s"%filepath)
    return allfile

if __name__ == '__main__':
    allfile=[]
    get_filename("/home/wc/test", allfile)
    for tmp in allfile:
        print(tmp)

输出:

the file is not code:/home/wc/test/WINGDNG3.TTF the file is not
code:/home/wc/test/WINGDNG3.TTF the file is not
code:/home/wc/test/WINGDNG3.TTF the file is not
code:/home/wc/test/webdings.ttf the file is not
code:/home/wc/test/webdings.ttf the file is not
code:/home/wc/test/webdings.ttf the file is not
code:/home/wc/test/graph2.png the file is not
code:/home/wc/test/graph2.png the file is not
code:/home/wc/test/graph2.png the file is not
code:/home/wc/test/graph.png the file is not
code:/home/wc/test/graph.png the file is not
code:/home/wc/test/graph.png the file is not
code:/home/wc/test/graph4.png the file is not
code:/home/wc/test/graph4.png the file is not
code:/home/wc/test/graph4.png the file is not
code:/home/wc/test/MTExtra.ttf the file is not
code:/home/wc/test/MTExtra.ttf the file is not
code:/home/wc/test/MTExtra.ttf the file is not
code:/home/wc/test/WINGDNG2.TTF the file is not
code:/home/wc/test/WINGDNG2.TTF the file is not
code:/home/wc/test/WINGDNG2.TTF the file is not
code:/home/wc/test/graph0.png the file is not
code:/home/wc/test/graph0.png the file is not
code:/home/wc/test/graph0.png the file is not
code:/home/wc/test/graph3.png the file is not
code:/home/wc/test/graph3.png the file is not
code:/home/wc/test/graph3.png the file is not
code:/home/wc/test/mnist.pkl.gz the file is not
code:/home/wc/test/mnist.pkl.gz the file is not
code:/home/wc/test/mnist.pkl.gz the file is not
code:/home/wc/test/wingding.ttf the file is not
code:/home/wc/test/wingding.ttf the file is not
code:/home/wc/test/wingding.ttf the file is not
code:/home/wc/test/graph1.png the file is not
code:/home/wc/test/graph1.png the file is not
code:/home/wc/test/graph1.png /home/wc/test/test.cpp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东心十

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值