-1

I am a middle school student and just started learning about functions. This code is about exponents, but I cannot use import math. Is there a way to make this code into a one-liner or make it shorter?

def power(a, n):
    x = 1
    if n == 0:
        print(1)
    elif n > 0:
        for i in range(n):
            x = x * a
    else:
        for i in range(-n):
            x = x * a

    return x


print(power(2, 5))
halfer
  • 19,824
  • 17
  • 99
  • 186
Cayden Lin
  • 25
  • 3
  • 2
    There's builtin function ```pow(base, exp)``` in python. – MarkSouls May 20 '21 at 07:10
  • 5
    Or you know, good old `base**exp`. – Prometheus May 20 '21 at 07:11
  • @Prometheus That's... why couldn't I think of it? – MarkSouls May 20 '21 at 07:24
  • 1
    Here's cursed power function ```func = (lambda base, exp: (lambda ff: ff(ff, base, exp))(lambda f, n, p: 1 if p == 0 else n if p == 1 else ((lambda x: x*x)(f(f, n, p//2))*(n if p % 2 == 1 else 1))))``` in single line without ```pow``` or ```**``` – MarkSouls May 20 '21 at 07:29
  • If you don't want to use `**` you could at least drop the `elif` part and change the loop in the `else` path to `for i in range(abs(n)):`. And of course `print(1)` has to be replaced by `return 1`. – Matthias May 20 '21 at 07:35

10 Answers10

5

You can use the pow function instead of "x = x * a". Also even if you don't want to use that function, you can simplify it as "x *= a".

Dolfinwu
  • 262
  • 1
  • 14
  • 1
    How can that be the accepted answer - and have up votes - when it is obivously wrong and has been posted after @Prometheus' correct comment? Or am I missing something? I mean `x *= a` is a shorthand for `x = x * a`, but that's multiplication not exponentiation which was the original question, right? – AlWo23 May 23 '21 at 19:25
4

You could look into recursive functions, they are a bit tricky at first but are a good brain exercise. A recursive power function would look like that :

def power(a, n):
    if n == 0:
        return 1
    elif n == 1:
        return a
    else:
        return a * power(a, n-1)
Clement
  • 113
  • 1
  • 8
3

You can use the in-built operator **.

Example:

print(2**2) # Will return 4
bluevulture
  • 422
  • 4
  • 16
2

Use the ** operator directly:

print(2 ** 5)
Azuuu
  • 853
  • 8
  • 17
1

If you want to shorten your function with one line, you can use lambda statements like this: Shortening code with lambda statement in python

And you can use the ** builtin operator in place of multiplying the number over and over in a loop. So for example, 5**4 is equivalent to 5 * 5 * 5 * 5 and will give the exact same output.

P.S. I am also just beginning with programming, so I might not have the best answer.

Jason0591
  • 59
  • 7
1

You can use this

**

which will return the value of 4 to the power of 3 (same as 4 * 4 * 4):

Siti
  • 83
  • 1
  • 1
  • 8
1

You can use ** This is an exponentiation operator.

def power(a, n):
    return a**n
print(power(2, 5))

or simply use lambda function. You can read about it here.

print((lambda x,y:x**y)(2,5))
0

If the goal is to use your own function. You can shorter an if with:

value_when_true if condition else value_when_false;

Putting a simple if-then-else statement on one line

Charly Roch
  • 368
  • 1
  • 4
  • 15
0

I can see your overall need is to get the power of x.

If you can't use import math, then go with the inbuilt arithmetic operator for exponential that is **

Ex. You need 5 to the power 4 then code will be

print(5**4) # In one line
0
def power(b,n):
    n=abs(n)
    print(b**n)
print(power(2,-5))

well how about this here you can use the abs function to convert negative number to positive so no need for extra code simple use of ** can do everything

Bijin Abraham
  • 1,709
  • 2
  • 11
  • 25