Chapter 1

Introduction to Python

1.1Introduction

Before we begin discussion on image acquisition and processing using Python, we will provide an overview of the various aspects of Python. This chapter focuses on some of the basic materials covered by many other books [Bea09], [Het10], [Lut06], [Vai09] and also from the book, “Essential Python” ([PC18]), a book from the authors of this book. If you are already familiar with Python and are currently using it, then you can skip this chapter.

We begin with an introduction to Python. We will then discuss the installation of Python with all the modules using the Anaconda distribution. Once the installation has been completed, we can begin exploring the various features of Python. We will quickly review the various data structures such as list, dictionary, and tuples and statements such as for-loop, if-else, iterators and list comprehension.

1.2What Is Python?

Python is a popular high-level programming language. It can handle various programming tasks such as numerical computation, web development, database programming, network programming, parallel processing, etc.

Python is popular for various reasons including:

1.It is free.

2.It is available on all the popular operating systems such as Windows, Mac or Linux.

3.It is an interpreted language. Hence, programmers can test portions of code on the command line before incorporating it into their program. There is no need for compiling or linking.

4.It gives the ability to program faster.

5.It is syntactically simpler than C/C++/Fortran. Hence it is highly readable and easier to debug.

6.It comes with various modules that are standard or can be installed in an existing Python installation. These modules can perform various tasks like reading and writing various files, scientific computation, visualization of data, etc.

7.Programs written in Python can be run on various OS or platforms with little or no change.

8.It is a dynamically typed language. Hence the data type of variables does not have to be declared prior to their use, making it easier for people with less coding experience.

9.It has a dedicated developer and user community and is kept up to date.

Although Python has many advantages that have made it one of the most popular interpreted languages, it has a couple of drawbacks that are discussed below:

1.Since its focus is on the ability to program faster, the speed of execution suffers. A Python program might be 10 times or more slower (say) than an equivalent C program, but it will contain fewer lines of code and can be programmed to handle multiple data types easily. This drawback in the Python code can be overcome by converting the computationally intensive portions of the code to C/C++ or by the appropriate use of data structure and modules.

2.Indentation of the code is not optional. This makes the code readable. However, a code with multiple loops and other constructs will be indented to the right, making it difficult to read the code.

1.3Python Environments

There are several Python environments from which to choose. Some operating systems like Mac, Linux, Unix, etc. have a built-in interpreter. The interpreter may contain all modules but is not turn-key ready for scientific computing. Specialized distributions have been created and sold to the scientific community, pre-built with various Python scientific modules. When using these distributions, the users do not have to individually install scientific modules. If a particular module that is of interest is not available in the distribution, it can be installed. One of the most popular distributions is Anaconda [Ana20b]. The instructions for installing Anaconda distribution can be found at https://www.anaconda.com/distribution/.

1.3.1Python Interpreter

The Python interpreter built into most operating systems can be started by simply typing python in the terminal window. When the interpreter is started, a command prompt ( > > >) appears. Python commands can be entered at the prompt for processing. For example, in Mac, when the built-in Python interpreter is started, an output similar to the one shown below appears:

(base) mac:ipaup ravi$ python
Python 3.7.3 | packaged by conda-forge |
(default, Dec 6 2019, 08:36:57)
[Clang 9.0.0 (tags/RELEASE_900/final)] :: Anaconda, Inc.
on darwin
Type "help", "copyright", "credits" or "license"
for more information.
>>>

Notice that in the example above, the Python interpreter is version 3.7.3. It is possible that you might have a different version.

1.3.2Anaconda Python Distribution

The Anaconda Python Distribution [Ana20a] provides programmers with close to 100 of the most popular scientific Python modules like scientific computation, linear algebra, symbolic computing, image processing, signal processing, visualization, integration of C/C++ programs to Python etc. It is distributed and maintained by Continuum Analytics. It is available for free for academics and is available for a price to all others. In addition to the various modules built into Anaconda, programmers can install other modules using the conda [Ana20b] package manager, without affecting the main distribution.

