0

if True expression

Can someone point me to the documentation that explain why this if expression and statement prints/display 1. I feel I need to fully understand why this Python code accepted the Expression if True: and no other expression is need. if True: Print("1")

16th Sep 2020, 10:45 AM
cgayle
9 Réponses
+ 3
Try to see it this way If 1 == 1: print"'1") #this prints "1" because they are equal, which is the Boolean equivalent of True. #hence If True: print("1") out put: 1
16th Sep 2020, 10:52 AM
E1ias
E1ias - avatar
+ 3
This is how `if` is supposed to work. It takes an expression and if that expression evaluates to True then it executes the block associated with it otherwise executes line immediately after end of if block. See, even if you use some expressions like 1==1 , 2>0 at the end they are evaluated to boolean value. ie. either True or False. you can try execution this code print(1==1) #True print(2<1) #False
16th Sep 2020, 10:55 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
@Slick... thank you for the example.. Your code: list = [] if list: print("List is filled.") else: print("List is empty") I agree your code and the if statement/expression follows the same logic if True: Print("1") I also agree with you that everything has a value or it doesn't. therefore, your code "if list:" has no value therefore in the current state of the code, we would expect the following results: List is empty. because no value. Now following the same concept "value or no value." The expression "if True:" has no value... so how can we say it is true... I am asking the simple question... what is true? and how do we quantify it. or better yet, how did Python evaluate what is true to quantify printing '1'?
17th Sep 2020, 3:31 PM
cgayle
+ 2
Thanks for working with me Slick... I now understand... my interpretation is this: at its basic the boolean True carry a value of 1. using the code you provided. I also created a code of my own if True: x=True +2 print(x) which prints 3.. So following the same logic, is the following code the same as the above if True==True: x=True +2 print(x) which will also prints 3 and if they are then the following logic implies that False either carries a value of 0 (zero) or no value or null. To test if it is zero or null.. if False==False: x=False * 2 print(x) which prints 0 (zero) thus, False equals to 0.
19th Sep 2020, 1:40 AM
cgayle
+ 1
0 is false, 1 is true. Its deeper than python though. it goes all the way down to bits in binary. You know those 1's and 0's? Theyre basically electric pulse signals 1 is on and 0 is off. Look it up if you want to know more. But to keep it simple, just reread the first sentence.
16th Sep 2020, 10:55 AM
Slick
Slick - avatar
+ 1
Thanks for the quick response... I have used if statement before... if statement is one of my fav code to use when writing logic. What I am having trouble rapping my head around is... it just seems the expression needs more... so if the code was written as: if 1==1: print(True) Therefore, if the expression was written as: if 1 > 2: print("1") another way to look at it is this: if I wrote the expression as and use the same statement: if False: print("1") This would not print. The statement would not print because the logic if false. I feel I need to understand what is Python is accepting as True in the expression for the statement to print 1. if True: Print("1")
17th Sep 2020, 2:23 AM
cgayle
+ 1
its not accepting anything. if (1 > 2) evaluates to: if False or an infinite loop: while(1): #your code while 1 == while True everything eith has a value or not and thats what determines True or False (1 or 0). besides, boolean is just a data type to represent this. In C, the boolean type isnt even an official type Just like if you want to test if a list is empty: list = [] if list: print("List is filled.") else: print("List is empty")
17th Sep 2020, 7:18 AM
Slick
Slick - avatar
+ 1
I follow you, mabey this will help. https://code.sololearn.com/cT3FX0sx8jzH/?ref=app
18th Sep 2020, 5:35 PM
Slick
Slick - avatar
+ 1
You got it! And I love that even though you were told, you still found out by doing your own tests. Huge part of fully understanding concepts is just messing with code to test things out. Awesome though, wish you well.
19th Sep 2020, 7:15 AM
Slick
Slick - avatar