Рубрики
Без рубрики

В чем разница между «is» и ‘= = = ‘в Python

Оператор «is» сравнивает идентичность (местоположение памяти) двух объектов, когда оператор «==» Comp … Теги с Python, начинающими, CodeNewie, 100Дасофкодом.

Оператор «is» сравнивает идентичность (местоположение памяти) двух объектов, когда оператор ‘==’ сравнивает значения двух объектов.

Оператор «==» возвращает true, когда значения двух операндов равны и ложны иначе.

print(True == 1) 
# True means 1 (Python convert the boolean 'True' into integer 1) so True == 1 means 1 == 1

print('1' == 1) 
# '1' means string one and 1 means integer one, in python two different types are not comparable 

print([] == 1) 
# Empty list can't be equal to one

print(10 == 10.0) 
# Python can convert integer and float number, here Python convert them in the same type

print([1,2,3] == [1,2,3]) 
# I hope you understand it

Вывод

Истинная ложная ложная верная правда

Оператор «IS» возвращает true, если переменные с обеих сторон оператора указывают на тот же объект (то же местоположение памяти) и false в противном случае.

print(True is 1) 
print('1' is 1)
print([] is 1)
print(10 is 10.0)
print([1,2,3] is [1,2,3])
# in all examples both side of 'is' are in the difference location in memory (they are not identical) or you can say they are not same.

Вывод

False false false false false

Оригинал: “https://dev.to/iamprogrammmer/what-is-the-difference-between-is-and-in-python-56oa”