Python OR Operator [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
So, I do not understand why Python does not evaluate correctly this code:
def makes10(a, b):
if (a or b == 10) or (a + b == 10):
return True
else:
return False
while the following is interpreted as expected:
def makes10(a, b):
if a == 10 or b == 10 or (a + b == 10):
return True
else:
return False
They look the same to me, but obviously (a or b == 10) was not interpreted as (a == 10) or (b == 10). Can someone please explain why this happens?
Correct
Incorrect

I'm not entirely sure but it might be because the first statement is not an operation? So there might not be a need for parentheses.

Related

Why does this boolean expression evaluate to False? [duplicate]

This question already has answers here:
python operator precedence of in and comparison [duplicate]
(3 answers)
Closed 3 years ago.
Can someone explain to me why the Python interpreter evaluates this expression to be False?
1 in [1] == True
I would expect that 1 in [1] would evaluate to True, and obviously True == True would be True. However this isn't what happens - the expression is False. Why does this happen?
== and in are both comparison operators. And when you have multiple comparison operators like this, Python considers it a chained comparison. For example, 1 < x < 10 is equivalent to 1 < x and x < 10.
In your case, 1 in [1] == True is equivalent to (1 in [1]) and ([1] == True), which evaluates to False.
If you have an expression like this python split it to more statements. In fact:
1 in [1] == True equals to: (1 in [1]) and ([1] == True)
Right side is false since [1] != True and whole sentence is false

Why this statement is evaluated to False even it is true? [duplicate]

This question already has answers here:
python operator precedence of in and comparison [duplicate]
(3 answers)
Closed 3 years ago.
I started learning python but I've noticed something unsual, something that I do not understand, why the expression provided below it's evaluated to false even it is true??
l = [1,2,3,4,5,"Hi"]
"Hi" in l # returns True
"Hi" in l == True # returns False
"Hi" in l == True is evaluated as ("Hi" in l) and (l == True) which is False.
Explanation from documentation:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent
to x < y and y <= z

Checking if string contains if either of two substring returns true [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 4 years ago.
Results I get for this code is true but the results are false.Checked in atom code editor and online editor
a = "https://www.reddit.com/comments/ado0ym/use_reddit_coins_to_award_gold_to_your_favorite/"
b = "aaaaaaa"
c = "somthing random"
if b or c in a:
print("true")
else:
print("false")
# Results return True
Python doesnt run this code correctly
if b or c in a:
print("true")
The reason is that python percieves this as
if (b) or (c in a):
And as if b is always True so code doesnt work as expected
This should be
if (b in a) or (c in a):
print("true")
Hope it helps
The problem with your code is the following:
if b or c in a:
print("true")
This first checks if b exists. which it does. So it gives True.
What you should do is:
if (b in a) or (c in a):

Why does this function not return anything? [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 4 years ago.
I have the following function written in python 3
def nullstelle(f,a,b,counter=0,TOL=10 ** -4):
counter = counter + 1
if counter <= 100:
g = f((a + b)/2)
if f(a) * g <= 0:
b = (a + b)/2
else:
a = (a + b)/2
if abs(a-b) <= 2*TOL:
return (a,b)
else:
nullstelle(f,a,b,counter,TOL)
else:
return (a,b)
my problem is, that for the input
def f(x):
return x ** 3 -2
nullstelle(f,0,3)
it does not return anything. I really do not understand how this is possibly.
Im sorry if this looks like a trivial question to you guys, but programming is absolutely not my main field of interest and I know nearly nothing about it (yet).
I feel like this is a duplicate, but couldn't find one quickly. The problem is that you're not returning the result of your recursive call, so you get None if you have to recurse. Change that line to:
return nullstelle(f,a,b,counter,TOL)

How is this statement including an if statement evaluated? [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 5 years ago.
Suppose that I have the following Python Code:
def fun5(a, b, c):
return a <= b if b <= c else False
fun5(2, 4, 6)
The output of fun5 is True. How is this code being evaluated by Python?
I was expecting a SyntaxError because of the lack of indentations and Python requires indentation.
What you're looking at is called a conditional expression/ternary operator, and it is perfectly valid syntax.
It's equivalent to:
if b <= c:
return a<=b
else:
return False

Categories

Resources