菜鸟学python之程序初体验

作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2684

1.字符串操作:

  • 解析身份证号:生日、性别、出生地等。

 

  • 凯撒密码编码与解码

 

  • 网址观察与批量生成

 

def idption():
    str_id= input("请输入身份证号:")
    if(len(str_id)!=18):
        print("你输入的身份证号有误,请重新输入")
        idption()
    else:
        print("你出生省份为:"+str_id[0:2])
        print("你出生市区为:" + str_id[2:4])
        print("你出生县区为:" + str_id[4:6])
        print("你出生日期为:" + str_id[6:14])
        print("你出生户口派出所为:" + str_id[14:16])
        if(str_id[16:17]=='1'):
            print("你为男性且编码:" + str_id[16:17])
        else:
            print("你为女性且编码:" + str_id[16:17])
        print("校验码为:" + str_id[17:18]+"\n")

运行结果:

菜鸟学python之程序初体验

2.英文词频统计预处理

  • 下载一首英文的歌词或文章或小说。
  • 将所有大写转换为小写
  • 将所有其他做分隔符(,.?!)替换为空格
  • 分隔出一个一个的单词
  • 并统计单词出现的次数。

 

def danciption():
    text='''I'm a Big big girl
in a Big big world
It's not a Big big thing if you leave me
but I do do feel that
I too too will miss you much
miss you much...
I can see the first leaf falling
it's all yellow and nice
It's so very cold outside
like the way I'm feeling inside
I'm a Big big girl
in a Big big world
It's not a big big thing if you leave me
but I do do feel that
I too too will miss you much
miss you much...
Outside it's now raining
and tears are falling from my eyes
why did it have to happen
why did it all have to end
I'm a big big girl
in a big big world
It's not a big big thing if you leave me
but I do do feel that
I too too will miss you much
miss you much...
I have your arms around me ooooh like fire
but when I open my eyes
you're gone...
I'm a big big girl
in a big big world
It's not a big big thing if you leave me
but I do do feel that
I too too will miss you much
miss you much...
I'm a big big girl
in a big big world
It's not a big big thing if you leave me
but I do feel I will miss you much
miss you much...'''
    s=',.!?:'
    for c in s:
        text=text.replace(c,'')
    print(text.split())
    print("单词词频次数:")
    print("big:",text.count('big'))
    print("Big:",text.count('Big'))
    print("a:",text.count('a'))

截图如下:

菜鸟学python之程序初体验

 

3.文件操作

  • 同一目录、绝对路径、相对路径
  • 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
  • 词频统计:下载一首英文的歌词或文章或小说,保存为utf8文件。从文件读入文本进行处理。
#同一路径
f= open(r"Plaintext", 'r', encoding='utf8')
#相对路径
f2=open(r"..\Lin\file_text\Plaintext", 'r', encoding='utf8')
#绝对路径
f3=open(r"G:\PycharmProjects\Lin\file_text\Plaintext", 'r', encoding='utf8')
text=f.read()
f.close()
t=text.find(',')
print(text[0:t])
print(text[t+1:len(text)])

截图

 

 

 4.函数定义

  • 加密函数
  • 解密函数
  • 读文本函数
上一篇:基于邻接表的新顶点的增加


下一篇:vue组件中各方法执行顺序