文本进度条实例

  1. #!/usr/bin/env python3
  2. import time
  3. #for i in range(101):
  4. # print ("\r{:3.0f}%".format(i),end="")
  5. # time.sleep(0.1)
  6. scale = 50
  7. print("执行开始".center(scale//2,"-"))
  8. start = time.perf_counter()
  9. for i in range(scale+1):
  10. a = '*' * i
  11. b = '-' * (scale - i)
  12. c = (i/scale)*100
  13. time.sleep(0.1)
  14. dur = time.perf_counter() - start
  15. print ("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end="")
  16. print("")
  17. print("执行结束".center(scale//2,"-"))

BMI指数计算(if条件)

  1. #!/usr/bin/env python3
  2. def BMI():
  3. height,weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]:"))
  4. bmi = weight / pow(height,2)
  5. print("BMI指数为:{:0.2f}".format(bmi))
  6. who,nat="",""
  7. if bmi < 18.5:
  8. who, nat = "偏瘦","偏瘦"
  9. elif 18.5 <= bmi < 24:
  10. who, nat = "正常","正常"
  11. elif 24 <= bmi < 25:
  12. who, nat = "正常","偏胖"
  13. elif 25 <= bmi < 28:
  14. who, nat = "偏胖","偏胖"
  15. elif 28 <= bmi < 30:
  16. who, nat = "偏胖","肥胖"
  17. else:
  18. who, nat = "肥胖","肥胖"
  19. print("BMI指标为:国际:{} 国内:{}".format(who,nat))
  20. try:
  21. BMI()
  22. except:
  23. print("输入错误")

π值计算(公式和蒙特卡罗方法)

  1. #!/usr/bin/env python3
  2. #计算pi
  3. pi = 0
  4. N = 100
  5. for k in range(N):
  6. pi += 1/pow(16,k)*(4/(8*k+1)-2/(8*k+4)-1/(8*k+5)-1/(8*k+6))
  7. print("圆周率是:{}".format(pi))
  8.  
  9. #蒙特卡洛方法
  10.  
  11. from random import random
  12. from time import perf_counter
  13. DARTS = 1000*1000
  14. hits = 0.0
  15. start = perf_counter()
  16. for i in range(1,DARTS+1):
  17. x,y = random(),random()
  18. dist = pow(x**2+y**2,0.5)
  19. if dist <= 1.0:
  20. hits += 1
  21. pi = 4* (hits/DARTS)
  22. print("圆周率是:{}".format(pi))
  23. print("运行时间是:{:.2f}s".format(perf_counter()-start))

异常处理

  1. #!/usr/bin/env python3
  2. try:
  3. num = eval(input("请输入一个整数:"))
  4. print(num ** 2)
  5. except:#try执行错误后执行
  6. print("输入错误")
  7. else:#正常运行后执行
  8. print("输入正确")
  9. finally:#无论try是否执行正确,在最后都会执行
  10. print("程序结束")

递归实例:斐波那契数列、汉诺塔、科赫雪花

  1. #!/usr/bin/env python3
  2. #斐波那契数列
  3. def fibo(n):
  4. if n == 1 or n == 2:
  5. return 1
  6. else:
  7. return fibo(n-1)+fibo(n-2)
  8. print(fibo(8))
  9. #汉诺塔
  10. def hano(n,src,mid,dst):
  11. if n == 1:
  12. print(n,"{}->{}".format(src,dst))
  13. else:
  14. hano(n-1,src,dst,mid)
  15. print(n,"{}->{}".format(src,dst))
  16. hano(n-1,mid,src,dst)
  17. hano(3,"A","B","C")
  18. #科赫雪花
  19. import turtle
  20. def koch(size,n):
  21. if n == 0:
  22. turtle.fd(size)
  23. else:
  24. for angle in [0,60,-120,60]:
  25. turtle.left(angle)
  26. koch(size/3,n-1)
  27. def main():
  28. turtle.setup(800,800)
  29. turtle.penup()
  30. turtle.goto(-200,100)
  31. turtle.pendown()
  32. turtle.pensize(2)
  33. level = 3
  34. koch(400,level)
  35. turtle.right(120)
  36. koch(400,level)
  37. turtle.right(120)
  38. koch(400,level)
  39. turtle.hideturtle()
  40. main()

词频统计

  1. #统计单词频率
  2. def getTtext(filename):
  3. txt = open(filename,'r',encoding='utf-8').read()
  4. txt = txt.lower()
  5. for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
  6. txt = txt.replace(ch,' ')
  7. return txt
  8. text = getTtext('text.txt')
  9. words = text.split()
  10. counts = {}
  11. for word in words:
  12. counts[word] = counts.get(word,0) + 1
  13. items = list(counts.items())
  14. items.sort(key=lambda x:x[1],reverse=True)
  15. for i in range(10):
  16. word,count = items[i]
  17. print("{0:<10}{1:>5}".format(word,count))

jieba jieba分词的三种模式
# 精确模式:jieba.lcut把文本的切分开,不存在冗余单词
# 全模式:把文本中所有可能的词语都扫描出来,冗余
# 搜索引擎模式:在精确基础上,对长词再次切分

  1. In [1]: import jieba
  2. In [2]: jieba.lcut("中国是一个伟大的国家").
  3. Out[2]: ['中国', '是', '一个', '伟大', '的', '国家']
  4. In [3]: jieba.lcut("中国是一个伟大的国家",cut_all=True)
  5. Out[3]: ['中国', '国是', '一个', '伟大', '的', '国家']
  6. In [4]: jieba.lcut_for_search("中华人民共和国是伟大的")
  7. Out[4]: ['中华', '华人', '人民', '共和', '共和国', '中华人民共和国', '是', '伟大', '的']
  8. In [5]: jieba.add_word("故园旧梦")

Python3学习笔记(MOOC)的更多相关文章

  1. Python3学习笔记(urllib模块的使用)转http://www.cnblogs.com/Lands-ljk/p/5447127.html

    Python3学习笔记(urllib模块的使用)   1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None,  ...

  2. Python3学习笔记 - 准备环境

    前言 最近乘着项目不忙想赶一波时髦学习一下Python3.由于正好学习了Docker,并深深迷上了Docker,所以必须趁热打铁的用它来创建我们的Python3的开发测试环境.Python3的中文教程 ...

  3. python3学习笔记(7)_listComprehensions-列表生成式

    #python3 学习笔记17/07/11 # !/usr/bin/env python3 # -*- conding:utf-8 -*- #通过列表生成式可以生成格式各样的list,这种list 一 ...

  4. python3学习笔记(6)_iteration

    #python3 学习笔记17/07/10 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #类似 其他语言的for循环,但是比for抽象程度更高 # f ...

  5. python3学习笔记(5)_slice

    #python3 学习笔记17/07/10 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #切片slice 大大简化 对于指定索引的操作 fruits ...

  6. Python3学习笔记01-环境安装和运行环境

    最近在学习Python3,想写一些自己的学习笔记.方便自己以后看,主要学习的资料来自菜鸟教程的Python3教程和廖雪峰官方网站的Python教程. 1.下载 1)打开https://www.pyth ...

  7. python3学习笔记(9)_closure

    #python 学习笔记 2017/07/13 # !/usr/bin/env python3 # -*- conding:utf-8 -*- #从高阶函数的定义,我们可以知道,把函数作为参数的函数, ...

  8. python3学习笔记(8)_sorted

    # python学习笔记 2017/07/13 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #python 内置sorted()函数 可以对list进 ...

  9. python3学习笔记(4)_function-参数

    #python学习笔记 17/07/10 # !/usr/bin/evn python3 # -*- coding:utf-8 -*- import math #函数 函数的 定义 #定义一个求绝对值 ...

  10. python3学习笔记(1)_string

    #python学习笔记 17/07/07 # !/usr/bin/evn python3 # -*- coding:utf-8 -*- #r"" 引号当中的字符串不转义 #练习 # ...

随机推荐

  1. lambda 分组后的count

    var list = stuList.GroupBy(b => b.PersonalId).Select(g => (new { personalId = g.Key, count = g ...

  2. 【记录】spring/springboot 配置mybatis打印sql

    ======================springboot mybatis 打印sql========================================== 方式 一: ##### ...

  3. Tutorial1

    一 Introduction to tf2 本部分是关于tf2简单介绍,比如tf2能做什么,并使用一个turtlesim的例子来显示tf2在多机器人中的一些能力.同时也包括一些工具的使用,比如tf2_ ...

  4. Java8 stream基础

    List<Integer> list = new ArrayList<Integer>(); list.add(2); list.add(4); list.add(0); li ...

  5. 【串线篇】Mybatis之SSM整合

    SSM:Spring+SpringMVC+MyBatis 建立Java web项目 一.导包 1).Spring: [aop核心] com.springsource.net.sf.cglib-2.2. ...

  6. HugeGraph图数据库--测试

    2018年百度的HugeGraph.实现了Apache TinkerPop3框架及完全兼容Gremlin查询语言.开源项目https://github.com/hugegraph HugeGraph典 ...

  7. Java对象流与序列化学习

    对象流与序列化 对象流有两个类 ObjectOutputStream:将java对象的基本数据类型和图形写入OutputStream ObjectInputStream:对以前使用ObjectOutp ...

  8. Redis GeoHash

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11632810.html 背景 微信找附近的人,滴滴找附近的单车,饿了么找附近的餐馆 GeoHash算法 ...

  9. Python基础教程(020)--集成开发环境IDE简介--Pycharm

    前言 学会掌握Pycharm工具 内容 集成了开发软件需要的所有工具 1,图形用户界面 2,代码编译器(支持代码补全,自动缩进) 3,编译器,解释器 4,调试器(断点,单步执行) Pycharm介绍 ...

  10. element upload上传前对文件专门bs64上传

    <!-- 文件上传 --> <template> <section class="file-upload"> <p class=" ...