自学python
python对函数库的引用方式
第一种方式:
import <库名>
例如:import turtle
如果需要用到函数库中的函数,需要使用:
<库名>.<函数名>
例如:
>>>import turtle
>>>turtle.fd(100)
第二种方式:
from <库名> import <函数名>
from <库名> import *
调用函数不需要<库名>,直接使用<函数名>
列如:
>>>from turtle import *
>>>fd(100)
代码如下
#绘制五角星
import turtle
turtle.speed(3)
turtle.pensize(5)
turtle.color('black','yellow')
#turtle.fillcolor('red')
turtle.begin_fill()
for i in range(5):
turtle.forward(200)
turtle.right(144)
turtle.end_fill()
或者
#绘制五角星
from turtle import speed,pensize,color,begin_fill,forward,right,end_fill
speed(3)
pensize(5)
color('black','yellow')
begin_fill()
for i in range(5):
forward(200)
right(144)
end_fill()