python一些比较基础的练习题

  • #####求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
sum1 = 0
print('请输入元素的值:')
a = int(input())
list1 = [a]
print('请输入数的个数:')
n = int(input())
for i in range(1, n):
    list1.append(list1[i - 1] * 10 + a)
print(sum(list1))

输出结果:
请输入元素的值:
2
请输入数的个数:
5
24690
  • #####打印图形1
img = '★'
list1 = [img]
print('请输入图形行数:')
n = int(input())
for i in range(0, n):
    if i == 0:
       print(list1[i])
    else:
        list1.append(list1[i-1]+img)
        print(list1[i])

输出结果:
5
★
★★
★★★
★★★★
★★★★★
  • #####打印图形2
print('请输入图形行数:')
n = int(input())
picture = '@'
list1 = [picture]
img = [picture]
for i in range(0, n):
    if i == 0:
        print(img[i].center(n*2))
    else:
        list1.append(list1[i - 1] + picture*2)
        img.append(list1[i])
        print(img[i].center(n*2))

输出结果:
4
   @    
  @@@   
 @@@@@  
@@@@@@@
  • #####打印图形3
    “`Python
    img = ‘★’
    list1 = [img]
    list2 = []
    print(‘请输入图形行数:’)
    n = int(input())
    for i in range(0, n):
    if i == 0:
    continue
    else:
    list1.append(list1[i-1]+img)
    for i in range(0, n):
    list2.append(list1[n-1])
    n -= 1
    print(list2[i])

输出结果:
4
★★★★
★★★
★★

- #####输入两个正整数m和n,求其最大公约数和最小公倍数。
```Python
m = int(input('请输入第一个正整数:'))
n = int(input('请输入第二个正整数:'))
max_1 = 0
min_1 = 0
for i in range(1, m*n+1):
    if (m % i == 0) and (n % i == 0):
        max_1 = i
    # if (i % m == 0) and (i % n == 0):
    #     min_1 = i
    #     break
    min_1 =int(m * n / max_1)
print('最大公约数为:', max_1, '最小公倍数为:', min_1)


输出结果:
请输入第一个正整数:15
请输入第二个正整数:18
最大公约数为: 3 最小公倍数为: 90




"se-preview-section-delimiter">
  • #####一个数如果恰好等于它的因子之和,这个数就称为 “完数 “。例如6=1+2+3.编程 找出1000以内的所有完数
number = [1]
n = 1001
for j in range(1, n):
    number2 = []
    for i in range(1, j):
        if j % i == 0:
            number2.append(i)
    a = sum(number2)
    if a == j:
        number.append(j)
print(number)

输出结果:
[1, 6, 28, 496]




class="se-preview-section-delimiter">div>
  • ##### 输出9*9⼝口诀
b = []
for i in range(1, 10):
    a = []
    for j in range(1, i+1):
        a.append(i * j)
        b = a[:]
        print(j, '*', i, '=', b[-1], end="      ")
        if i == j:
            print('\t')

输出结果:
1 * 1 = 1       
1 * 2 = 2      2 * 2 = 4        
1 * 3 = 3      2 * 3 = 6      3 * 3 = 9         
1 * 4 = 4      2 * 4 = 8      3 * 4 = 12      4 * 4 = 16        
1 * 5 = 5      2 * 5 = 10      3 * 5 = 15      4 * 5 = 20      5 * 5 = 25       
1 * 6 = 6      2 * 6 = 12      3 * 6 = 18      4 * 6 = 24      5 * 6 = 30      6 * 6 = 36       
1 * 7 = 7      2 * 7 = 14      3 * 7 = 21      4 * 7 = 28      5 * 7 = 35      6 * 7 = 42      7 * 7 = 49       
1 * 8 = 8      2 * 8 = 16      3 * 8 = 24      4 * 8 = 32      5 * 8 = 40      6 * 8 = 48      7 * 8 = 56      8 * 8 = 64       
1 * 9 = 9      2 * 9 = 18      3 * 9 = 27      4 * 9 = 36      5 * 9 = 45      6 * 9 = 54      7 * 9 = 63      8 * 9 = 72      9 * 9 = 81       






<div class="se-preview-section-delimiter">div>
  • #####一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同
while True:
    n = int(input("请输入一个五位数:"))
    s = str(n)
    if len(s) == 5:
        if s[0] == s[4] and s[1] == s[3]:
            print(n, '是一个回文数')
        else:
            print(n, '不是一个回文数')
    else:
        print("你的输入有误,程序退出")
        break

输出结果:
请输入一个五位数:12321
12321 是一个回文数
请输入一个五位数:12345
12345 不是一个回文数
请输入一个五位数:1
你的输入有误,程序退出




"se-preview-section-delimiter"></div>
  • #####打印图形(菱形)
n = int(input('请输入图形行数:'))
picture = '@'
list1 = [picture]
img = [picture]
for i in range(0, n):
    if i == 0:
        print(img[i].center(n*2))

    else:
        list1.append(list1[i - 1] + picture*2)
        img.append(list1[i])
        print(img[i].center(n*2))
while i > 0:
    print(img[i-1].center(n * 2))
    i -= 1

输出结果:
请输入图形行数:4
   @    
  @@@   
 @@@@@  
@@@@@@@ 
 @@@@@  
  @@@   
   @    





"se-preview-section-delimiter"></div>
  • ##### 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
str1 = input("请输入一串字符:")
a = []
char = 0
number = 0
blank = 0
other = 0
for i in range(0, len(str1)):
    a.append(str1[i])
for j in range(0, len(a)):
    if a[j] == ' ':
        blank += 1
    elif 'a' <= a[j] <= 'z' or 'A' <= a[j] <= 'Z':
        char += 1
    elif "0" <= a[j] <= '9':
        number += 1
    else:
        other += 1
print('char =', char, 'number =', number, 'blank =', blank, 'other =', other)


输出结果:
请输入一串字符:hjsakj2 44hkjbwr3 +fe-w+ +df
char = 17 number = 4 blank = 3 other = 4

你可能感兴趣的:(python练习题)