第2章

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第2章相关的知识,希望对你有一定的参考价值。

2-2:

(d)代码单独执行时,没有任何输出;在交互式解释器中执行会输出表达式的计算结果。

(e)

#!/usr/bin/env python
print 1 + 2 * 4

2-4:

(a)

#!/usr/bin/env python
inp = raw_input()
print inp

(b)

#!/usr/bin/env python
inp = int(raw_input())
print inp

2-5:

(a)

#!/usr/bin/python
a = 0
while a <= 10:
    print a
    a += 1

(b)

#!/usr/bin/env python
for item in range(11):
    print item

2-6:

固定数值:

#!/usr/bin/env python
num = a
if type(num) in (int, float):
    if num < 0:
        print num is less than 0
    elif num > 0:
        print num is greater than 0
    else:
        print num is equal to 0
else:
    print invalid num

获取输入:

#!/usr/bin/env python
num = float(raw_input(Enter a number: ))
if num < 0:
    print num is less than 0
elif num > 0:
    print num is greater than 0
else:
    print num is equal to 0

2-7:

while实现

#!/usr/bin/python
mystr = raw_input(Enter a string: )
index = 0
while index < len(mystr):
    print mystr[index]
    index += 1

for实现

#!/usr/bin/python
myStr = raw_input(Enter a number: )
for c in myStr:
    print c

2-8:

固定数值,while实现:

#!/usr/bin/env python
aList = [1,2,3,4,5]
index = 0
s = 0
while index < len(aList):
    s += aList[index]
    index += 1
print s

固定数值,for实现:

#!/usr/bin/env python
aList = [1,2,3,4,5]
s = 0
for item in aList:
    s += item
print s  

用户输入,while实现:

#!/usr/bin/env python
a = 0
s = 0
while a < 5:
    s += int(raw_input(->))
    a += 1
print s

用户输入,for实现:

#!/usr/bin/env python
s = 0
print Enter 5 numbers:
for i in range(5):
    s += int(raw_input(->))
print s

2-9:

#!/usr/bin/env python
aList = [1,2,5,6,7]
s = 0
for item in aList:
    s += item
ans = float(s) / len(aList)
print ans

2-10:

#!/usr/bin/env python
while True:
    num = float(raw_input(Enter a number between 1 and 100: ))
    if 1 < num < 100:
        print succeed
        break
    else:
        print the number is not between 1 and 100

2-11:

#!/usr/bin/env python
print Enter 5 numbers:
s = 0
for i in range(5):
    s += float(raw_input(->))
while True:
    print 1. get the sum of 5 numbers
    print 2. get the avg of 5 numbers
    print X. exit
    i = raw_input()
    if i == 1:
        print s
    elif i == 2:
        print s / 5
    elif i == x or i == X:
        break

 2-12:

(a)键入dir()后出现:

[‘__builtins__‘, ‘__doc__‘, ‘__name__‘, ‘__package__‘]

打印各值的结果如下:

__guiltins__: <module ‘__builtin__‘ (built-in)>

__doc__: None

__name__: __main__

__package__: None

(b)返回信息如下:

<built-in function dir>

此信息表示dir是一个内建函数

(c)type(dir)的结果为:<type ‘builtin_function_or_method‘>

2-14:

print ((-2) * 4) + (3 ** 2)

2-15

(a):

#!/usr/bin/env python
print Please enter 3 numbers:
a = int(raw_input(->))
b = int(raw_input(->))
c = int(raw_input(->))
if a > b:
    a, b = b, a
if a > c:
    a, c = c, a
if b > c:
    b, c = c, b
print a, b, c

(b):

#!/usr/bin/env python
print Please enter 3 numbers:
a = int(raw_input(->))
b = int(raw_input(->))
c = int(raw_input(->))
if a > b:
    a, b = b, a
if a > c:
    a, c = c, a
if b > c:
    b, c = c, b
print c, b, a

 

以上是关于第2章的主要内容,如果未能解决你的问题,请参考以下文章

读《梦断代码》第2章有感

读第一行代码第12章-2.滑动菜单

第2章—装配Bean—通过java代码装配bean

复现经典:《统计学习方法》第 7 章 支持向量机

20172327 2017-2018-2 《第一行代码Android》第一章学习总结

python k近邻算法的示例代码。参考:机器学习在行动第2章。