逻辑运算符:
" > < >= <= == "
or(或):你和你媳妇最少得一个人去才能办理成功
#encoding:utf-8
you = raw_input("你去不去?")
yourwife = raw_input("你媳妇去不去?")
if you == "去" or yourwife == "去":
print("可以办理成功")
else:
print("不能办理")
and(并且): 你和你媳妇必须都去才能办理成功
#encoding:utf-8
you = raw_input("你去不去?")
yourwife = raw_input("你媳妇去不去?")
if you == "去" and yourwife == "去":
print("可以办理成功")
else:
print("不能办理")
不在0-50之间
a = 30
if not (a>0 and a<500):
print("在0-50之间")
条件elif
if 条件1:
xxxxx
elif 条件2:
xxxxx
elif 条件3:
xxxxx
.......
#encoding:utf-8
sex = raw_input("请输入你的性别:")
if sex == "男":
print("你是男生")
elif sex == "女":
print("你是女生")
elif sex == "中":
print("你是人妖")
else:
print("你不是地球人")
程序的执行流程
while 循环
while 条件:
条件满足的时候执行
打印1-40
num = 1
while(num <= 40):
print(num)
num = num + 1
注意:python里没有 do while,没有++,没有--,没有switch
嵌套:
if嵌套
if xxx:
print(“俄乌鸡汤hrci”)
if xxx2:
xxxxxx
#encoding:utf-8
ticket = input("请输入车票:")
knifeLenght = 8
if ticket ==1:
print("有车票,请进入火车站,接受安检")
if knifeLenght <= 10:
print("通过安检进入候车室")
else:
print("你安检没通过")
else:
print("你没有票,请去买票")
#encoding:utf-8
sex = raw_input("请输入你的性别:")
if sex == "男" :
color = raw_input("你白吗?")
money = input("输入你的财产总额:")
beautiful = raw_input("你美吗?")
if color == "白" and money > 100000 and beautiful == "美":
print("这个妹子是个白富美")
else:
print("矮矬穷")
else:
print("判断高富帅在下面")
while 嵌套
while 条件:
条件满足做的事情1
条件满足做的事情2
.....
while 条件2:
条件2满足时候做的事情
打印矩形星花:
i = 1
while i<=5:
j = 1
while j <= 5:
print("*"),
j = j + 1
print("")
i = i + 1
打印结果:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
打印三角形
i = 1
while i<=5:
j = 1
while j <= i:
print("*"),
j = j + 1
print("")
i = i + 1
*
* *
* * *
* * * *
* * * * *
打印99乘法表
i = 1
while i<=9:
j = 1
while j <= i:
print("%d*%d=%d/t"%(i,j,i*j)),
j = j + 1
print("")
i = i + 1
打印结果:
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
剪刀石头布
#encoding:utf-8
import random
player = input("输入剪刀0,石头1,布2:")
computer = random.randint(0,2)
if( player ==0 and computer == 2) or(player == 1 and computer ==0) or (player ==2 and computer ==1):
print("你赢了")
elif player==computer:
print("平局了")
else:
print("输了")
for循环
break
当程序遇到break,立即跳出程序
例如:打印1-100之间的前20个偶数
i = 1
num = 0
while i<=100:
if i%2 == 0:
print(i)
num+=1
if num == 20:
break
i+=1
continue:跳出本循环