https://docs.python.org/zh-cn/3/library/turtle.html#methods-of-turtlescreen-screen
exitonclick() 当点击时退出
知识点
##python自动有回车
print('百日衣衫尽')
print('+'*15) //表示字符串重复15次
print('黄河入海流')
print('+'*15)
print('欲穷千里目')
#百日衣衫尽
#+++++++++++++++
#黄河入海流
#+++++++++++++++
#欲穷千里目
print('+++++++++++++++')
优化1:
print('-'*15)
优化2:
def fun():
print('-'*15)
fun()
优化3:
def fun(char,number):
print(char*number)
fun('+',10)
fun('-',12)
1、避免代码冗余
2、提高程序的可维护性
3、还可以提高程序的灵活性
4、提高代码的可重用性
def hello():
for i in range(2):
print('hello world')
return 1000
n=hello()
print(n)
#hello world
#hello world
#1000
#【默认参数】
#不写return 就默认为return None;
def fun(a,b,c): #形参 没有具体类型
print(a,b,c) #a,b,c 实参
fun(1,2,3) #实参 有具体类型
#1 2 3
fun(a=1,b=2,c=3) #【关键字参数】
fun(a=1,c=3,b=2)
def fun(a,b,c=99):
print(a,b,c)
fun(1,2,3) #覆盖默认值
#1 2 3
#-------------------
def fun(a,b,c=99): #【有默认值的参数】
print(a,b,c)
fun(1,2)
#1 2 99
#【可变长参数】
def fun(a,b,*c): #*c表示可变长参数,接收任意数据打包成元组
print(a,b,c)
fun(1,2,3,4,5,6)
#1 2 (3,4,5,6) 打包成元组
def fun(a,*b,c):
print(a,b,c)
fun(1,2,3,4,5,6)
#报错
ef fun(a,*b,c): #*c表示可变长参数,接收任意数据打包成元组
print(a,b,c)
fun(1,2,3,4,5,c=6)
#1 (2,3,4,5) 6
#一个函数中,只能有一个可变长参数
模块(python文件)
module,文件和模块是一个东西
模块和模块之间可以相互调用,生成相应的内容
.py .pyc .pyd(c语言编辑的.dll文件) .pyo .pyw .so文件就是一个模块
项目执行结构: 1、顶层文件 2、模块
**顶层文件:**对其他模块调配,调用 (例如:main) 本质也是个模块
**模块:**模块和模块之间相互调用
模块使用的语法:
1、import 语句 :
import time #从python中导入time模块,此时可使用time模块中的功能
print(time.time())
#1586953038.35177 #时间戳(是可变的)从1970年到现在多少s
time=100
print(time.time()) #报错 以前time是一个大功能,后来变成一个变量
2、import 别名
import time as hehe
time=100
print(hehe.time()) #1586953038.35177 #从模块里面去取方法
print(time) #100
3、from…import 语句
from time import time
print(time()) #省去了模块
from time import time
time=100
print(time()) #报错
from time import ctime
time=100
print(ctime()) #显示日期
类库
类库网站:https://pypi.org/
课后下载request
模块的分类:
1、官方模块:安装python时已经打包好的模块
2、自定义模块:自己写的模块
3、第三方模块:第三方提供的
安装第三方模块:pip install 模块名
pip 默认从www.pypi.org 下载模块 — 海外网站 —下载比较慢
清华大学镜像源
pip install 模块名 -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
turtle库
turtle不用安装,是默认的模块
import turtle
#创建海龟对象,命名为T
T=turtle.Turtle()
#设置颜色
t.color('red','pink') #线条,内容
#开始画画
t.begin_fill()
#转动头部
t.left(140)
#向前移动指定元素
t.forward(111.65)
#圆函数
def fun():
for i in range(200):
t.right(i)
t.forward(i)
#画圆
fun()
#掉头
t.left(120)
#画另外半个
fun()
#直接前行
t.forward(111.65)
#停止画画
t.end_fill()
默认的角度:
Requests库
Requests 库是一个非常容易上手的 python 爬虫库,相比于 urllib 库, requests 库非常简洁。
Requests 库中有很多方法,但所有的方法在底层都是通过调用 request() 方法来实现的。因此,严格来说,Requests 库只有一个 request() 方法。但一般都不会直接使用这个方法。
import requests # 一开始要导入 Requests 模块
response = requests.get('http://www.baidu.com') # 可以从response中获取我们想要的信息
print(response.content.decode('utf-8'))
print(type(response))
print(response.text)
print(response.cookies)
pyInstaller库
学习博文
把.py程序转化成.exe完美在windows上运行
一,抢火车票的程序.无奈别人的电脑没又装python没法运行.然后就想着把写好得程序转成.exe在windows上直接运行
法一:
1、安装pyinstaller库
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
2、cmd
3、pyinstaller -F aixin.py
4、
法二:
怎么从github上下载文件
首先打开登录github,搜索一个项目,然后进入项目详情页,点击“clone or download”按钮,然后点击“Download ZIP”即可下载项目的压缩包,点击“Open in Desktop”即可打开当前的项目
1、将aixin.py放到目录E:\python六天项目实训\3\pyinstaller-develop下。
2、在CMD命令行进入该目录,并执行命令:python pyinstaller.py -F aixin.py
因为相对路径和绝对路径的问题,而报错了
参考了这个文章: 错误解析
解决办法:写命令时,将文件名写为绝对路径。
E:\python六天项目实训\3\pyinstaller-develop>python pyinstaller.py -F E:\python六天项目实训\3\pyinstaller-develop\aixin.py
案例:
打开pyinstaller-develop\aixin\dist\aixin.exe