Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I not getting a Boolean?

>>> 'a' in 'aeiou' or 'steve'
True
>>> 'S' in 'Sam' and 'Steve'
'Steve'
>>> 'a' in 'aeiou' and 'steve'
'steve'
>>> 's' in 'aeiou' or 'AEIOU'
'AEIOU'

I was working on a class for some students and was surprised by the last three outputs. I was expecting a boolean. Can anyone shed some light on this?

like image 791
Samuel Focht Avatar asked Dec 24 '22 22:12

Samuel Focht


1 Answers

Boolean Operations

  1. or

x or y | if x is false, then y, else x

Demo

>>> 0 or 1
1
>>> 0 or 0
0
  1. and

x and y | if x is false, then x, else y

Demo

>>> 0 and 1
0
>>> 1 and 0
0
>>> 1 and 1
1
>>> 

Note: These only evaluate their second argument if needed for their outcome.


  1. in

This will return True when condition is satisfy otherwise False.

Demo:

>>> "a" in "abc"
True
>>> "a" in "xyz"
False
>>> 

Now about our statement:

1. As 'a' in 'aeiou' return True value and we are performing or operation, so this will return True because First(Left) value of expression is True.

Demo:

>>> 'a' in 'aeiou'
True
>>> 'a' in 'aeiou' or 'steve'
True
>>> 

2. As 'S' in 'Sam' return True and we are performing and operation, So this will return second value from the expression.

Demo:

>>> 'S' in 'Sam'
True
>>> 'S' in 'Sam' and 'Steve'
'Steve'
>>> 

3. Same as second statement.

4. As 's' in 'aeiou' return False and we are performing or operation, So this will return second value from the expression.

Demo:

>>> 's' in 'aeiou'
False
>>> 's' in 'aeiou' or 'AEIOU'
'AEIOU'
>>> 
like image 111
Vivek Sable Avatar answered Feb 06 '23 17:02

Vivek Sable