Python Final Review Questions

1. Palindrome number judgment. Suppose n is an arbitrary natural number, if the natural number obtained by reversely arranging the digits of n is equal to n, then n is called a palindromic number. Input a number from the keyboard, please write a program to judge whether the number is a palindrome, if it returns True, otherwise it returns False.
[Input example] 12321
[Output example] True

def digital():
    a=list(input())
    b=a[::-1]
    if a==b:
        return True
    else:
        return False
print(digital())

Two prime number judgment. Write a function isPrime(x) that accepts a positive integer as a parameter and judges whether the number is a prime number (only divisible by 1 and itself), if it returns True, otherwise it returns False.
【Input example】3
【Output example】True

def isPrime(x):
    for i in range(2,x):
        if x%i == 0:
            return False
    return True
a=eval(input())
print(isPrime(a))

Three Count the occurrences of letters. Write a function that takes a string as an argument and counts the number of occurrences of each letter, and returns a dictionary whose keys are letters and values ​​are the number of occurrences.
【Input example】'abb'
【Output example】{'a': 1, 'b': 2}

def count(x):
    c={
    
    }
    for i in x:
        if i in c:
            c[i]+=1
        else:
            c[i]=1
    return c
print(count('abb'))

Four Determine whether the list has repeated elements. Write a function that accepts a list as a parameter and judges whether there are duplicate elements in the list, and returns True if there are duplicate elements, otherwise returns False.
【Input example】[1,2,3,4,5,3]
【Output example】True
The first(Compare the length of the original list with the length of the set list)

def list_count(x):
    if len(x) !=len(set(x)):
        return True
    else:
        return False
list1=[1,2,3,4,5,3]
print(list_count(list1))

the second

def list_count(x):
    for i in range(len(x)):
        for j in range(len(x)):
            if j < i and x[i] == x[j]:
                return True
    return False
list1 = [1,2,3,4,5,3]
print(list_count(list1))

5 Calculates the average value in the list. Given a list of numbers, write a program to calculate the average of all the numbers in the list.
【Input example】[1,2,3,4,5]
【Output example】3

def list_avg(x):
    c=0
    for i in x:
        c+=i
    avg=c/len(x)
    return int(avg)
list1=[1,2,3,4,5]
print(list_avg(list1))

6 Inverts an integer. Write a function that takes an integer as an argument and returns the inverse of that integer.
【Input example】123
【Output example】321

def list_int(x):
    list1=list(x)
    list2=list1[::-1]
    str1="".join(map(str, list2))
    return str1

b=input()
print(list_int(b))

An easier way:

def id(x):
    b=x[::-1]
    return b
a=input()
print(id(a))

Seven counts the number of vowels in the string. Input a string from the keyboard and count the number of vowels (a, e, i, o, u) in the string.
【Input example】'abe'
【Output example】2

def a_count(x):
    b=list(x)
    count=0
    for i in b:
        if 'a' == i:
            count += 1
        if 'e' == i:
            count += 1
        if 'i' == i:
            count += 1
        if 'o' == i:
            count += 1
        if 'u' == i:
            count += 1
    print(count)
a_count(input())

Eight Count the number of daffodils. "Narcissus number" means that the sum of the cubes of the digits in a three-digit number is equal to the number itself. For example, 153 is a daffodil number, because 153=the sum of the cubes of each digit. Write a program to calculate the total number of daffodils between 200 and 500.

def flower():
    count=0
    for x in range(200,501):
        b = x // 100  # 百位
        a = x % 100 // 10  # 十位
        c = x % 10  # 个位
        if(b**3+a**3+c**3==x):
            count+=1
    return count
print(flower())

Nine String separated printing. Obtain a string (including spaces) input by the user, split the string according to spaces, and print it line by line.
【Input example】'Python XYU 666'
【Output example】Python
XYU
666

str1=input()
str2=""
for i in range(len(str1)):
    if str1[i] != ' ':
        str2+=str1[i]
    else:
        str2+='\n'
print(str2)

Eleven balls fall freely from a height of 100 meters, bounce back to half of the original height after each landing, and then fall again.
How many meters did it travel when it hit the ground for the 10th time? How high is the 10th bounce?
[Output example] A total of 299.71 meters has passed, and the height of the 10th bounce is 0.10.
(Hint: watch out for formatted output of strings)

hight=100.0
sum=0.0
for i in range(1,11):
    sum += hight
    hight=hight/2
    if i==10:
        ten=hight
print("一共经过{:.2f}米,第10次反弹的高度是{:.2f}".format(sum+100,ten))

Eleven Given two positive integers a, b (1<=a <= b<=10^5), please count how many times ten Arabic numerals appear between a and b. For example, when a=11, b=20, the numbers between a and b are [11,12,13,14,15,16,17,18,19,20], then the 10 numbers 0-9 The number of occurrences are 1, 10, 2, 1, 1, 1, 1, 1, 1, 1, respectively.
Now give you a and b, please output the number of occurrences of ten Arabic numerals respectively;
output in ten lines, the first line indicates the number of occurrences of 0, the second line indicates the number of occurrences of 1, ..., the last line indicates the number of occurrences of 9 frequency.
[Input example] a =11,b =20
[Output example] 1, 10, 2, 1, 1, 1, 1, 1, 1, 1 (output by line)

a=eval(input())
b=eval(input())
list1=[]
count0,count1,count2,count3,count4,count5,count6,count7,count8,count9=0,0,0,0,0,0,0,0,0,0
for i in range(a,b+1):
    list1.append(i)
str1="".join(map(str,list1))
for j in str1:
    if '0' ==j:
        count0+=1
    if '1' ==j:
        count1+=1
    if '2' ==j:
        count2+=1
    if '3' == j:
        count3 += 1
    if '4' == j:
        count4 += 1
    if '5' == j:
        count5 += 1
    if '6' == j:
        count6 += 1
    if '7' == j:
        count7 += 1
    if '8' == j:
        count8 += 1
    if '9' == j:
        count9 += 1
print(count0,count1,count2,count3,count4,count5,count6,count7,count8,count9)

Twelve You are given two integers a and b (-10000<a, b<10000), please judge whether there are two integers whose sum is a and their product is b. If it exists, output Yes, otherwise output No.
【Input example】a=9,b=15
【Output example】No

a=eval(input('a='))
b=eval(input('b='))
for i in range(10000):
    for j in range(10000):
        if i+j==a and i*j==b:
            d=1
            break
        else:
            d=0
            break
if d==1:
    print('Yes')
else:
    print('No')

Guess you like

Origin blog.csdn.net/weixin_72138633/article/details/131144521