Why am I not getting 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 worked on a class for some students and was surprised at the last three results. I expected a logical one. Can anyone shed some light on this?

+4
source share
5 answers

Logical operations

  • 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:  They only evaluate their second argument, if necessary for their result.


  • at

This will return Truewhen the condition satisfies otherwise False.

Demo:

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

Now about our statement:

1. 'a' in 'aeiou' return True , or, True, () True.

:

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

2. 'S' in 'Sam' return True, and, .

:

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

3. , .

4. 's' in 'aeiou' return False, or, .

:

>>> 's' in 'aeiou'
False
>>> 's' in 'aeiou' or 'AEIOU'
'AEIOU'
>>> 
+5

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

or and, .

, or LHSE ( ), Truthy (. ), ( ). Falsy (. ), RHSE.

, and LHSE, Falsy, , RHSE.

Truthy and Falsy

.

, , : False, None, ( , , , , ). .

, ('a' in 'aeiou') True, True.

('S' in 'Sam') False, ('Steve'), Steve.

('a' in 'aeiou') - True, ('Steve'), Steve.

('s' in 'aeiou') - False, ('AEIOU'), AEIOU.

, , Truthy Falsy bool,

>>> bool('a' in 'aeiou' or 'steve')
True
>>> bool('S' in 'Sam' and 'Steve')
True
>>> bool('a' in 'aeiou' and 'steve')
True
>>> bool('s' in 'aeiou' or 'AEIOU')
True
+4

:

'a' in somestring or 'steve'

:

('a' in somestring) or 'steve'

True, 'steve', , a somestring. 'aeiou', 'xyzzy':

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

, , :

('a' in 'aeiou') or ('a' in 'steve')
+1

, http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/boolean.html. BODMAS , bool ( true - https://docs.python.org/2/library/stdtypes.html).

, :

  • 's' False = False # False x False
  • False 's' = False
  • 's' False = s # False x x
  • False or 's' = s
  • 's' True = True #! false True = 2
  • True 's' = s
  • 's' True = s #! false True = 2
  • True 's' = True
  • 's' 'a' = a #! false ! false = 2
  • 's' 'a' = s
0

Python

  • : , boolean, true, false, . : False 3 return 3, 4 3 return 3, '' False - 1 return -1.

  • : != False , False.

:

1 2 3 4 5 5: 1,2,3,4,5 False, 5.

-1 3 4 return 4; -1 ; 3,4 , . , False: D.  0 3 0. 0 - False, False

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

'a' 'aeiou' ; "steve" False, "a" "aeiou" True.

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

'S' '' - ; "" In False, "Steve", resule - "Steve"

>>> 'a' in 'aeiou' and 'steve'
'steve'

'a' in 'aeiou' is True, 'steve' False "steve"

>>> 's' in 'aeiou' or 'AEIOU'
'AEIOU'

's' 'aeiou' - False; "AEIOU" False "AEIOU"

: http://www.diveintopython.net/power_of_introspection/and_or.html

0
source

All Articles