python练手题_python小练手题1

1."""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"""num= int(input('请输入需要计算的数:'))deffact(num):if num ==0:return 1

return num * fact(num-1)print(fact(num))2."""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}"""num= int(input('请输入'))

d={}for i in range(1,num+1):

d.update({i:i*i})print(d)3."""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')"""defspl(word):

li=[]for i in word.split(','):

li.append(i)print()

tup= '('+str(li)[1:-1]+')'

return print(li,'\n',tup)

word= input("")

spl(word)4."""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"""

importmathdeffun(D):

C= 50H= 30li=[]for d in D.split(','):

Q= int(math.sqrt((2 * C * int(d)) /H))

li.append(Q)return print(str(li)[1:-1])

D=input()

fun(D)5."""Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.

Note: i=0,1.., X-1; j=0,1,¡­Y-1.

Example

Suppose the following inputs are given to the program:

3,5

Then, the output of the program should be:

[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]"""

defarray(x,y):

li=[]for i inrange(x):

li.append([])for j inrange(y):

li[i].append(0)

li[i][j]= i*jreturn print(li)

array(3,5)6."""Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.

Suppose the following input is supplied to the program:

without,hello,bag,world

Then, the output should be:

bag,hello,without,world"""

deffun(word):

li= [w for w in (word.split(','))]

li.sort()#new = sorted(li,key=lambda i:i[0])

return print(','.join(li))

fun('without,hello,bag,world')7."""Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.

Suppose the following input is supplied to the program:

Hello world

Practice makes perfect

Then, the output should be:

HELLO WORLD

PRACTICE MAKES PERFECT"""

deffun():

li=[]whileTrue:

s=input()ifs:

li.append(s.upper())continue

else:for i inli:print(i)breakfun()8."""Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence.

Example:

0100,0011,1010,1001

Then the output should be:

1010

Notes: Assume the data is input by console."""

deffun(word):

li=[]for i in word.split(','):if int(i,base=2)%5==0:

li.append(i)return print(','.join(li))

fun('0100,0011,1010,1001')9."""Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.

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

deffun():

li=[]

flag=0for i in range(1000,3000):for j inlist(str(i)):if int(j)%2 ==0:

flag+= 1

if flag == 4:

li.append(i)

flag=0else:

flag=0return print(li)

fun()10."Write a program that accepts a sentence and calculate the number of letters and digits.

Suppose the following input issupplied to the program:

hello world!123Then, the output should be:

LETTERS10DIGITS3""

"""import re

def fun(word):

alp = re.findall('[a-z,A-Z]',word)

num = re.findall('[0-9]',word)

return print('字母{}\n数字{}'.format(len(alp),len(num)))

fun('hello world! 123')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《Python练手经典100例》是一本适合初学者练习和巩固Python编程基础的经典教材。这本教材共包含100个不同难度的编程实例,涵盖了Python中的各个基本知识点和应用场景。 这些实例从简单到复杂,每个实例都有明确的指导和解答。通过实例练习,读者可以学会如何使用Python语言进行编程,并且学习到一些常见的编程技巧和思维方式。 这本教材的实例内容非常丰富多样,例如计算器、猜数字、生成斐波那契数列、实现进度条等等。每个实例都带有详细的说明和代码,读者可以按照教材进行一步步的实践。 通过练习这些实例,读者可以加深对Python语言的理解和掌握,培养编程思维和逻辑思维能力。同时,由于实例有多个难度级别,读者可以根据自己的编程水平挑选相应的实例进行练习,逐渐提高自己的编程能力。 总之,《Python练手经典100例》是一本非常适合初学者练习的经典教材,无论是对于掌握Python编程基础知识,还是培养编程思维能力,都有很大的帮助。对于想要通过实践提高编程水平的读者来说,这本教材绝对是不可或缺的学习资料。 ### 回答2: Python练手经典100例是一本非常受欢迎的Python编程教程,其目的是通过实践来提高Python编程技能。这本教程包括了100个经典的练习,涵盖了Python语法的各个方面,适合初学者和有一定基础的学习者。 这本教程的例子非常实用,从简单到复杂,逐步引导学习者提高编程能力。通过这些例子的实践,学习者可以了解并掌握Python的基本语法、条件语句、循环语句、字符串操作、列表操作、字典操作、文件操作等常用编程技巧。同时,这些例子涉及到了常见的算法和数据结构,学习者可以通过实现这些例子来加深对算法和数据结构的理解和应用。 除了提高编程能力,这本教程还培养了学习者的解决问的能力。每个例子都是一个具体的问,学习者需要用编程语言来解决这个问。通过思考和实践,学习者可以发展自己的解决问的方法和思维方式,培养了学习者的逻辑思维和分析问的能力。 这本教程的方法非常灵活,学习者可以按照自己的节奏进行学习。每个例子都有详细的目描述和示例代码,学习者可以先尝试自己解决问,然后再参考示例代码进行对比和学习。同时,这本教程还提供了额外的提示和技巧,帮助学习者更好地理解问和解决方法。 总之,Python练手经典100例是一本非常优秀的Python编程教程。通过实践这些例子,学习者可以提高编程能力,掌握Python语法和常用编程技巧,并培养解决问的能力。无论是初学者还是有一定基础的学习者,都可以从中受益匪浅。 ### 回答3: Python练手经典100例是一个常见的编程练习集合,旨在帮助初学者熟悉Python编程语言和解决问的能力。 这个集合有100个问,每个问提供了一个特定的场景或需求,要求编写Python代码实现相应的功能。 这些问涵盖了Python编程的各个方面,包括基本的数据类型操作、字符串处理、列表和字典的使用、条件判断和循环语句、文件IO、函数定义和参数传递等。 通过练习这100个例子,可以学习和掌握Python的基础语法和常用的编程技巧,提高解决问的思维能力和编码能力。 值得一提的是,这些练习并不仅限于初学者,对于有一定经验的Python开发者来说,也是很好的巩固和提高编程能力的途径。 总之,Python练手经典100例是一个非常有用的编程练习集合,通过反复练习这些例子,可以加深对Python语言的理解和应用,提升编程技能。对于想要学习或进一步提高Python编程能力的人来说,这是一个很好的资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值