## 笨方法学python IV 顶原

渺渺星辰

27.笨办法学 Python-第二十七题

```#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第二十七题
#逻辑运算关系
print '''

and  与
or   或
not  非
!=   不等于
==   等于
>=   大于等于
<=   小于等于
True 真
False 假
'''

print '真值表:'
print '''

NOT        True?

not False  True
not True   False

OR             True?

True or False  True
True or True   True
False or True  True
False or False False

AND             True?

True and False  False
True and True   True
False and True  False
False and False False

NOT OR               True?

not (True or False)  False
not (True or True)   False
not (False or True)  False
not (False or False) True

NOT AND               True?

not (True and False)  True
not (True and True)   False
not (False and True)  True
not (False and False) True

!=     True?

1 != 0 True
1 != 1 False
0 != 1 True
0 != 0 False

==     True?

1 == 0 False
1 == 1 True
0 == 1 False
0 == 0 True
'''```

and  与
or   或
not  非
!=   不等于
==   等于
>=   大于等于
<=   小于等于
True 真
False 假

NOT        True?

not False  True
not True   False

OR             True?

True or False  True
True or True   True
False or True  True
False or False False

AND             True?

True and False  False
True and True   True
False and True  False
False and False False

NOT OR               True?

not (True or False)  False
not (True or True)   False
not (False or True)  False
not (False or False) True

NOT AND               True?

not (True and False)  True
not (True and True)   False
not (False and True)  True
not (False and False) True

!=     True?

1 != 0 True
1 != 1 False
0 != 1 True
0 != 0 False

==     True?

1 == 0 False
1 == 1 True
0 == 1 False
0 == 0 True

28.笨办法学 Python-第二十八题

```#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第二十八题
#计算逻辑运算式的结果
print True and True    #True
print False and True   #False
print 1==1 and 2==1    #False
print 'test' == 'test' #True
print 1==1 or 2!=1     #True
print True and 1==1    #True
print False and 0 != 0 #False
print True or 1 == 1   #True
print "test" == "testing" #False
print 1 != 0 and 2 == 1   #False
print "test" != "testing" #True
print "test" == 1         #False
print not (True and False) #True
print not (1 == 1 and 0 != 1) #False
print not (10 == 1 or 1000 == 1000) #False
print not (1 != 10 or 3 == 4) #False
print not ("testing" == "testing" and "Zed" == "Cool Guy") #True
print 1 == 1 and not ("testing" == 1 or 1 == 0)  #True
print 3 == 3 and not ("testing" == "testing" or "Python" == "Fun") #False```

1. 找到相等判断的部分 (== or !=),将其改写为其最终值 (True 或 False)。
2. 找到括号里的 and/or,先算出它们的值。
3. 找到每一个 not,算出他们反过来的值。
4. 找到剩下的 and/or,解出它们的值。
5. 等你都做完后,剩下的结果应该就是 True 或者 False 了。

29.笨办法学 Python-第二十九题

```#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第二十九题
#条件判断语句if
people=20
cats=30
dogs=15

if people < cats:
print 'too many cats!too little people'
if people > cats:
print 'too many people,too little cats'
if people < dogs:
print 'the world is drooled on'
if people > dogs:
print 'the world is dry'

dogs+=5

if people >dogs:
print 'people > dogs'
if people <dogs:
print 'people <dogs'
if people == dogs:
print 'people are dogs'```

too many cats!too little people
the world is dry
people are dogs

30.笨办法学 Python-第三十题

```#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第三十题
'''

from sys import argv
script,people,cars,buses=argv #注意需要添加script
'''
#方法二
people=int(raw_input("please input the number of people:"))
cars=int(raw_input("please input the number of cars:"))
buses=int(raw_input("please input the numbers of buses:"))
if people < cars:
print "people < cars"
elif people >cars:
print 'people > cars'
else:
print 'people = cars'

if buses >cars:
print 'buses > cars'
elif buses < cars:
print 'buses < cars'
else:
print 'buses =cars'```

