How can I get Python to expand a term as much as possible?
I used the normal Python preset and want to ask here:
How can I get Python to expand a term as much as possible?
The term is
(binomial formula)
So my question is: How would you write this in Python? How do I make Python see n as a variable that's initially set to 1, and how do I make it so that I don't have to define a and b , so that it then calculates with a and b as variables, so to speak?
My current code:
n = 0 print(("a" + "b") ** n) n = n + 1
Perhaps you could explain to me how, once he has done that once, I can repeat the second and third steps five times, for example.
I would be extremely grateful if you could help me.
Kind regards
I don’t think it’s possible to use character strings in Python, for example, “a” or “b” not directly for computational operations.
for that there is a special library called “,sympy” the importer then, (important: from sympy import * you can also write import sympy, but you have to write that instead, “symbols” sympy.symbols etc.)
if you have imported it, you will use the method , symbol()” to generate symbolic variables (as in math, e.g. x, y).
Initializes the variable n and shows him Value 2 yes then you define your expression and store it under the variable, task”.
Then you use the “expand()” method that simplifies, expands, clamps, etc.
and then simply give it out;)
If you want to repeat this, you can or need to use loops (if you want to work effectively);)
e.g.
For schleife:
Hey, I just made it through coincidence thanks to you, on sympy I didn’t come, but then I fell back!! I made it a little different but it is basically the same:
from sympy import symbols, binomial, expand
# Define the symbolic variables
a, b = symbols(‘a b’)
for n in range(51):
formula = expand((a + b) ** n)
print(f)(a + b)^{n} = {formula}”)
the script then generates all up to the expponent 50
But now I have one more question: how can I let the whole thing be done in such a way that all the formalities are written mathematically? so instead of ** the pay in high so that I can copy everything, and can insert it into a document that it looks beautiful too
lg
jo so you can also make it
There are dozens of other possibilities btw:
e.g.
:).. which compiler do you use btw?
No matter, not so important, it didn’t work. I do that differently, but thank you, you helped me very much <3
hm.. with me this works. try with “import pprint” but then you have to write pprint.pprint()
so pprint seems undefined, doesn’t work. the message:
Traceback
(most recent call last):
File “/home/runner/mathstuff/main.py”, line 8, in
> pprint(f)(a + b)^{n} = {formula}”)
NameError: name ‘pprint’ is not defined. Did you mean: ‘print’?
Na print usually automatically generates a line break, but can do that anyway so that a line always remains free
Then you can write instead of “print()”, “pprint()” which embellished the output
and that it has a line break between everyone, because I can just write print (\n) at the end of the calculation, right?