python 逻辑运算符与比较运算符的差别
文章内容摘自:http://www.cnblogs.com/vamei/archive/2012/05/29/2524376.html
逻辑运算符 and, or, not
比较运算符 ==, !=, >, >=, <, <=, in
False
>>> 0 == False
True
>>> not 0
True
>>> 1 == True
True
>>> 1 == False
False
>>> not 1
False
>>> -1 == True
False
>>> -1 == False
False
>>> not -1
False
------------
Python 处理 -1 很奇怪么?
查阅下面的讨论:
http://stackoverflow.com/questions/7134984/why-does-1-true-but-2-true-in-python
关键在于:
True和False被当作两个整数对象。在进行比较的时候,没有进行类型转换。比如 -1 == True,相当于 -1 == 1.
如果在条件中,比如not -1中,由于not是boolean运算符,所以进行类型装换(not bool(-1)).
这确实是个有些违反直觉的设定。
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:
None
False
zero of any numeric type, for example, 0, 0L, 0.0, 0j.
any empty sequence, for example, '', (), [].
any empty mapping, for example, {}.
instances of user-defined classes, if the class defines a __nonzero__() 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.
python 逻辑运算符与比较运算符的差别的更多相关文章
- Java的算数运算符、关系运算符、逻辑运算符、位运算符
JAVA的运算符,分为四类: 算数运算符.关系运算符.逻辑运算符.位运算符 算数运算符(9):+ - * / % ++ -- 关系运算符(6):== != > >= & ...
- Python教程(2.3)——运算符和类型转换
Python里有很多运算符(operator),这节就让我们来详细学一学. 注意:本文没有特别说明的地方,只考虑bool.int.float三种类型.例如"两边操作数类型相同时,得到的结果为 ...
- python学习笔记之运算符
目录 前言 软件环境 身份运算符 算术运算符 比较运算符 位移运算符 自变运算符 位运算符 逻辑运算符 成员关系运算符 Python真值表 最后 前言 在前面的博文介绍了Python的数据结构之后,接 ...
- python基础之七种运算符
废话不多说,上节说的是数据类型,本篇讲讲数据运算. 在算式"1+2"中,"1"和"2"被称为操作数,"+"被称为运算符 ...
- Python逻辑运算符
逻辑运算符主要用来做逻辑判断,逻辑运算符和比较运算符放一起的,同样用于条件选择和循环. 以下假设变量 a 为 10, b为 20: 示例1: #and是并且,所有的条件都是True,结果才是True: ...
- Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助
Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助 目录 Pychar ...
- Python中的算数运算符
算数运算符 计算机,顾名思义就是负责进行 数学计算 并且 存储计算结果 的电子设备 目标 算术运算符的基本使用 01. 算数运算符 算数运算符是 运算符的一种 是完成基本的算术运算使用的符号,用来处理 ...
- 在python中有多少种运算符?解释一下算术运算符
在python中,我们有7种运算符:算术运算符.关系运算符.赋值运算符.逻辑运算符.位运算符.成员运算符.身份运算符 我们有7个算术运算符,能让我们对数值进行算术计算 1.加号(+),将两个值相加 2 ...
- Python web前端 06 运算符 循环
Python web前端 06 运算符 循环 一.运算符 #JS六大数据类型 #number 数字 #string 字符串 #boolean 布尔型 #function 函数 #undefined 未 ...
随机推荐
- NSUrl 的常见用法
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/s?tn=baiduhome_pg&bs=NSRUL&f=8 ...
- 解决eclipse中出现Resource is out of sync with the file system问题
解决eclipse中出现Resource is out of sync with the file system问题 . 分类: 嵌入式开发平台和环境相关 2011-12-27 16:18 4872人 ...
- Android开发-API指南-设备兼容性
Device Compatibility 英文原文:http://developer.android.com/guide/practices/compatibility.html 采集日期:2014- ...
- Mistral 工作流组件之一 概述
Mistral的前世今生: Mistral是Mirantis公司为Openstack开发的工作流组件,提供Workflow As a Service.典型的应用场景包括任务计划服务Cloud Cro ...
- 使用JAR命令打EAR包
恩,我又得了一个发布应用的活,常常使用JAR命令来打EAR包,所以下面记录一下,以免忘记! 前提条件如下: 1)我的WEB服务器是WebLogic Server (版本是: 10.3.6.0) 2)假 ...
- form表单 无法提交js动态添加的表单元素问题。。
第一种情况, 这种情况js动态添加的表单元素是不能提交到服务器端的 <table> <form method="post" action=" url ...
- Flexbox布局详解
弹性框布局 (flexbox) 添加了级联样式表级别 2 修订版 1 (CSS2.1) 中定义的四个基本布局模式:块布局.内联布局.表格布局和定位布局.使用弹性框布局功能,你可以更加轻松地设计复杂网页 ...
- Sublime 3114 + 转换GBK方法
下载地址: http://files.cnblogs.com/files/wuyifu/Sublime_Text_Build_3114_x64_Setup.zip -– BEGIN LICENSE - ...
- Something wrong with FTK OCR
A case about business secret the suspect took lots of photos and screenshots from BOM, RD papers... ...
- Sunglasses
It's hot this summer. It also reminds me of one case about sunglasses. She was new to this company a ...