零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门
在 Python 中 is 和==都说常用的运算符之一,主要用于检测两个变量是否相等,返回 True 或者 False ,具体区别在哪呢?
一.前言
在讲解 is 和 == 区别直接先讲解一下内置函数 id,其实在文章 Python 可变数据类型和不可变数据类型 中也对内置函数 id 有过讲解,主要用于获取变量的内存地址!关于内存现在不做过多讲解,你可以把内存地址当作一串数字符号,内存地址就好比每个人都有一个身份证号码一样!
# !usr/bin/env python# -*- coding:utf-8 _*-"""@Author:猿说编程@Blog(个人博客地址): www.codersrc.com@File:Python is 和 == 区别.py@Time:2021/3/25 23:00@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!"""a = 5b = Falsec = "hello"print("a内存地址:{}".format(id(a)))print("b内存地址:{}".format(id(b)))print("c内存地址:{}".format(id(c)))'''输出结果:a内存地址:1784504608b内存地址:1784012992c内存地址:2126520897696'''# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python is 和 == 区别.py @Time:2021/3/25 23:00 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ a = 5 b = False c = "hello" print("a内存地址:{}".format(id(a))) print("b内存地址:{}".format(id(b))) print("c内存地址:{}".format(id(c))) ''' 输出结果: a内存地址:1784504608 b内存地址:1784012992 c内存地址:2126520897696 '''# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python is 和 == 区别.py @Time:2021/3/25 23:00 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ a = 5 b = False c = "hello" print("a内存地址:{}".format(id(a))) print("b内存地址:{}".format(id(b))) print("c内存地址:{}".format(id(c))) ''' 输出结果: a内存地址:1784504608 b内存地址:1784012992 c内存地址:2126520897696 '''
二.Python 运算符 ==
如果两个变量的值相等,那么运算符 == 成立,返回 True ;反之返回 False ;示例代码如下:
# !usr/bin/env python# -*- coding:utf-8 _*-"""@Author:猿说编程@Blog(个人博客地址): www.codersrc.com@File:Python is 和 == 区别.py@Time:2021/3/25 23:00@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!"""a = 3b = 3print(a==b)# 整数与浮点数的比较a = 3b = 3.0print(a==b)a = 3b = 2.9999print(a==b)# 字符串比较a = "hello world"b = "HELLO WORLD"c = "hello "d = "hello world"print(a==b,a==c,b==c,a==d)'''输出结果:TrueTrueFalseFalse False False True'''# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python is 和 == 区别.py @Time:2021/3/25 23:00 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ a = 3 b = 3 print(a==b) # 整数与浮点数的比较 a = 3 b = 3.0 print(a==b) a = 3 b = 2.9999 print(a==b) # 字符串比较 a = "hello world" b = "HELLO WORLD" c = "hello " d = "hello world" print(a==b,a==c,b==c,a==d) ''' 输出结果: True True False False False False True '''# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python is 和 == 区别.py @Time:2021/3/25 23:00 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ a = 3 b = 3 print(a==b) # 整数与浮点数的比较 a = 3 b = 3.0 print(a==b) a = 3 b = 2.9999 print(a==b) # 字符串比较 a = "hello world" b = "HELLO WORLD" c = "hello " d = "hello world" print(a==b,a==c,b==c,a==d) ''' 输出结果: True True False False False False True '''
三.Python 运算符 is
- 1.两个变量的值相等;
- 2.变量地址也相同(可以通过内置函数 id 获取变量内存地址);
如果同时满足以上两个条件,运算符 is 才成立,返回 True ;反之,不满足其中任何一个条件都会返回 False ;示例代码如下:
# !usr/bin/env python# -*- coding:utf-8 _*-"""@Author:猿说编程@Blog(个人博客地址): www.codersrc.com@File:Python is 和 == 区别.py@Time:2021/3/25 23:00@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!"""a = 333333b = 333333.0print(id(a))print(id(b))print(a is b)print("***"*20)a = 3b = 3print(id(a))print(id(b))print(a is b)print("***"*20)a = "hello world"b = "HELLO WORLD"c = "hello "d = "hello world"print(id(a))print(id(b))print(id(c))print(id(d))print(a is b,a is c,b is c,a is d)'''输出结果:20392132400162039234381168False************************************************************17845045441784504544True************************************************************2039217328240203921732817620392172473762039217328240False False False True'''# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python is 和 == 区别.py @Time:2021/3/25 23:00 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ a = 333333 b = 333333.0 print(id(a)) print(id(b)) print(a is b) print("***"*20) a = 3 b = 3 print(id(a)) print(id(b)) print(a is b) print("***"*20) a = "hello world" b = "HELLO WORLD" c = "hello " d = "hello world" print(id(a)) print(id(b)) print(id(c)) print(id(d)) print(a is b,a is c,b is c,a is d) ''' 输出结果: 2039213240016 2039234381168 False ************************************************************ 1784504544 1784504544 True ************************************************************ 2039217328240 2039217328176 2039217247376 2039217328240 False False False True '''# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python is 和 == 区别.py @Time:2021/3/25 23:00 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ a = 333333 b = 333333.0 print(id(a)) print(id(b)) print(a is b) print("***"*20) a = 3 b = 3 print(id(a)) print(id(b)) print(a is b) print("***"*20) a = "hello world" b = "HELLO WORLD" c = "hello " d = "hello world" print(id(a)) print(id(b)) print(id(c)) print(id(d)) print(a is b,a is c,b is c,a is d) ''' 输出结果: 2039213240016 2039234381168 False ************************************************************ 1784504544 1784504544 True ************************************************************ 2039217328240 2039217328176 2039217247376 2039217328240 False False False True '''
四.猜你喜欢
- Python 简介
- Python Pycharm Anacanda 区别
- Python2.x 和 Python3.x,如何选择?
- Python 配置环境
- Python Hello World入门
- Python 代码注释
- Python 中文编码
- Anaconda是什么?Anconda下载安装教程
- Pycharm 提示:this license **** has been cancelled
- Pycharm设置开发模板/字体大小/背景颜色
- Python 列表 list
- Python 元组 tuple
ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容