Basic Programming with Python

From PharmWiki
Jump to navigation Jump to search

Learning the basics of the Spyder IDE

  1. Open Anaconda
  2. Set the Applications to PHRM_8010-Pyrthon
    1. If you did it correctly, it should look like: Begin001.png (Click to enlarge)
  3. Click the Launch button under Spyder.
    1. After a couple minutes, the new screen will look similar to: Py03.png (Click to enlarge)
  4. Go to Projects ⇒ New Project
  5. When a dialog comes up, put 001-Beginning_Python.
    1. If you did it correctly, it should look like: Begn002.png (Click to enlarge)
  6. Click the Create button in the dialog.
    1. If you did it correctly, it should look like: Begn003.png (Click to enlarge)
  7. Let us explore different parts to the screen.
    1. A the top is the Command Bar, which has a variety of pulldowns for you to use. It looks like: Bgn011.png (Click to enlarge)
    2. Below that is the Toolbar, which has icons for executing functions. It looks like: Bgn012.png (Click to enlarge)
    3. On the left side is a Project Directory and Files Explorer with the directory and files of the project: Begn004.png(Click to enlarge)
    4. The middle shows the individual File Explorer of the Python project: Begn005.png(Click to enlarge)
    5. The upper right you have a Output Explorer, which is separated into various tabs that we will check out later.
      1. Help tab: It provides basic information on how to use Spyder and will look like this: Bgn006.png(Click to enlarge)
      2. Variable Explorer tab: It is blank now because you do not have any variables. It looks like: Bgn007.png(Click to enlarge)
      3. Plots tab: It is blank because we do not have any plots. It looks like: Bgn008.png(Click to enlarge)
      4. Files tab: It on shows one folder and no files because we have not made any files yet. It looks like: Bgn009.png(Click to enlarge)
    6. The lower right is the console
      1. Under the IPython Console tab, we have the IPython Console for interactive python. The IPython Console let's you save the output of your Python commands right away. It looks like this: Bgn010.png (Click to enlarge)
      2. Under the History tab, we have the History Console, which shows all the commands that you executed with the software. It should look like; Bgn015.png (Click to enlarge)
  8. Let us quickly review the different parts. You need to know these.
    1. The two top parts are the Command bar and the Toolbar.
    2. The Project Directory and Files Explorer is on the left.
    3. The Individual File Explorer is in the middle.
    4. The Output Explorer is on the upper right. It has several tabs.
      1. Help: Provides basic instructions.
      2. Variable Explorer tab: This shows the output of variables from your programs.
      3. Plots tab: This shows the output of plots from your programs.
      4. Files tab: Lists the files in your project.
    5. On the lower right is a console:
      1. IPython Console is when the IPython console tab is set
      2. History is when the History tab is set.

Running my First Command

  1. In the IPython Console of Spyder, type print("Hello World!") and press return. You will get the following output: Bgn013.png (Click to enlarge)
    1. The output should be "Hello World!"
    2. The command that you just inputted is color coded. Note that the command is in orange and the text is in green. Bgn014.png

Running my First Python Program

  1. On the Command Bar, go to File ⇒ New File...
  2. On the Command Bar, go to File ⇒ Save As...
  3. Save the file as HelloWorld.py
  4. In the Individual File Explorer in HelloWorld.py, type print("Hello World!"). Then go to File ⇒ Save to save the file.
    1. If you did it correctly, it should look like this: Bgn016.png(Click to enlarge)
  5. Be sure that the console on the lower left is set to the IPython console. Once you have, click the Bgn017.png
    1. The output should be the following: Bgn018.png (Click to enlarge)

Next steps

  1. Go to the following website. The good thing about the website is that you can perform things online and on Spyder, but please learn how to input them on Spyder.
  2. You are going to skip some parts of the tutorial over others. Please go through the following in Spyder but not online, but the online part can help you learn it.
    1. Python Home
    2. Python Intro - You have already done this in Spyder.
    3. Python Syntax - Sections in Python are separated by indentations.
    4. Python Comments
    5. Python Variables
    6. Python Data Types
    7. Python Numbers
    8. Python Casting
    9. Python Strings
    10. Python Booleans
    11. To learn more about Python, keep going through each of the sections after Python Booleans until Python Arrays. I would stop here and know all the sections prior to it before proceeding forward.
  3. After you have gotten comfortable with those, you might try the following:
    1. Python Math
    2. Python Modules ⇒ Scipy Tutorial
      1. Scipy is short for scientific python.
    3. Python Matplotlib
      1. This has a variety of plotting functions that you can see in the Output explorer Plots tab.