Python练习题

from turtle import*

speed(10)

penup()
goto(-280,-250)
fillcolor("yellow")
begin_fill()
pendown()

for i in range(3):
    forward(600)
    left(120)
end_fill()

hideturtle()
exitonclick()
#############2——2
import turtle
turtle.fillcolor("yellow")
turtle.begin_fill()
for i in range(3):
    turtle.seth(i*120)
    turtle.fd(100)
turtle.end_fill()
turtle.hideturtle()
turtle.exitonclick()
##############2——3
import turtle
turtle.setup(600,400)
turtle.pensize(2)
turtle.penup()
turtle.goto(0,180)
turtle.left(135)
turtle.begin_fill()
turtle.color("pink","pink")
turtle.pendown()
turtle.circle(63.65,180)
turtle.goto(0,0)
turtle.goto(90,90)
turtle.setheading(45)
turtle.circle(63.65,180)
turtle.end_fill()
turtle.hideturtle()
turtle.exitonclick()
########4-2
for i in range(1, 10):
    for j in range(i, 10):
        print(f"{i}*{j}={i*j}", end="\t")
    print()
    print("\t"*2*i,end="")

# ############5-1
num_list=[23, 11, 12, 23, 9, 2, 1, 4]
new_list=set(num_list)
if len(num_list)==len(new_list):
    print("无重复")
else:
    print("有重复")
############5-3
run_ist=["0分钟","20分钟","40分钟","60分钟"]
swim_list=["0米","200米","400米","600米"]
calories_list=[i*200+j*100 for i in range(len(run_ist))for j in range(len(swim_list))]
print("卡路里列表:",calories_list)
print(f"运动计划中最多消耗{max(calories_list)}卡路里,最少消耗{min(calories_list)}卡路里")
# ############6-2
favorite_dict ={}
while True:
    classify=input("请输入您喜欢的事物输入空则退出循环):")
    if classify =="":
        break
    items=input("您喜欢的"+classify+"包括哪些(逗号隔开)?")
    favorite_dict[classify]=items.split(",")
print(favorite_dict)
for classify,items in favorite_dict.items():
    print(f"{classify}包括:",end="")
    for item in items:
        if items.index(item)==len(items)-1:
            print(item)
        else:
            print(item,end=",")
# ############7-1
def scene_province(scenic_spot,province,abbr_province):
    print(f"我最喜欢的景点是{scenic_spot}")
    print(f"它位于{province}")
    print(f"{province}的简称是{abbr_province}")
scene_dict={
    "scenic_spot'":"塔里拉马干",
    "province":"新疆",
    "abbr_province'":"疆"
}
scene_province(**scene_dict)

########7-2
def product(*number):
    sum = 1
    for item in number:
        sum *= item
    return sum
result = product(1,2,3)
print(result)

##########8-2
class House:
    number=0
    def __init__(self,name,length,width):
        self.name=name
        self.length=length
        self.width=width
        House.number +=1
    def cal_square(self):
        square = self.length * self.width
        return f"{self.name}的面积是{square}"
if __name__ =="__main__":
    h1 = House("2620",20, 10)
    print(h1.cal_square())
    h2 = House("2626",10, 10)
    print(h2.cal_square())
    print("房子的个数是", House.number)
##########8-3
from datetime import datetime
class Medicine:
    def __init__(self,name,price,pd,exp):
        self.name = name
        self.__price = price
        self.__pd = datetime.strptime(pd, "%Y-%m-%d")
        self.__exp = datetime.strptime(exp, "%Y-%m-%d")
    def guarantee_period(self):
        gp = self.__exp - self.__pd
        return gp.days
    def is_expire(self):
        today = datetime.today()
        until_now = (today - self.__pd).days
        gp = self.guarantee_period()
        if until_now > gp:
            print("商品已经过期")
        else:
            print("商品没有过期")
if __name__ =="__main__":
    m1 = Medicine("999感冒灵",20,"2023-1-1","2024-1-1")
    print("保质期共有:", m1.guarantee_period())
    m1.is_expire()
########9-2
class Student:
    number = 0

    def __init__(self, chinese, math, physics):
        self.chinese = chinese
        self.math = math
        self.physics = physics
        Student.number += 1

    def __add__(self, other):
        Student.number -=1
        chinese = self.chinese + other.chinese
        math = self.math + other.math
        physics = self.physics + other.physics
        return Student(chinese,math,physics)

    def __str__(self):
        return f"语文: {self.chinese},数学: {self.math},物理: {self.physics},总人数:{Student.number})"


s1 = Student(90, 100, 90)
s2 = Student(80, 80, 80)
s3 = Student(90, 100, 100)
sum = s1+s2+s3
print(sum)
print(f"语文平均分:{sum.chinese/sum.number:.2f},
数学平均分:{sum.math/sum.number:.2f},
物理平均分:{sum.physics/sum.number:.2f}")

Python 练习题是指设计用来提高编程技能和加深对Python语言理解的编程问题。这些练习题通常涉及不同的Python编程概念和技巧,从基础语法到高级主题都有涵盖。以下是一些常见的Python练习题类型: 1. 基础语法练习:这些练习帮助初学者熟悉Python的基本语法,比如变量声明、数据类型、控制结构(if-else语句、循环)以及函数定义等。 2. 数据结构操作:涉及Python内置的数据结构,如列表(list)、字典(dict)、元组(tuple)和集合(set)的操作练习,包括增删改查等。 3. 面向对象编程:包含类和对象的定义、继承、多态和封装等面向对象编程概念的实践题目。 4. 错误和异常处理:学习如何处理在代码执行过程中可能遇到的错误和异常。 5. 文件操作:练习如何在Python中读写文件,包括文本文件和二进制文件的操作。 6. 模块和包:学习如何导入和使用Python标准库中的模块,以及如何创建和使用自定义包。 7. 高级特性:练习Python的高级特性,比如列表推导式、生成器、装饰器和上下文管理器等。 8. 数据处理:包含使用Python进行数据分析和处理的练习,如使用Pandas库操作数据框(DataFrame)。 9. Web开发基础:涉及使用Flask或Django等框架进行简单的Web应用开发。 10. 测试:编写单元测试来验证代码的正确性。 通过解决这些问题,可以系统地提升Python编程技能,并在实际开发中更加得心应手。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值