python中的真假值:

Truth Value Testing

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

1.None

2.False

3.zero of any numeric type, for example, 0, 0.0, 0j.

4.any empty sequence, for example, '', (), [].

5.any empty mapping, for example, {}.

6.instances of user-defined classes, if the class defines a __bool__() or __len__() method, when that method returns the integer zero or bool value False. [1]

All other values are considered true — so objects of many types are always true.

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

总结:

#在python中,任何对象都可以判断其真假值:True,False
#在if或while条件判断中,下面的情况值为False:
#
# 1.None
# 2.Flase
# 3.数值为0的情况,如:0,0.0,0j
# 4.所有空序列,如:'',(),[]
# 5.所有空mapping,如:{}
# 6.instances of user-defined classes, if the class defines a __bool__() or __len__() method,
# when that method returns the integer zero or bool value False.
#
# All other values are considered true — so objects of many types are always true.
#
#
# 在运算操作和内建函数返回Boolean结果0或者Flase表示false
#1或True表示true
#

python中的Boolean运算如下:

Boolean Operations

 print('x or y -> if x is false,then y, else x ')
x, y = 2, 0
print('{} or {} = {}'.format(x, y, x or y))
x1, y1 = 0, 10
print('{} or {} = {}'.format(x1, y1, x1 or y1))
x2, y2 = 0, 0
print('{} or {} = {}'.format(x2, y2, x2 or y2)) print('#' * 50)
print('x and y -> if x is false,then x, else y ')
print('{} and {} = {}'.format(x, y, x and y))
x1, y1 = 0, 10
print('{} and {} = {}'.format(x1, y1, x1 and y1))
x2, y2 = 0, 0
print('{} and {} = {}'.format(x2, y2, x2 and y2)) print('#' * 50)
print('not x -> if x is false,then True,else False ')
x = 2
print('not {} = {}'.format(x, not x))
x = 0
print('not {} = {}'.format(x, not x))

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
x or y -> if x is false,then y, else x
2 or 0 = 2
0 or 10 = 10
0 or 0 = 0
##################################################
x and y -> if x is false,then x, else y
2 and 0 = 0
0 and 10 = 0
0 and 0 = 0
##################################################
not x -> if x is false,then True,else False
not 2 = False
not 0 = True
>>>

最新文章

  1. CDN模式介绍
  2. Three.js外部模型加载
  3. HDU 4635:Strongly connected(强连通)
  4. 共享内存 最快IPC 的原因
  5. KMP--路过
  6. JtextField的延时更新
  7. Iterator pattern(c++实现)
  8. img src某个php文件输出图片(回复更改图片readfile读取图片等)
  9. [Gradle系列]Gradle打包apk多版本,多渠道,多环境,多功能,多模块随心所欲
  10. bug日记之-------java.io.IOException: Server returned HTTP response code: 400 for URL
  11. DNS实战--2
  12. linux上安装完torch后仍报错:ImportError: No module named torch
  13. QWidget-控件层级关系
  14. HTML/CSS基础知识(四)
  15. “玲珑杯”ACM比赛 Round #18---图论你先敲完模板(DP+思维)
  16. typescript泛型(学习笔记非干货)
  17. [CTCI] 下一个较大元素
  18. Closest Common Ancestors---poj1470(LCA+离线算法)
  19. Hadoop源码学习笔记(4) ——Socket到RPC调用
  20. 创建Qt项目

热门文章

  1. C - A New Function (整除分块 + 玄学优化)
  2. C#串口serialPort操作
  3. P1084 疫情控制
  4. python类的继承和多态
  5. 使用Nginx代理Django
  6. python discover 函数介绍
  7. Python 3.6安装教程
  8. CentOS6.9下安装MariaDB10.2.11
  9. a:hover伪类在ios移动端浏览器内无效的解决方法
  10. numpy基础代码操练