Home Data Scientific Computing with Python - Second Edition

Scientific Computing with Python - Second Edition

By Claus Führer , Jan Erik Solem , Olivier Verdier
books-svg-icon Book
eBook $29.99 $20.98
Print $43.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $29.99 $20.98
Print $43.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Variables and Basic Types
About this book
Python has tremendous potential within the scientific computing domain. This updated edition of Scientific Computing with Python features new chapters on graphical user interfaces, efficient data processing, and parallel computing to help you perform mathematical and scientific computing efficiently using Python. This book will help you to explore new Python syntax features and create different models using scientific computing principles. The book presents Python alongside mathematical applications and demonstrates how to apply Python concepts in computing with the help of examples involving Python 3.8. You'll use pandas for basic data analysis to understand the modern needs of scientific computing, and cover data module improvements and built-in features. You'll also explore numerical computation modules such as NumPy and SciPy, which enable fast access to highly efficient numerical algorithms. By learning to use the plotting module Matplotlib, you will be able to represent your computational results in talks and publications. A special chapter is devoted to SymPy, a tool for bridging symbolic and numerical computations. By the end of this Python book, you'll have gained a solid understanding of task automation and how to implement and test mathematical algorithms within the realm of scientific computing.
Publication date:
July 2021
Publisher
Packt
Pages
392
ISBN
9781838822323

 
Getting Started

In this chapter, we will give a brief overview of the principal syntactical elements of Python. Readers who have just started learning programming are guided through the book in this chapter. Every topic is presented here in a how-to way and will be explained later in the book in a deeper conceptual manner and will also be enriched with many applications and extensions.

Readers who are already familiar with another programming language will, in this chapter, encounter the Python way of doing classical language constructs. This offers them a quick start to Python programming.

Both types of readers are encouraged to refer to this chapter as a brief guideline when zigzagging through the book. However, before we start, we have to make sure that everything is in place and that you have the correct version of Python installed together with the main modules...

 

1.1 Installation and configuration instructions

Before diving into the subject of the book, you should have all the relevant tools installed on your computer. We give you some advice and recommend tools that you might want to use. We only describe public domain and free tools.

 

1.1.1 Installation

There are currently two major versions of Python; the 2.x branch and the new 3.x branch. There are language incompatibilities between these branches and you have to be aware of which one to use. This book is based on the 3.x branch, considering the language is up to release 3.7.

For this book, you need to install the following:

  • The interpreter: Python 3.7 (or later)
  • The modules for scientific computing: SciPy with NumPy
  • The module for the graphical representation of mathematical results: matplotlib
  • The shell: IPython
  • A Python-related editor: preferably, Spyder (see Figure 1.1).

The installation of these is facilitated by the so-called distribution packages. We recommend that you use Anaconda.

 

1.1.2 Anaconda

Even if you have Python pre-installed on your computer, we recommend that you create your personal Python environment that allows you to work without the risk of accidentally affecting the software on which your computer's functionality might depend. With a virtual environment, such as Anaconda, you are free to change language versions and install packages without the unintended side effects.

If the worst happens and you mess things up totally, just delete the Anaconda directory and start again. Running the Anaconda installer will install Python, a Python development environment and editor (Spyder), the shell (IPython), and the most important packages for numerical computations: SciPy, NumPy, and matplotlib.

You can install additional packages with conda install within your virtual environment created by Anaconda (see also the official documentation).

 

1.1.3 Spyder

The default screen of Spyder consists of an editor window on the left, a console window in the lower-right corner, which gives access to an IPython shell, and a help window in the upper-right corner, as shown in the following figure:

Figure 1.1: The default screen of Spyder
 

1.1.4 Configuration

Most Python codes will be collected in files. We recommend that you use the following header in all your Python files:

from numpy import *
from matplotlib.pyplot import *

With this, you make sure that all fundamental data types and functions used in this book for scientific computing purposes are imported. Without this step, most of the examples in the book would raise errors.

Spyder gives syntax warnings and syntax error indicators. Warnings are marked by a yellow triangle; see Figure 1.2.

