zoukankan      html  css  js  c++  java
  • Python学习笔记三:逻辑操作符

    Python的逻辑操作有三种:and、or、not。分别对应与、或、非。
    举例:

    Python的逻辑操作有三种:and、or、not。分别对应与、或、非。
    举例:

    1 #coding:utf-8
    2  test1 = 12
    3 test2 = 0
    4  print (test1 > test2) and (test1 > 14) #result = False
    5  print (test1 < test2) or (test1 > -1) #result = True
    6  print (not test1) #result = False
    7  print (not test2) #result = True
    严格的说,逻辑操作符的操作数应该为布尔表达式。但Python对此处理的比较灵活。
    即使操作数是数字,解释器也把他们当成“表达式”。
    非0的数字的布尔值为1,0的布尔值为0.
    举例:
    1 #coding:utf-8
    2  test1 = 12
    3 test2 = 0
    4  print (test1 and test2) #result = 0
    5  print (test1 or test2) #result = 12
    6  print (not test1) #result = Flase
    7  print (not test2) #reslut = True
    在Python中,空字符串为假,非空字符串为真。非零的数为真。
    数字和字符串之间、字符串之间的逻辑操作规律是:
    对于and操作符:
    只要左边的表达式为真,整个表达式返回的值是右边表达式的值,否则,返回左边表达式的值
    对于or操作符:
    只要两边的表达式为真,整个表达式的结果是左边表达式的值。
    如果是一真一假,返回真值表达式的值
    如果两个都是假,比如空值和0,返回的是右边的值。(空值或0)
    举例:
    1 #coding:utf-8
    2  test1 = 12
    3 test2 = 0
    4 test3 = ''
    5 test4 = "First"
    6  print test1 and test3 #result = ''
    7  print test3 and test1 #result = ''
    8  print test1 and test4 #result = "First"
    9  print test4 and test1 #result = 12
    10  print test1 or test2 #result = 12
    11  print test1 or test3 #result = 12
    12  print test3 or test4 #result = "First"
    13  print test2 or test4 #result = "First"
    14  print test1 or test4 #result = 12
    15  print test4 or test1 #result = "First"
    16  print test2 or test3 #result = ''
    17  print test3 or test2 #result = 0

    {网购拿返利,购物新选择}

  • 相关阅读:
    高可用性机制
    Moodle课程资源系统安装
    Windows 10 安装 chocolatey
    centos7安装samba服务器
    抽签网页板代码
    CentOS7系统操作httpd服务
    centos7.2下放行端口
    centos7没有netstat命令的解决办法
    Linux
    Linux下常用服务的端口号超详细整理
  • 原文地址:https://www.cnblogs.com/dabiao/p/1681223.html
Copyright © 2011-2022 走看看