Python 新手入门习题及代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 新手入门习题及代码相关的知识,希望对你有一定的参考价值。



```## 1.输入圆半径,求面积与周长

r = int(input("R="))
s = 3.14*(r**2)
c = 2*3.14*r
print("S=",s,"\t","C=",c)

## 2.随机输入两个数,比较大小后,从小到大打印
a = int(input("number="))
b = int(input("number="))
if a < b:
    print(a,b)
else:
    print(b,a)

## 3.输入两个数,打印最大值,按回车结束
a = int(input("number:"))
while 1:
    b = input("number:")
    if b == ‘‘:
        break
    else:
        b = int(b)
        if b < a:
            print(a)
        else:
            print(b)
            a = b

## 4.求输入数据平均值,输入0也加入计算
a,b = 0,0
c = int(input(‘输入:‘))
while 1:
    b += 1
    a += c
    print("均值:",a/b)
    c = input(‘输入:‘)
    if c == ‘‘:
        print(‘End‘)
        break
    else:
        c = int(c)

## 5.打印边长为N的正方形
n=int(input())
for i in range(1,n+1):
    if i==1 or i==n:
        print(‘*‘*n)
    else:
        print(‘*‘,‘ ‘*(n-2),‘*‘)

## 6.X范围内奇,偶数之和(包含X)
z = 0
x = int(input(‘MAX 1000 000:‘))
y = int(input(‘偶0 奇1:‘))
if (x <= 1000000)&(0 <= y <= 1):
    for i in range(y,x+1,2):
        z += i
    print(z)
else:
    print(‘input error‘)

## 7.查询成绩表
while 1:
    x = int(input(‘number:‘))
    if 90 <= x <= 100:
        x = ‘A‘
        print(x)
    elif 80 <= x <= 89:
        x = ‘B‘
        print(x)
    elif 70 <= x <= 79:
        x = ‘C‘
        print(x)
    elif 60 <= x <= 69:
        x = ‘D‘
        print(x)
    elif 0 <= x <= 59:
        x = ‘E‘
        print(x)
    else:
        print(‘Input Error‘)

## 8.1到N的阶乘之和
x = int(input(‘number:‘))
m = 1
h = 0
for i in range(1,x+1):#X阶层计算
    m = i*m
    h += m
print(h)

## 9.九九乘法表
for i in range(1,10):
    k = 1
    for n in range(i,10):
        z = i*n
        if i == 1:
            print(i,‘x‘,n,‘=‘,z,end=‘ ‘)
        elif i != 1:
            print(‘\n‘*k,i,‘x‘,n,‘=‘,z,end=‘ ‘)
            k -= 1

## 10.N以内的斐波那契数列
n = int(input(‘范围:‘))
a,b = 0,1
while 1:
    print(a,end=‘ ‘)
    a,b = b,a+b
    if a> = n:
        break

## 11.斐波那契数列的第N项
a,b,c = 0,1,0
n = int(input(‘查询项:‘))
while 1: 
    a,b,c = b,a+b,c+1
    if c == n:
        print(a)
        break 

## 12.输入100位以内数据按位位打印
a = int(input())
x = 100#最大权
w = 10**x
log = 1
while log:
    if a//w == 0:#找最高位
        x = x-1
        w = 10**x
    else:#找到最高位
        log = 0
while w:
    b = a//w#首位
    a %= w#为下次循环变量改为余数
    w //= 10#降权
    print(b,end=‘ ‘)

## 13.给出随机0100中的数字,使其排序算法,如出现具体数字则排序结束`
import random
li = [1]
k = 0
while k != 88:
    k = random.choice(range(100))
    if k in li:
        continue
    li.append(k)
    li.sort()
    print(li)

## 14.求x以内的素数,给一个随机数,如果该数能整除质数,那该数就为合数
li = [2]
x = int(input())
for i in range(2,x):
    a = 1
    for n in li:
        if i%n == 0:
            a = 0
            break
    if a:
        li.append(i)
print(li)

## 15.求1-2+3-4+5.......99的值
x,y = 0,1
for i in range(1,100):
    i = i*y
    x += i
    y *= (-1)
print(‘count:‘,x)

## 16.用户登录,连续错误三次退出
name=input(‘user name:‘)
c = 3
while c:
    key=input(‘pass word:‘)
    if name == ‘1‘ and key == ‘2‘:
        print(‘True‘)
        break
    else:
        print( ‘False‘)
        c = c-1

## 17.输入随机数字列出所有能整除它的数字
x = int(input())
li = []
for i in range(1,x+1):
    if x%i == 0:
        li.append(i)
print(li)

