Q on syntax - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Q on syntax (/thread-25616.html) |
Q on syntax - ebolisa - Apr-05-2020 Hi, The following 2 codes yield the same results. What's the difference between them? If the first one is the traditional way of coding, what's the second? I'm puzzled. TIA a=1 if(a==1): p=8 else: p=9 print(p) a = 1 print(8 if(a==1) else 9) RE: Q on syntax - buran - Apr-05-2020 https://python-forum.io/Thread-Basic-Ternary-Conditional-Expressions RE: Q on syntax - nadim313 - Apr-05-2020 HI THERE In the first example you have assigned number 1 to letter a, in the condition it is truly equal to 1 and will print 8. change the value of a to another number then it will jump to else condition. In the second Example: it is the same value but it will not print else condition, since you didn't have used print. RE: Q on syntax - ebolisa - Apr-05-2020 @nadim313 actualy it does print if you change the variable a. That's why I said that both codes yeld the same results. try this... a = 1 print(8 if(a==1) else 9) #prints 8 a = 2 print(8 if(a==1) else 9) #prints 9 RE: Q on syntax - ndc85430 - Apr-05-2020 The crux of it is the first is a statement and the second an expression. |