+ 1

How do 'is statement' work?

a=[1,2,3] b=[1,2,3] Print(a is b) Why it false??

20th Dec 2021, 7:34 AM
Hasan Zakaria Tahhan
Hasan Zakaria Tahhan - avatar
8 Antworten
+ 6
is’ operator – Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. Here is an example a = [1,2,3] b = a print(a is b) #both variables point to the same object. #You example above does not point to the same object Hasan Zakaria Tahhan
20th Dec 2021, 7:38 AM
MATOVU CALEB
MATOVU CALEB - avatar
+ 2
Ravi Kiran I search and find more information, a=[1,2,3] b=a b[2]=5 Print(a) ==> [1,2,5] If we need to copy list from variable to another We can use copy method b=a.copy() b[2]=5 Print(a) ==> [1,2,3]
20th Dec 2021, 6:47 PM
Hasan Zakaria Tahhan
Hasan Zakaria Tahhan - avatar
+ 1
It Outputs true
20th Dec 2021, 7:42 AM
MATOVU CALEB
MATOVU CALEB - avatar
+ 1
MATOVU CALEB Thanks a lot
20th Dec 2021, 7:51 AM
Hasan Zakaria Tahhan
Hasan Zakaria Tahhan - avatar
0
MATOVU CALEB This give true ?
20th Dec 2021, 7:39 AM
Hasan Zakaria Tahhan
Hasan Zakaria Tahhan - avatar
0
Thank U Hassan Zakaria Tahhan today I learnt new thing from ur question.
20th Dec 2021, 2:55 PM
Ravi V
Ravi V - avatar
0
Good
21st Dec 2021, 1:46 AM
Ravi V
Ravi V - avatar
0
Is statement simply check the memory location. That means if a and b are similar the (is) statement will print false But if a and b are from same memory location then (is) statement will print true.
21st Dec 2021, 7:24 PM
Sujeet Tiwari
Sujeet Tiwari - avatar