Defines the width of the pen. Patameter - integer is a value type of int | |
is a conditional statement. | |
is a loop statement in which operations repeat itself for a limited amount of time |
List is a box full of Strings
or Numbers
. For example:
# List of Number
list_of_number = [2, 3, 5, 1, 6]
print(list_of_number) # [2, 3, 5, 1, 6]
# List of String
list_of_string = ["Python", "kids", "fun"]
print(list_of_string) # ["Python", "kids", "fun"]
Look at the diagram below to know how does loop statement work.
# Define a list of apples
box = ["appleA", "appleB", "appleC", "appleD", "appleE"]
for apple in box: # Check if there is a next apple in the box
print(apple) # If there is a next apple, print it out.
Look at the diagram below to know how does if…else statement work
# Define a list of fruits
box = ["appleA", "pearB", "appleC", "appleD", "pearE"]
# fruit_label is the label of fruits in the box which starts from 0
for fruit_label in range(len(box)):
if fruit_label == 1 or fruit_label == 4:
print("This is a pear" + box[fruit_label])
else:
# If it's not pear, it's a apple since we only have two types of fruit in the box.
print(box[fruit_label])
Following example presents the usage of For loops
, value assignment
, mathmatical incremention
.
import random
from turtle import *
colors = ["red","green","blue","orange","purple","pink","yellow"]
width(10)
length = 5
for count in range(100):
speed(100)
a_color = random.choice(colors)
color(a_color)
forward(length)
right(135)
length = length + 5
done()
Following example presents the usage of For loops
, range()
, random.choice()
and Lists
.
from turtle import *
import random
shape("turtle")
setup(800,600)
colors = ["red","green","blue","orange","purple","pink","yellow"]
titles = ["怎么这么好看!", "小乌龟是个艺术家唉!"]
penup()
setpos(0, 230)
pendown()
pencolor(random.choice(colors))
write(random.choice(titles), align="center", font=("Arial", 28, "bold"))
penup()
for i in range(1,20):
color(random.choice(colors))
speed(100)
width(5)
circle(i*10)
penup()
sety(-i*10)
pendown()
done()
Homework : Create the logo of Olympics
Given five lists with two items in each. First item is the coordinate of the circle, second item is the color of the circle. Please write a for loop
to create logo of olympics.
Input
blue = [-120,0,"blue"]
black = [0,0,"black"]
red = [120,0,"red"]
yellow = [-60,-60,"yellow"]
green = [60,-60,"green"]
from turtle import *
setup(800, 600)
setpos(-120,0)
pencolor("red")
pensize(5)
Output