Python学习笔记3

这篇博客主要介绍了Python中的常见函数,包括内置函数如abs、max、min等,以及math模块中的数学函数。此外,还详细阐述了字符串处理、字符编码,如ord和chr函数。接着,讲解了如何格式化数字和字符串,特别是format函数的使用。最后,重点讨论了Turtle库中的图形绘制函数,如penup、pencolor、circle等,用于创建简单的图形和动画。

1.常见的Python函数

常用的自带函数
- abs(x) 返回x的绝对值

  • max(x1,x2,…) 返回x1,x2,…的最大值

  • min(x1,x2,…) 返回x1,x2,…的最小值

  • pow(a,b) 类似a**b

  • round(x) 返回与x最接近的证书,如果x与两个证书接近程度相同,则返回偶数值

  • round(x,n) 保留小数点后n位小数的浮点值

常见的math模块的函数

  • fabs(x) 将x作为一个浮点数,返回它的绝对值(也是浮点数)

  • ceil(x) x向上取最近的整数,然后返回这个整数

  • floor(x) x向下取最近的整数,然后返回这个整数

  • exp(x) 返回函数e的x次幂

  • log(x) 返回x的自然对数值

  • log(x,base) 返回以某个特殊值为底的x对数值

  • sqrt(x) 返回x的平方根值

  • sin(x)

  • cos(x)

  • asin(x)

  • acos(x)

  • tan(x) 以上为x的三角函数

  • degree(x) 将x从弧度转成角度

  • randians(x) 将x从角度转为弧度

2.字符串和字符

字符串是一连串的字符,Python处理字符和字符串的方式是一样的。字符串是一串包括文本和数字的字符,字符串必须被括在一对单引号或双引号里面, python没有字符数据类型,一个字符的字符串代表一个字符。

ASCII码使用0到127来表示字符,将字符映射成对应的二进制被称为字符编码

ord(ch)函数返回字符ch的ASCII码
chr(code)函数返回code所代表的字符

常用的转义字符

  • \b 退格符

  • \t 制表符

  • \n 换行符

  • \f 换页符

  • \r 回车符

  • \ 反斜线

  • \’ 单引号

  • \” 双引号

str函数:将一个数字转换成一个字符串

字符串的连接操作

id和type函数在程序设计中很少用到,但他们是学习有关对象和变量的好工具

3.格式化数字和字符串

format函数返回格式化的字符串
format(item,format-specifier)

interest = 111.2345
print(“Interest is ”,format(interes,“.2f”))    #格式化的浮点数

print(format(57.46239,"10.2e"))    #科学计数法

print(fromat(0.5347,"10.2%"))      #带百分号

#d、x、o、b分别用来格式化十进制、十六进制、八进制和二进制整数

#s将一个字符串格式化为一个指定宽度的字符串

4.Turtle中的相关函数

移动笔、设置笔的大小等函数

  • turtle.pendown()将画笔下拉,移动画笔的时候绘制

  • turtle.penup()将画笔上拉,移动画笔的时候不绘制

  • turtle.pensize(width)设定线宽

  • turtle.forward(d)朝着Turtle指定的方向移动指定的距离

  • turtle.backward(d)朝着Turtle指定的反方向移动指定的距离

  • turtle.right(angle)将Turtle向右转动指定的角度

  • turtle.left(angle)将Turtle向左转动指定的角度

  • turtle.goto(x,y)将Turtle移动到某个绝度位置

  • turtle.setx(x)将Turtle的x坐标移动到指定位置

  • turtle.sety(y)将Turtle的y坐标移动到指定位置

  • turtle.setheading(angle)将Turtle的方向设定为固定角度,0-东,90-北,180-西,270-南

  • turtle.home()将Turtle移动到(0,0)位置和向东

  • turtle.circle(r,ext,step)绘制一个指定半径,范围和阶数的元

  • turtle.dot(diameter,color)绘制一个指定直径、颜色的点

  • turtle.undo()撤销最后一个图形操作

  • turtle.speeds(s)设置Turtle的速度为一个1到10之间的整数,10最大

#SimpleShapes.py

import turtle

turtle.pensize(3)
turtle.penup()
turtle.goto(-200, -50)
turtle.pendown()
turtle.circle(40, steps = 3) # Draw a triangle

turtle.penup()
turtle.goto(-100, -50)
turtle.pendown()
turtle.circle(40, steps = 4) # Draw a square

turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.circle(40, steps = 5) # Draw a pentagon

turtle.penup()
turtle.goto(100, -50)
turtle.pendown()
turtle.circle(40, steps = 6) # Draw a hexagon

turtle.penup()
turtle.goto(200, -50)
turtle.pendown()
turtle.circle(40) # Draw a circle

turtle.done() 

SimpleShapes

Turtle对象设置颜色和字体

  • turtle.color(c)设置笔的颜色

  • turtle.fillcolor(c)设置笔填充颜色

  • turtle.begin_fill()在填充图形前访问这个函数

  • turtle.end_fill()结束图形填充访问这个函数

  • turtle.filling()返回填充状态,True代表填充,False代表没有填充

  • turtle.clear()清除窗口

  • turtle.reset()将状态和位置恢复到默认状态

  • turtle.screensize(w,h)设置画布的宽度和高度

  • turtle.hideturtle()隐藏turtle

  • turtle.showturtle()显示turtle

  • turtle.isvisibel()如果turtle可见则返回true

  • turtle.write(s,font(“Arial”,8,”normal”))在turtle的位置编写字符串s,字体是由字体名,字号,字体类型组成

#ColorShapes.py

import turtle

turtle.pensize(3)       #Set pen thickness to 3 pixels
turtle.penup()          #Pull the pen up
turtle.goto(-200, -50)
turtle.pendown()        # Pull the pen down
turtle.begin_fill()     # Begin to fill color in a shape
turtle.color("red")
turtle.circle(40, steps = 3) # Draw a triangle
turtle.end_fill()       # Fill the shape

turtle.penup()
turtle.goto(-100, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("blue")
turtle.circle(40, steps = 4) # Draw a square
turtle.end_fill() # Fill the shape

turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("green")
turtle.circle(40, steps = 5) # Draw a pentagon
turtle.end_fill() # Fill the shape

turtle.penup()
turtle.goto(100, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("yellow")
turtle.circle(40, steps = 6) # Draw a hexagon
turtle.end_fill() # Fill the shape

turtle.penup()
turtle.goto(200, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("purple")
turtle.circle(40) # Draw a circle
turtle.end_fill() # Fill the shape

turtle.color("green")
turtle.penup()
turtle.goto(-100, 50)
turtle.pendown()
turtle.write("Cool Colorful Shapes", 
  font = ("Times", 18, "bold"))
turtle.hideturtle()

turtle.done() 

ColorShapes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值