一、判断值是否相等
1、使用双等号判断两个数值是否相等,也可用于判断两个字符串是否相等
1==2
>>> 1==2
False
>>>
'a'=='b'
>>> 'a'=='b'
False
>>>
2、在Python中,序列也可以用来比较,如果每一个序列每一个位置的值都相同,那么序列相等
a=[1,2,3]
b=[1,3,2]
c=[1,2,3]
a==b
a==c
>>> a=[1,2,3]
>>> b=[1,3,2]
>>> c=[1,2,3]
>>> a==b
False
>>> a==c
True
>>>
3、字典也可以比较,字典的键与值必须与另一个字典的键与值相等,(顺序可不同)
apple={"red":3,"yellow":4,"green":5}
apple1={"red":3,"yellow":5,"green":5}
apple2={"red":3,"green":5,"yellow":4}
apple==apple1
apple==apple2
>>> apple={"red":3,"yellow":4,"green":5}
>>> apple1={"red":3,"yellow":5,"green":5}
>>> apple2={"red":3,"green":5,"yellow":4}
>>> apple==apple1
False
>>> apple==apple2
True
>>>
4、字母比较大小,大写的"A"是最小的大写字母,依次到"Z",小写的"a" 是最小的小写字母,依次到"z","z"是最大的小写字母,"a" >"Z"
二、if判断:
1、使用if语句判断:判断冰箱里的食材与所需食材的数量,如果大于所需,添加食材到have_ingredients列表;
omelet_contents={"egg":8,"mushroom":5,"pepper":1,"cheese":1,"milk":1,"tomato":5}
fridge_contents={"egg":10,"mushroom":20,"pepper":3,"cheese":2,"tomato":4,"milk":15}
have_ingredients=[False]
if fridge_contents["egg"]>omelet_contents["egg"]:
have_ingredients[0]=True
have_ingredients.append("egg")
print(have_ingredients)
2、使用多个if条件语句中依次判断每样食材,如果每样食材都满足要求,打印出材料列表,并输出我能制作煎蛋卷了(不考虑西红柿);
omelet_contents={"egg":8,"mushroom":5,"pepper":1,"cheese":1,"milk":1,"tomato":5}
fridge_contents={"egg":10,"mushroom":20,"pepper":3,"cheese":2,"tomato":4,"milk":15}
have_ingredients=[False]
#判断鸡蛋是否满足需求
if fridge_contents["egg"]>omelet_contents["egg"]:
have_ingredients[0]=True
have_ingredients.append("egg")
#判断辣椒是否满足需求
if fridge_contents["pepper"]>omelet_contents["pepper"]:
if have_ingredients[0]==False:
have_ingredients[0]=True
have_ingredients.append("pepper")
#判断蘑菇是否满足需求
if fridge_contents["mushroom"]>omelet_contents["mushroom"]:
if have_ingredients[0]==False:
have_ingredients[0]=True
have_ingredients.append("mushroom")
#判断奶酪是否满足需求
if fridge_contents["cheese"]>omelet_contents["cheese"]:
if have_ingredients[0]==False:
have_ingredients[0]=True
have_ingredients.append("pepper")
#判断牛奶是否满足需求
if fridge_contents["milk"]>omelet_contents["milk"]:
if have_ingredients[0]==False:
have_ingredients[0]=True
have_ingredients.append("milk")
#判断是否满足做蛋卷条件
if have_ingredients[0]==True:
print(have_ingredients)
print("I have the ingredients to make an omelet")
3、if-elif-else:创建一个测试链,考虑食材西红柿,如果冰箱里的西红柿不足够做煎蛋卷,那么输出短缺的食材,并打印没有足够的西红柿去做煎蛋卷。
if fridge_contents["tomato"]>omelet_contents["tomato"]:
if have_ingredients[0]==False:
have_ingredients[0]=True
else:
have_ingredients[0]=False
shortage_food=["tomato"]
print(have_ingredients)
print(shortage_food)
print("We don't have the tomatoes to make an omelet")
4、if语句注意事项:
注意冒号的使用,if和冒号之间的语句被判定为真时,if条件才会执行
不要忘记缩进
if -elif-else 语句中,只有if语句不满足条件时,后面的语句才会被执行,而一系列if语句中,所有的if语句都会被执行
三、循环
1、while循环,测试一个条件的真值,因此表示为while……。
while循环:
i=10
while i > 0:
print(i)
i -= 1
2、for循环,for运算,使用一个列表的所有值,因此表示为 for……in……。
for循环:
for i in range(10):
print(i)
3、终止循环:
使用break,终止循环
设计一个程序,用户输入年龄,如果用户输入年龄,则输出年龄,如果用户输入NO,则终止程序
age = 0
while True:
how_old = input("请输入你的年龄:")
if how_old == "no":
print("Don't be ashamed of your age!") #print需在break语句之前执行,否则将不执行
break
num=int(how_old) #将输出转化为字符型
age = num
print("Your age is %d"% age)
break语句只会跳出最近一层的循环,如有一个while循环,包含一个缩进的for循环,那么for循环中的break不会终止while循环。
4、for和while循环中使用else语句
再循环不是由break 语句退出的情况下执行
for food in ("apple","pear","peach","orange"):
if food == 'peach':
break
else:
print("There is no peach.")
for food in ("apple","pear","orange"):
if food == 'peach':
break
else:
print("There is no peach.")
5、用continue继续循环
不退出循环,而是跳过当前循环剩余的部分,
for food in ("apple","pear","peach","orange"):
if food == 'peach':
continue
print(food)