Python - nesting if else inside return - python

Can an if-else expression be the argument of 'return'?
Here's an example of what I'm trying to do:
return m +
if a:
x
elif b:
y
else c:
z
I could write as:
addend = m
if a:
m += x
elif b:
m += y
else c:
m += z
return m

Well, you can use Python's ternary method, such as:
return m + (x if a else y if b else z)
But it may be more readable to just do something like:
if a: return m + x
if b: return m + y
return m + z
As an aside, else c: is not really sensible code: you use if/elif if you have a condition, or else for default action (no condition).
For example, in terms of the code you posted in a comment, you could opt for the succinct, yet still self-documenting:
def rental_car_costs(days):
basecost = days * 40
discount = 50 if days >= 7 else 20 if days >= 3 else 0
return basecost - discount

Related

Python IF statement to a single line

Is it possible to put this code into a single line?
if x == 0:
a += j["sum"]
elif x == 1:
b += j["sum"]
e.e. :D
This is not a working example just for demonstration purposes
a += j["sum"] if x == 0 else b += j["sum"]
You can do it this way if you have Python 3.8 or later for the assignment expression operator :=:
(a := a + j["sum"]) if x == 0 else (b := b + j["sum"]) if x == 1 else None
but really the original is best. It's preferable that code is clear and straightforward.
I may suggest you this one-line dual assignment:
a, b = a + (j["sum"] if x == 0 else 0), b + (j["sum"] if x == 1 else 0)
But you can also use the old good semi-colon to perform two instructions on one line:
a += (j["sum"] if x == 0 else 0); b += (j["sum"] if x == 1 else 0)
I would strongly discourage it, but I guess one could use boolean true as the multiplication identity to do:
a, b = a + (x==0) * j["sum"], b + (x==1) * j["sum"]
This seems to work as I expect in a little loop
a=100
b=200
jsum=10
for x in range(3):
a, b = a + (x==0) * jsum, b + (x==1) * jsum
print(a, b)
Giving:
110 200
110 210
110 210
While I would not code like this myself, one might throw caution to the wind and leverage exec():
exec(f"{['a','b'][x]} += j['sum']" if x in [0,1] else "pass")
or potentially even better (credit to #wjandrea):
if x in {0, 1}: exec(f"{['a','b'][x]} += j['sum']")
Note, if you know x is always either 0 or 1 this would simplify to:
exec(f"{['a','b'][x]} += j['sum']")
Your pseudo-code solution suggests that might be so but your questions suggests it is not.
example:
a=100
b=200
jsum=10
for x in range(3):
exec(f"{['a','b'][x]} += jsum" if x in [0,1] else "pass")
print(a, b)
giving:
110 200
110 210
110 210
#wjandrea has suggested a nice improvement that produces the same results and is likely faster given the ability to bypass exec() if x is out of range.
a=100
b=200
jsum=10
for x in range(3):
if x in {0, 1}: exec(f"{['a','b'][x]} += jsum")
print(a, b)

How do I check every possible combination for validity?

For context, I am trying to find all viable solutions to this question:
here's the code I have so far, but I am having trouble with the part that is supposed to iterate through every possible combination.
x = 1
y = 1
z = 10
a = 10
while x < 10 and y < 10 and z < 100 and a < 100: #iterates through every possible combination
x = x + 1
y = y + 1
z = z + 1
a = a + 1
if x != y: #checks if x and are the same
if a/x == z/y or z/x == a/y: #checks if x and y are proportional to a and z
a = str(a) #converting each int to string
z = str(z)
x = str(x)
y = str(y)
if a.count(x) < 1 and a.count(y) < 1 and z.count(y) <1 and z.count(x) < 1: #checks if any number reapeats
print(x, y, z, a) #prints viable solution```
You have six boxes to fill. Just run through all permutations of 6 members and check the conditions:
import itertools
for a,b,c,d,e,f in itertools.permutations([0,1,2,3,4,5,6,7,8,9],6):
if a == 0 or b == 0 or c == 0 or e == 0:
continue
if (10*c + d) / a == (10*e + f) / b:
print( a, b, c*10+d, e*10+f )
It looks like there are 57 solutions.

Get logarithm without math log python

I need to generate the result of the log.
I know that:
Then I made my code:
def log(x, base):
log_b = 2
while x != int(round(base ** log_b)):
log_b += 0.01
print(log_b)
return int(round(log_b))
But it works very slowly. Can I use other method?
One other thing you might want to consider is using the Taylor series of the natural logarithm:
Once you've approximated the natural log using a number of terms from this series, it is easy to change base:
EDIT: Here's another useful identity:
Using this, we could write something along the lines of
def ln(x):
n = 1000.0
return n * ((x ** (1/n)) - 1)
Testing it out, we have:
print ln(math.e), math.log(math.e)
print ln(0.5), math.log(0.5)
print ln(100.0), math.log(100.0)
Output:
1.00050016671 1.0
-0.692907009547 -0.69314718056
4.6157902784 4.60517018599
This shows our value compared to the math.log value (separated by a space) and, as you can see, we're pretty accurate. You'll probably start to lose some accuracy as you get very large (e.g. ln(10000) will be about 0.4 greater than it should), but you can always increase n if you need to.
I used recursion:
def myLog(x, b):
if x < b:
return 0
return 1 + myLog(x/b, b)
You can use binary search for that.
You can get more information on binary search on Wikipedia:
Binary search;
Doubling search.
# search for log_base(x) in range [mn, mx] using binary search
def log_in_range(x, base, mn, mx):
if (mn <= mx):
med = (mn + mx) / 2.0
y = base ** med
if abs(y - x) < 0.00001: # or if math.isclose(x, y): https://docs.python.org/3/library/math.html#math.isclose
return med
elif x > y:
return log_in_range(x, base, med, mx)
elif x < y:
return log_in_range(x, base, mn, med)
return 0
# determine range using doubling search, then call log_in_range
def log(x, base):
if base <= 0 or base == 1 or x <= 0:
raise ValueError('math domain error')
elif 0 < base < 1:
return -log(x, 1/base)
elif 1 <= x and 1 < base:
mx = 1
y = base
while y < x:
y *= y
mx *= 2
return log_in_range(x, base, 0, mx)
elif 0 <= x < 1 and 1 < base:
mn = -1
y = 1/base
while y > x:
y = y ** 0.5
mn *= 2
return log_in_range(x, base, mn, 0)
import math
try :
number_and_base = input() ##input the values for number and base
##assigning those values for the variables
number = int(number_and_base.split()[0])
base = int(number_and_base.split()[1])
##exception handling
except ValueError :
print ("Invalid input...!")
##program
else:
n1 = 1 ##taking an initial value to iterate
while(number >= int(round(base**(n1),0))) : ##finding the most similer value to the number given, varying the vlaue of the power
n1 += 0.000001 ##increasing the initial value bit by bit
n2 = n1-0.0001
if abs(number-base**(n2)) < abs(base**(n1)-number) :
n = n2
else :
n = n1
print(math.floor(n)) ##output value
Comparison:-
This is how your log works:-
def your_log(x, base):
log_b = 2
while x != int(round(base ** log_b)):
log_b += 0.01
#print log_b
return int(round(log_b))
print your_log(16, 2)
# %timeit %run your_log.py
# 1000 loops, best of 3: 579 us per loop
This is my proposed improvement:-
def my_log(x, base):
count = -1
while x > 0:
x /= base
count += 1
if x == 0:
return count
print my_log(16, 2)
# %timeit %run my_log.py
# 1000 loops, best of 3: 321 us per loop
which is faster, using the %timeit magic function in iPython to time the execution for comparison.
It will be long process since it goes in a loop. Therefore,
def log(x,base):
result = ln(x)/ln(base)
return result
def ln(x):
val = x
return 99999999*(x**(1/99999999)-1)
log(8,3)
Values are nearly equal but not exact.

Complex numbers and for loops

I need to make a function that tests if a complex number, c is in the Mandelbrot set which is defined as zn+1 = zn2 + c. The values of n are in subscript if that was confusing. The function accepts variables c (some complex number of the form 0 + 0j) and n (number of iterations). To see if c is in the set, I need to test z = z**2 + c > 2. If > 2 I need to return false. Now I know that with c = 0 + 0j and n = 25 I should get True. But I can only get true with very small values of n. What do I need to do differently.
def inMSet(c,n):
for x in range(0, n):
z = n**2 + c
if abs(z) > 2:
return False
else:
return True
The definition has an iterative formula, starting with z=0.
def inMSet(c,n):
z = 0
for x in range(0, n):
z = z**2 + c
if abs(z) > 2:
return False
return True
>>> inMSet(0+0j,25)
True

Why do these two implementations of expmod differ for large values?

I've written out a couple of functions for doing expmod, that is, (x ** y) % n. These are both standard functions, I've checked and re-checked both but can't find any silly errors.
Here's the recursive one:
def expmod(x,y,m):
if y == 0: return 1
if y % 2 == 0:
return square(expmod(x,y/2,m)) % m # def square(x): return x*x
else:
return (x * expmod(x,y-1,m)) % m
...and here's the non-recursive one:
def non_recursive_expmod(x,y,m):
x = x % m
y = y % m
result = 1
while y > 0:
if(y%2 == 1):
result = (result * x) % m
x = (x*x) % m
y = y/2
return result
They agree for small values:
>>> expmod(123,456,789) - non_recursive_expmod(123,456,789)
0
...but don't for larger ones:
>>> expmod(24354321,5735275,654) - non_recursive_expmod(24354321,5735275,654)
-396L
What's going on?
Your function non_recursive_expmod has some suspicious steps in it: Remove the %m for x and y at the beginning. Both are not needed.
Additionally make sure that the division of y is an integer division by using y = y // 2.
In total the function should look like this:
def non_recursive_expmod(x, y, m):
result = 1
while y > 0:
if y % 2 == 1:
result = (result * x) % m
x = (x * x) % m
y = y // 2
return result
Non recursive does not decrease y in case it is odd, and even part needs else in case y == 1

Categories