Python核心编程_第二章课后习题

以下是自己在学习Python核心编程时,做的课后练习题。现在把它们贴出来,以记录自己的学习过程。小弟是机械出身,很多练习题目写的很是机械。虽然写出来的脚本都能满足题目要求,但效率可能不是最好的,所以,小弟还是厚着脸皮把它们给贴出来,一来可以让高手指点,二来可以与我一样在学习Python的兄弟共同学习。

以下的程序均以题目标号命名,如2-3这个题目,程序名就为2_3.py。

习题2_3.py

#!/usr/bin/env python
A = 10
B = 4

print "A plus B is:",
print (A + B)

print

print "A minus B is:",
print (A - B)

print "A multi B is:",
print (A * B)

print "A divide B is:",
print (A / B)

print "A mod B is:",
print (A % B)

print "A pow B is:",
print (A ** B)

习题2_4.py

#!/usr/bin/env python
str1 = raw_input()

print "You will get the string str1:",
print str1

int1 = int(raw_input())
print "You will get the dicimal int1:",
print int1

习题2_5.py

#!/usr/bin/env python
i = 0

while i < 11:
    print i,
    i += 1
    
print

print "The next example used range() function"

for i in range(0, 11):
    print i,

习题2_6.py

#!/usr/bin/env python

A = 10

if A > 0:
    print "A is positive"

elif A < 0:
    print "A is negative"

else:
    print "A is equal 0"

print
print "Edition 2nd"

A = int(raw_input())

if A > 0:
    print "A is positive"

elif A < 0:
    print "A is negative"

else:
    print "A is equal 0"

习题2_7.py

#!/usr/bin/env python

str1 = str(raw_input())

strLength = len(str1)

i = 0

while i < strLength:
    print "%5d" % i, str1[i]
    i += 1

print
print ‘--------------‘

for eachString in str1:
    print eachString

习题2_8.py

#!/usr/bin/env python

print "While condition"
print "---------------"

aList = [1, 2, 3, 4, 5]

i = 0
iSum = 0

while i < len(aList):
    iSum += aList[i]
    i += 1
    
print "iSum is:", iSum

print

print "For condition"
print "-------------"

bList = [1, 2, 3, 4, 5]

iSum1 = 0

for eachList in bList:
    iSum1 += eachList

print "iSum1 is:", iSum1

习题2_9.py

#!/usr/bin/env python

i = 0
aList = []

while i < 5:
    aList.append(float(raw_input()))
    i += 1

sumList = sum(aList)

aveList = sumList / len(aList)

print "average is:", aveList

习题2_10.py

#!/usr/bin/env python

while True:
    d = int(raw_input(‘Please input an integer: ‘))
    if d >= 1 and d <= 100:
        print ‘Successfully‘
        break
    else:
        continue

习题2_11.py

#!/usr/bin/env python

print "S. (S)um of five integers"
print "A. (A)verage five integers"
print "X. E(x)it"

MyList = []

def GetNumber():
    i = 0
    
    while i < 5:
        num = int(raw_input())
        print "input the %dth number:", i
        MyList.append(num)
        i += 1
        
    return MyList

def MySum(x):
    return sum(x)

def MyAverage(x):
    return MySum(x) / 5

while True:
    input_string = raw_input(‘Choose Your Function: ‘)

    if input_string == "S":
        result = MySum(GetNumber())
        print ‘the sum is‘, result
        
    elif input_string == "A":
        result = MyAverage(GetNumber())
        print ‘the average is‘, result
        
    elif input_string == "X":
        break
        
    else:
        print "No more sense! Try again!"

习题2_13.py

#!/usr/bin/env python

print dir()

import sys

print dir()

print sys.version

print sys.platform

sys.exit(0)

习题2_15.py

#!/usr/bin/env python

MyList = []
i = 0

while i < 3:
    print ‘input %dth number‘ % i
    MyInput = int(raw_input())
    MyList.append(MyInput)
    i += 1

print ‘the original list is:‘, MyList

