4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで「オブジェクトが同一かどうかの比較する(== 演算子と is 演算子の違い)」の動作を確認してみた

Posted at

概要

Pythonで「オブジェクトが同一かどうかの比較する(== 演算子と is 演算子の違い)」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(list1 == list2)
print(list1 is list2)

list1 = [1, 2, 3]
list2 = list1
print(list1 == list2)
print(list1 is list2)

str1 = "Apple"
str2 = "Apple"
print(str1 == str2)
print(str1 is str2)

list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(id(list1))
print(id(list2))

list1 = [1, 2, 3]
list2 = list1
print(id(list1))
print(id(list2))

num1 = 10
num2 = 10
print(id(num1))
print(id(num2))

以下のコマンドを実行しました。

$ python3 sample.py 
True
False
True
True
True
True
140512374329088
140512374251008
140512372795648
140512372795648
140512375439888
140512375439888

まとめ

何かの役に立てばと。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?