To access Python from the command line, start the ‘Anaconda Prompt’ executable and then type python.

1.4Running a Python Program

Using any Python interpreter (built-in or from a distribution), you can run your program using the command at the operating system (OS) command prompt. If the file firstprog.py is a Python file that needs to be executed, then type the following command on the OS command prompt.

>> python firstprog.py

The >> is the terminal prompt and >> > represents the Python prompt.

The best approach to running Python programs under any operating systems is to use an Integrated Development Environment like IDLE or Spyder as it provides an ability to edit the file and also run it under the same interface.

1.5Basic Python Statements and Data Types

Indentation

In Python, a code block is indicated by indentation. For example in the code below, we first print a message, ‘We are computing squares of numbers between 0 and 9’. Then we loop through values in the range of 0 to 9 and store it in the variable ‘i’ and also print the square of ‘i’. Finally we print the message, ‘We completed the task …’ at the end.

In other languages, the code block under the for-loop would be identified with a pair of curly braces {}. However, in Python we do not use curly braces. The code block is identified by moving the line print(i*i) four spaces to the right. You can also choose to use a tab instead.

print('Computing squares of numbers between 0 and 9')
for i in range(10):
 print(i*i)
print('Completed the task …')

There is a significant disadvantage to indentation especially to new Python programmers. A code containing multiple for-loops and if-statements will be indented farther to the right making the code unreadable. This problem can be alleviated by reducing the number of for-loops and if-statements. This not only makes the code readable but also reduces computational time. This can be achieved by programming using data structures like lists, dictionary, and sets appropriately.

Comments

Comments are an important part of any programming language. In Python, a single line comment is denoted by a hash # at the beginning of a line. Multiple lines can be commented by using triple quoted strings (triple single quotes or triple double quotes) at the beginning and at the end of the block.

# This is a single line comment

'''
This is
a multiline
comment
'''

# Comments are a good way to explain the code.

Variables

Python is a dynamic language and hence you do not need to specify the variable type as in C/C++. Variables can be imagined as containers of values. The values can be an integer, float, string, lists, tuples, dictionary, set, etc.

>>> a = 1
>>> a = 10.0
>>> a = 'hello'

In the above example the integer value of 1, float value of 10.0, and a string value of hello for all cases are stored in the same variable. However, only the last assigned value is the current value for a.

Operators

Python supports all the common arithmetic operators such as +, − , *, /. It also supports the common comparison operators such as >, < , = =, ! = , > =, < =, etc. In addition, through various modules Python provides many operators for performing trigonometric, mathematical, geometric operations, etc.

Loops

The most common looping construct in Python is the for-loop statement, which allows iterating through the collection of objects. Here is an example:

>>> for i in range(1,5):
… print(i)

In the above example the output of the for-loop are the numbers from 1 to 5. The range function allows us to create values starting from 1 and ending with 5. Such a concept is similar to the for-loop normally found in C/C++ or most programming languages.

The real power of for-loop lies in its ability to iterate through other Python objects such as lists, dictionaries, sets, strings, etc. We will discuss these Python objects in more detail subsequently.

>>> a = ['python', 'scipy']
>>> for i in a:
… print(i)

In the program above, the for-loop iterates through each element of the list and prints it.

In the next program, the content of a dictionary is printed using the for-loop. A dictionary with two keys lang and ver is defined. Then, using the for-loop, the various keys are iterated and the corresponding values are printed.

>>> a = {
          'lang':'python'
          'ver': '3.6.6'
        }
>>> for keys in a:
…   print(a[key])

The discussion about using a for-loop for iterating through the various lines in a text file, such as comma separated value file, is postponed to a later section.

if-else statement

The if-else is a popular conditional statement in all programming languages including Python. An example of if-elif-else statement is shown below.

if a < 10:
 print('a is less than 10')
elif a < 20:
 print('a is between 10 and 20')
else:
 print('a is greater than 20')

The if-else statement conditionals do not necessarily have to use the conditional operators such as <, > , = =, etc.

