Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

How to write Python code to write polynomials?

How to write Python code to write polynomials?  For example, how to write Python code to output following polynomial.

User generated image

Avatar of naseeam
naseeam
Flag of United States of America image

ASKER

My OS is Ubuntu 18.04.5 LTS
My Python version is 3.6.9
Avatar of naseeam

ASKER

from sympy import *

init_printing(use_latex="mathjax")

x, y, z = symbols("x y z")

3*x**2 + 2*x - 7

Open in new window

User generated imageDo I need to install sympy module?  If yes, how to install for python3 ?
Avatar of David H.H.Lee
You can install SymPy using the Python package manager pip. The command below installs SymPy into the current environment.

$ pip install sympy

Open in new window


You can confirm your SymPy installation by opening up the Python REPL and typing the two commands below.

>>> import sympy
>>> sympy.__version__
'1.4'
Avatar of naseeam

ASKER

When I type "python" from linux terminal, I get python 2.7.17.

When I type "python3" from linux terminal, I get python 3.6.9.  

Is above command going to install sympy for python 2.7.17 or python 3.6.9.  I need sympy module for python 3.6.9.
It will install to your current python version environment
Avatar of naseeam

ASKER

>>  It will install to your current python version environment
Is my current python version environment Python version 2.7.17 or Python 3.6.9 ?   How do I find out?
You can type this in command to check your current python version
python --version or
python -V or
python -VV

Open in new window

Avatar of naseeam

ASKER

I know the version of my python.  versions 2.7.17 and 3.6.9 are installed.
$ pip install symp

Open in new window

Will above command install symp module for Python 3.6.9 ?   I thought pip3 is the command to install modules for python 3 ?
Likely not, you can test pip --version

See if you have the same pip and pip3 to match your python/python3
ASKER CERTIFIED SOLUTION
Avatar of Andrei Fomitchev
Andrei Fomitchev
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of naseeam

ASKER

>> python3.9 -m pip install sympy
Since I have python 3.6.9, Is following the exact command I need to enter?
python3.6 -m pip install sympy

What is the meaning of -m option?
-m module to use.
man python3
"Since I have python 3.6.9, Is following the exact command I need to enter?
python3.6 -m pip install sympy"
Yes, it is.
Avatar of naseeam

ASKER

User generated imageUser generated image
Avatar of noci
noci

Just like there is a python3.., there should be a pip3 to accompany the python3 processor.

Avatar of naseeam

ASKER

So, the exact command should be?  python3.6 -m pip3 install sympy 
Avatar of naseeam

ASKER

python3.6 -m pip3 install sympy 
output is:  /usr/bin/python3.6: No module named pip3
you either run
python3 -m pip install sympy
or you run
pip3 install sympy

pip is the module, comonly when you have multiple version of python on the system
python version 2.x
puthon3 version 3,0
the modules often are similarly named
pip  for python 2.x
and
pip3 for python 3.x
Note that Python2.7 has been deprecated for several years and Since  jan. 2020 all support for it has been withdrawn.
So use python 2.x with caution.
My Advise update to a current version  V3.8  or V3.9 and keep moving with newer versions.