Python Forum
I'm looking for the syntax of a complex exponent, like: 2 ** (n - m)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm looking for the syntax of a complex exponent, like: 2 ** (n - m)
#1
. I'm looking for the syntax of a complex exponent, like: 2 ** (n - m)
. I would like to do this:
(code 1) :
p = n - m
NUMERO = (98 ** p) + (2 ** p)
. I would like to write (code 1) in a single line of code:
(code 2) :
NUMERO = (98 ** (n - m)) + (2 ** (n - m))
-> Is the syntax (code 2) correct?
Yoriz write Dec-29-2023, 01:33 PM:
Please post all code, output and errors (in its entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
(Dec-29-2023, 10:08 AM)CHANCEMAN Wrote: -> Is the syntax (code 2) correct?
Of course it is correct. The downside is that n - m is computed twice.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
The walrus := could be used to only compute n - m only once.
import itertools

n = 3
m = 1
p = n - m
numbers = (
    (98**p) + (2**p),
    (98 ** (n - m)) + (2 ** (n - m)),
    (98 ** (result := n - m)) + (2 ** (result)),
)

for index, (number, other_number) in enumerate(itertools.pairwise(numbers), 1):
    print(
        (
            f"Comparing numbers {index} & {index+1}: {number} == {other_number}"
            f" = {number == other_number}"
        )
    )
Output:
Comparing numbers 1 & 2: 9608 == 9608 = True Comparing numbers 2 & 3: 9608 == 9608 = True
Gribouillis and buran like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas dataframe into csv .... exponent issue mg24 10 1,801 Jan-20-2023, 08:15 PM
Last Post: deanhystad
  Fractional Exponent Question Flexico 2 5,211 Dec-04-2016, 01:00 AM
Last Post: Flexico

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020