python中逻辑运算符例子(Python运算符之逻辑运算符)
python中逻辑运算符例子(Python运算符之逻辑运算符)或orand与a and b
python逻辑运算与数学中的逻辑运算一样。逻辑运算符可以操作任何类型的表达式 同时结果也可以是任意数据类型。
| 
     运算符  | 
     描述  | 
     用法举例  | 
| 
     and  | 
     与  | 
     a and b  | 
| 
     or  | 
     或  | 
     a or b  | 
| 
     not  | 
     非  | 
     not a  | 
Python逻辑运算规则如下:
and运算符:两边值都为真结果才为真,否则为假。
- 当左边表达式为假,则不计算右边表达式,结果为假;
 - 若左边表达式为真,右边表达式为假,则结果为假;
 - 若左右两边的结果都为真,则结果为真。
 
or运算符:两边值,一个为真结果为真,否则结果为假。
- 当左边表达式为真,则不计算右边表达式,结果为真;
 - 若左边表达式为假,右边表达式为真,则结果为真;
 - 若左右两边结果都为假,则结果为假。
 
not运算符:not运算符相当于取反。
- 若表达式为真,结果为假;
 - 若表达式为假,结果为真。
 
代码示例:
print("简单示例分割线------------")
print(100 and 200)
print(100 and 0)
print("" or "null")
print(not 1)
print("AND 示例分割线------------")
#输入年龄判断年龄是否在18到35之间。
age = int(input("年龄"))
if age>=18 and age<=35:
    print("恭喜年龄符合条件")
else:
    print("其他")
print("or 示例分割线------------")
#遍历list列表,输出1,7
list1 = [1 2 3 4 5 6 7]
for i in list1:
    if i==1 or i==7:
        print(i)
print("NOT 示例分割线------------")
#遍历list列表,输出非1的值
for i in list1:
    if  not i==1:
        print(i)
    

#头条群星8月榜#




