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. selenium3 web自动化测试框架 二:页面基础操作、元素定位方法封装、页面操作方法封装

    学习目的: 掌握自动化框架中需要的一些基础web操作 正式步骤: 使用title_contains检查页面是否正确 # -*- coding:utf-8 -*- import time from se ...

  2. [译]如何使用Python构建指数平滑模型:Simple Exponential Smoothing, Holt, and Holt-Winters

    原文连接:How to Build Exponential Smoothing Models Using Python: Simple Exponential Smoothing, Holt, and ...

  3. 最新 58集团java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.58集团等10家互联网公司的校招Offer,因为某些自身原因最终选择了58集团.6.7月主要是做系统复习.项目复盘.Leet ...

  4. thinkphp5相关

    THINKPHP5代码风格规范(基于PSR): https://www.jianshu.com/p/e53d26407e68

  5. jquery清除元素的点击事件

    $("#id").css("pointer-events", "none");

  6. HDU3191 【输出次短路条数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3191 How Many Paths Are There Time Limit: 2000/1000 M ...

  7. sleep(0) 的作用

    思考下面这两个问题: 假设现在是 2019-5-18 12:00:00.00,如果我调用一下 Thread.Sleep(1000) ,在 2019-5-18 12:00:01.00 的时候,这个线程会 ...

  8. golang数据基本数据类型和string类型的转换

    基本类型之间的转换 golang在不同类型的变量之间赋值时需要显式转换,也就是说golang中数据类型不能自动转换. 表达式T(v)将值v转换为类型T 1.数据类型的转换可以是从范围小——>范围 ...

  9. CPA ,CFA,ACCA

    CPA是“注册会计师”(Certified Public Accountant,CPA)的简称,是指取得注册会计师证书并在会计师事务所执业的人员,是从事社会审计/中介审计/独立审计的专业人士,CPA为 ...

  10. 给postmessage加上callback方法

    postmessage双向通信中,是不能使用回调函数的. window.postmessage({msg:'hello',callback:function(e){ do something with ...