Getting Started with Python — Python Numerical Methods
Getting Started with Python — Python Numerical Methods
This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide Contents
for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods. Set up working environment
Three ways to run Python code
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better
The Zen of Python
learning experience. The code is released under the MIT license. If you find this content useful, please
consider supporting the work on Elsevier or Amazon!
There are different ways to install Python and related packages, here we recommend to use Anaconda or
Miniconda to install and manage your packages. Depending on the operating systems (OS) you are using, i.e
Windows, Mac OS X, or Linux, you need to download a specific installer for your machine. Both Anaconda and
Miniconda are aiming to provide easy ways to manage Python work environment in scientific computing and data
sciences.
Here we will use Mac OS X as an example to show you the install processes. For windows users, please skip the
rest of this section and read Appendix A for all the processes. The main differences between Anaconda and
Miniconda are:
• Anaconda is a complete distribution framework that includes the Python interpreter, package manager as
well as the commonly used packages in scientific computing.
• Miniconda is a light version of Anaconda that does not include the common packages, therefore, you need
to install all the different packages by yourself. But it does have the Python interpreter and package
manager.
The option we choose here is to use Miniconda to manage our installation of the packages. This way we can only
install the ones we need.
Here you can choose a different installer based on your OS. We choose the Mac OS X and Python 3.7 as an
example.
After you run the installer, follow the guide and you will successfully install it.
One thing to note is that you can change the installation location by giving it an alternative location on your
machine, but the default is your home directory.
After installation, you can check the installed packages by type the following commands:
Let us first install some packages for our book - ipython, numpy, scipy, pandas, matplotlib and jupyter
notebook. We will talk more about the management of the packages using pip and conda later.
The easiest way to run Python code is through the Python shell or Ipython Shell (which stands for Interactive
Python). The Ipython shell is richer than Python shell, such as Tab autocompletion, color-highlighted error
messages, basic UNIX shell integration and so on. Since we just installed Ipython, let us try to run the “hello
world” example with it. The way we launch either Python or Ipython shell is by typing it in a terminal (see the
figure below). Then we can run Python command by typing it into the shell, by pressing Enter, we immediately
see the results from the command. For example, we can print out “Hello World” by typing print("Hello
World"):
In the above command, the print() is a function in Python, and “Hello World” is a string data type that we will
introduce them later in the book.
The second way to run Python code is to put all the commands into a file and save it as a file with extension .py
(the extension of the file could be anything, but by convention, it is usually .py). For example, use your favorite
text editor (Showing here is the Visual Studio Code), put the command in a file called hello_world.py:
The third way to run Python is through Jupyter notebook. It is a very powerful browser-based Python
environment, we will talk more about it in details later in this chapter. Here we just quickly see how we could run
the code from a Jupyter notebook. Run the jupyter notebook in the bash command line:
jupyter notebook
Then you will see a local web page will pop up, from the upper right button to create a new Python3 notebook:
Running code in Jupyter notebook is easy, you type your code in the cell, and press shift + enter to run the cell,
the results will be shown below the code.
import this
© Copyright 2020.