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

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

Related

How to return the NAME of greater of two variables in python3? [duplicate]

This question already has answers here:
Python ? (conditional/ternary) operator for assignments [duplicate]
(2 answers)
Closed 2 years ago.
This could obviously be done with an if-elif-else logic, but I wish to ask for a better - more pythonic way to implement it. eg.
a,b = 14,73
if a>b:
print('a')
elif a<b:
print('b')
else:
print('a=b') #not_required_though
You can do if else in one line
print('a' if a>b else 'b')
or you can also assign it to variable
res = 'a' if a>b else 'b' if b>a else 'a==b'

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):

Python OR Operator [duplicate]

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.

How can I make something like a?b:c in python? [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 7 years ago.
In java, I usually use
boolean x = a? b : c;
instead
if(a)
boolean x = b;
else
boolean x = c;
Is there any possibility for doing that in python?
You can do:
x = b if a else c
which means the same thing.

Categories

Resources