please input the number of people:20
please input the number of cars:30
please input the numbers of buses:40
people < cats
buses > cars

31.笨办法学 Python-第三十一题

```#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第三十一题

print 'you enter a dark room with two door. do you go through door #1 or door #2'
door=raw_input(">")
#选择进入第一扇门
if door=='1':
print 'there is a giant bear hear here eating a cheese cake.what do you do?'
print '1.take the cake '
print '2 scream at the bear'
#今入第一扇门后的选择
bear=raw_input('>')
#选择1
if bear=='1':
print 'the bear eats your face off.good job'
#选择2
elif bear=='2':
print  'the brar 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 Cthuhu is retina'
print '1.bluebarries'
print '2 yellow jacket clothspins'
print '3.understanding revolvers yelling melodies'
insanity=raw_input('>')
#选择1或者2:
if insanity=='1' or insanity=='2':
#选择3或者是其他
else:
print 'the insanity rots your eyes into a poll of muck.good jobs'
else:
print 'you stumble around and fall on a knief and die.good job!'```

you enter a dark room with two door. do you go through door #1 or door #2
>2
you stare into the endless abyss at Cthuhu is retina
1.bluebarries
2 yellow jacket clothspins
3.understanding revolvers yelling melodies
>1

32.笨办法学 Python-第三十二题

```#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第三十二题
#列表的定义和循环语句的使用
count=[1,2,3,4,5]
fruits=['apple','oranges','pears']
change=[1,'pennies',2,'dimes',3,'quarters']
#遍历count列表
for num in count:
print 'the number is %r' %num

#遍历水果
for fruit in fruits:
print 'the fruit is %s' %fruit

#遍历change
for i in change:
print i

#列表的添加元素
for ii in range(6,11):
count.append(ii)
#列表删除最后一个元素
count.pop()
#列表删除一个元素(该元素在列表中):
count.remove(5)
#查看列表元素:
for nums in count:
print nums```

1) list.append(obj)      在列表末尾添加新的对象

2) list.count(obj)       统计某个元素在列表中出现的次数

3) list.extend(seq)      在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）

4) list.index(obj)       从列表中找出某个值第一个匹配项的索引位置

5) list.insert(index, obj)   将对象插入列表

6) list.pop(obj=list[-1])    移除列表中的一个元素（默认最后一个元素），并且返回该元素的值

7) list.remove(obj)          移除列表中某个值的第一个匹配项

8) list.reverse()            反向列表中元素

9) list.sort([func])         对原列表进行排序

the number is 1
the number is 2
the number is 3
the number is 4
the number is 5
the fruit is apple
the fruit is oranges
the fruit is pears
1
pennies
2
dimes
3
quarters
1
2
3
4
6
7
8
9

33.笨办法学 Python-第三十三题

```#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第三十三题
#while 循环的使用
i=0
numbers=[]
while i<6:
print 'at the top i is %d' %i
numbers.append(i)
i+=1
print 'numbers now: ',numbers
print 'at the bottom i is %d' %i+'\n'

print 'the numbers:'
for num in numbers:
print num```

While 循环有一个问题,那就是有时它会永不结束了,避免这样的问题,需要遵循下面的规定:
1). 尽量少用 while-loop,大部分时候 for-loop 是更好的选择。
2). 重复检查你的 while 语句,确定你测试的布尔表达式最终会变成 False 。
3). 如果不确定,就在 while-loop 的结尾打印出你要测试的值。看看它的变化。

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

34.笨办法学 Python-第三十四题

```#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第三十四题
#列表切片的使用和相应的取值
animals = ['bear', 'python', 'peacock', 'kangaroo', 'whale', 'platypus']

print 'the animal at 1 is %s' %animals[1]
print 'the 3rd animal is %s' %animals[2]
print 'the 1st animal is %s' %animals[0]
print 'the animal at 3 is %s' %animals[3]
print 'the 5th animal is %s' %animals[4]
print 'the animal at 2 is %s' %animals[2]
print 'the 6th animal is %s' %animals[5]
print 'the animal at 4 is %s' %animals[4]

print '列表倒数第一个: %s' %animals[-1]

print '列表第二到第四个:%s' %animals[1:4]```

