2-4

 #2-4(a)
print "enter a string" inputstring = raw_input() print"the string is: ", inputstring
 #2-4(b)
print "enter a number:" num = raw_input() print "the number is: ", int(num)

2-5

#2-5(a)
i = 0
while i<=10:
print i
i+=1
 # 2-5(b)
for i in range(11)
print i

2-6

 #2-6
print "please input a number:" num = raw_input() if int(num) < 0:
print" a negative number!!!"
elif int(num) > 0:
print "a positive number!!!"
else:
print "zero"

2-7

 #2-7
print "please input a string:" inputstring = raw_input() while for e in inputstring:
print e

2-8

 #2-8
print "please input a string:" inputstring = raw_input()
i = 0
while i<len(inputstring):
print inputstring[i],
i+=1
print
for e in inputstring:
print e

2-9

 #2-9
arrary = [1, 2, 3, 4, 4] sum = 0
for e in arrary:
sum += e ev = float(sum)/float(len(arrary)) print ev

2-10

 #2-10
print "please input a number between 1-100" num = int(raw_input()) while num<1 or num >100:
print "please input again:"
num = int(raw_input()) print "num is valid

2-11

 #2-11
# to support cn
#encoding: utf-8
print """(1)取五個數的和"""
print "(2)取五個數的平均值..."
print "(x)退出"
def sum(arrary):
s = 0
for e in arrary:
s += e
return s def average(arrary):
if len(arrary) == 0:
return 0
else:
return float(sum(arrary))/float(len(arrary)) arr = [0, 1, 2, 3, 3] while True:
opt = raw_input()
if opt == 'x':
print "退出"
break
elif int(opt) == 1:
print "the sum of the arrary is :", sum(arr)
elif int(opt) == 2:
print "the average of the arrary is:", average(arr)
else:
print "please input again"

2-14

 #2-14
#sort print "enter three number:" print "the first is:"
a = raw_input()
print "the second is:"
b = raw_input()
print "the third is:"
c = raw_input() print "after sort for low to high:" if a > b:
tmp = a
a = b
b = tmp
if a > c:
tmp = a
a = c
c = tmp if b > c:
tmp = b
b = c
c = tmp print a, b, c print "after sort for high to low:" if a < b:
tmp = a
a = b
b = tmp
if a < c:
tmp = a
a = c
c = tmp if b < c:
tmp = b
b = c
c = tmp print a, b, c

python 核心编程课后练习(chapter 2)的更多相关文章

  1. python 核心编程课后练习(chapter 6)

    6-1 #6-1 #help(string) import string str = "helloworld" substr = "h1e" if string ...

  2. python 核心编程课后练习(chapter 5)

    5-2 #5-2 def mul(x, y): return x * y print mul(4,5) 5-3 #5-3 def value_score(num): if 90<=num< ...

  3. python 核心编程课后练习(chapter 3)

    3-8 #3-8 "makeTextFile.py -- create text file" import os ls = os.linesep #get filename fna ...

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

    1. 字符串, string模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? str1 = 'abcdefghijklmnopqrstuv' print st ...

  5. Python 核心编程 课后习题 第五章

    2. 操作符. (a) 写一个函数, 计算并返回两个数的乘积. (b) 写一段代码调用这个函数, 并显示它的结果. def multi(a,b): return a * b result = mult ...

  6. Python核心编程 课后练习 第二章

    2.4 使用raw_input()函数得到用户输入. (a) 创建一段脚本使用raw_input()函数从用户输入得到一个字符串, 然后显示这个用户杠杠输入的字符串. #coding = utf-8 ...

  7. python核心编程(第二版)习题

    重新再看一遍python核心编程,把后面的习题都做一下.

  8. Python核心编程这本书的一些错误

    <Python核心编程第二版>这本书比<Python基础教程第二版修订版>详细很多,丰富了很多细节,虽然它是一本经典的入门书,但我发现还是存在一些明显的错误.在面向对象编程这一 ...

  9. Python核心编程-描述符

    python中,什么描述符.描述符就是实现了"__get__"."__set__"或"__delete__" 方法中至少一个的对象.什么是非 ...

随机推荐

  1. bzoj 3531 旅行

    动态开点线段树+树链剖分  对于每一种宗教信仰都开一颗线段树 空间: QlogN 即每一次修改都只会改变logN 个点 时间 O(QlogN)  naive题  边没有开两倍  QAQ bzoj 35 ...

  2. RMQ算法模板

    分别写了下标从0和1开始的两种 #include<stdio.h> #include<string.h> #include<algorithm> #include& ...

  3. matchesSelector 匹配选择器表达式sizzle的实现

    Sizzle.matchesSelector = function( node, expr ) {     return Sizzle( expr, null, null, [node] ).leng ...

  4. android NDK debug 遇到的问题与解决方法

    最近在研究android  NDK 的eclipse调试,遇到点问题,总结一下: 1.Unknown Application ABI :在application.mk里面添加APP_PLATFORM ...

  5. 更新日志(建议升级到2016.12.17) && 更新程序的方法

    更新程序的方法: 1,在控制面板里点击备份当前数据库文件到磁盘,把当天获取的信息从内存写到磁盘/存储卡.2,下载最新版的源码 wget -O "infopi.zip" " ...

  6. sql 2008 游标

    begin declare PlatformBulletin --定义游标 open PlatformBulletin --打开游标 declare @userid int,@zmscompanyid ...

  7. ORA-00907: 缺失右括号 整理解决

    ORA-00907: 缺失右括号 前言 最近在开发过程中使用oracle数据库,在程序中进行查询数据时遇到了“ORA-00907: 缺失右括号”的问题,但是如果直接把sql语句直接在数据库或PL/SQ ...

  8. 在CentOS上安装并运行SparkR

    环境配置—— 操作系统:CentOS 6.5 JDK版本:1.7.0_67 Hadoop集群版本:CDH 5.3.0 安装过程—— 1.安装R yum install -y R 2.安装curl-de ...

  9. 在eclipse运行程序

    1.开始的时候由于另一个财务共享的项目,其中也应用了maven管理项目,因此就不用额外安装配置maven. 2.这里关键的一点就是需要通过maven和jetty配合运行lenmonOA. 3.jett ...

  10. Comparable和Comparator实现对象比较

    由文生义: 继承Comparable ,表示该类的实例是可以相互比较的; 继承Comparator,表示该类是一个比较器,里面设置了按什么属性比较,list需要按这个比较器里的规则来比较; 使用方法如 ...