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. Kali中安装 Shodan

    工具介绍 Shodan 是一个搜索引擎,但它与 Google 这种搜索网址的搜索引擎不同,Shodan 是用来搜索网络空间中在线设备的,你可以通过 Shodan 搜索指定的设备,或者搜索特定类型的设备 ...

  2. 忘记Linux的root登录密码?不用害怕,破解掉它就可以了!!!

    在开启centos 7主机后会出现下面这个界面,此时要把光标放到界面内,然后按“e”进入下一个界面. 按完“e”会出现一个新的界面,然后按“↓”找到“Linux16”这行,在最后面的UTF-8后面输入 ...

  3. CORS扫描工具

    参数链接: https://github.com/chenjj/CORScanner 未发现Cors风险 已发现Cors风险 py2遇到的坑: 提示https ssl告警 /usr/local/lib ...

  4. SIFT学习笔记之二 特征提取

    特征提取函数: int _sift_features( IplImage* img, struct feature** feat, int intvls, double sigma, double c ...

  5. java实现List<People>的排序

    1.首先新建测试的实体类(People类): import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsCon ...

  6. SQLSERVER 查看服务器IP地址的命令

    今天进行负载均衡的测试的时候 想查询一下数据库相关信息 百度了下 找到解决方案为: SELECT SERVERNAME = CONVERT(NVARCHAR(),SERVERPROPERTY('SER ...

  7. Win7 Eclipse 搭建spark java1.8环境:WordCount helloworld例子

    [学习笔记] Win7 Eclipse 搭建spark java1.8环境:WordCount helloworld例子在eclipse oxygen上创建一个普通的java项目,然后把spark-a ...

  8. mysql 字段定义不要用null的分析

    一 NULL 为什么这么经常用 (1) java的null null是一个让人头疼的问题,比如java中的NullPointerException.为了避免猝不及防的空指针,需要小心翼翼地各种if判断 ...

  9. c# bitmap的拷贝及一个图像工具类

    using (Bitmap bmp = new Bitmap(scanImgPath)) { Bitmap bitmap = new Bitmap(bmp.Width, bmp.Height, Pix ...

  10. Maven学习存档(3)——eclipse集成maven

    一.安装Maven插件 在eclipse的菜单中选择Help——Install New Software 在弹出框的Work with中写入插件安装地址:http://m2eclipse.sonaty ...