【Python爬虫】-笨办法学 Python 习题27-34

EX27

代码:

#ex27练习,布尔逻辑表达式,我用笔在本子上书写记录了下来。

#and中的运算,有错就错,全对才对

#or中的运算,有对就对,全错才错

现在使用这些表格创建你自己的卡片,再花一个星期慢慢记住它们。记住一点,这本书不会要求你成功或者失败,只要每天尽力去学,在尽力的基础上多花一点功夫就可以了

我自己用笔记下来了,感觉理解难度还可以,有诀窍。

EX28

代码:

#ex28的练习

True
False
False
True
True
True
False
True
False
False
True
False
True
False
False
False
True
True
False
False

加分习题

  1. Python 里还有很多和 != 、 == 类似的操作符 . 试着尽可能多地列出 Python 中的等价运算符。
    例如 < 或者 <= 就是。
  2. 写出每一个等价运算符的名称。例如 != 叫 “ not equal (不等于)”。
  3. 在 python 中测试新的布尔操作。在敲回车前你需要喊出它的结果。不要思考,凭自己的第一感就可以了。把表达式和结果用笔写下来再敲回车,最后看自己做对多少,做错多少。
  4. 把习题 3 那张纸丢掉,以后你不再需要查询它了。

这章的练习有点懵,不清楚做这个到底是干嘛的,跟着练习吧。

ex29

代码:

#ex29的练习

people = 20
cats = 30
dogs = 15

if people < cats:
    print("Too many cats! The world is doomed!")

if people > cats:
    print("Not many cats! The world is saved!")

if people < dogs:
    print("The world is drooled on")

if people > dogs:
    print("The world is dry!")


dogs += 5

if people >= dogs:
    print("People are greater than or equal to dogs.")

if people <= dogs:
    print("People are less than or equal to dogs.")

if people == dogs:
    print("People are dogs.")
加分习题

猜猜“ if 语句”是什么,它有什么用处。在做下一道习题前,试着用自己的话回答下面的问题 :

  1. 你认为 if 对于它下一行的代码做了什么? \ 判断
  2. 为什么 if 语句的下一行需要 4 个空格的缩进?\ python的统一格式,只要统一的空格数就可以了。
  3. 如果不缩进,会发生什么事情?
  4. 把习题 27 中的其它布尔表达式放到 if 语句 中会不会也可以运行呢?试一下。
  5. 如果把变量 people, cats, 和 dogs 的初始值改掉,会发生什么事情? \ 其实是没有影响的,他们是变量,只是比较后面的数字大小。

+= 是什么意思?
x += 1 和 x = x + 1 一样,只不过可以少打几个字母.这句话蛮重要。

ex30

代码

# ex30练习的代码

people = 30#变量
cars = 40
buses = 15

if cars > people:#如果汽车的数量大于人们的数量
    print("We should take the cars.")#输出我们应该乘坐汽车
elif cas < people:#如果汽车的数量,小于人们的数量
    print("We should not take the cars.")#输出我们不应该乘坐汽车
else:
    print("We can't decide.")

if buses > cars:
    print("That's too many buses.")
elif buses < cars:
    print("Maybe we could take the buses.")
else:
    print("We still can't decide.")

if people > buses:
    print("Alright, let's just take the buses.")
else:
    print("Fine, let's stay home then")

总结:
如果多个 elif 区块都是 True 是 python 会如何处理?
Python 只会运行它碰到的是 True 的第一个区块,所以只有第一个为 True 的区块会被运行。

ex31
#ex31的代码练习

print("You enter a dark room with two doors. Do you go through door #1 or #2?")

door = input("> ")#b百度了下,input的用法在3.0以上的版本,使用方法改变了

if door == "1":
    print("There's a giant bear here eating a cheese cake. What do you do?")
    print("1. Take the cake.")
    print("2. Scream at the bear.")

    bear = input("> ")

    if bear == "1":
        print("The bear eats your face off. Good job!")
    elif bear == "2":
        print("The bear eats your legs off. Good job!")
    else:
        print("Well, doing %s is probably better. Bear runs away." % bear)

elif door == "2":
    print("You stare into the endless abyss at Cthulhu's tetina.")
    print("1. Blueberries.")
    print("2. Yellow jacket clothespins.")
    print("3. Understanding revolvers yelling melodies.")

    insanity = input("> ")

    if insanity == "1" or insanity == "2":
        print("Your body survives powered by a mind of jello. Good job!")
    else:
        print("The insanity rots your eyes into a poor of muck. Good job!")

