Python3学习笔记(MOOC)
文本进度条实例
- #!/usr/bin/env python3
- import time
- #for i in range(101):
- # print ("\r{:3.0f}%".format(i),end="")
- # time.sleep(0.1)
- scale = 50
- print("执行开始".center(scale//2,"-"))
- start = time.perf_counter()
- for i in range(scale+1):
- a = '*' * i
- b = '-' * (scale - i)
- c = (i/scale)*100
- time.sleep(0.1)
- dur = time.perf_counter() - start
- print ("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end="")
- print("")
- print("执行结束".center(scale//2,"-"))
BMI指数计算(if条件)
- #!/usr/bin/env python3
- def BMI():
- height,weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]:"))
- bmi = weight / pow(height,2)
- print("BMI指数为:{:0.2f}".format(bmi))
- who,nat="",""
- if bmi < 18.5:
- who, nat = "偏瘦","偏瘦"
- elif 18.5 <= bmi < 24:
- who, nat = "正常","正常"
- elif 24 <= bmi < 25:
- who, nat = "正常","偏胖"
- elif 25 <= bmi < 28:
- who, nat = "偏胖","偏胖"
- elif 28 <= bmi < 30:
- who, nat = "偏胖","肥胖"
- else:
- who, nat = "肥胖","肥胖"
- print("BMI指标为:国际:{} 国内:{}".format(who,nat))
- try:
- BMI()
- except:
- print("输入错误")
π值计算(公式和蒙特卡罗方法)
- #!/usr/bin/env python3
- #计算pi
- pi = 0
- N = 100
- for k in range(N):
- pi += 1/pow(16,k)*(4/(8*k+1)-2/(8*k+4)-1/(8*k+5)-1/(8*k+6))
- print("圆周率是:{}".format(pi))
- #蒙特卡洛方法
- from random import random
- from time import perf_counter
- DARTS = 1000*1000
- hits = 0.0
- start = perf_counter()
- for i in range(1,DARTS+1):
- x,y = random(),random()
- dist = pow(x**2+y**2,0.5)
- if dist <= 1.0:
- hits += 1
- pi = 4* (hits/DARTS)
- print("圆周率是:{}".format(pi))
- print("运行时间是:{:.2f}s".format(perf_counter()-start))
异常处理
- #!/usr/bin/env python3
- try:
- num = eval(input("请输入一个整数:"))
- print(num ** 2)
- except:#try执行错误后执行
- print("输入错误")
- else:#正常运行后执行
- print("输入正确")
- finally:#无论try是否执行正确,在最后都会执行
- print("程序结束")
递归实例:斐波那契数列、汉诺塔、科赫雪花
- #!/usr/bin/env python3
- #斐波那契数列
- def fibo(n):
- if n == 1 or n == 2:
- return 1
- else:
- return fibo(n-1)+fibo(n-2)
- print(fibo(8))
- #汉诺塔
- def hano(n,src,mid,dst):
- if n == 1:
- print(n,"{}->{}".format(src,dst))
- else:
- hano(n-1,src,dst,mid)
- print(n,"{}->{}".format(src,dst))
- hano(n-1,mid,src,dst)
- hano(3,"A","B","C")
- #科赫雪花
- import turtle
- def koch(size,n):
- if n == 0:
- turtle.fd(size)
- else:
- for angle in [0,60,-120,60]:
- turtle.left(angle)
- koch(size/3,n-1)
- def main():
- turtle.setup(800,800)
- turtle.penup()
- turtle.goto(-200,100)
- turtle.pendown()
- turtle.pensize(2)
- level = 3
- koch(400,level)
- turtle.right(120)
- koch(400,level)
- turtle.right(120)
- koch(400,level)
- turtle.hideturtle()
- main()
词频统计
- #统计单词频率
- def getTtext(filename):
- txt = open(filename,'r',encoding='utf-8').read()
- txt = txt.lower()
- for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
- txt = txt.replace(ch,' ')
- return txt
- text = getTtext('text.txt')
- words = text.split()
- counts = {}
- for word in words:
- counts[word] = counts.get(word,0) + 1
- items = list(counts.items())
- items.sort(key=lambda x:x[1],reverse=True)
- for i in range(10):
- word,count = items[i]
- print("{0:<10}{1:>5}".format(word,count))
jieba jieba分词的三种模式
# 精确模式:jieba.lcut把文本的切分开,不存在冗余单词
# 全模式:把文本中所有可能的词语都扫描出来,冗余
# 搜索引擎模式:在精确基础上,对长词再次切分
- In [1]: import jieba
- In [2]: jieba.lcut("中国是一个伟大的国家").
- Out[2]: ['中国', '是', '一个', '伟大', '的', '国家']
- In [3]: jieba.lcut("中国是一个伟大的国家",cut_all=True)
- Out[3]: ['中国', '国是', '一个', '伟大', '的', '国家']
- In [4]: jieba.lcut_for_search("中华人民共和国是伟大的")
- Out[4]: ['中华', '华人', '人民', '共和', '共和国', '中华人民共和国', '是', '伟大', '的']
- In [5]: jieba.add_word("故园旧梦")
Python3学习笔记(MOOC)的更多相关文章
- Python3学习笔记(urllib模块的使用)转http://www.cnblogs.com/Lands-ljk/p/5447127.html
Python3学习笔记(urllib模块的使用) 1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, ...
- Python3学习笔记 - 准备环境
前言 最近乘着项目不忙想赶一波时髦学习一下Python3.由于正好学习了Docker,并深深迷上了Docker,所以必须趁热打铁的用它来创建我们的Python3的开发测试环境.Python3的中文教程 ...
- python3学习笔记(7)_listComprehensions-列表生成式
#python3 学习笔记17/07/11 # !/usr/bin/env python3 # -*- conding:utf-8 -*- #通过列表生成式可以生成格式各样的list,这种list 一 ...
- python3学习笔记(6)_iteration
#python3 学习笔记17/07/10 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #类似 其他语言的for循环,但是比for抽象程度更高 # f ...
- python3学习笔记(5)_slice
#python3 学习笔记17/07/10 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #切片slice 大大简化 对于指定索引的操作 fruits ...
- Python3学习笔记01-环境安装和运行环境
最近在学习Python3,想写一些自己的学习笔记.方便自己以后看,主要学习的资料来自菜鸟教程的Python3教程和廖雪峰官方网站的Python教程. 1.下载 1)打开https://www.pyth ...
- python3学习笔记(9)_closure
#python 学习笔记 2017/07/13 # !/usr/bin/env python3 # -*- conding:utf-8 -*- #从高阶函数的定义,我们可以知道,把函数作为参数的函数, ...
- python3学习笔记(8)_sorted
# python学习笔记 2017/07/13 # !/usr/bin/env python3 # -*- coding:utf-8 -*- #python 内置sorted()函数 可以对list进 ...
- python3学习笔记(4)_function-参数
#python学习笔记 17/07/10 # !/usr/bin/evn python3 # -*- coding:utf-8 -*- import math #函数 函数的 定义 #定义一个求绝对值 ...
- python3学习笔记(1)_string
#python学习笔记 17/07/07 # !/usr/bin/evn python3 # -*- coding:utf-8 -*- #r"" 引号当中的字符串不转义 #练习 # ...
随机推荐
- lambda 分组后的count
var list = stuList.GroupBy(b => b.PersonalId).Select(g => (new { personalId = g.Key, count = g ...
- 【记录】spring/springboot 配置mybatis打印sql
======================springboot mybatis 打印sql========================================== 方式 一: ##### ...
- Tutorial1
一 Introduction to tf2 本部分是关于tf2简单介绍,比如tf2能做什么,并使用一个turtlesim的例子来显示tf2在多机器人中的一些能力.同时也包括一些工具的使用,比如tf2_ ...
- Java8 stream基础
List<Integer> list = new ArrayList<Integer>(); list.add(2); list.add(4); list.add(0); li ...
- 【串线篇】Mybatis之SSM整合
SSM:Spring+SpringMVC+MyBatis 建立Java web项目 一.导包 1).Spring: [aop核心] com.springsource.net.sf.cglib-2.2. ...
- HugeGraph图数据库--测试
2018年百度的HugeGraph.实现了Apache TinkerPop3框架及完全兼容Gremlin查询语言.开源项目https://github.com/hugegraph HugeGraph典 ...
- Java对象流与序列化学习
对象流与序列化 对象流有两个类 ObjectOutputStream:将java对象的基本数据类型和图形写入OutputStream ObjectInputStream:对以前使用ObjectOutp ...
- Redis GeoHash
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11632810.html 背景 微信找附近的人,滴滴找附近的单车,饿了么找附近的餐馆 GeoHash算法 ...
- Python基础教程(020)--集成开发环境IDE简介--Pycharm
前言 学会掌握Pycharm工具 内容 集成了开发软件需要的所有工具 1,图形用户界面 2,代码编译器(支持代码补全,自动缩进) 3,编译器,解释器 4,调试器(断点,单步执行) Pycharm介绍 ...
- element upload上传前对文件专门bs64上传
<!-- 文件上传 --> <template> <section class="file-upload"> <p class=" ...