编写简单的小程序
文章目录
1.turtle的认识与使用
启动窗口:turtle.setup(width, height, startx, starty)
width, height:窗口的宽度、高度startx, starty:窗口左上角在屏幕中的坐标位置
1.1turtle常用的函数
1.2用turtle画小蛇
注意缩进,python的缩进是有说法的
import turtle
def drawSnake(radius, angle, length):
for i in range(length):
turtle.pencolor("pink")
turtle.circle(radius, angle)
turtle.pencolor("grey")
turtle.circle(-radius, angle)
turtle.circle(radius, angle/2)
turtle.forward(radius/2)# 前进
turtle.circle(15, 180)
turtle.forward(radius/4)
if __name__== "__main__":
turtle.penup()
turtle.forward(-300)
turtle.pendown()
turtle.setup(700, 300, 150, 150)
turtle.pensize(25)# 画笔尺寸
turtle.seth(-40)# 前进的方向
drawSnake(50, 80, 4)
turtle.mainloop() # 使窗口保持打开
1.3begin_fill和end_fill绘制太阳花
from turtle import *
color('red','yellow')
begin_fill()
for i in range(50):
forward(200)
left(170)
end_fill()
done()
2.变量
2.1变量的创建
在Python中,每个变量在使用前必须赋值,变量赋值后,该变量才会被创建。通过直接赋值可创建不同类型的变量。
变量的类型可使用内置函数Type()来返回变量的类型。
a = "hello world!"#字符串
b = 123#整型
c = 12.3#浮点型
Type()的使用
print(type(a))
<class 'str'>
2.2命名规则
- 标识符由字母(A~Z,a~z)、数字(0~9)或下划线( _ )组成,并且**必须以字母或下划线(“_”)**开头。
- 标识区分大小写,sum、SUM、Sum是不同的标识符。
- 不使用保留字作为标识符。
2.3保留字及查看方法
可以使用:
import keyword
keyword.kwlist
保留字
['False',
'None',
'True',
'and',
'as',
'assert',
'async',
'await',
'break',
'class',
'continue',
'def',
'del',
'elif',
'else',
'except',
'finally',
'for',
'from',
'global',
'if',
'import',
'in',
'is',
'lambda',
'nonlocal',
'not',
'or',
'pass',
'raise',
'return',
'try',
'while',
'with',
'yield']
3.运算符
3.1算数运算符
运算符 | 描 述 |
---|---|
+ | 加 —— 两个对象相加 |
- | 减 —— 得到负数或是一个数减去另一个数 |
***** | 乘 ——两个数相乘或是返回一个被重复若干次的字符串 |
/ | 除 —— x除以y //——可以得到整数 |
% | 取模 —— 返回除法的余数 |
** | 幂 —— 返回x的y次幂 |
总结:/:普通的除法计算,有整数有小数
//:得到整数,向下取整
%:取余数
******:幂
3.2关系运算符
操作符 | 操作符含义 |
---|---|
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
== | 等于 |
!= | 不等于 |
3.3逻辑运算符
操作符 | 操作符含义 |
---|---|
and | 与 |
or | 或 |
not | 非 |
4.注释与缩进
缩进
1个缩进相当于4个空格
注释
单行注释
单行注释:在语句前用“#”号。
#这是注释部分
多行注释
一对(三个双引号或三个单引号)引起来的语句,如:
''' 这是注释部分 '''
5.赋值语句
单变量赋值
基本格式:变量=表达式
a = 3
b = 4
同时赋值
基本格式:变量1,变量2,…,变量n=表达式1,表达式2,…,表达式n
a,b = 1,2
6.输出与输入
print()函数
基本格式:print(<表达式1>,<表达式2>,……,< 表达式N>)
print("hello World!")
input()函数
id=input("Please enter your ID:")#使用id来接收输入的值,输入的值会被转换成字符串类型
input()函数接收多个参数
num1,num2=eval(input("Enter two numbers seperated by a comma: "))#输入时使用英文的逗号隔开即可同时赋值
sum=(num1+num2)
print("sum is :",sum)
7.数据类型
7.1字符串的索引
字符串的索引从左往右是0到11,同时可取负值实现从右往左
取值形式:
name= 'HELLO WORLD'
name[0]#H
7.2列表
形式[]的数据
ls=["orange","apple","banana"]
取值方式同上:
ls[1]#"apple"
8.字符编码
- ASCII:每个英文字母有一一对应的8位数字串,可存储256个字符。英文字符
- GBK:GB2312编码集是由中国国家标准总局发布,收录了6763个汉字、拉丁字母、希腊字母、日文平假名及片假名字母、俄语西里尔字母在内的682个字符标准。但是没有包括罕见字和繁体字,GBK、GB18030S是在GB2312基础上的扩充。GBK兼容ASCII,英文用1个字节表示,汉字、日文等字符2个字节表示。阿拉伯语?
- Unicode:统一地球上所有语言文字。 实现方式:这些字符如何传输、存储?如何让两个不同平台之间相互识别同一字符?UTF-8、UTF-16,使用最广的是UTF-8标准。
UTF-8(Universal Character Set/Unicode Transformation Format)是针对Unicode的一种可变长度字符编码。
8.2乱码问题
- Python解释器设置的默认编码
- 源文件设置的编码
- 终端编码
8.3编码检测工具—chardet
import chardet
a = "世界你好".endode('utf-8')
chardet.detect(a)
{'encoding': 'utf-8', 'confidence': 0.938125, 'language': ''}
encodeing编码,confidence为精度,language为检测到的文本语言