Installation Program_Execution Data_Structures Data_Input Data_Handling Functions Data_Manipulation

Goal

Learn how to run Python programs from different environments. This should show you the difference between
  • the Python interpreter,
  • IPython/Jupyter,
  • and an IDE.

Additional Information

Here you can find additional information on ...


Write a simple program

  • Generate a file with the name first_demo.py, with the following content:

    print('hello, world!')

Python
Python programs can be executed in a number of different ways. (1) For figuring out the syntax, and for data input, the Integrated Development Environments can be extremely helpful. (2) Interactive Development Environments (IDEs) are useful for debugging programs. (3) Finished programs are often run from a command console (terminal).

Running Python ...

... from a command terminal

  1. Open a command terminal (WIN+cmd), go to the folder with first_demo.py, and check with dir first*.py if you are in the correct folder.
  2. Test your Python installation by typing where python. (This should show you where your Python executable is located.) Then start Python by typing python. Stop the Python interpreter by entering exit().

    If Python is NOT running properly, please to back to Installation and configure your computer properly.

  3. If Python runs, you can run your program from the command line(!) with python first_demo.py

... with Jupyter

  • Next, start a Jupyter QtConsole (with jupyter qtconsole) or JupyterLab (with jupyter lab). Enter pwd ("print working directory") to check if you are in the correct directory. If this is the case type ls *.py to show all the available Python programs in the current directory. If you are not in the correct directory, use cd <...> to go to the directory where you have first_demo.py
  • Execute your program with %run first_demo.py.

... in an IDE

  • Start the IDE of your choice. This can be Wing, Spyder, PyCharm ...

    (The next few steps will be described for Wing. Try to find the corresponding commands for your IDE.)

  • Ensure that your IDE sees the correct Python interpreter. In Wing, you can check this by looking at the tab Python Shell.
  • Open your program first_demo.py, and start it by pushing Play. Ensure that you can see the resulting text.

Exercise

  • Perform the same tasks with a program that generates and plots a simple sine-wave:
                              import numpy as np
                              import matplotlib.pyplot as plt
    
                              t = np.arange(0, 20, 0.1)
                              x = np.sin(t)
    
                              plt.plot(t, x)
                              plt.show()
                          
  • Comment your program properly. One-line comments are indicated with # at the beginning of the comment, and multiline comments start and end with """