1.
print('Hello world') Hello world
2.简单交互
name = input('请输入姓名:') print('%s 同学,你好'%name) print('%s 同学,我觉得可以'%name) print('%s 同学,那你很棒啊'%name)
3.用户输入两个数字,计算并输出两个数字之和
1 num1 = input('请输入第一个数字:') 2 请输入第一个数字:5 3 num2 = input('请输入第二个数字:') 4 请输入第二个数字:6 5 sum = float(num1)+float(num2) 6 print(float(sum)) 7 11.0
一句话输入两个数字求和
1 print('两数之和为 %.1f' %(float(input('输入第一个数字:'))+float(input('输入第二个数字:')))) 2 输入第一个数字:8 3 输入第二个数字:6 4 两数之和为 14.0
4.用户输入三角形三边长度,并计算三角形的面积
1 a = float(input('输入三角形第一边长: ')) 2 b = float(input('输入三角形第二边长: ')) 3 c = float(input('输入三角形第三边长: ')) 4 5 s = (a + b + c) / 2 6 7 area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 8 print('三角形面积为 %0.2f' %area)
5.输入半径,计算圆的面积
1 r = 10 2 area = 3.1415 * r * r 3 print(area) 4 print("{:.2f}".format(area)) 5 6 314.15000000000003 7 314.15
6.画一组同切圆
1 import turtle 2 turtle.pensize(3) 3 turtle.circle(10) 4 turtle.circle(30) 5 turtle.circle(60) 6 turtle.circle(90) 7 turtle.circle(120)
7.画一个五角星
1 import turtle 2 turtle.forward(100) 3 turtle.left(144) 4 turtle.forward(100) 5 turtle.left(144) 6 turtle.forward(100) 7 turtle.left(144) 8 turtle.forward(100) 9 turtle.left(144) 10 turtle.forward(100)
8.画一个全黄色的五角星
1 from turtle import * 2 fillcolor("yellow") 3 begin_fill() 4 while True: 5 forward(200) 6 right(144) 7 if abs(pos())< 1: 8 break 9 end_fill()
9.画一组同心圆
1 for i in range(4): 2 turtle.pu() 3 turtle.goto(0,-(i+1)*10) 4 turtle.pd() 5 turtle.circle((i+1)*10)
10.国旗上的五个五角星
1 import turtle 2 turtle.penup() 3 turtle.goto(-300,300) 4 turtle.pendown() 5 turtle.color("red") 6 turtle.fillcolor("red") 7 turtle.begin_fill() 8 turtle.forward(500) 9 turtle.right(90) 10 turtle.forward(300) 11 turtle.right(90) 12 turtle.forward(500) 13 turtle.right(90) 14 turtle.forward(300) 15 turtle.right(90) 16 turtle.end_fill() 17 turtle.penup() 18 turtle.goto(-250,250) 19 turtle.pendown() 20 turtle.color("yellow") 21 turtle.fillcolor("yellow") 22 turtle.begin_fill() 23 for i in range(5): 24 turtle.forward(150) 25 turtle.right(144) 26 turtle.end_fill() 27 turtle.penup() 28 turtle.goto(-90,280) 29 turtle.pendown() 30 turtle.begin_fill() 31 for i in range(5): 32 turtle.forward(40) 33 turtle.right(144) 34 turtle.end_fill() 35 turtle.penup() 36 turtle.goto(-80,230) 37 turtle.pendown() 38 turtle.begin_fill() 39 for i in range(5): 40 turtle.forward(40) 41 turtle.right(144) 42 turtle.end_fill() 43 turtle.penup() 44 turtle.goto(-90,190) 45 turtle.pendown() 46 turtle.begin_fill() 47 for i in range(5): 48 turtle.forward(40) 49 turtle.right(144) 50 turtle.end_fill() 51 turtle.penup() 52 turtle.goto(-95,140) 53 turtle.pendown() 54 turtle.begin_fill() 55 for i in range(5): 56 turtle.forward(40) 57 turtle.right(144) 58 turtle.end_fill()