A value doesn't print()

@rob42
Yes, x is any number in the defined range (-inf, 1), which was calculated mathematically in this math example: |x - 1| + |x -2| = 6. (Don’t worry about math part for now.)

@Rosuav
Sorry, I got stuck on for x in the range (float(“-inf”), 1), so I can’t write further.

Here is what I’m trying to solve using Python: What’s the value of x?

|x - 1| + |x -2 | = 6

Okay, so you’re going to need to establish some equation-solving techniques, then turn those into an algorithm for finding the solutions. Once that’s settled, you’ll be able to write that algorithm in Python code, and have Python solve the problem for you.

Yes. Python should automatically solve the x value. If the for-loop I wrote can be processed, it would be fine. So there is no solution to my question?
Mathematically, I know the x range: x <=1 or x >=2. (x is in <=1 or >= 2 range; ideally Python should define exact x values.)

Any idea?


xRange = (xRange for xRange in (float("-inf"), 1))
while True: 
    yRange = 1-xRange + 2-xRange == 6
    print(xRange)
    break

error:

    yRange = 1-xRange + 2-xRange == 6
TypeError: unsupported operand type(s) for -: 'int' and 'generator'

I’m sorry, to me that makes no sense whether I interpret it in terms of mathematics (domains are not “in” ranges, and “range of -infinity” means nothing) or in terms of Python.

I guess you are trying to do maths in Python code, in particular, you want to work out infinite sums using Python.

You can’t. End of story. An infinite loop would take an infinite amount of time to run.

Python is a programming language, not symbolic mathematics. You might try Sage or Sympy, but I strongly recommend you learn a little bit of Python first. Especially for Sympy.

The range function is a short-cut for generating objects which are
(approximately) the same as a list of integers:

  • range(10) is equivalent to [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  • range(5, 10) is equivalent to [5, 6, 7, 8, 9]

You can’t build an infinite list in Python, if you converted every atom of matter in the universe into computer memory there wouldn’t be enough, and even if you could, it would take an infinite amount of time to iterate over it one item at a time.

If you want to evaluate some sort of infinite sum like “sum of 2 to the power of negative n for n from 1 to infinity” you either need symbolic maths software (try Sage, or Mathematica) or you need to find a closed form solution without the infinite sum that you can evaluate in Python.

No it shouldn’t. Writing symbolic maths solvers is a huge job, far out of scope for a programming language like Python.

If you want to do symbolic maths, you need something designed to do symbolic maths. Try Mathematica, or Sage. (Sage is written in Python.)

You might be able to do this in Python using Sympy.

Indeed, I suppose that’s the end of the story for me. The domain is infinite, but I thought there should be a way for Python to calculate the values within infinity, as I defined the function rule mathematically. It’s a contradiction for me.

Thanks for the information. I will look into those plug-ins…

There is no contradiction. You are suffering under the misapprehension that programming languages are mathematics. They aren’t. They are sets of instructions for the computer to follow.

It might help if you tell us what your aim is. What are you trying to do. We can then advise you how to do it.

Programming in Python (like most commonly used programming languages) fundamentally works differently from how you seem to expect. Writing x = y is not an assertion that x shall be equal to y in the future, or a constraint used for solving for the values of x and y. It is instead an instruction, that means “figure out what y is right now, and set x to be equal to that”. Names like x and y are not quantities to solve for; they are labels that are used for values that are actively being processed by the program, perhaps repeatedly.

For example: writing y = 3 and then y = 2 * x does not cause x to become 1.5. It does not cause anything to happen to x. It will cause an error, if x did not already have a value assigned. If x did have a value, the y = 2 * x will cause y to change accordingly, replacing the previous assignment.

For example: writing x = y and then y = 2 * x does not cause both to become 0 "because that’s the only value that satisfies both equations. They are not equations; they are assignments. Assuming that y had a value before, x will become that value (replacing any previous assignment to x), and then y will become twice that value, replacing the previous assignment.

I can’t understand the intention. What do you think should be the value of x, the first time the loop runs? What do you think should be the value of x, the second time it runs? Why? (Incidentally: this is another example of the same sort of assignment: the value of x will change, repeatedly, and the code inside the loop will run again according to that changed value.

Well, you could try that while True loop or something similar. However, among the multiple problems with the underlying concept of this attempted loop is the issue of what would happen with the value of x during the iterations, if the loop could iterate. That value should not change, since adding 1 to -∞ leaves us with -∞.

Since the attempted for loop fails to execute, let’s watch the value of x in an analogous while loop. However, if you decide to execute the following, please first find out how to kill the process, since you are about to initiate an infinite loop.

x = float("-inf")
while x < 1:
    print(x)
    x += 1

The following is an infinitesimal portion of the output:

-inf
-inf
-inf
-inf
-inf
-inf
-inf
-inf
-inf
-inf