+ 1

Why it's different in python? Please explain...

x=2 s=2 If x>1 & s>1: print (True) else: print (False) Output: False -------------------------- x=2 s=2 If (x>1) & (s>1): print (True) else: print (False) Output: True

9th Jan 2021, 8:02 AM
Hushnudbek
Hushnudbek - avatar
11 Réponses
+ 12
'&' is called bitwise And operator. Check it out for reference. https://www.sololearn.com/learn/4072/?ref=app x=2 s=2 If x>1 & s>1: print (True) else: print (False) Here, & operator has higher precedence than > operator So, x>1 & s>1 => 2> 1&2 >1 => 2>0>1 => True>1 => false (x>1) & (s>1) here, operations are same but you have to consider the parenthesis since it has the highest precedence.
9th Jan 2021, 8:31 AM
Simba
Simba - avatar
+ 4
Hushnudbek for explanation read the answer of Jan Markus sir ! x=2 s=2 print(x>1 & s > 1) # False because "&" have higher precedence than ">" so first python will evaluate the expression --> 1 & s means 1 & 2 # s=2 now see what "&" operator does binary values of 1 and 2 are : 0000 0001 0000 0010 ------------------ 0000 0000 = 0 in decimal so 1&2 = 0 now our expression becomes --->> x > 0 > 1 which is, x=2 so.. --->> 2>0 >1 --->> True > 1 --->> False ----------------------------------------------------- print( (x>1) & (s>1) ) #True here, "()" paranthesis have higher precedence than "&" so first python will evaluate the expression inside the paranthesis.. (x>1) = True (s>1) = True so now our expression becomes print( True & True) #True how? let's see : The value of True is 1 so 1 & 1 0000 0001 0000 0001 ----------------- 0000 0001 = 1 in decimal and 1 means True -->> (2 > 1) & (2> 1) -->> 1 & 1 -->> 1 >>> True Hope you are clear now!
9th Jan 2021, 9:07 AM
Ratnapal Shende
Ratnapal Shende - avatar
+ 2
Simba 1 & 2 evaluate 0. not 1 as you shown in your example !
9th Jan 2021, 1:30 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
What do you mean?
9th Jan 2021, 8:08 AM
Makar Mikhalchenko
Makar Mikhalchenko - avatar
+ 1
Variables are declared X and S, then there is a comparison. If X is greater than 1 and S is greater than 1 at the same time, then output trueif otherwise, then false
9th Jan 2021, 8:14 AM
Makar Mikhalchenko
Makar Mikhalchenko - avatar
0
Are there another differences in python syntax
9th Jan 2021, 8:05 AM
Hushnudbek
Hushnudbek - avatar
0
Explain the syntax, please
9th Jan 2021, 8:09 AM
Hushnudbek
Hushnudbek - avatar
0
???
9th Jan 2021, 8:13 AM
Hushnudbek
Hushnudbek - avatar
0
Change "&" by "and" and solved if x>1 and s>1: if (x>1) and (s>1):
9th Jan 2021, 7:43 PM
David Ordás
David Ordás - avatar
0
Python is high level language but it is little bit less compparitable than other language And in python and works like this X=2 S=2 If x>1 & s>1: It will check x>1--->2>1==>false And It will check x>2---->2>1==>true And in python it happens like this True + false =>false |||||||||||||||||||||||||||||||||||||||| ________₹____________ Thats why the output is false T think you understand this explanation 🤗🤗🤗🤗😃😃😃😃
11th Jan 2021, 3:29 AM
Anurag Kumar
Anurag Kumar - avatar
0
Hey in python coding evaluating is different from other languages IN PYTHON :- True + True ====>True True + False====>False False + False ===> False It happens like this : )
11th Jan 2021, 3:31 AM
Anurag Kumar
Anurag Kumar - avatar