-5

I am trying some operations in Python but I am not understanding the underlying concept of it. I tried various combinations which might be bit tough for you but my objective was how it is working internally in Python.Please find the code below:

>>> 2 or 3
2
>>> 3 or 2
3
>>> 3 or 3
3
>>> 3 or -3
3
>>> -3 or 3
-3
>>> 0 or 3
3
>>> 0 or -9
-9
>>> 3 and 4
4
>>> 3 and 6
6

>>> 0 or None
>>> 0 and None
0
>>> None and 0
>>> None or 0
0


>>> 5 and 2
2
>>> -3 and 6
6
>>> 3 and -6
-6
>>> 3 and 0
0
>>> 0 and 0
0
>>> 0 and 0.0
0
>>> 0.0 and 0
0.0
>>> 0.0 or 0
0
>>> 0 or 0.0
0.0
>>> [] or 3
3
>>> 3 or []
3
>>> 0 or []
[]
>>> [] or 0
0
>>> [] and 3
[]
>>> 3 and []
[]
>>> [] or {}
{}
>>> [] and {}
[]
>>> [] and {}
[]
>>> {} or []
[]
>>> {} and []
{}
Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
user3148499
  • 7
  • 1
  • 4
  • 3
    What is your question? – VMRuiz Jun 21 '17 at 07:25
  • refer to https://stackoverflow.com/help/how-to-ask for future questions – Eric Semwenda Jun 21 '17 at 07:28
  • It appears there's no question in this question. What do you expect us to do; do you want us to post a full explanation of how the logical operators work? There are plenty of those readily available. I doubt the internet would benefit from having _another one_ of those. – Aran-Fey Jun 21 '17 at 07:30
  • Thanks to those who answered the question rather than ppl who simply commented. – user3148499 Jun 21 '17 at 08:36

4 Answers4

0

This is actually very well described in most tutorials; the general rule is: a and b is b if a is true, else it is a; a or b is a if a is true, else it is b. Now, False, None, (), [], {}, '', 0 and 0.0 are all considered false; practically everything else is true.

Błotosmętek
  • 12,717
  • 19
  • 29
0

I believe you are referring to how truth tables work here. See the following link:

https://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/Boolean_Expressions

Hope it helps.

Eric Semwenda
  • 436
  • 6
  • 16
0

This is short-circuiting in action:

  • a and b
    • a is False -> a immediately returned
    • a is True, b is False -> b immediately returned
    • both True -> the last object is returned (b)
  • a or b
    • a is True -> a is immediately returned
    • a is False, b is True -> b is returned
    • both False -> the last object returned (b)

Now, some values are considered 'falsey', e.g. bool(value) == False, for example:

  • False itself
  • the number zero (0, 0.0, 0 + 0j)
  • empty containers ([], tuple(), "", {}, set())
  • None
  • anything else whose __bool__ method returns False

Others are considered 'truthy'.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

I think you are expecting and and or to produce a boolean value, that is, True or False. But they don't. or will return the first operand if that evaluates to True; otherwise it will return the second operand. and will return the first operand if that evaluates to False; otherwise it will return the second operand. For the evaluation, the following count as False: zero integers and floats, the empty string, and empty tuples, lists and dicts. Anything else counts as True, including, for example, the string "0".

BoarGules
  • 16,440
  • 2
  • 27
  • 44