Python重点例题

持续更新中…

1.替换两数并输出

print("Enter:")

a=input()
b=input()

print("before swapping\na=", a, " b=", b)

temp = a
a = b
b = temp

print("\nafter swapping\na=", a, " b=", b)

2.用turtle库画填充三角形

import turtle
import turtle as t
d=0
t.bgcolor("black")
t.pencolor("white")
turtle.fillcolor("orange")
turtle.begin_fill()
for i in range(4):
    turtle.fd(200)
    d=d+120
    turtle.seth(d)

turtle.end_fill()
turtle.hideturtle()

3.米和英尺转换

print("输入身高:")
a=float(input())
b = int(a / 0.3048)
c = (a / 0.3048 - b) * 12
print("英尺:",b,"英寸",c)

4.字符串输出

str1=input("输入第一个字符串:")
str2=input("输入第二个字符串:")
s=str1+str2
S=s.upper()
length=len(S)
print(S,length)
print(S[length-2])
print(S[1:length-2])

5.画一条x轴上的正弦函数

import turtle
import math

turtle.speed(0)
turtle.delay(0)
turtle.tracer(False)
i=-150
while(i<=150):
    i=i+0.1
    turtle.penup()
    y = 60*math.sin(0.05*i)
    turtle.goto(i,y)
    turtle.pendown()
    turtle.dot(4)
turtle.penup()
turtle.goto(0,0)
turtle.pendown()
turtle.seth(90)
turtle.fd(230)
turtle.seth(-70)
turtle.fd(15)
turtle.penup()
turtle.goto(0,230)
turtle.pendown()
turtle.seth(-110)
turtle.fd(15)
turtle.penup()
turtle.goto(0,0)
turtle.pendown()
turtle.seth(-90)
turtle.fd(90)
turtle.penup()
turtle.goto(0,0)
turtle.pendown()
turtle.seth(0)
turtle.fd(150)
turtle.seth(-160)
turtle.fd(15)
turtle.penup()
turtle.goto(0,0)
turtle.pendown()
turtle.seth(-180)
turtle.fd(150)
turtle.penup()
turtle.goto(0,-120)
turtle.write("正弦函数方程图像:y = 60*math.sin(0.05*i)",True,align="center")
turtle.goto(0,-300)
turtle.done()

6.

在这里插入图片描述

import random
import time

a=1
n=0
sum_1=0
sum_2=0
while a<=10:
    n = random.randint(0, 100)
    print(n)
    if a<=5:
        t0 = time.time()
        sum_1 += n
        time.sleep(1)
        if a==5:
            print("前五个数和:",sum_1/5)
    if a>5:
        t1 = time.time()
        sum_2 += n
        time.sleep(1)
        if a==10:
            print("后五个数和:",sum_2/5)
    a = a + 1

7.

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值