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. event.stopPropagation()与event.preventDefault()

    <div id='div0'> <div id='div1'> <a href="#" id='div2'>2222</a> < ...

  2. git提交远程仓库命令

    在已有的git库中搭建新库,并且将本地的git仓库,上传到远程服务器的git库中,从而开始一个新的项目 首先,在本地新建文件夹abc,进入到abc里面,然后git init.这样就在本地初始化了一个g ...

  3. React 学习笔记(学习地址汇总)

    好的博文地址:http://www.ruanyifeng.com/blog/2015/03/react.html 官网学习地址:http://facebook.github.io/react/docs ...

  4. 《Android深度探索HAL与驱动开发》第一章阅读心得

    首先了解到Android系统架构是由四层构成:其中第一层是Linux内核,他的作用是负责Linux的驱动程序以及内存.进程.电源等管理操作:第二层是C/C++代码库,也就是Linux下.so的文件:第 ...

  5. Android驱动开发前的准备(四)

    源代码的下载和编译 4.1 下载.编译和测试Android源代码 4.2下载和编译linux内核源代码 4.1.1 配置Android源代码下载环境 (1) 创建一个用于存放下载脚本文件的目录 # m ...

  6. AFN的初步封装(post、GET、有无参数)

    #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MyURLPost : NSObjec ...

  7. C#序列化与反序列化方式简单总结

    序列化和反序列化 相关类: System.SerializableAttribute特性(或称为属性), System.Runtime.Serialization.ISerializable(自定义序 ...

  8. mybatis 与 xml

    mybatis的两大重要组件:配置和映射文件,都是可以通过xml配置的(新版本新增了注解的方式配置Mapper),下面来解析下mybatis是怎么做的 其中,关于配置文件解析的主要是在这个类XMLCo ...

  9. CentOS 7 vs CentOS 6的不同

    (1)桌面系统 [CentOS6] GNOME 2.x [CentOS7] GNOME 3.x(GNOME Shell) (2)文件系统 [CentOS6] ext4 [CentOS7] xfs (3 ...

  10. 一条SQL查询多个统计结果

    例如以下情况,假如字段3是日期类型,按照小时分组统计字段1为空的个数,并对字段2大于5的值求和: SELECT SUM(CASE WHEN field1 IS NULL THEN 1 ELSE 0 E ...