For example, the following if statement is legal in Python. This if statement checks for the condition that the list d is not empty.

>>> d = [ ]
>>> if d:
…     print('d is not empty')
… else:
…     print('d is empty')
d is empty

In the above code, since d is empty, the else clause is true and we enter the else block and print d is empty.

1.5.1Data Structures

The real power of Python lies in the liberal usage of its data structure. The common criticism of Python is that it is slow compared to C/C++. This is especially true if multiple for-loops are used in programming Python. This can be alleviated by appropriate use of data structures such as lists, tuples, dictionary and sets. We describe each of these data structures in this section.

Lists

Lists are similar to arrays in C/C++. But, unlike arrays in C/C++, lists in Python can hold objects of any type such as int, float, string and including another list. Lists are mutable, as their size can be changed by adding or removing elements. The following examples will help show the power and flexibility of lists.

>>> a = ['python','scipy', 3.6]
>>> a.pop(-1)
3.6
>>> print(a)
a = ['python','scipy']
>>> a.append('numpy')
>>> print(a)
['python','scipy', 3.6]
>>> print(a[0])
python
>>> print(a[-1])
numpy
>>> print(a[0:2])
['python','scipy']

In the first line, a new list is created. This list contains two strings and one floating-point number. In the second line, we use the pop function to remove the last element (index = −1). The popped element is printed to the terminal. After the pop, the list contains only two elements instead of the original three. We use append, and insert a new element, “numpy” to the end of the list. Finally, in the next two commands we print the value of the list in index 0 and the last position indicated by using “−1” as the index. In the last command, we introduce slicing and obtain a new list that contains only the first two values of the list. This indicates that one can operate on the list using methods such as pop, insert, or remove and also using operators such as slicing.

A list may contain another list. Here is an example. We will consider the case of a list containing four numbers and arranged to look like a matrix.

>>> a = [[1,2],[3,4]]
>>> print(a[0])
[1,2]
>>> print(a[1])
[3,4]
>>> print(a[0][0])
1

In line 1, we define a list of the list. The values [1, 2] are in the first list and the values [3, 4] are in the second list. The two lists are combined to form a 2D list. In the second line, we print the value of the first element of the list. Note that this prints the first row or the first list and not just the first cell. In the fourth line, we print the value of the second row or the second list. To obtain the value of the first element in the first list, we need to index the list as given in line 6. As you can see, indexing the various elements of the list is as simple as calling the location of the element in the list.

Although the list elements can be individually operated, the power of Python is in its ability to operate on the entire list at once using list methods and list comprehensions.

List functions/methods

Let us consider the list that we created in the previous section. We can sort the list using the sort method as shown in line 2. The sort method does not return a list; instead, it modifies the current list. Hence the existing list will contain the elements in a sorted order. If a list contains both numbers and strings, Python sorts the numerical values first and then sorts the strings in alphabetical order.

>>> a = ['python','scipy','numpy']
>>> a.sort()
>>> a
['numpy','python','scipy']

List comprehensions

A list comprehension allows building a list from another list. Let us consider the case where we need to generate a list of squares of numbers from 0 to 9. We will begin by generating a list of numbers from 0 to 9. Then we will determine the square of each element.

>>> a = list(range(10))
>>> print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = [x*x for x in a]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> b = []
>>> for x in a:
 b.append(x*x)
>>> print(b)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In line 1, a list is created containing values from 0 to 9 using the function “range” and the print command is given in line 2. In line number 4, list comprehension is performed by taking each element in ’a’ and multiplying by itself. The result of the list comprehension is shown in line 5. The same operation can be performed by using lines 6 to 8 but the list comprehension approach is compact in syntax as it eliminates two lines of code, one level of indentation, and a for-loop. It is also faster when applied to a large list.

For a new Python programmer, the list comprehension might seem daunting. The best way to understand and read a list comprehension is by imagining that you will first operate on the for-loop and then begin reading/writing the left part of the list comprehension. In addition to applying for-loop using list comprehension, you can also apply logical operations like if-else.

