python 100题_python 100题练习记录(一)

本文提供了一系列Python编程的基础练习,包括寻找在指定范围内除以7且不被5整除的数字,计算阶乘,生成平方数字典,处理逗号分隔的数字列表,创建获取和打印字符串的类,以及根据公式计算并打印结果。这些练习涵盖了基本的输入输出、条件判断、循环、函数、类和数学运算,是学习Python的好素材。
摘要由CSDN通过智能技术生成

Question 1

Level 1

Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,

between 2000 and 3200 (both included).

The numbers obtained should be printed in a comma-separated sequence on a single line.

list = []

for num in range(2000,3201):

if num % 7 == 0 and num % 5 != 0:

list.append(num)

for n in list:

print(n,end=",")

Question 2

Level 1

Question:

Write a program which can compute the factorial of a given numbers.

The results should be printed in a comma-separated sequence on a single line.

Suppose the following input is supplied to the program:

8

Then, the output should be:

40320

Hints:

In case of input data being supplied to the question, it should be assumed to be a console input.

def fact(x):

if x == 0:

return 1

return x * fact(x - 1)

x=int(input("请输入你需要计算的数值:"))

print("%d的阶乘结果是%d"%(x,fact(x)))

Question 3

Level 1

Question:

With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.

Suppose the following input is supplied 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}

Hints:

In case of input data being supplied to the question, it should be assumed to be a console input.

Consider use dict()

dic = dict()

n = int(input("请输入数值:"))

for i in range(1,n+1):

dic[i] = i*i

print(dic)

Question 4

Level 1

Question:

Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.

Suppose the following input is supplied to the program:

34,67,55,33,12,98

Then, the output should be:

[‘34’, ‘67’, ‘55’, ‘33’, ‘12’, ‘98’]

(‘34’, ‘67’, ‘55’, ‘33’, ‘12’, ‘98’)

Hints:

In case of input data being supplied to the question, it should be assumed to be a console input.

tuple() method can convert list to tuple

list = []

n = int(input("请输入你要的元素个数:"))

for i in range(1,n+1):

num = int(input("请输入元素值:"))

list.append(num)

list.sort()

print(list)

# tu = tuple(list)

print(tuple(list))

Question 5

Level 1

Question:

Define a class which has at least two methods:

getString: to get a string from console input

printString: to print the string in upper case.

Also please include simple test function to test the class methods.

Hints:

Use init method to construct some parameters

class TestString(object):

def __init__(self):

self.string = None

def getSting(self):

self.string = input("请输入字符串:")

def printString(self):

print(self.string)

if __name__ == '__main__':

ts = TestString()

ts.getSting()

ts.printString()

Question 6

Level 2

Question:

Write a program that calculates and prints the value according to the given formula:

Q = Square root of [(2 * C * D)/H]

Following are the fixed values of C and H:

C is 50. H is 30.

D is the variable whose values should be input to your program in a comma-separated sequence.

Example

Let us assume the following comma separated input sequence is given to the program:

100,150,180

The output of the program should be:

18,22,24

Hints:

If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26)

In case of input data being supplied to the question, it should be assumed to be a console input.

import math

c,h =50,30

str = input("请输入您需要计算的值,以逗号做分隔:")

s = str.split(',')

for i in s:

d = int(i)

q = math.sqrt(2*c*d/h)

print(int(round(q)))

原文链接:https://blog.csdn.net/jokerhappy/article/details/108895666

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值