Syntax warnings indicate statements that are correct but that you are discouraged from using for some reason. The preceding statement, from, causes such a warning. We will discuss the reasons for this later in this book. In this particular case, we ignore the warning.

Figure 1.2: Warning triangles in Spyder

Many editors, such as Spyder, provide the possibility to create a template for your files. Look for this feature and put the preceding header into a...

 

1.1.5 Python shell

The Python shell is good, but not optimal, for interactive scripting. We therefore recommend using IPython instead [25].

 IPython can be started in different ways:

  • In a terminal shell by running the following command: ipython
  • By directly clicking on an icon called Jupyter QT Console:
  • When working with Spyder, you should use an IPython console (see Figure 1.1).
 

1.1.6 Executing scripts

You often want to execute the contents of a file. Depending on the location of the file on your computer, it is necessary to navigate to the correct location before executing the contents of a file:

  • Use the command cd in IPython in order to move to the directory where your file is located.
  • To execute the contents of a file named myfile.py, just run the following command in the IPython shell:
run myfile
 

1.1.7 Getting help

Here are some tips on how to use IPython:

  • To get help on an object, just type ? after the object's name and then press the Return key.
  • Use the arrow keys to reuse the last executed commands.
  • You may use the Tab key for completion (that is, you write the first letter of a variable or method and IPython shows you a menu with all the possible completions).
  • Use Ctrl+D to quit.
  • Use IPython's magic functions. You can find a list and explanations by applying %magic on the command prompt.

You can find out more about IPython in its online documentation.

 

1.1.8 Jupyter – Python notebook

The Jupyter notebook is a fantastic tool for demonstrating your work. Students might want to use it to make and document homework and exercises and teachers can prepare lectures with it, even slides and web pages.

If you have installed Python via Anaconda, you already have everything for Jupyter in place. You can invoke the notebook by running the following command in the terminal window:

jupyter notebook

 

A browser window will open and you can interact with Python through your web browser.

 

1.2 Program and program flow

A program is a sequence of statements that are executed in top-down order. This linear execution order has some important exceptions:

  • There might be a conditional execution of alternative groups of statements (blocks), which we refer to as branching.
  • There are blocks that are executed repetitively, which is called looping (see Figure 1.3).
  • There are function calls that are references to another piece of code, which is executed before the main program flow is resumed. A function call breaks the linear execution and pauses the execution of a program unit while it passes the control to another unit – a function. When this gets completed, its control is returned to the calling unit.
Figure 1.3: Program flow

Python uses a special syntax to mark blocks of statements: a keyword, a colon, and an indented sequence of statements, which belong to the block (see Figure 1.4).

Figure 1.4: Block command
 

1.2.1 Comments

If a line in a program contains the symbol #, everything following on the same line is considered as a comment:

# This is a comment of the following statement
a = 3  # ... which might get a further comment here  
 

1.2.2 Line joining

A backslash \ at the end of the line marks the next line as a continuation line, that is, explicit line joining. If the line ends before all the parentheses are closed, the following line will automatically be recognized as a continuation line, that is, implicit line joining.

 

1.3 Basic data types in Python

Let's go over the basic data types that you will encounter in Python.

 

1.3.1 Numbers

A number may be an integer, a real number, or a complex number. The usual operations are as follows:

  • Addition and subtraction, + and -
  • Multiplication and division, * and /
  • Power, **

Here is an example:

2 ** (2 + 2) # 16
1j ** 2 # -1
1. + 3.0j

The symbol j denotes the imaginary part of a complex number. It is a syntactic element and should not be confused with multiplication by a variable.

 

1.3.2 Strings

Strings are sequences of characters, enclosed by single or double quotes:

'valid string'
"string with double quotes"
"you shouldn't forget comments"
'these are double quotes: ".." '

You can also use triple quotes for strings that have multiple lines:

"""This is
 a long,
 long string"""
 

1.3.3 Variables

A variable is a reference to an object. An object may have several references. You use the assignment operator = to assign a value to a variable:

x = [3, 4] # a list object is created
y = x # this object now has two labels: x and y
del x # we delete one of the labels
del y # both labels are removed: the object is deleted

The value of a variable can be displayed by the print function:

x = [3, 4] # a list object is created
print(x)
 