list切片操作:

1)列表名[n,n1]  ----->  取出下标从n到(n1-1)的值 (包含起始索引,不包含结束索引)

2)list[0,3]<===>list[:3]    ------>取出前两个值

3)list[:] ---->取出所有的值

4)list[::n]------>每隔N个,取出一个值

5)倒序切片:倒数第一个元素的索引是-1,倒序切片包含起始索引,不包含结束索引!

the animal at 1 is python
the 3rd animal is peacock
the 1st animal is bear
the animal at 3 is kangaroo
the 5th animal is whale
the animal at 2 is peacock
the 6th animal is platypus
the animal at 4 is whale

35.笨办法学 Python-第三十五题

```#!/usr/bin/python
#coding=utf-8
#笨办法学 Python-第三十五题
from sys import exit
def gold_room():
print 'the rom is full of gold.how much do you take?'
next=raw_input('>')
if '0' in next or 'i' in next:
how_much=int(next)
else:
if how_much <50:
print "nice,you are not greedy,you win"
exit(0)
else:

def bear_room():
print 'there is a bear here.'
print 'the bear has a bunch of honey,熊有一堆蜂蜜'
print 'the fat bear is in front of another door,胖熊在另一个门前'
print 'you can choose take honey or taunt bear or taunt bear twice or open door '
bear_moved=False
while True:
next=raw_input('>')
if next =="take honey":
dead('the bear looks at you then slaps you face off,熊看着你然后拍打你的脸')
elif next=='taunt bear' and not bear_moved:
print 'the bear has moved from the door,you can go through it now'
bear_moved=True
elif next=='taunt bear twice' and  bear_moved:
elif next=='open door' and bear_moved:
gold_room()
else:
print 'i got no idea what that mean'

def cthulhu_room():
print 'here you see the great evil cthulhu,这里你看到伟大的邪恶的恶魔'
print 'he,it,whatever stares at you and you go insane,他把你盯毛了'
next=raw_input(">")
if 'flee' in next:
start()
else:
cthulhu_room()

print why,'good job'
exit(0)

def start():
print 'you are in dark room'
print 'there is a door to your right and left'
print 'which one do you take?'

next=raw_input('>')
if next=="left":
bear_room()
elif next=="right":
cthulhu_room()
else:
dead('you stumble around the romm until you starve,你跌倒在房间里直到你饿死')

start()```

### 渺渺星辰

20 本优秀的 Python 电子书

oschina
2012/12/30
11.4K
19

2015/06/29
445
2
python-35:urllib 和 urllib2 模块

2015/12/01
336
0

2018/05/08
0
0
[深度基础]·小白如何快速入门深度学习

[深度基础]·小白如何快速入门深度学习 个人主页--> https://xiaosongshine.github.io/ 随着近年深度学习的兴起，很多研究者都投入这个领域当中，由于各个大学都将自己的课程放到了网上，出现...

03/26
0
0

15分钟前
1
0
【2019年8月版本】OCP 071认证考试最新版本的考试原题-第6题

choose three Which three statements are true about indexes and their administration in an Orade database? A) An INVISIBLE index is not maintained when Data Manipulation Language......

oschina_5359
18分钟前
1
0

23分钟前
1
0

Linux磁盘管理好坏直接关系到整个系统的性能问题，Linux磁盘管理常用三个命令为df、du和fdisk。 df df命令参数功能：检查文件系统的磁盘空间占用情况。可以利用该命令来获取硬盘被占用了多少...

xiangyunyan
26分钟前
3
0
js 让textarea的高度自适应父元素的高度

textarea按照普通元素设置height是没有作用的,可以这么来设置, 下面给上一段项目代码 JS代码: \$.fn.extend({ txtaAutoHeight: function () { return this.each(function () {...

26分钟前
1
0