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

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.

Related

Name binding in function statements [duplicate]

This question already has answers here:
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Python Argument Binders
(7 answers)
Closed 2 years ago.
The following Python program:
d = {}
for x in range(3):
d[x] = lambda: x
print(d[0](), d[1](), d[2]())
outputs:
2 2 2
x is bound by reference in the lambda expression. After the for statement, x is bound to 2, which explains the output.
I would like x to be bound by value instead, to get the following output:
0 1 2
How can I achieve this?

Is returning a list a good programming practice? [duplicate]

This question already has answers here:
What's the best way to return multiple values from a function? [duplicate]
(6 answers)
Alternatives for returning multiple values from a Python function [closed]
(14 answers)
Closed 2 years ago.
So i have a function that has to return 3 values, i haven't found a better way to do this other than returning a list. Is this code a good programming practice? And if not how to fix it.
Example function:
def func():
#code
return [a,b,c]
Main code:
#code
list = func()
k = list[0]
l = list[1]
m = list[2]
You can pack/unpack directly in python:
def func():
a = 1
b = 2
c = 3
return a, b, c
k, l, m = func()

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

Python is operator [duplicate]

This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Two variables in Python have same id, but not lists or tuples
(5 answers)
Closed 5 years ago.
I understand that the "is" operator checks identity between two objects, but what is this?
a=25
b=25
a is b
True
Why is this true if
a = [1,2,3]
b = [1,2,3]
a is b
False
And from this post, https://www.quora.com/Python-is-operator, it says all immutable objects, like integers from the first example, will have the first example's result, True, but tuples are immutable and this happens:
a = (1,2)
b = (1,2)
a is b
False
Can someone explain please?
Thanks.

Boolean-logic condition in Python [duplicate]

This question already has answers here:
and / or operators return value [duplicate]
(4 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
It's just a little problem I have with logical condition in Python but :
What about a or b == m --> return true !
I'm going to explain with a piece of code
p = 58
m = 100
s = true
p or s == m #returns me 58
s or p == m #returns me 1
p or s # returns me 58
s or p # returns me 1
I can't explain this ! p or s is not equal to 100 .. And also the last 2 condition in the code ! might seem like a silly question but what why comparing a true and an int returns me an int ?
Sorry if I seem stupid ^^'

Categories

Resources