SymPy/Initialization and Documentation

From PrattWiki
Revision as of 17:52, 21 January 2023 by DukeEgr93 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Starting Work

The way you will start depends on what platform you are using.

  • If you are going to write scripts in an IDE such as Spyder, you can run Spyder
  • If you are going to use a Jupyter Notebook through Anaconda, you can either start the Anaconda Navigator and then start Jupyter Notebook from there or, if you are using Windows, you can start Jupyter Notebook directly. Once started, Jupyter Notebook should open a notebook in a browser window.
  • If you are going to use Google Colab, you can go to https://colab.research.google.com/ or you can point a web browser to a Google Drive folder containing an IPython Notebook file and open it from there.

Documenting Your Work

Use a docstring set off with triple quotes at the start of your code to give your name, NetID, and any other useful information about the code in general. You can use #, #%%, or docstring comments throughout your code to make it easier to follow. If you are using a notebook, you could instead add a text section with the information - and in that case you can also use math markup if it makes sense to do so!

Clearing the Variables

Remember that when Python runs a script, it "remembers" everything that it has done in the session. For that reason, it is good programming practice to clear the variables in Python on occasion to make sure your script or notebook is actually doing everything it needs to work.

  • For scripts in Spyder, the trash can icon above the variable explorer will clear the session.
  • If you are running a notebook, you will occasionally want to restart the kernel.

Saving Your Work

For a script, if you hit "play" or F5 to save and run your work, your work will be saved to whatever file name you give it. Note that if you are just running parts of your code by selecting code or a cell and clicking the run current line or run current cell button, that will not save the code!

If you are working with Jupyter Notebook, you will need to save your files. If you are working with Google Colab, your files will be automatically saved when you make changes - just be sure to give the system enough time to synchronize before closing the window!

Importing SymPy

Before using any SymPy commands, you will need to import it with:

import sympy as sym

While SymPy is contained within most major distributions of Python, and is part of the modules pre-installed in Google Colab, if you find that you do not have SymPy, you can get it from https://www.sympy.org/en/index.html.


Questions

Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.

External Links

References