以下是自己在学习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-10-21 17:45:39