SortedList = sorted(MyList)

print ‘sorted list is:‘, SortedList

print ‘reversed list is:‘,

i = 2

while i >= 0:
    print SortedList[i],
    i -= 1
时间: 2024-08-02 02:51:19

Python核心编程_第二章课后习题的相关文章

python核心编程2第二章课后练习

2-1 print来显示变量的内容,仅用变量名时,输出的字符串使用单引号括起来的,这是为了让非字符串对象也能以字符串的方式显示在屏幕上,print语句使用str()函数显示对象,交互解释器调用repr()函数来显示对象 2-2 (a)运算1+2*4 (b)只会做运算不会输出 (c)运算未显示结果 (d)交互解释器输入一段语句后会返回语句结果 (e)print ‘1+2*4’   2-3   2-4 (a) #!/usr/etc/env python string =raw_input("plea

Python核心编程 第六章课后习题

6–1. 字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串 是否是另一个大字符串的一部分? Answer:in     not in 6-2. #! /usr/bin/env python # coding: utf-8 ''' 6–2. 字符串标识符.修改例 6-1 的 idcheck.py 脚本,使之可以检测长度为一的标识符,并且 可以识别 Python 关键字,对后一个要求,你可以使用 keyword 模块(特别是 keyword.kelist)来帮你.

python核心编程第4章课后题答案(第二版75页)

4-1Python objects All Python objects have three attributes:type,ID,and value. All are readonly with a possible expection of the value(which can be changed only if the object is mutable). 4-5str()and repr() repr() is a built-in function while str() wa

python核心编程第2章课后题答案(第二版36页)

2-5 Loops and Numbers a) i = 0    while i <11:     print i    i += 1 b) for i in range(0,11): print i 2-6 Conditionals n =int( raw_input('enter a number:')) if n < 0: print 'negative' elif n > 0: print 'positive' else: print 'zero' 2-7 Loops and

Python核心编程2第一章课后练习

1-1 在windows下的安装方法在网上下载python2.7直接安装到C盘1)在系统变量中找到path. 2)编辑path值,添加你安装的python路径,C:\Python27. 3)检验python是否安装配置成功,打开cmd,输入python,如果出现以下界面,则说 明你的python安装成功了.   1-2 a.三种方法执行python 1)启动交互式解释器powershell python,每次输入一行python代码 2)运行python的脚本 3)集成开发环境图形界面(IDLE

【0】python核心编程,第二章

1.print语句也支持将输入重定向到文件,示例: 1 logfile = open('/tmp/mylog.txt', 'a') 2 print >> logfile, 'Fatal error: invalid input!' 3 logfile.close() 2.使用逻辑运算符可以将任意表达式连接在一起,并得到一个布尔值: 1 >>> 2 < 4 and 2 == 4 2 False 3 >>> 2 > 4 or 2 < 4 4 T

python 核心编程第六章课后题自己做的答案

6–6. 字符串.创建一个 string.strip()的替代函数:接受一个字符串,去掉它前面和后面的 空格(如果使用 string.*strip()函数那本练习就没有意义了) 1 'Take a string and remove all leading and trailing whitespace' 2 3 def newStrip(str): 4 'delete blanks around a string' 5 _end = len(str) 6 _start = 0 7 8 # de

python核心编程第5章课后题答案

5-8Geometry import math def sqcube(): s = float(raw_input('enter length of one side: ')) print 'the area is:', s ** 2., '(units squared)' print 'the volume is:', s ** 3., '(cubic units)'def cirsph(): r = float(raw_input('enter length of radius: ')) p

Python核心编程(第二版) 第六章习题答案

6–1.字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? 答:有,string.find(str,beg,end) 6–2.字符串标识符.修改例 6-1 的 idcheck.py 脚本,使之可以检测长度为一的标识符,并且可以识别 Python 关键字,对后一个要求,你可以使用 keyword 模块(特别是 keyword.kelist)来帮你. 1 #!/usr/bin/python 2 3 import string 4 impo