1、Hello World
print('hello world')
2、简单交换
name = input("输入姓名:") print("{}同学,学好python,前途无量!".format(name)) print("{}大侠,学好python,前途无量!".format(name[0])) print("{}哥哥,学好python,前途无量!".format(name[1:]))
3、输入两个数,计算两数之和(一行代码)
print('结果:%.0f'%float(float(input('输入第一个数字:'))+float(input('输入第二个数字:'))))
4、输入三角形三边长度,计算三角形面积(海伦公式)
l1 = float(input('请输入第一条边的长度:')) l2 = float(input('请输入第二条边的长度:')) l3 = float(input('请输入第三条边的长度:')) p = (l1+l2+l3) / 2 s = (p*(p-l1)*(p-l2)*(p-l3))**0.5 print('三角形的面积为:%.2f'%s)
5、输入半径,计算圆的面积
radius = float(input("半径:")) area = float(3.1415 * radius * radius) print("面积:%.2f"%area)
6、画一组同切圆
import turtle turtle.circle(10) turtle.circle(20) turtle.circle(30) turtle.circle(40) turtle.circle(50)
7、画一个五角星
import turtle turtle.forward(200) turtle.right(144) turtle.forward(200) turtle.right(144) turtle.forward(200) turtle.right(144) turtle.forward(200) turtle.right(144) turtle.forward(200)
8、画一个全黄色五角星
import turtle turtle.color('yellow') turtle.fillcolor('yellow') turtle.begin_fill() turtle.forward(200) turtle.right(144) turtle.forward(200) turtle.right(144) turtle.forward(200) turtle.right(144) turtle.forward(200) turtle.right(144) turtle.forward(200) turtle.end_fill()
9、画一组同心圆
import turtle turtle.penup() turtle.goto(0,-200) turtle.pendown() turtle.circle(200) turtle.penup() turtle.goto(0,-150) turtle.pendown() turtle.circle(150) turtle.penup() turtle.goto(0,-100) turtle.pendown() turtle.circle(100)
10、画国旗
import turtle turtle.color('yellow') turtle.bgcolor('red') turtle.fillcolor('yellow') turtle.begin_fill() turtle.up() turtle.goto(-200,100) turtle.down() turtle.forward(150) turtle.right(144) turtle.forward(150) turtle.right(144) turtle.forward(150) turtle.right(144) turtle.forward(150) turtle.right(144) turtle.forward(150) turtle.end_fill() turtle.begin_fill() turtle.up() turtle.goto(-80,160) turtle.down() turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.end_fill() turtle.begin_fill() turtle.up() turtle.goto(-22,68) turtle.down() turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.end_fill() turtle.begin_fill() turtle.up() turtle.goto(-30,110) turtle.down() turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.end_fill() turtle.begin_fill() turtle.up() turtle.goto(-45,-10) turtle.down() turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.right(144) turtle.forward(50) turtle.end_fill()