How is the result, of this python code, false?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How is the result, of this python code, false??

a = [] a += "python" b =["python"] if a==b: print(True) else: print(False)

29th Apr 2019, 6:20 AM
Madhav Panicker
Madhav Panicker - avatar
11 Answers
+ 13
Madhav Panicker i think you are looking for some think called as '.append()' method of list, Here i have made a code to understand the difference between them.. Hope that helped 😄 Edit: anything you would like to add HonFu? https://code.sololearn.com/cYmDd0KiwhsI/?ref=app
29th Apr 2019, 9:13 AM
Aaditya Deshpande
Aaditya Deshpande - avatar
+ 11
Try printing it and see why :) a = ['p', 'y', 't', 'h', 'o', 'n'] b = ['python'] of course they are not equal! ;)
29th Apr 2019, 6:51 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 6
But also so convenient! Basically Python does for you what you would otherwise do anyway, like comparing each item. You can also bool a container just by naming the container itself, not its length. apples = [] if not apples: postpone_cake_baking()
29th Apr 2019, 8:55 AM
HonFu
HonFu - avatar
+ 5
A string is actually an array of characters. When using "+=" to add two arrays, you add the entries of the array to the end of the first one. In this case, the individual characters in the string are "unpacked" into the empty list. The .append() method takes an ENTIRE variable and adds it to a list. In this case, using the append() instead of += would have resulted in True For example: ls = [] ls += [1, 2, 3] ls.append([1,2,3]) print(ls) >>> [1, 2, 3, [1, 2, 3]]
29th Apr 2019, 5:49 PM
Trigger
Trigger - avatar
+ 3
The problem is that you cant just compare arrays. You have to compare each element on your own. Edit: Looks like i was wrong and you can compare arrays in python. look at Kuba Siekierzyński answer.
29th Apr 2019, 6:40 AM
Dragonxiv
Dragonxiv - avatar
+ 3
Dragonxiv, in Python you can!
29th Apr 2019, 8:10 AM
HonFu
HonFu - avatar
+ 3
HonFu you can in python? Crazy language :D
29th Apr 2019, 8:50 AM
Dragonxiv
Dragonxiv - avatar
+ 3
About that i am aware. It was just that i had arrays memorized as no language can do it properly and i always do it myself. :D
29th Apr 2019, 8:59 AM
Dragonxiv
Dragonxiv - avatar
+ 3
Sebriel But a==c is True :)
29th Apr 2019, 3:17 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
Here I leave a code that shows Kuba Siekierznski's explanation: https://code.sololearn.com/cEF4eOGcp1sc/?ref=app
29th Apr 2019, 11:58 AM
Sebriel
Sebriel - avatar
0
Oh Yes! Should I add it to the example?
29th Apr 2019, 3:26 PM
Sebriel
Sebriel - avatar