数据类型--python练习题

本文包含多个Python编程实例,如生成随机数列表并排序、计算最大公约数与最小公倍数、判断闰年、计算硬币最少数量及几何形状的面积与体积等。每个实例都附带详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.随机数。
生成一个由N各元素的由随机数n组成的列表,其中N和n的去值范围分别为(1<=N<=100),(0<=n<=2^31-1)。然后再随机从这个列表中取N(1<=N<=100)个随机数出来,对它们进行排序,然后显示整个子集

def suijishu():
    import random
    num=random.randint(1,100)
    lst=[]
    for item in range(num):
        tmp=random.randint(0,(pow(2,31)-1))
        lst.append(tmp)
    lst.sort()
    print lst
suijishu()

2.最大公约数和最小公倍数

def fun7():
    a=input('please input a number:')
    b=input('please input another number:')
    su=[]
    if a>b:
        smaller=b
    else:
        smaller=a
    for i in range(1,smaller+1):
        if a%i==0 and b%i==0:
            su.append(i)
    l=len(su)
    p=su[l-1]
    print '最大公约数:',p
    print '最小公倍数:',a*b/p
while True:
    fun7()

3.判断是否是闰年
判断是否为闰年条件:
1)能够被4整除
2)能够被100整除且可以被400整除

def fun3():
    year=input('please input a year to test if the year is leap year:')
    if year%4==0:
        answer='leap year'

    elif year%100==0 and year%400==0:
        answer='leap year'
    else:
        answer='common year'
    return ('%d is %s' % (year, answer))

4.取余
取一个任意小于1美元的金额,计算可以换算为最少多少枚硬币。硬币有1美分,5美分,10美分,25美分,1美元可以兑换100美分

def fun4():
    money=input('please a number of money you want to have some change:')
    if money not in range(100):
        return ('please input the number which is in the range about 0~100')
    else:
        change25=money/25
        change10=money%25/10
        change5=money%25%10/5
        change1=money%25%10%5
        return('%d can have the less change are %d 25$, %d 10$, %d 5$, %d 1$'
           % (money, change25,change10, change5, change1))

5.几何
计算面积和体积
1)正方形和立方体
2)圆和球体

def fun6():
    print '''
    1.area
    2.volume'''
    chioce1=input('please input the chioce:')
    if chioce1==1:
        print '''
        1.square
        2.circle'''
        chioce2=input('please input the chioce you want to compute:')
        if chioce2==1:
            a=input('please input the lenth:')
            b=input(('please input the width:'))
            return (a*b)
        elif chioce2==2:
            r=input('please input the radii:')
            return (3.14*(float(r)**2))
        else:
            return ('please input the chioce like (1|2)')
    elif chioce1==2:
        print '''
        1.cube
        2.ball'''
        chioce2 = input('please input the chioce you want to compute:')
        if chioce2 == 1:
            a = input('please input the lenth:')
            b = input('please input the width:')
            c = input('please input the highth:')
            return (a * b * c)
        elif chioce2 == 2:
            r = input('please input the radii:')
            return (3.14 * (float(r) ** 2 * 4/3))
        else:
            return ('please input the chioce like (1|2)')
    else:
        return ('please the input the chioce like (1|2)')

6.算术
输入两个数和运算符,可以支持的运算为+,-,,/,%,*

def fun5():
    numbers=raw_input('please input two numbers and operation like this (num1,num2,oper): ')
    num1=float(numbers.split(',')[0])
    num2=float(numbers.split(',')[1])
    oper=numbers.split(',')[2]
    if oper=='+':
        return (num1+num2)
    elif oper=='-':
        return (num1-num2)
    elif oper=='*':
        return (num1*num2)
    elif oper=='/':
        return (num1/num2)
    elif oper=='%':
        return (num1%num2)
    elif oper=='**':
        return (num1**num2)
    else:
        return ('please input the oper like this (+|-|*|/|%|**)')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值