1.3.4 Lists

Lists are a very useful construction and one of the basic types in Python. A Python list is an ordered list of objects enclosed by square brackets. You can access the elements of a list using zero-based indexes inside square brackets:

L1 = [5, 6]
L1[0] # 5
L1[1] # 6
L1[2] # raises IndexError
L2 = ['a', 1, [3, 4]]
L2[0] # 'a'
L2[2][0] # 3
L2[-1] # last element: [3,4]
L2[-2] # second to last: 1

The indexing of the elements starts at zero. You can put objects of any type inside a list, even other lists. Some basic list functions are as follows:

  • list(range(n))} creates a list with n elements, starting with zero:
      print(list(range(5))) # returns [0, 1, 2, 3, 4]
  • len gives the length of a list:
      len(['a', 1, 2, 34]) # returns 4
len(['a',[1,2]]) # returns 2
  • append is used to append an element to a list:
      L = ['a', 'b', 'c']
      L[-1] # &apos...
 

Operations on lists

  • The operator + concatenates two lists:
      L1 = [1, 2]
      L2 = [3, 4]
      L = L1 + L2 # [1, 2, 3, 4]
  • As you might expect, multiplying a list by an integer concatenates the list with itself several times:

n*L is equivalent to making n additions:

      L = [1, 2]
      3 * L # [1, 2, 1, 2, 1, 2]
 

1.3.6 Boolean expressions

A Boolean expression is an expression that has the value True or False. Some common operators that yield conditional expressions are as follow:

  •  Equal: ==
  •  Not equal: !=
  •  Strictly less, less  or equal: <, <=
  •  Strictly greater, greater or equal: >, >=

You combine different Boolean values with or and and. The keyword not gives the logical negation of the expression that follows. Comparisons can be chained so that, for example, x < y < z is equivalent to x < y and y < z. The difference is that y is only evaluated once in the first example. In both cases, z is not evaluated at all when the first condition, x < y, evaluates to False:

2 >= 4 # False 
2 < 3 < 4 # True
2 < 3 and 3 < 2 # False
2 != 3 < 4 or False # True
2 <= 2 and 2 >= 2 # True
not 2 == 3 # True...
 

1.4 Repeating statements with loops

Loops are used to repetitively execute a sequence of statements while changing a variable from iteration to iteration. This variable is called the index variable. It is successively assigned to the elements of a list:

L = [1, 2, 10]
for s in L:
    print(s * 2) # output: 2 4 20

The part to be repeated in the for loop has to be properly indented:

my_list = [...] # define a list
for elt in my_list:
... #do_something
... #something_else
print("loop finished") # outside the for block
 

1.4.1 Repeating a task

One typical use of a for loop is to repeat a certain task a fixed number of times:

n = 30
for iteration in range(n):
... # a statement here gets executed n times
 

1.4.2 break and else

The for statement has two important keywords: break and else. The keyword break quits the for loop even if the list we are iterating is not exhausted:

x_values=[0.5, 0.7, 1.2]
threshold = 0.75
for x in x_values: if x > threshold: break print(x)

The finalizing else checks whether the for loop was broken with the break keyword. If it was not broken, the block following the else keyword is executed:

x_values=[0.5, 0.7]
threshold = 0.75
for x in x_values: if x > threshold: break else: print("all the x are below the threshold")
 

1.5 Conditional statements

This section covers how to use conditions for branching, breaking, or otherwise controlling your code.

A conditional statement delimits a block that will be executed if the condition is true. An optional block starting with the keyword else will be executed if the condition is not fulfilled (see Figure 1.4). We demonstrate this by printing, , the absolute value of :

The Python equivalent is as follows:

x = ...
if x >= 0:
    print(x)
else:
    print(-x)

Any object can be tested for the truth value, for use in an if or while statement. The rules for how the truth values are obtained are explained in Section 2.3.2, Boolean casting.

 

1.6 Encapsulating code with functions

Functions are useful for gathering similar pieces of code in one place. Consider the following mathematical function:

The Python equivalent is as follows:

def f(x):
    return 2*x + 1

