Python学习--第四天

第一题、编写函数实现输入任意两个集合及对应运算符,能够返回两个集合的计算的结果。

  • 如集合A = {1,2,3} B = {2,3} 运算符 - 得到计算结果为A与B的差集,Result = {1}
A=set(input("请输入一个集合:").split())
B=set(input("请输入一个集合:").split())
s=input("请输入一个运算符(-差集 +并集 &交集 ^对称集):")
def set_operation(setA,setB,symbol):
 if symbol=='&':
   print("交集:", setA & setB)
   print("交集:", setA.intersection(setB))
 elif symbol=='+':
   print("并集:", setA | setA)
   print("并集:", setA.union(setA))
 elif symbol=='-':
   print("差集:", setA - setB)
   print("差集:", setA.difference(setB))
 elif symbol=='^':
   print("对称差集:",setA ^ setB)
   print("对称差集:",setA.symmetric_difference(setB))
 else:
    print("请重新输入运算符!")

set_operation(A,B,s)

运行结果:

"F:\Python 3.8.0\python.exe"
请输入一个集合:1 2 3
请输入一个集合:2 3
请输入运算符(-差集 +并集 &交集 ^对称差集)-
差集: {'1'}
差集: {'1'}
进程已结束,退出代码 0

第二题、编写函数实现输入任意键值均为数字的字典,可以返回按字典的键值升序排列的新字典。

  • 如字典dict = {‘a’:1,‘b’:3,‘c’:2}
  • 输出为[(‘a’, 1), (‘c’, 2), (‘b’, 3)]
import operator
A=input("请重输入Key:")
B=input("请重输入value:")
def dictionary_Order(ListA,ListB):
 mydict = dict(zip(ListA,ListB))
 print(mydict)
 after_ordering=sorted(mydict.items(),key=operator.itemgetter(1))
 print(after_ordering)
dictionary_Order(A,B)

运行结果:

"F:\Python 3.8.0\python.exe" 
请重输入Key:abc
请重输入value:132
{'a': '1', 'b': '3', 'c': '2'}
[('a', '1'), ('c', '2'), ('b', '3')]
进程已结束,退出代码 0

第三题、定义Circle(圆)类,成员包括圆心位置、半径、颜色等属性。编写构造方法和其他方法,计算周长和面积。

class Circle:
    def __init__(self,site, radius, color):
        self.center = site
        self.radius = radius
        self.color = color
    def perimeter(self):#周长
        return 3.14 * 2 * self.radius
    def area(self):#面积
        return 3.14 * self.radius * self.radius
A=int(input("请重输入半径:"))
circle = Circle((10,12),A,"绿色")
print(circle.perimeter())#周长
print(circle.area())#面积
print(circle.color)#颜色

运行结果:

"F:\Python 3.8.0\python.exe" 
请重输入半径:8
50.24
200.96
绿色
进程已结束,退出代码 0
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值