Explain this code plz anyone | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Explain this code plz anyone

a = [8,6,9,4,6] If (a is a[:]): Print(True) else: Print(False) Why Is the results False(am just confused)

12th Feb 2023, 8:38 PM
Abdul rahmon
Abdul rahmon - avatar
3 Answers
+ 3
If you want more details, you can read this mini tutorial: https://code.sololearn.com/c89ejW97QsTN/?ref=app
14th Feb 2023, 4:22 PM
HonFu
HonFu - avatar
+ 18
the expression *a is a[:]* returns False, since *a[:]* creates a (deep)copy of the list *a* with a different id / identity / reference (memory address). *is* operator checks for identity. checking for equality with *a == a[:]* returns True, since both lists have equal elements.
12th Feb 2023, 8:45 PM
Lothar
Lothar - avatar
+ 7
id(a) is not equal to id(a[:])
12th Feb 2023, 9:07 PM
Jayakrishna 🇮🇳