+ 6

Get confused with if

if 3: print("hi") else: print("no") why if + any number except 0 always True I know that 0 = False , 1 = True but why the other numbers are true??

9th Feb 2018, 12:17 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
9 Answers
+ 3
Hello! This is a good question. In fact, this is defined in Python itself. Maybe other languages treat this differently. See how it happens here: https://docs.python.org/3/library/stdtypes.html#truth
9th Feb 2018, 1:44 PM
Pedro Maimere
Pedro Maimere - avatar
+ 16
every number except 0 is a true and every string containing characters is true
9th Feb 2018, 12:28 PM
Mazin Ayash
Mazin Ayash - avatar
+ 16
every number except 0 is a true and every string containing characters is true
9th Feb 2018, 12:28 PM
Mazin Ayash
Mazin Ayash - avatar
+ 5
but why? I think there must be a reason for that 😅
9th Feb 2018, 12:33 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 4
When you convert ant integer to boolean, it is always true unless it is 0.
9th Feb 2018, 12:27 PM
Jacob Pembleton
Jacob Pembleton - avatar
+ 1
because earlier in C there wasn't type called boolean, so the represantation: 0 for false and {1,2,3,...} all for true, but now we can use the two terms true and false as a builting type in python(remember python based from C)
10th Feb 2018, 10:41 AM
LMN BMR
LMN BMR - avatar
+ 1
Yea earlier there was no type called boolean so its representation was 0
10th Feb 2018, 5:04 PM
Nature
Nature - avatar
0
the secret key for understand the Python is "Python based from C"
11th Feb 2018, 9:58 AM
LMN BMR
LMN BMR - avatar
0
and if you want to know the ansewrs of most questions related to this language features you should take a quick review to the C basics, thank you for your attention.
11th Feb 2018, 10:07 AM
LMN BMR
LMN BMR - avatar