python is and the difference between ==

In one of python, and is commonly say == operators, mainly used to detect whether two variables are equal, returns True or False, specifically the difference between what it?

I. Introduction

In the explanation is the difference between == and explain directly to the built-in function id () , in fact, in the article  python variable data types and immutable data types  are also built-in function id () had to explain, is mainly used to obtain the memory address of the variable ! Now do not do too much to explain about memory, you can put the memory address of the string of numbers as symbols, like the same memory address each person has a card number!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

# !usr/bin/env python

# -*- coding:utf-8 _*-

"""

@Author: Why grief

@Blog (personal blog address): shuopython.com

@WeChat Official Account (micro-channel public number): ape says python

@Github:www.github.com

 

@File:python_is.py

@Time:2019/10/26:25

 

@Motto: short step a thousand miles, no small streams converge into a wonderful required the ocean, the program of life relentlessly accumulate!

"""

 

a = 5

b = False

c = "hello"

 

Print ( "A memory address: {}" . the format ( ID ( A ) ) )

Print ( "B Memory Address: {}" . the format ( ID ( B ) ) )

Print ( "memory address C: {}" . the format ( ID ( C ) ) )

Output:

1

2

3

a memory address: 1,784,504,608

b Memory Address: 1784012992

c memory addresses: 2126520897696

 

Two .python operator ==

If the two variables are equal, then the operator == established, returns True; conversely return False ; sample code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

a = 3

b = 3

print(a==b)

 

# Integer and floating point comparison

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)

输出结果:

1

2

3

4

True

True

False

False False False True

 

 

三.python运算符 is

1.两个变量的值相等;

2.变量地址也相同(可以通过内置函数id获取变量内存地址);

如果同时满足以上两个条件,运算符 is 才成立,返回True;反之,不满足其中任何一个条件都会返回False;示例代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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)

输出结果:

1

2

3

4

5

6

7

8

9

10

11

12

13

2039213240016

2039234381168

False

************************************************************

1784504544

1784504544

True

************************************************************

2039217328240

2039217328176

2039217247376

2039217328240

False False False True

 

 

猜你喜欢:

1.python可变数据类型和不可变数据类型

2.python全局变量和局部变量

3.python字符串/列表/元组/字典之间的相互转换

 

转载请注明猿说Python » python is和==区别


Guess you like

Origin blog.51cto.com/14531342/2467802