## 18.3文钱能买一只公鸡,2文钱能买一母鸡,1文钱能买3小鸡,现在花100钱买100鸡,如何买
for x in range(1,101):
    c = 100-x
    v = 100-x/3
    for m in range(1,100):
        if c-m == (v-2*m)/3 and c-m >= 0:
            g = c-m
            print(‘公鸡:‘,g,‘母鸡:‘,m, ‘小鸡:‘,x)

## 19.输入三个数,看是否能构成三角形
a = 5
b = 75
c = 6
if a+b<c or a+c<b:
    print(‘ng‘)
else:
    print(‘ok‘)

## 20.水仙花数
l1 = []
l2 = []
for i in range(100,1000):
    w = 100
    ii = i
    while w:
        a = i//w#首位
        i %= w#余数
        w //= 10#降权
        l1.append(a)
    ## 如果该数的各位数的立方之和等于该数,那该数称为水仙花数
?    if l1[0]**3+l1[1]**3+l1[2]**3 == ii:
?        l2.append(ii)
?    else:
?        l1 = []
print(l2)

## 21.一个正整数分解质因数
l1,l2 = [2],[]
n,log = 0,1
for i in range(2,1000):#范围内质数并输出到列表1
    k = 1
    for x in l1:
        if i%x == 0:#如果迭代值为合数,则终止当前循环且不把该值加入列表1
            k = 0
            break
        else:
            pass
    if k:
        l1.append(i)
#print(l1)#打印质数表

cc=int(input(‘正整数:‘))
while log:
    if cc%l1[n] == 0 and cc != l1[n]:#如果输入值能整除当前质数且不为当前质数,分解输入值并把质数加入列表
        l2.append(l1[n])
        cc = cc / l1[n]
    elif cc == l1[n]:#输入值经过计算或直接等于素数,把该素数加入列表
        l2.append(l1[n])
        log = 0
    elif cc > l1[n]:
        n = n + 1
print(‘质因数:‘,l2)

## 22.球从100米高处下落,每次反弹上次距离的一半,10次后,球运动多少米
b = 200
x = 100
for i in range(1,10):
    b = b/2
    x = x+b
print(x)

## 23.1,2,3,4个数字,能组成多少个不重复的数,每个数字只能用一次
l1 = [1,2,3,4]
l2 = []
a,b,c,d = 0,0,0,0
for a in l1:
    for b in l1:
        if a == b:
            pass
        else:#第一位数不等于第二位数
            for c in l1:
                if a == c or b == c:
                    pass
                else:#第一位数不等于第三位数
                    for d in l1:
                        if a == d or b == d or c == d:
                            pass
                        else:#第一位数不等于第四位数
                            l2.append([a,b,c,d])
print(l2)

## 24.一个数+100后是一个完全平方数,再在加168又是一个,求该数
for i in range(1000000):
    if 250000>i>10000:
        t = 100
        k = 500
    elif 490000>i>250000:
        t = 500
        k = 700
    elif 810000>i>490000:
        t = 700
        k = 900
    elif 1000000>i>810000:
        t = 900
        k = 1000 
    else:
        t = 0
        k = 100
    for u in range(t,k):
        if i+100 == u*u:
            for y in range(t,k):
                if i+268 == y*y:
                    print(i,u,y)

## 25.猴子吃桃,x个桃子,每天早上吃掉一半+1,第10天早上发现剩1,求x
a = 1
for i in range(1,10):
    a = 2*(a+1)
print(a)

## 26.已知a,b,c,与x,y,z--------------c==y,a!=x,
c=‘y‘
l1=[‘x‘,‘y‘,‘z‘]
for i in l1:
    if i == c:
        for a in l1:
            if a != ‘x‘ and a != c:
                for b in l1:
                    if b != c and b != a:
                        print(‘c:‘,c,‘  b:‘,b,‘  a:‘,a)

## 27.,打出该序列2/1,3/2,5/3,8/5,13/8,12/13......前20项之和
a = 2
b = 1
c = 0
for i  in range(1,21):
    c += a/b
    a, b = a+b, a
print(c)

## 28.求1+2n+3n+4n+5n...20n
n,k = 2,0
for i in range(2,21):
    k += i*n
    #print(i,end=‘ ‘)
k+1

## 29.输入5位数看是否为回文数
a = input()
b = a[ :1]
c = a[1:2]
d = a[2:3]
e = a[3:4]
f = a[4:5]
if b == f and c == e:
    print(‘yes‘)
else:
    `print(‘no‘)```

以上是关于Python 新手入门习题及代码的主要内容,如果未能解决你的问题,请参考以下文章

《python编程:从入门到实践》课后习题及答案

Python3练习题系列(08)——代码阅读方法及字典跳转表理解

python 入门练习题

Python25道练习题及详细答案解析,爆肝八小时总结

算法竞赛入门经典第二版 第二章习题及思考题

Python练习题 024:求位数及逆序打印