Tuples

Tuples are similar to lists except that they are not mutable, i.e., the length and the content of the tuple cannot be changed at runtime. Syntactically, the list uses [ ] while tuples use ( ). Similar to lists, tuples may contain any data type including other tuples. Here are a few examples:

>>> a = (1,2,3,4)
>>> print(a)
(1,2,3,4)
>>> b = (3,)
>>> c = ((1,2),(3,4))

In line 1, we define a tuple containing four elements. In line 4, we define a tuple containing only one element. Although the tuple contains only one element, we need to add the trailing comma, so that Python understands it as a tuple. Failure to add a comma at the end of this tuple will result in the value 3 being treated as an integer and not a tuple. In line 5, we create a tuple inside another tuple.

Sets

A set is an unordered collection of unique objects. To create a set, we need to use the function set or the operator {}. Here are some examples:

>>> s1 = set([1,2,3,4])
>>> s2 = set((1,1,3,4))
>>> print(s2)
set([1,3,4])

In line 1, we create a set from a list containing four values. In line 2, we create a set containing a tuple. The elements of a set need to be unique. Hence when the content of s2 is printed, we notice that the duplicates have been eliminated. Sets in Python can be operated using many of the common mathematical operations on sets such as union, intersection, set difference, symmetric difference, etc.

Since sets do not store repeating values and since we can convert lists and tuples to sets easily, they can be used to perform useful operations faster which otherwise would involve multiple loops and conditional statements. For example, a list containing only unique values can be obtained by converting the list to a set and back to a list. Here is an example:

>>> a = [1,2,3,4,3,5]
>>> b = set(a)
>>> print(b)
set([1,2,3,4,5])
>>> c = list(b)
>>> print(c)
[1,2,3,4,5]

In line 1, we create a list containing six values with one duplicate. We convert the list into a set by using the set() function. During this process, the duplicate value 3 is eliminated. We can then convert the set back to list using the list() function.

Dictionaries

Dictionaries store key-value pairs. A dictionary is created by enclosing a key-value pair inside {}.

>>> a = {
                     'lang':'python'
                     'ver': '3.6.6'
 }

Any member of the dictionary can be accessed using [ ] operator.

>>> print a['lang']
python

To add a new key,

