python之if条件

1.if条件循环,注意缩进

1 # if条件
2 a = {"Alice":12}
3 if a.get("Alice") >= 18:
4     print("恭喜你,成年了")#注意此处缩进
5 else:
6     print("未成年")

输出结果

未成年

2.and:条件同时满足

1 #and
2 age = int(input("plase input your age:"))
3 grade = int(input("plase input your grade(0-700):"))
4 if age >=18 and age <= 40 and grade >=500:
5     print("you have been accepted") #注意此处缩进
6 else:
7     print("we catn't admit you")

输出结果

plase input your age:10
plase input your grade(0-700):600
we catn't admit you

3.or满足其中一个条件即可

1 # or
2 age = int(input("plase input your age:"))
3 grade = int(input("plase input your grade(0-700):"))
4 if age >=18 or grade >=500:
5     print("you can play games") #注意此处缩进
6 else:
7     print("we catn't play games")

输出结果

plase input your age:18
plase input your grade(0-700):100
you can play games

4.not取反

1 # not
2 age = int(input("plase input your age:"))
3 if not age < 18 :
4     print("you can play games") #注意此处缩进
5 else:
6     print("we catn't play games")

输出结果

plase input your age:15
we catn't play games

5.if多条件

 1 # if多条件
 2 number1 = int(input("plase input a number:"))
 3 if number1 >= 10 :
 4     print(number1," >= 10") #注意此处缩进
 5 elif number1 >= 5:
 6     print("5<=",number1," < 10")
 7 elif number1 >=1:
 8     print("1<=",number1," < 5")
 9 else:
10     print(number1,"<=0")

输出结果

plase input a number:-1
-1 <=0
原文地址:https://www.cnblogs.com/zhangyating/p/8287213.html