0

I came across a couple of questions asking me to predict the output of code that looks like this:

a=5
b=6 
print ((a and b) or (a>b))

I am not quiet sure how to make sense out of that :( Here's another one. Could someone please help me out ?

a=4
b=6
c=10
print((a<=b) and print(c) ((c==a) and (not(c<b))))

Edit: got a couple of comments saying the syntax is wrong, hence i attached a picture of the original question...

enter image description here

mozway
  • 194,879
  • 13
  • 39
  • 75
SYT
  • 15
  • 4

2 Answers2

1

you can run following code to see the output.

x = 10
print(x > 5 and x < 15)

#This returns True because 10 is greater than 5 AND 10 is less than 15.


#or
x = 10
print(x > 5 or x < 2)

#This returns True because one of the conditions are true.
#10 is greater than 5, but 10 is not less than 2.


#not
x = 10
print(not(x > 5 and x < 15))
#This returns False because not reverses the result of and.

you can not call print function in a print function. Click here for more info

1

You have syntax errors, so I'll assume the following

code 1

a=5
b=6 
print ((a and b) or (a>b))

NB. I added the missing parenthesis

This will print 6 as both a and b evaluate as Truthy, so the operation evaluates as b. The condition after or is not evaluated since a and b is True.

code 2

a=4
b=6
c=10
print((a<=b) and print(c) or ((c==a) and (not(c<b))))

NB. I added the missing operator, I chose or as it's more interesting

This should return:

10
False

(a<=b) evaluates as b ; print(c) prints 10 and evaluates as None (which evaluates as False), so this whole first part (a<=b) and print(c) evaluates as None/False. This leads to the right-hand part or the or to be evaluated: (c==a) is False, so the whole thing evaluates as False, which is printed as the final output.

mozway
  • 194,879
  • 13
  • 39
  • 75