Is There '?' Control Flow in Python? [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python Ternary Operator
Is there control flow operator similar to '?' of C/C++ in python?
If there is a chunk of code similar to this:
return n <= 1 ? n : fibo(n-1) + fibo(n-2)
Will got an error like this:
File "fibonacci.py", line 2
return n <= 1 ? n : fibo(n-1) + fibo(n-2)
^
SyntaxError: invalid syntax

Yes, the conditional expression is available in Python 2.5+:
return n if n <= 1 else fibo(n-1) + fibo(n-2)

You can try this short circuit expression return n > 1 and fibo(n-1) + fibo(n-2) or n. While this is not the ternary statement, it is concise and does the job in this scenario.

Related

While loop performing more loops than instructed [duplicate]

This question already has answers here:
What does the ^ (XOR) operator do? [duplicate]
(6 answers)
How do I do exponentiation in python? [duplicate]
(3 answers)
Closed 3 years ago.
In this piece of code the iterations should stop at one position earlier than they do.
k = 0
while 2^k < 5:
k += 1
print(k)
I expect it to print '3' but it prints out '4'.
^ is the bitwise XOR operator. You probably meant ** for exponentiation.
In Python the ^ operator is bitwise exclusive-or - not exponentiation. Use ** for exponentiation.

Trying to understand "or" operator inside lambda [duplicate]

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
Closed 4 months ago.
I came across following code while solving this problem:
f=lambda n:"a"[n:]or f(n-1)+chr(97+n)+f(n-1)
The function generates abacaba sequence of specific depth n
For example:
n = 2, output: 'abacaba'
n = 3, output: 'abacabadabacaba'
The question is, how does the code work? Namely, how does "or" operator work inside lambda? (I assume code above uses recursion, and to my knowledge, normally we use loops for recursion, but I don't see anything that resembles loops in the code above)
It works the same way it does anywhere else. If the left-hand argument of or is truthy, the expression evaluates to that; otherwise, it evaluates to the right-hand argument. In this case, "a"[n:] is the empty string when n > 0, so it's equivalent to
def f(n):
if n == 0:
return "a"
else:
return f(n-1) + chr(97+n) + f(n-1)
Let's break it down.
f = lambda # declare a variable f that is a function
n: # that takes an int parameter 'n'
"a"[n:] # if n is 0 return 'a'
or # else
f(n-1) # return a recursive call at n - 1
+ # plus
chr(97+n) # character from code 97 + n
+ # plus
f(n-1) # another recursive call

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

equals sign after a decrement operator in C++ code [duplicate]

This question already has answers here:
"num - 1" vs "num -= 1"
(9 answers)
Closed 8 years ago.
I have trying to translate code from C++ to python and on one line(in for loop), I have:
x -= (t = u/1.0+ MIN(c, EPS))
I want to know what the '=' sign after a decrement indicates? how can I translate this line in python
thank u
In c assigments are functions at there own, in Python assignments are expressions.
In python this means
t = u/1.0 + min(c, EPS)
x -= t # same as x = x - t

Categories

Resources