0

Another question about how we got this output in Python

Input: a = range(1,7,2) if(a[1] == 3): print(int(type(a)==list), end= 'n') print(sum(a)) output: 0n9

30th Apr 2018, 11:23 AM
stephanie
stephanie - avatar
5 odpowiedzi
+ 2
since the condition in the if is true, it prints out a boolean that checks if a is a list, which it isn't, followed by the "n". then it prints out the sum, which is 1 + 3 + 5 = 9
30th Apr 2018, 11:29 AM
hinanawi
hinanawi - avatar
+ 2
stephanie not sure about python, but in most languages, false as an integer is 0, while true is 1 also, if parsing the other way around, any integer other than 0 can be considered as true, because of how negative numbers are written in computer memory
30th Apr 2018, 11:36 AM
hinanawi
hinanawi - avatar
+ 1
so int(false) is equal to 0? Does that always hold as true?
30th Apr 2018, 11:35 AM
stephanie
stephanie - avatar
+ 1
perfect. Thank you so much! I understand this now :)
30th Apr 2018, 11:38 AM
stephanie
stephanie - avatar
+ 1
Yes
30th Apr 2018, 11:42 AM
Yugabdh
Yugabdh - avatar