>>> a['creator'] = 'Guido Von Rossum'
>>> print(a)
{'lang': 'python', 'ver': '3.6.6', 'creator': 'Guido Von
Rossum'}

In the example above, we added a new key called creator and stored the string, “Guido Von Rossum.”

In certain instances, the dictionary membership needs to be tested using the ‘in’ operator. To obtain a list of all the dictionary keys, use the keys() method.

1.5.2File Handling

This book is on image processing; however, it is important to understand and be able to include in your code, reading and writing text files so that the results of computation can be written or the input parameters can be read from external sources. Python provides the ability to read and write files. It also has functions, methods and modules for reading specialized formats such as the comma separated value (csv) file, Microsoft Excel (xls) format, etc. We will look into each method in this section.

The following code reads a csv file as a text file.

>>> fo = open('myfile.csv')
>>> for i in fo.readlines():
… print(i)
>>> fo.close()
Python,3.6.6
 
Django, 3.0.5
 
Apache, 2.4

The first line opens a file and returns a new file object which is stored in the file object “fo.” The method readlines in line 2, reads all the lines of input. The for-loop then iterates over each of those lines, and prints. The file is finally closed using the close method.

The output of the print command is a string. Hence, string manipulation using methods like split, strip, etc., needs to be applied in order to extract elements of each column. Also, note that there is an extra newline character at the end of each print statement.

Reading CSV files

Instead of reading a csv file as a text file, we can use the csv module.

>>> import csv
>>> for i in csv.reader(open('myfile.csv')):
… print(i)
['Python', '3.6.6']
['Django', ' 3.0.5']
['Apache', ' 2.4']

The first line imports the csv module. The second line opens and reads the content of the csv file using the reader function in the csv module. In every iteration of the loop, the content of one line of the csv file is returned and stored in the variable ‘i’. Finally, the value of ‘i’ is printed.

Reading Excel files

Microsoft Excel files can be read and written using the openpyxl module. The openpyxl module has to be installed before we can use it. To install this module, you can go to the Python prompt and type ‘pip install openpyxl’. Alternately, the module can be installed by selecting the Environment tab in Anaconda Navigator. Here is a simple example of reading an Excel file using the openpyxl module.

from openpyxl import load_workbook
wb = load_workbook('myfile.xlsx')
for sheet in wb:
 for row in sheet.values:
       for col in row:
         print(col, end=' | ')
       print()

In line 2, the open_workbook() function is used to read the file. We loop through all sheets in the file. In this particular example, there is only one sheet. Then we loop through each row in the sheet followed by looping through every column. In the print function where the value in a column is printed, we are indicating that a column must be separated from the next column by a | (pipe symbol). Finally the last print function adds a new line, so that the next row can be printed in a new line.

Date | Time |
2020-01-02 00:00:00 | 10:15:00 |
2020-01-05 00:00:00 | 11:00:00 |
2020-01-07 00:00:00 | 15:00:00 |

1.5.3User-Defined Functions

A function is a reusable section of code that may take input and may or may return an output. If there is any block of code that will be called many times, it is advisable to convert it to a function. Calls can then be made to this function.

A Python function can be created using the def keyword. Here is an example:

import math
def circleproperties(r):
 area = math.pi*r*r;
 circumference = 2*math.pi*r
 return area, circumference
 
 
a, c = circleproperties(5) # Radius of the circle is 5
print("Area and Circumference of the circle are", a, c)

The function circleproperties takes in one input argument, the radius (r). The return statement at the end of the function definition passes the computed values (in this case, area and circumference) to the calling function. To invoke the function, use the name of the function and provide the radius value as an argument enclosed in parentheses. Finally, the area and circumference of the circle are displayed using the print function call.

The variables area and circumference have local scope. Hence the variables cannot be invoked outside the body of the function. It is possible to pass on variables to a function that have global scope using the global statement.

When we run the above program, we get the following output:

Area and Circumference of the circle are 78.539 31.415

1.6Summary

Python is a popular high-level programming language. It is used for most common programming tasks such as scientific computation, text processing, building dynamic websites, etc.

Python distributions such as Anaconda Python distribution are pre-built with many scientific modules and enable scientists to focus on their research instead of installation of modules.

Python, like other programming languages, uses common relational and mathematical operators, comment statements, for-loops, if-else statements, etc.

To program like a Pythonista, use lists, sets, dictionary and tuples liberally.

Python can read most of the common text formats like CSV, Microsoft Excel, etc.

1.7Exercises

1.If you are familiar with any other programming language, list the differences between that language and Python.

2.Write a Python program that will print numbers from 10 to 20 using a for-loop.

3.Create a list of state names such as states = [‘Minnesota’, ‘Texas’, ‘New York’, ‘Utah’, ‘Hawaii’]. Add another entry ‘California’ to the end of the list. Then, print all the values of this list.

4.Print the content of the list from Question 3 and also the corresponding index using the list’s enumerate method in the for-loop.

5.Create a 2D list of size 3-by-3 with the following elements: 1, 2, 3 | 4, 5, 6 | 6, 7, 8

6.It is easy to convert a list to a set and vice versa. For example, a list mylist = [1, 1, 2, 3, 4, 4, 5]′ can be converted to a set using the command newset = set(mylist). The set can be converted back to a list using newlist = list(newset). Compare the contents of mylist and newlist. What do you infer?

7.Look up documentation for the join method and join the contents of the list [‘Minneapolis’,‘MN’,‘USA’] and obtain the string ‘Minneapolis, MN, USA.’

8.Consider the following Python code:

      a = [1,2,3,4,2,3,5]
      b = []
      for i in a:
       if i>2:
       b.append(i)
      print(b)

Rewrite the above code using list comprehension and reduce the number of lines.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.238.70