当前位置:文档之家› Python程序设计习题与答案

Python程序设计习题与答案

while True:
result = (x)
if result:
if (0) != 0:
x = x[:(0)+1]+'i'+x[(0)-1:]
else:
x = x[:(0)]+'i'+x[(0)-1:]
else:
break
print(x)
有一段英文文本,其中有单词连续重复了2次,编写程序检查重复的单词并只保留一个。例如文本内容为“This is is a desk.”,程序输出为“This is a desk.”
print(x)
print(Sorted(x))
第6章面向对象程序设计
继承节例2中的Person类生成Student类,填写新的函数用来设置学生专业,然后生成该类对象并显示信息。
答:Python代码为
import types
class Person(object): #基类必须继承于object,否则在派生类中将无法使用super()函数
x = 'capital = little = digit = other =0'
print(demo(x))
在Python程序中,局部变量会隐藏同名的全局变量吗?请编写代码进行验证。
答案:会。
>>> def demo():
a=3
print a
>>> a=5
>>> demo()
3
>>> a
5
编写函数,可以接收任意多个整数并输出其中的最大值和所有整数之和。
编写函数,模拟内置函数sorted()。
答:这里给出Python代码,如果使用Python的话只需要修改其中的print()函数为print语句即可。
def Sorted(v):
t = v[::]
r = []
while t:
tt = min(t)
(tt)
(tt)
return r
x = [1,3,5,2,1,0,9,7]
x = input('Please input an integer of more than 3 digits:')
try:
x = int(x)
x = x
else:
print(x)
except BaseException:
print('You must input an integer.')
2)Python代码:
《Python程序设计》习题与参考答案
第1章基础知识
简单说明如何选择正确的Python版本。
答:
在选择Python的时候,一定要先考虑清楚自己学习Python的目的是什么,打算做哪方面的开发,有哪些扩展库可用,这些扩展库最高支持哪个版本的Python,是Python还是Python,最高支持到Python还是Python。这些问题都确定以后,再做出自己的选择,这样才能事半功倍,而不至于把大量时间浪费在Python的反复安装和卸载上。同时还应该注意,当更新的Python版本推出之后,不要急于更新,而是应该等确定自己所必须使用的扩展库也推出了较新版本之后再进行更新。
答:这里给出Python代码,如果使用Python的话只需要修改其中的print()函数为print语句即可。
def Sum(v):
s = 0
for i in v:
s += i
return s
x = [1,2,3,4,5]
print(Sum(x))
x = (1,2,3,4,5)
print(Sum(x))
答:这里给出Python代码,如果使用Python的话只需要修改其中的print()函数为print语句即可。
1)不使用正则表达式
x = "i am a teacher,i am man, and i am 38 years am not a businessman."
x = ('i ','I ')
编写函数,判断一个整数是否为素数,并编写主程序调用该函数。
答:这里给出Python代码,如果使用Python的话只需要修改其中的print()函数为print语句即可。
import math
def IsPrime(v):
n = int(v)+1)
for i in range(2,n):
if v%i==0:
答:这里给出Python代码,如果使用Python的话只需要修改其中的print()函数为print语句即可。
1)方法一
import re
x = 'This is a a desk.'
pattern = (r'\b(\w+)(\s+\1){1,}\b')
matchResult = (x)
x = (1),x)
尽管如此,Python 3毕竟是大势所趋,如果您暂时还没想到要做什么行业领域的应用开发,或者仅仅是为了尝试一种新的、好玩的语言,那么请毫不犹豫地选择Python系列的最高版本(目前是Python)。
为什么说Python采用的是基于值的内存管理模式?
答:
Python采用的是基于值的内存管理方式,如果为不同变量赋值相同值,则在内存中只有一份该值,多个变量指向同一块内存地址,例如下面的代码。
print(x)
2)方法二
x = 'This is a a desk.'
pattern = (r'(?P<f>\b\w+\b)\s(?P=f)')
matchResult = (x)
x = (0),(1))
简单解释Python的字符串驻留机制。
答:
Python支持字符串驻留机制,即:对于短字符串,将其赋值给多个不同的对象时,内存中只有一个副本,多个对象共享该副本。这一点不适用于长字符串,即长字符串不遵守驻留机制,下面的代码演示了短字符串和长字符串在这方面的区别。
def __init__(self, name = '', age = 20, sex = 'man'):
(name)
(age)
(sex)
def setName(self, name):
if not isinstance(name,str):
print('name must be string.')
print(sum(x))
print(sum(range(1,100)[::2]))
编写程序,实现分段函数计算,如下表所示。
x
y
x<0
0
0<=x<5
x
5<=x<10
3x-5
10<=x<20
20<=x
0
答:Python代码如下,如果使用Python只需要把其中的print()函数改为print语句即可。
答:这里给出Python代码,如果使用Python的话只需要修改其中的print()函数为print语句即可。
import re
x = "I am a teacher,I am man, and I am 38 years am not a busInessman."
print(x)
pattern = (r'(?:[\w])I(?:[\w])')
>>> x = 3
>>> id(x)
>>> y = 3
>>> id(y)
>>> y = 5
>>> id(y)
>>> id(x)
在Python中导入模块中的对象有哪几种方式?
答:常用的有三种方式,分别为
import模块名[as别名]
from模块名import对象名[ as别名]
from math import *
x = input('Please input x:')
x = eval(x)
if x<0 or x>=20:
print(0)
elif 0<=x<5:
print(x)
elif 5<=x<10:
print(3*x-5)
elif 10<=x<20:
print*x-2)
第4章字符串与正则表达式
假设有一段英文,其中有单独的字母“I”误写为“i”,请编写程序进行纠正。
return 'No'
else:
return 'Yes'
print(IsPrime(37))
print(IsPrime(60))
print(IsPrime(113))
编写函数,接收一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并以元组的形式返回结果。
答:这里给出Python代码,如果使用Python的话只需要修改其中的print()函数为print语句即可。
t = t/i
else:
i+=1
print x,'=','*'.join(map(str,result))
编写程序,至少使用2种不同的方法计算100以内所有奇数的和。
答:Python代码如下,如果使用Python只需要把其中的print()函数改为print语句即可。
x = [i for i in range(1,100) if i%2==1]
相关主题