+ 1

What is the output of the code ? Explain ?

a = 0 and 1 or 0 b = 0 and 0 or 1 c = 1 or 0 and 0 d = 0 or 1 and 0 print(a, b, c, d, sep = '')

31st Oct 2020, 6:11 AM
Manish Kumar
14 Answers
+ 6
`and` has higher preference over `or` So, a = (0 and 1) or 0 # and likewise others
31st Oct 2020, 6:44 AM
777
777 - avatar
+ 6
@vrintle I think the word "precedence" suits ur comment rather than "preference". But after all the idea is conveyed.. :)
31st Oct 2020, 6:56 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 5
Manish Kumar This is a simple question if u knw the working of AND /OR operator. Read this lesson carefully and if u still didn't understand I'll explain. https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2279/
31st Oct 2020, 6:44 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 4
0110 if ur code is correct. But it shows EOL error so u should correct it like print(a,b,c,d,sep="")
31st Oct 2020, 6:21 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 4
31st Oct 2020, 6:54 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 2
Yeah, Alphin K Sajan, nice catch!
31st Oct 2020, 8:22 AM
777
777 - avatar
+ 1
Alphin K Sajan how ? Can you explain ? And btw I have used single quote instead of double ; )
31st Oct 2020, 6:32 AM
Manish Kumar
+ 1
Alphin K Sajan @vrintle thanks you : ) )) I was confused about the value of "c" . I was getting "0" instead of "1" , because I didn't know that "and " has higher preference over "or"
31st Oct 2020, 6:53 AM
Manish Kumar
+ 1
Alphin K Sajan thank you again for letting us know the suitable word :))
31st Oct 2020, 7:00 AM
Manish Kumar
+ 1
Muhammad Abdulmalik The separator between the arguments to print() function in Python is space by default (softspace feature) , which can be modified and can be made to any character, integer or string as per our choice. The ‘sep’ parameter is used to achieve the same. 1. print('G','F','G', sep='') #output : GFG 2. print('09','12','2016', sep= ' - ') #output : 09-12-2016 3. print('maumahad' , 'abdul', sep=' @') #output : maumahad@abdul source:-gfg
31st Oct 2020, 10:43 AM
Manish Kumar
+ 1
output is “0110” a = “0 and 1 > False (0) “or 0” > False (0) result > false or false > False(0) b = “0 and 0” > False (0) “or 1” > True (1) result > false or true > True(1) ... and so on. if you use “and” operator then two value of its must be true (1 and 1) for true (1) result. next you do calculation for “or” operator and one “true” (1) value is enough for the result True “1” and if both are false (0) then result is false too (0)
31st Oct 2020, 8:14 PM
YAKUP KARAKAŞ
YAKUP KARAKAŞ - avatar
+ 1
Muhammad Abdulmalik sep=“” meaning is seperator. for example: result of this code is 0110 you see there is no any spaces between. if you put sep=“-“ then output will be as 0-1-1-0
31st Oct 2020, 8:18 PM
YAKUP KARAKAŞ
YAKUP KARAKAŞ - avatar
0
AND operator evaluates LHS and RHS. It always returns the RHS unless the LHS is falsy; false/0/empty OR operator only evaluates the LHS first. It returns the LHS if it is true, otherwise it returns the RHS, even if the RHS is False So they're kinda opposites Also, AND is evaluated before OR due to operator precedence
1st Nov 2020, 11:50 AM
Paschal Uwakwe
Paschal Uwakwe - avatar
- 1
First of all let me know what does this sep = " means ?
31st Oct 2020, 9:04 AM
Muhammad Abdulmalik
Muhammad Abdulmalik - avatar