posted on 2023-05-07 20:36 read(796) comment(0) like(3) collect(4)
Hi everyone, the paper is here again, here are 100 classic Python practice questions
It took a week to sort out 100 Python exercises. If you are a beginner, then this exercise will bring you great help. If you can complete this exercise completely independently Question, you have already started with Python, and the practice questions cover most of the basics of Python:
Write a program that finds all such numbers that are divisible by 7 but not multiples of 5 (between 2000 and 3200 (both inclusive)). The obtained numbers should be printed on one line in comma separated order.
Consider using the range(#begin, #end) method.
My solution: Python 3
use for loop
- l=[]
- for i in range(2000, 3201):
- if (i%7==0) and (i%5!=0):
- l.append(str(i))
-
- print ','.join(l)
Using generators and list comprehensions
print(*(i for i in range(2000, 3201) if i%7 == 0 and i%5 != 0), sep=",")
Write a program that can calculate the factorial of a given number, the result should be printed on one line in comma separated order, suppose the following input is given to the program: 8 Then, the output should be: 40320
If input data is provided to a question, it should be assumed to be console input.
My solution: Python 3
使用While循环
n = int(input()) #input() function takes input as string type
#int() converts it to integer type
fact = 1
i = 1
while i <= n:
fact = fact * i;
i = i + 1
print(fact)
使用For循环
n = int(input()) #input() function takes input as string type
#int() converts it to integer type
fact = 1
for i in range(1,n+1):
fact = fact * i
print(fact)
Use Lambda function
n = int(input())
def shortFact(x): return 1 if x <= 1 else x*shortFact(x-1)
print(shortFact(n))
- while True:
- try:
- num = int(input("Enter a number: "))
- break
- except ValueError as err:
- print(err)
-
- org = num
- fact = 1
- while num:
- fact = num * fact
- num = num - 1
- print(f'the factorial of {org} is {fact}')
- from functools import reduce
-
- def fun(acc, item):
- return acc*item
-
- num = int(input())
- print(reduce(fun,range(1, num+1), 1))
With a given integer n, write a program to generate a dictionary containing (i, ixi) as integers between 1 and n (both inclusive). The program should then print the dictionary. Assuming the following input is given to the program: 8 \
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
If input data is provided to a question, it should be assumed to be console input. Consider using dict()
My solution: Python 3:
use for loop
- n = int(input())
- ans = {}
- for i in range (1,n+1):
- ans[i] = i * i
- print(ans)
use dictionary comprehension
- n = int(input())
- ans={i : i*i for i in range(1,n+1)}
- print(ans)
- # 演进
- try:
- num = int(input("Enter a number: "))
- except ValueError as err:
- print(err)
-
- dictio = dict()
- for item in range(num+1):
- if item == 0:
- continue
- else:
- dictio[item] = item * item
- print(dictio)
- num = int(input("Number: "))
- print(dict(enumerate([i*i for i in range(1, num+1)], 1)))
These are the problems solved on day one. The above problems are very easy for basic beginner learners. I show some simple coding methods in the solution. Let's see how to face and deal with new problems the next day.
question
Write a program that takes as input a comma-separated sequence of 4-digit binary numbers and checks whether they are divisible by 5. Numbers divisible by 5 will be printed in comma separated order.
example:
0100,0011,1010,1001
Then the output should be:
1010
NOTE: Data is assumed to be entered via the console.
If input data is provided to a question, it should be assumed to be console input.
- def check(x): #转换二进制整数&返回由5零中频整除
- total, pw = 0, 1
- reversed(x)
-
- for i in x:
- total+=pw * (ord(i) - 48) #ORD()函数返回ASCII值
- pw*=2
- return total % 5
-
- data = input().split(",") #输入此处,并在','位置分割
- lst = []
-
-
-
- for i in data:
- if check(i) == 0: #如果零发现它是指由零整除并添加到列 lst.append(i)
-
- print(",".join(lst))
or
- def check(x):#如果被5整除,则check函数返回true
- return int(x,2)%5 == 0 #int(x,b)将x作为字符串,将b作为基数,
- #将其转换为十进制
- 数据 = 输入()。分割(',')
-
- data = list(filter(check(data)))#在filter(func,object)函数中,如果通过'check'函数
- print(“,”。join(data)找到True,则从'data'中选取元素。
or
- data = input().split(',')
- data = [num for num in data if int(num, 2) % 5 == 0]
- print(','.join(data))
Write a program that finds all numbers between 1000 and 3000 (inclusive) such that every digit of the number is even. The obtained numbers should be printed on one line in comma separated order.
If input data is provided to a question, it should be assumed to be console input.
My solution: Python 3
- lst = []
-
- for i in range(1000,3001):
- flag = 1
- for j in str(i): #每个整数编号i被转换成字符串
-
- if ord(j)%2 != 0: #ORD返回ASCII值并且j是i
- flag = 0
- if flag == 1:
- lst.append(str(i)) #i作为字符串存储在列表中
-
- print(",".join(lst))
or
- def check(element):
- return all(ord(i)%2 == 0 for i in element) #所有返回true如果所有的数字,i是即使在元件
-
- lst = [str(i) for i in range(1000,3001)] #创建所有给定数字的列表,其字符串数据类型为
- lst = list(filter(check,lst)) #如果检查条件失败,则过滤器从列表中删除元素
- print(",".join(lst))
- lst = [str(i) for i in range(1000,3001)]
- lst = list(filter(lambda i:all(ord(j)%2 == 0 for j in i), lst)) #使用lambda来在过滤器功能内部定义函数
- print(",".join(lst))
Write a program that takes a sentence and counts the number of letters and numbers.
Suppose the following input is given to the program:
hello world! 123
Then, the output should be:
- LETTERS 10
- DIGITS 3
If input data is provided to a question, it should be assumed to be console input.
- word = input()
- letter,digit = 0,0
-
- for i in word:
- if ('a'<=i and i<='z') or ('A'<=i and i<='Z'):
- letter+=1
- if '0'<=i and i<='9':
- digit+=1
-
- print("LETTERS {0}\nDIGITS {1}".format(letter,digit))
or
- word = input()
- letter, digit = 0,0
-
- for i in word:
- if i.isalpha(): #返回true如果字母表
- letter += 1
- elif i.isnumeric(): #返回true如果数字
- digit += 1
- print(f"LETTERS {letter}\n{digits}") #两种解决方案均显示两种不同类型的格式化方法
All questions above 10-13 are mostly string related. The main part of the solution consists of string replacement functions and comprehension methods to write down the code in a shorter form.
Write a program that takes a sentence and counts the number of uppercase and lowercase letters.
Suppose the following input is given to the program:
Hello world!
Then, the output should be:
- UPPER CASE 1
- LOWER CASE 9
If input data is provided to a question, it should be assumed to be console input.
My solution: Python 3
- word = input()
- upper,lower = 0,0
-
- for i in word:
- if 'a'<=i and i<='z' :
- lower+=1
- if 'A'<=i and i<='Z':
- upper+=1
-
- print("UPPER CASE {0}\nLOWER CASE {1}".format(upper,lower))
or
- word = input()
- upper,lower = 0,0
-
- for i in word:
- lower+=i.islower()
- upper+=i.isupper()
-
- print("UPPER CASE {0}\nLOWER CASE {1}".format(upper,lower))
or
- string = input("Enter the sentense")
- upper = 0
- lower = 0
- for x in string:
- if x.isupper() == True:
- upper += 1
- if x.islower() == True:
- lower += 1
-
- print("UPPER CASE: ", upper)
- print("LOWER CASE: ", lower)
Author:Ineverleft
link:http://www.pythonblackhole.com/blog/article/344/fdaae48c18b4b3587b98/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!