else:
    print("You stumble around and fall on a knife and die. Good job!")

总结:#b百度了下,input的用法在3.0以上的版本,使用方法改变了
另外一个,我的代码运行的结果,我不确定正确与否,虽然没有报错。

ex32

代码

#ex32的代码练习

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pear', 'apricots'] #水果 = 苹果 橘子 梨子 杏仁
change = [1, 'pennies', 2, 'dimes']#变化 = 1,便士,2,10美分

# this first kind of for-loop goes through a list
for number in the_count:
   print("This is count %d" % number)

# same as above   #t同上
for fruit in fruits:
   print("A fruit of type: %s" % fruit)

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:#  for XX  in  YY  ,这个格式很重要
   print("I got %r" % i)

# We can also build lists, first start with an empty one  #我们还可以创建列表,首先从空列表开始。
elements = []

# then use the range fuction to do 0 to 5 counts
for i in range(0, 6):
   print("Adding %d to the list." % i)
   # append is a funcation that lists understand #添加一个功能列表
   elements.append(i)

# now we can print them out too
for i in elements:
   print("Element was: %d" % i)

运行结果

"C:\Program Files\Python36\python.exe" D:/小克学习/python/项目/ex32.py
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pear
A fruit of type: apricots
I got 1
I got 'pennies'
I got 2
I got 'dimes'
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

Process finished with exit code 0

加分习题

  1. 注意一下 range 的用法。查一下 range 函数并理解它。

\ 函数原型:range(start, end, scan):

参数含义:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);

end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5

scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
示例

range(5) #代表从0到5(不包含5)
[0, 1, 2, 3, 4]
range(0,5) #代表从0到5(不包含5)
[0, 1, 2, 3, 4]
range(1,5) #代表从1到5(不包含5)
[1, 2, 3, 4]
range(1,5,2) #代表从1到5,间隔2(不包含5)
[1, 3]

  1. 在第 22 行,你可以可以直接将 elements 赋值为 range(0,6) ,而无需使用 for 循环?
  2. 在 Python 文档中找到关于列表的内容,仔细阅读以下,除了 append 以外列表还支持哪些操作?
ex33

代码

#ex33 while循环的练习

i = 0
numbers = []

while i < 6:
    print("At the top i is %d" % i)
    numbers.append(i)

    i = i + 1
    print("Numbers now:", numbers)
    print("At the bottom i is %d" % i)


print("The numbers:")

for num in numbers:
    print(num)

运行结果

"C:\Program Files\Python36\python.exe" D:/小克学习/python/项目/wx33.py
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
0
1
2
3
4
5

Process finished with exit code 0

常见问题回答

for-loop 和 while-loop 有何不同?
for-loop 只能对一些东西的集合进行循环, while-loop 可以对任何对象进行驯化。
然而, while-loop 比起来更难弄对,而一般的任务用 for-loop 更容易一些。
循环好难理解啊,我该怎样理解?

觉得循环不好理解,很大程度上是因为不会顺着代码的运行方式去理解代码。当循环开始时,它会运行整个区块,区块结束后回到开始的循环语句。如果想把整个过程视觉化,你可以在循环的各处塞入 print 语句,用来追踪变量的变化过程。你可以在循环之前、循环的第一句、循环中间、以及循环结尾都放一些 print 语句,研究最后的输出,并试着理解循环的工作过程。

总结:

有点朦胧,这个时候多写,总有一天会豁然开朗的。

ex34

代码

# ex34 访问列表的元素的练习

animals = ['bear', 'python', 'peacock', 'kangaroo', 'whale', 'platypus']

The animals at 1 and is the 2nd animals and is python
The animals at 2 and is the 3rd animals and is peacock
The animals at 0 and is the 1st animals and is bear
The animals at 3 and is the 4th animals and is kangaroo
The animals at 4 and is the 5th animals and is whale
The animals at 2 and is the 3rd animals and is peacock
The animals at 5 and is the 6th animals and is platypus
The animals at 4 and is the 5th animals and is whale
总结:

这几天事情比较多,没及时完成作业,现在总算补全了,松了口气。

训练量一直有,如果拉下很多,以后更不好补,可能就放弃了,以后尽量不要补作业了。

你可能感兴趣的:(【Python爬虫】-笨办法学 Python 习题27-34)