is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.

>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True
>>> b == a
True
>>> b = a[:]
>>> b is a
False
>>> b == a
True

In your case, the second test only works because Python caches small integer objects, which is an implementation detail. For larger integers, this does not work:

>>> 1000 is 10**3
False
>>> 1000 == 10**3
True

The same holds true for string literals:

>>> "a" is "a"
True
>>> "aa" is "a" * 2
True
>>> x = "a"
>>> "aa" is x * 2
False
>>> "aa" is intern(x*2)
True

Is there a difference between `==` and `is` in Python?的更多相关文章

  1. Is there a difference between `==` and `is` in Python?

    There is a simple rule of thumb to tell you when to use == or is. == is for value equality. Use it w ...

  2. 【LeetCode】389. Find the Difference 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:异或 方法三:排序 日 ...

  3. python运维开发坎坷之路-01

    前言 2014年9月,新疆乌鲁木齐,在51CTO学院看着alex老师的python教学视频,不得不说这是我第一次接触python这门高级语言,从最开始的一无所知到现在能够用python写脚本,再到未来 ...

  4. Do we need other languages other than C and C++?

    There were hundreds of or thousands of programming languages created since the invention of computer ...

  5. RColorBrewer的使用

    RColorBrewer是一个R包,使用http://colorbrewer2.org/这个网站提供的颜色.我们画一个包括八个box的boxplot时,或者在x-y散点图上画六条线时,该怎样选择颜色呢 ...

  6. Django Model field reference

    ===================== Model field reference ===================== .. module:: django.db.models.field ...

  7. python的面试问题

    WHAT 1. 什么是Python? Python是一种编程语言,它有对象.模块.线程.异常处理和自动内存管理.可以加入与其他语言的对比.下面是回答这一问题的几个关键点: a. Python是一种解释 ...

  8. swagger 自动生成接口测试用例

    ---整体更新一波--- 1.实际工作中,因为要动手输入的地方比较多,自动生成的异常接口用例感觉用处不大,就先去掉了,只保留了正常的: 2.接口有改动的,如果开发人员没有及时告知或没有详细告知,会增加 ...

  9. Python面试题整理-更新中

    几个链接: 编程零基础应当如何开始学习 Python ? - 路人甲的回答 网易云课堂上有哪些值得推荐的 Python 教程? - 路人甲的回答 怎么用最短时间高效而踏实地学习 Python? - 路 ...

随机推荐

  1. C#使用CUDA

    随着信息处理的爆炸增长,传统使用CPU计算已经无法满足计算作业增长的需求,GPU的出现为批量作业提供了新的契机.GPU计算拥有很类库,比如CUDA.OpenCL等,但是可以发现CUDA是其中相对比较成 ...

  2. windows7-tomcat配置

    1.下载 2.解压缩 3.配置环境变量 (1)计算机属性--高级系统配置--高级--环境变量--系统变量--新建 (2)CATALINA_HOME 如:C:\apache-tomcat-7.0.73 ...

  3. manjaro 给笔记本安装18.1系统时发现中文变成了方块字

    解决方案: 1.连接网络,wifi/本地连接 2.打开终端 3.同步数据 如果是第一次,我们需要先同步本地数据包,先输入 sudo pacman -Syy 4.安装字体 sudo pacman -S ...

  4. Java High Level REST Client 之 创建索引

    1. 创建索引请求 CreateIndexRequest request = new CreateIndexRequest("twitter"); 2.设置 2.1 分别设置 2. ...

  5. JavaScript 真值和假值

    常见的假值有 值 说明 var a=false;  值为假 var a =0;  值为0 var a='';  值为空 var a=10/'abc' 算式错误 var a; 未赋值变量 常见的真值有 ...

  6. C#进阶系列—WebApi

    参考学习优秀博客地址:http://www.cnblogs.com/landeanfen/p/5177176.html

  7. 如何获得select被选中option的value和text和......

    我想获取select选中的value,或者text,或者…… 比如这个: <select id="select"> <option value="A&q ...

  8. Design Excel Sum Formula

    Your task is to design the basic function of Excel and implement the function of sum formula. Specif ...

  9. 打印 request 请求中的参数

    @SuppressWarnings({"rawtypes"})private void showParams(HttpServletRequest request) { Map&l ...

  10. 在Eclipse中手动为其添加spring组件开发支持

    https://blog.csdn.net/Tajyl/article/details/79410897 注意找对应spring版本 进入eclipse >>help>>abo ...