In Figure 1.5, the elements of a function block are explained: 

  • The keyword def tells Python we are defining a function.
  • f is the name of the function.
  • x is the argument or input of the function.
  • What is after return is called the output of the function.
Figure 1.5: Anatomy of a function

Once the function is defined, it can be called using the following code:

f(2) # 5 
f(1) # 3
 

1.7 Understanding scripts and modules

A collection of statements in a file (which usually has a py extension) is called a script. Suppose we put the contents of the following code into a file named smartscript.py:

def f(x):
    return 2*x + 1
z = []
for x in range(10):
    if f(x) > pi:
        z.append(x)
    else:
        z.append(-1)
print(z)

In a Python or IPython shell, such a script can then be executed with the exec command after opening and reading the file. Written as a one-liner, it reads as follows:

exec(open('smartscript.py').read())

The IPython shell provides the magic command %run as a handy alternative way to execute a script:

%run smartscript
 

1.7.1 Simple modules – collecting functions

Often, you collect functions in a script. This creates a module with additional Python functionality. To demonstrate this, we create a module by collecting functions in a single file, for example, smartfunctions.py:

def f(x): 
return 2*x + 1
def g(x):
return x**2 + 4*x - 5
def h(x):
return 1/f(x)
  • These functions can now be used by any external script or directly in the IPython environment.
  • Functions within the module can depend on each other._
  • Grouping functions with a common theme or purpose gives modules that can be shared and used by others.

Again, the command exec(open('smartfunctions.py').read()) makes these functions available to your IPython shell (note that there is also the IPython magic function, run). In Python terminology, you say that they are put into the actual namespace.

 

1.7.2 Using modules and namespaces

Alternatively, the modules can be imported by the command import. This creates a namespace named after the filename. The command from puts the functions into the general namespace without creating a separate namespace:

import smartfunctions
print(smartfunctions.f(2))      # 5

from smartfunctions import g    #import just this function
print(g(1)) # 0
  
from smartfunctions import *    #import all
print(h(2)*f(2))                # 1.0

Import the commands import and from. Import the functions only once into the respective namespace. Changing the functions after the import has no effect on the current Python session.

 

1.8 Python interpreter

The Python interpreter executes the following steps:

  1. First, it checks the syntax.
  2. Then it executes the code line by line.
  3. The code inside a function or class declaration is not executed, but its syntax is checked:
      def f(x):
          return y**2  
      a = 3   # here both a and f are defined

You can run the preceding program because there are no syntactical errors. You get an error only when you call the function f.

In that case, we speak about a runtime error:

f(2) # error, y is not defined
 

Summary

In this chapter, we briefly addressed the main language elements of Python without going into detail. You should now be able to start playing with small pieces of code and test different program constructs. All this is intended as an appetizer for the chapters to follows, where we will provide you with the details, examples, exercises, and more background information.

About the Authors
  • Claus Führer

    Claus Führer is a professor of scientific computations at Lund University, Sweden. He has an extensive teaching record that includes intensive programming courses in numerical analysis and engineering mathematics across various levels in many different countries and teaching environments. Claus also develops numerical software in research collaboration with industry and received Lund University’s Faculty of Engineering Best Teacher Award in 2016.

    Browse publications by this author
  • Jan Erik Solem

    Jan Erik Solem is a Python enthusiast, former associate professor, and computer vision entrepreneur. He co-founded several computer vision startups, most recently Mapillary, a street imagery computer vision company, and has worked in the tech industry for two decades. Jan Erik is a World Economic Forum technology pioneer and won the Best Nordic Thesis Award 2005-2006 for his dissertation on image analysis and pattern recognition. He is also the author of "Programming Computer Vision with Python" (O'Reilly 2012).

    Browse publications by this author
  • Olivier Verdier

    Olivier Verdier began using Python for scientific computing back in 2007 and received a PhD in mathematics from Lund University in 2009. He has held post-doctoral positions in Cologne, Trondheim, Bergen, and Ume and is now an associate professor of mathematics at Bergen University College, Norway.

    Browse publications by this author
Latest Reviews (2 reviews total)
excellent content and information
Alles war OKAY ! Was sonst ?
Scientific Computing with Python - Second Edition
Unlock this book and the full library FREE for 7 days
Start now