Linked Questions

40 votes
4 answers
45k views

and / or operators return value [duplicate]

I was watching a 2007 video on Advanced Python or Understanding Python, and at 18'27" the speaker claims "As some may know in Python and and or return one of the two values, whereas not returns always ...
rxmnnxfpvg's user avatar
  • 31.1k
37 votes
7 answers
88k views

Python's Logical Operator AND [duplicate]

I'm a little confused with the results I'm getting with the logical operators in Python. I'm a beginner and studying with the use of a few books, but they don't explain in as much detail as I'd like. ...
BubbleMonster's user avatar
22 votes
4 answers
61k views

if 'a' or 'b' in L, where L is a list [duplicate]

I am having trouble with the following logic: Lets say I have a list: L = ['a', 'b', 'c'] Both items are in the list... if ('a' or 'b') in L: print "It's there!" else: print 'No ...
Brian Leach's user avatar
  • 4,164
14 votes
7 answers
35k views

How do "and" and "or" work when combined in one statement? [duplicate]

For some reason this function confused me: def protocol(port): return port == "443" and "https://" or "http://" Can somebody explain the order of what's happening behind the scenes to make this ...
orokusaki's user avatar
  • 57.5k
27 votes
2 answers
25k views

How does the logical `and` operator work with integers? [duplicate]

So, I was playing with the interpreter, and typed in the following: In [95]: 1 and 2 Out[95]: 2 In [96]: 1 and 5 Out[96]: 5 In [97]: 234324 and 2 Out[97]: 2 In [98]: 234324 and 22343243242 Out[98]:...
oyed's user avatar
  • 441
12 votes
2 answers
18k views

Evaluation of boolean expressions in Python [duplicate]

What truth value do objects evaluate to in Python? Related Questions Boolean Value of Objects in Python: Discussion about overriding the way it is evaluated
Casebash's user avatar
  • 120k
6 votes
2 answers
3k views

Assigning string with boolean expression [duplicate]

I am trying to understand this code from someone else's project. If you want the context it's here: https://github.com/newsapps/beeswithmachineguns/blob/master/beeswithmachineguns/bees.py#L501 IS_PY2 ...
Mascros's user avatar
  • 361
1 vote
5 answers
2k views

How do 'and' and 'or' act with non-boolean values in Python? [duplicate]

22 and 333/12 or 1 I have encountered the above line of code. The result is 27, but I could not quite understand what does and and or mean in this case. Can somebody explain to me, preferred with ...
sammiwei's user avatar
  • 3,228
3 votes
6 answers
4k views

How does boolean operator work on string in python [duplicate]

True and "asdf" or "qwer" result => "asdf" False and "asdf" or "qwer" result => "qwer" I can't understand how those things work. I thought that boolean operator on string would make type error, but ...
jinhwan's user avatar
  • 1,227
5 votes
4 answers
2k views

Logical operators in Python [duplicate]

While reading about logical operators in python, I came across the following expressions: 5 and 1 output: 1 5 or 1 output: 5 Can anyone explain how this is working? I know that the operands of the ...
BoRRis's user avatar
  • 1,051
6 votes
2 answers
12k views

Use of OR operator in python lambda function [duplicate]

There is a code example in the O Reilly Programming Python book which uses an OR operator in a lambda function. The text states that "[the code] uses an or operator to force two expressions to be run"....
czolbe's user avatar
  • 611
2 votes
4 answers
1k views

"4 and 5" is 5, while "4 or 5" is 4. Is there any reason? [duplicate]

When I test the difference between and and or, I meet this problem. Could you please help me understand it?
user3217670's user avatar
3 votes
5 answers
370 views

Concise way to compare a variable against multiple values [duplicate]

I've been trying to understand if it is possible to use an if statement similar to the likes of what I have demonstrated here below. It is my understand that it is not? for i in range(10): if i == (...
Revaliz's user avatar
  • 33
0 votes
2 answers
3k views

Why does not(True or False) return False? [duplicate]

Just starting out learning Python. Is the reason for not(True or False) returning False because: "True or False" is not a Falsy. Hence, not Falsy = Truthy. Therefore, not(Truthy) = False; ...
Jay's user avatar
  • 17
1 vote
1 answer
1k views

Complex compound boolean expression [duplicate]

I know how to test multiple boolean conditions (as below), but am wondering if I can combine testing multiple boolean conditions by grouping variables together, as below? Is there a way to shorten the ...
Mike's user avatar
  • 201

15 30 50 per page
1
2 3 4 5
8