100% found this document useful (1 vote)
6 views

Introduction To Python With Applications In Optimization Image And Video Processing And Machine Learning David Bezlpez David Alfredo Bez Villegas instant download

The document introduces a book titled 'Introduction to Python with Applications in Optimization, Image and Video Processing, and Machine Learning' aimed at advanced undergraduate and graduate students in quantitative sciences. It covers both introductory and advanced Python topics, making it suitable for beginners and professionals alike. The book includes a solutions manual and additional resources for further learning.

Uploaded by

kholoduhk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
6 views

Introduction To Python With Applications In Optimization Image And Video Processing And Machine Learning David Bezlpez David Alfredo Bez Villegas instant download

The document introduces a book titled 'Introduction to Python with Applications in Optimization, Image and Video Processing, and Machine Learning' aimed at advanced undergraduate and graduate students in quantitative sciences. It covers both introductory and advanced Python topics, making it suitable for beginners and professionals alike. The book includes a solutions manual and additional resources for further learning.

Uploaded by

kholoduhk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

Introduction To Python With Applications In

Optimization Image And Video Processing And


Machine Learning David Bezlpez David Alfredo Bez
Villegas download
https://ebookbell.com/product/introduction-to-python-with-
applications-in-optimization-image-and-video-processing-and-
machine-learning-david-bezlpez-david-alfredo-bez-
villegas-57213568

Explore and download more ebooks at ebookbell.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

An Introduction To Statistics With Python With Applications In The


Life Sciences 2nd 2nd Edition Thomas Haslwanter

https://ebookbell.com/product/an-introduction-to-statistics-with-
python-with-applications-in-the-life-sciences-2nd-2nd-edition-thomas-
haslwanter-47278200

An Introduction To Statistics With Python With Applications In The


Life Sciences Thomas Haslwanter

https://ebookbell.com/product/an-introduction-to-statistics-with-
python-with-applications-in-the-life-sciences-thomas-
haslwanter-49052704

An Introduction To Statistical Learning With Applications In Python


1st Gareth James

https://ebookbell.com/product/an-introduction-to-statistical-learning-
with-applications-in-python-1st-gareth-james-50703060

The Statistics And Calculus With Python Workshop A Comprehensive


Introduction To Mathematics In Python For Artificial Intelligence
Applications Peter Farrell

https://ebookbell.com/product/the-statistics-and-calculus-with-python-
workshop-a-comprehensive-introduction-to-mathematics-in-python-for-
artificial-intelligence-applications-peter-farrell-23900110
Introduction To Python Programming And Developing Gui Applications
With Pyqt Bm Harwani

https://ebookbell.com/product/introduction-to-python-programming-and-
developing-gui-applications-with-pyqt-bm-harwani-33747390

Introduction To Tornado Modern Web Applications With Python Michael


Dory Adam Parrish Brendan Berg

https://ebookbell.com/product/introduction-to-tornado-modern-web-
applications-with-python-michael-dory-adam-parrish-brendan-
berg-43273008

Introduction To Computation And Programming Using Python With


Application To Understanding Data Mit Press John V Guttag

https://ebookbell.com/product/introduction-to-computation-and-
programming-using-python-with-application-to-understanding-data-mit-
press-john-v-guttag-35701908

Introduction To Computation And Programming Using Python With


Application To Understanding Data Mit Press John V John V Guttag

https://ebookbell.com/product/introduction-to-computation-and-
programming-using-python-with-application-to-understanding-data-mit-
press-john-v-john-v-guttag-36029556

Introduction To Computation And Programming Using Python With


Application To Understanding Data Mit Press John V Guttag

https://ebookbell.com/product/introduction-to-computation-and-
programming-using-python-with-application-to-understanding-data-mit-
press-john-v-guttag-37332568
Introduction to Python
Introduction to Python: with Applications in Optimization, Image and Video Pro-
cessing, and Machine Learning is intended primarily for advanced undergraduate and
graduate students in quantitative sciences such as mathematics, computer science, and
engineering. In addition to this, the book is written in such a way that it can also serve as a
self-contained handbook for professionals working in quantitative fields including finance,
IT, and many other industries where programming is a useful or essential tool.

The book is written to be accessible and useful to those with no prior experience of Py-
thon, but those who are somewhat more adept will also benefit from the more advanced
material that comes later in the book.

Features
•  Covers introductory and advanced material. Advanced material includes lists, diction-
aries, tuples, arrays, plotting using Matplotlib, object-oriented programming
•  Suitable as a textbook for advanced undergraduates or postgraduates, or as a reference
for researchers and professionals
•  Solutions manual, code, and additional examples are available for download.
Chapman & Hall/CRC
The Python Series
About the Series
Python has been ranked as the most popular programming language, and it is widely used
in education and industry. This book series will offer a wide range of books on Python
for students and professionals. Titles in the series will help users learn the language at an
introductory and advanced level, and explore its many applications in data science, AI, and
machine learning. Series titles can also be supplemented with Jupyter notebooks.

Image Processing and Acquisition using Python, Second Edition


Ravishankar Chityala and Sridevi Pudipeddi

Python Packages
Tomas Beuzen and Tiffany-Anne Timbers

Statistics and Data Visualisation with Python


Jesús Rogel-Salazar

Introduction to Python for Humanists


William J.B. Mattingly

Python for Scientific Computation and Artificial Intelligence


Stephen Lynch

Learning Professional Python Volume 1: The Basics


Usharani Bhimavarapu and Jude D. Hemanth

Learning Professional Python Volume 2: Advanced


Usharani Bhimavarapu and Jude D. Hemanth

Learning Advanced Python from Open Source Projects


Rongpeng Li

Foundations of Data Science with Python


John Mark Shea

Data Mining with Python: Theory, Applications, and Case Studies


Di Wu

A Simple Introduction to Python


Stephen Lynch

Introduction to Python: with Applications in Optimization, Image and Video Pro-


cessing, and Machine Learning
David Baez-Lopez and David Alfredo Báez Villegas

For more information about this series please visit: https://www.crcpress.com/Chapman-


-HallCRC/book-series/PYTH
Introduction to Python
With Applications in
Optimization, Image and Video
Processing, and Machine Learning

David Báez-López
David Alfredo Báez Villegas
Designed cover image: ShutterStock Images

First edition published 2024


by CRC Press
2385 NW Executive Center Drive, Suite 320, Boca Raton FL 33431

and by CRC Press


4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN

CRC Press is an imprint of Taylor & Francis Group, LLC

© 2024 David Báez-López, David Alfredo Báez Villegas

Reasonable efforts have been made to publish reliable data and information, but the author and publisher
cannot assume responsibility for the validity of all materials or the consequences of their use. The authors
and publishers have attempted to trace the copyright holders of all material reproduced in this publica-
tion and apologize to copyright holders if permission to publish in this form has not been obtained. If
any copyright material has not been acknowledged please write and let us know so we may rectify in any
future reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, trans-
mitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter
invented, including photocopying, microfilming, and recording, or in any information storage or retrieval
system, without written permission from the publishers.

For permission to photocopy or use material electronically from this work, access www.copyright.com
or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-
750-8400. For works that are not available on CCC please contact [email protected]

Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used
only for identification and explanation without intent to infringe.

ISBN: 978-1-032-11767-6 (hbk)


ISBN: 978-1-032-11910-6 (pbk)
ISBN: 978-1-003-22211-8 (ebk)

DOI: 10.1201/9781003222118

Typeset in CMR10 font


by KnowledgeWorks Global Ltd.

Publisher’s note: This book has been prepared from camera-ready copy provided by the authors.
Dedicated to Laura and Gary, my children, and to
Alina, my granddaughter.
The future is yours.

For David, Lucero, Fidel, and Eric.


Who have made everything I have achieved possible.
Contents

Preface xiii

Author Biographies xv

1 Introduction to Python 1
1.1 What is the Python Programming Language . . . . . . . . 1
1.2 The Python Programming Language . . . . . . . . . . . . . 1
1.2.1 Downloading Python . . . . . . . . . . . . . . . . . 2
1.2.2 The Python’s Integrated Development and Learning
Environment . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Book Organization . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5.1 Types of Variables . . . . . . . . . . . . . . . . . . . 5
1.5.2 Variable Assignment . . . . . . . . . . . . . . . . . . 6
1.5.3 Basic Operations . . . . . . . . . . . . . . . . . . . . 7
1.6 Input and Output in Python . . . . . . . . . . . . . . . . . 7
1.6.1 Escape Sequences . . . . . . . . . . . . . . . . . . . 9
1.7 Programs in Python . . . . . . . . . . . . . . . . . . . . . . 10
1.8 Comments in a Program . . . . . . . . . . . . . . . . . . . . 11
1.8.1 Operations with Integer and Floating-point Numbers 13
1.9 Functions in Python . . . . . . . . . . . . . . . . . . . . . . 13
1.10 Modules and Libraries . . . . . . . . . . . . . . . . . . . . . 14
1.11 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.11.1 Arithmetic Operators . . . . . . . . . . . . . . . . . 16
1.11.2 Relational Operators . . . . . . . . . . . . . . . . . 16
1.11.3 Logical Operators . . . . . . . . . . . . . . . . . . . 17
1.11.4 Assignment Operators . . . . . . . . . . . . . . . . . 18
1.12 Alphanumeric Variables . . . . . . . . . . . . . . . . . . . . 19
1.13 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.14 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.15 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.16 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.17 Python Instructions for Chapter 1 . . . . . . . . . . . . . . 26
1.18 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.19 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

vii
viii Contents

2 Conditionals and Loops 30


2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.2 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.3 The Conditional if-else . . . . . . . . . . . . . . . . . . . 34
2.4 Nested Conditionals . . . . . . . . . . . . . . . . . . . . . . 35
2.5 Exceptions and Errors . . . . . . . . . . . . . . . . . . . . . 38
2.6 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.7 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . 40
2.8 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.8.1 The Function range . . . . . . . . . . . . . . . . . . 47
2.9 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.10 The Instruction break . . . . . . . . . . . . . . . . . . . . . 53
2.11 The Instruction continue . . . . . . . . . . . . . . . . . . . 54
2.12 Additional Examples . . . . . . . . . . . . . . . . . . . . . . 56
2.13 Python Instructions for Chapter 2 . . . . . . . . . . . . . . 59
2.14 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.16 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . 64

3 Data Structures: Strings, Lists, Tuples, and Dictionaries 65


3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.2.1 Concatenation or Sum and Multiplication of Strings 67
3.3 Functions on Strings . . . . . . . . . . . . . . . . . . . . . . 68
3.3.1 Length of a String . . . . . . . . . . . . . . . . . . . 68
3.3.2 Split of a String . . . . . . . . . . . . . . . . . . . . 68
3.4 Immutability of Strings . . . . . . . . . . . . . . . . . . . . 69
3.4.1 Functions on Strings . . . . . . . . . . . . . . . . . . 70
3.4.2 Conditions and Loops . . . . . . . . . . . . . . . . . 73
3.5 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.5.1 Operations on Lists . . . . . . . . . . . . . . . . . . 74
3.5.2 Mutability . . . . . . . . . . . . . . . . . . . . . . . 75
3.5.3 Conversion from a List of Strings to a Single String 75
3.6 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
3.6.1 Tuple Assignment . . . . . . . . . . . . . . . . . . . 78
3.6.2 Functions on Tuples . . . . . . . . . . . . . . . . . . 79
3.7 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.7.1 Functions for Dictionaries . . . . . . . . . . . . . . . 80
3.8 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.8.1 Sets by Comprehension . . . . . . . . . . . . . . . . 83
3.8.2 Frozen Sets . . . . . . . . . . . . . . . . . . . . . . . 84
3.9 Python Instructions for Chapter 3 . . . . . . . . . . . . . . 84
3.10 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Contents ix

4 Arrays 88
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
4.2 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . 88
4.3 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4.3.1 Access to Vectors . . . . . . . . . . . . . . . . . . . 93
4.3.2 Vectors by Comprehension . . . . . . . . . . . . . . 93
4.3.3 The Instruction append . . . . . . . . . . . . . . . . 94
4.4 Examples with Vectors in Python . . . . . . . . . . . . . . . 94
4.4.1 Vector Sorting . . . . . . . . . . . . . . . . . . . . . 102
4.5 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
4.6 Arrays in Python . . . . . . . . . . . . . . . . . . . . . . . . 105
4.6.1 Array Generation by Indexing . . . . . . . . . . . . 106
4.6.2 Array Generation by Comprehension . . . . . . . . 108
4.7 Matrix Operations using Linear Algebra with numpy . . . . 110
4.7.1 Sum, Difference, and Multiplication of Matrices . . 110
4.7.2 Sum of Matrices . . . . . . . . . . . . . . . . . . . . 110
4.7.3 Product of a Matrix by a Matrix . . . . . . . . . . . 111
4.7.4 Product of Matrices in Python . . . . . . . . . . . . 113
4.7.4.1 Matrix Multiplication Using np.dot . . . . 113
4.7.4.2 Matrix Multiplication Using np.matrix . . 113
4.8 Special Matrices . . . . . . . . . . . . . . . . . . . . . . . . 116
4.8.1 The Identity Matrix . . . . . . . . . . . . . . . . . . 116
4.8.2 The Transpose Matrix . . . . . . . . . . . . . . . . . 116
4.8.3 Transpose by Comprehension . . . . . . . . . . . . . 118
4.9 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
4.10 Arrays in Pandas . . . . . . . . . . . . . . . . . . . . . . . . 125
4.10.1 Data Frames . . . . . . . . . . . . . . . . . . . . . . 129
4.10.2 Generation of Data Frames . . . . . . . . . . . . . . 132
4.10.3 Functions for Series and Data Frames . . . . . . . . 133
4.11 Python Instructions for Chapter 4 . . . . . . . . . . . . . . 136
4.12 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
4.13 Selected Bibliography . . . . . . . . . . . . . . . . . . . . . 137
4.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

5 Functions 142
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
5.2 Subprograms . . . . . . . . . . . . . . . . . . . . . . . . . . 142
5.3 Functions in Python . . . . . . . . . . . . . . . . . . . . . . 143
5.4 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
5.5 Anonymous Functions or lambda Functions . . . . . . . . . 149
5.6 Pass by Reference . . . . . . . . . . . . . . . . . . . . . . . 150
5.7 Local and Global Variables . . . . . . . . . . . . . . . . . . 151
5.8 Keyword and Default Arguments . . . . . . . . . . . . . . . 152
5.9 Variable-length Arguments . . . . . . . . . . . . . . . . . . 154
5.10 Additional Examples . . . . . . . . . . . . . . . . . . . . . . 155
x Contents

5.11 Python Instructions in Chapter 5 . . . . . . . . . . . . . . . 162


5.12 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
5.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

6 Object-Oriented Programming 165


6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
6.2 The Object-Oriented Programming Paradigm . . . . . . . . 165
6.3 Classes in Python . . . . . . . . . . . . . . . . . . . . . . . 167
6.3.1 Creation and Use of a Class . . . . . . . . . . . . . 168
6.3.2 Declaration and Use of Setters and Getters . . . . . 169
6.3.3 Static Methods . . . . . . . . . . . . . . . . . . . . . 171
6.3.4 Encapsulation . . . . . . . . . . . . . . . . . . . . . 174
6.3.5 Inheritance . . . . . . . . . . . . . . . . . . . . . . . 176
6.3.6 Overloading . . . . . . . . . . . . . . . . . . . . . . 178
6.4 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
6.5 Python Instructions for Chapter 6 . . . . . . . . . . . . . . 185
6.6 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
6.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
6.8 Selected Bibliography . . . . . . . . . . . . . . . . . . . . . 186

7 Reading and Writing to Files 187


7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
7.2 Writing Data to a File . . . . . . . . . . . . . . . . . . . . . 187
7.2.1 Writing Alphanumeric Data to a File . . . . . . . . 188
7.2.2 The Instruction with . . . . . . . . . . . . . . . . . 190
7.3 Writing Numerical Data to a File . . . . . . . . . . . . . . . 190
7.4 Data Reading from a File . . . . . . . . . . . . . . . . . . . 194
7.4.1 Reading Data from a File . . . . . . . . . . . . . . . 194
7.4.2 The Instruction readline . . . . . . . . . . . . . . . 197
7.4.3 The Instruction readlines . . . . . . . . . . . . . . 198
7.5 Reading and Writing Data from and To Excel . . . . . . . . 199
7.6 Reading and Writing Binary Files . . . . . . . . . . . . . . 204
7.7 Python Instructions in Chapter 7 . . . . . . . . . . . . . . . 209
7.8 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
7.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

8 Plotting in Python 211


8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
8.2 Plots in Two Dimensions . . . . . . . . . . . . . . . . . . . . 211
8.2.1 Color and Marker Options . . . . . . . . . . . . . . 218
8.3 The Package seaborn . . . . . . . . . . . . . . . . . . . . . 221
8.4 Other Two-dimensional Plots . . . . . . . . . . . . . . . . . 223
8.4.1 Polar Plots . . . . . . . . . . . . . . . . . . . . . . . 224
8.5 Pie Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
8.5.1 Histogram Plots . . . . . . . . . . . . . . . . . . . . 229
Contents xi

8.5.2 Stem Plots . . . . . . . . . . . . . . . . . . . . . . . 233


8.5.3 Scatter Plots . . . . . . . . . . . . . . . . . . . . . . 235
8.6 Multiple Figures . . . . . . . . . . . . . . . . . . . . . . . . 237
8.6.1 Plots in Separate Figures . . . . . . . . . . . . . . . 237
8.6.2 Subplots . . . . . . . . . . . . . . . . . . . . . . . . 239
8.7 Three-Dimensional Plots . . . . . . . . . . . . . . . . . . . . 242
8.8 Python Instructions for Chapter 8 . . . . . . . . . . . . . . 254
8.9 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
8.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
8.11 Selected Bibliography . . . . . . . . . . . . . . . . . . . . . 257

9 Optimization 258
9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
9.2 Optimization Concepts . . . . . . . . . . . . . . . . . . . . . 258
9.2.1 Parameters, Variables, and Functions . . . . . . . . 259
9.3 General Format of the Optimization Process . . . . . . . . 260
9.4 Optimization with Python . . . . . . . . . . . . . . . . . . . 261
9.5 The minimize Function . . . . . . . . . . . . . . . . . . . . 262
9.6 Linear Programming . . . . . . . . . . . . . . . . . . . . . . 271
9.7 Quadratic Programming . . . . . . . . . . . . . . . . . . . . 280
9.8 Python Instructions for Chapter 9 . . . . . . . . . . . . . . 283
9.9 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
9.10 Selected Bibliography . . . . . . . . . . . . . . . . . . . . . 284

10 Image Processing with OpenCV 285


10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
10.2 Reading and Writing Images and Videos . . . . . . . . . . . 287
10.2.1 Image Representation in Python . . . . . . . . . . . 290
10.3 Video Capture and Display . . . . . . . . . . . . . . . . . . 293
10.3.1 Saving a Video File . . . . . . . . . . . . . . . . . . 294
10.4 Binary Images . . . . . . . . . . . . . . . . . . . . . . . . . 295
10.5 Histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
10.5.1 Histogram Equalization . . . . . . . . . . . . . . . . 301
10.6 Draw Geometric Shapes and Text on an Image . . . . . . . 303
10.7 Contour Detection . . . . . . . . . . . . . . . . . . . . . . . 305
10.8 Frequency Domain Processing . . . . . . . . . . . . . . . . . 309
10.9 Noise Addition to Images . . . . . . . . . . . . . . . . . . . 317
10.9.1 Denoising . . . . . . . . . . . . . . . . . . . . . . . . 318
10.10 Morphological Image Processing . . . . . . . . . . . . . . . 321
10.10.1 Erosion and Dilation . . . . . . . . . . . . . . . . . 321
10.10.2 Dilation and Erosion . . . . . . . . . . . . . . . . . 322
10.11 Python Instructions in Chapter 10 . . . . . . . . . . . . . . 327
10.12 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
10.13 Selected Bibliography . . . . . . . . . . . . . . . . . . . . . 328
xii Contents

11 Machine Learning 329


11.1 Types of Machine Learning Systems . . . . . . . . . . . . . 329
11.1.1 Supervised Machine Learning . . . . . . . . . . . . . 329
11.1.2 Multiple Features . . . . . . . . . . . . . . . . . . . 334
11.1.3 Feature Scaling . . . . . . . . . . . . . . . . . . . . . 335
11.2 Gradient Descent Algorithm . . . . . . . . . . . . . . . . . . 336
11.3 Multivariate Regression . . . . . . . . . . . . . . . . . . . . 342
11.3.1 Feature Scaling for the Multivariate Case . . . . . . 342
11.4 The Normal Equation . . . . . . . . . . . . . . . . . . . . . 343
11.5 The Package scikit-learn . . . . . . . . . . . . . . . . . . 346
11.6 Polynomial Regression . . . . . . . . . . . . . . . . . . . . . 352
11.7 Classification with Logistic Regression . . . . . . . . . . . . 357
11.7.1 Logistic regression . . . . . . . . . . . . . . . . . . . 358
11.8 Unsupervised Learning . . . . . . . . . . . . . . . . . . . . . 377
11.9 Clustering Using k-means . . . . . . . . . . . . . . . . . . . 378
11.9.1 Cost Function . . . . . . . . . . . . . . . . . . . . . 379
11.10 Python Instructions in Chapter 11 . . . . . . . . . . . . . . 386
11.11 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 386

12 Neural Networks 387


12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
12.2 A Model for a Neuron . . . . . . . . . . . . . . . . . . . . . 387
12.3 Activation Functions . . . . . . . . . . . . . . . . . . . . . . 393
12.4 Cost Function . . . . . . . . . . . . . . . . . . . . . . . . . . 395
12.5 Tensor Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
12.6 Convolutional Neural Networks . . . . . . . . . . . . . . . . 404
12.6.1 Padding . . . . . . . . . . . . . . . . . . . . . . . . . 406
12.6.2 Strided convolution . . . . . . . . . . . . . . . . . . 408
12.6.3 Pooling . . . . . . . . . . . . . . . . . . . . . . . . . 408
12.7 A Layer of a Convolutional Filter . . . . . . . . . . . . . . . 409
12.8 Python Instructions in Chapter 12 . . . . . . . . . . . . . . 421
12.9 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 422

A Installation of Libraries and Packages and Running the


Scripts 423
A.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
A.2 Running a Script using Google Colab . . . . . . . . . . . . 423
A.3 Installation of Python . . . . . . . . . . . . . . . . . . . . . 423
A.4 Modules and Libraries Installation with pip . . . . . . . . . 426
A.5 Installation of Modules or Libraries . . . . . . . . . . . . . . 427
A.6 Installation of OpenCV . . . . . . . . . . . . . . . . . . . . 428
A.6.1 Installation in Windows for the IDLE . . . . . . . . 428
A.6.2 Installation in the Mac for the IDLE . . . . . . . . 429

Index 431
Preface

This book was born from a programming course taught at Universidad de


las Americas-Puebla, Mexico. In that time, Python was gaining strength as
a powerful programming language, but nobody foresaw that it could become
one of the most used programming languages. In fact, the TIOBE index ranks
Python as the most used programming language in the world (April 2024).
This fact is due to the characteristics it possesses. Python is a high-level
programming language that supports procedural, imperative, object-oriented,
and functional modes. This book covers the first three modes. Python has
a simple easy to learn syntax. Python supports modules, libraries, and pack-
ages, encouraging program modularity and code reuse. Python and the major-
ity of supporting libraries are open-source, a characteristic that is appealing
for designers and users. This fact has enriched the modules and tools freely
available. In fact, Python and most of the libraries available are open-source.
There are libraries for science, engineering, finance, mathematics, statistics,
machine learning, data analysis, and many more.
The book may be divided in two parts. The first part, comprised of Chap-
ters 1 to 8, can be used by a person interested in learning Python from scratch;
that is, he/she does not have any knowledge of Python. The learning goes from
installing Python to implementing functions and generating complex plots.
The second part covers advanced topics such as optimization, image and video
processing, and machine learning using Keras, TensorFlow, and neural net-
works. Throughout the book, the use of external modules or libraries makes
the Python programs very powerful.
The book starts with basic Python instructions and introduces the con-
cept of algorithmic solutions. The basic concepts in a Python program are pre-
sented. The second chapter is about conditionals and loops. The availability of
these tools are of paramount importance in any programming language.The
concepts of nested conditionals and loops are presented. These instructions
allow the implementation of complex algorithms. The concepts of data struc-
tures in Python is covered in Chapter 3. The data structures are covered in
detail together with their methods. Arrays are covered next. They are de-
fined and used in matrix algebra. Arrays from Pandas are presented. Chapter
5 treats functions and their use in larger programs. Chapter 6 presents the
object-oriented programming paradigm. The different properties that are as-
sociated are described. Reading data from files and writing data to files is pre-
sented in Chapter 7. The visualization of data is an important part of modern

xiii
xiv Preface

programming languages and Python has associated a library that permits to


create complex plots, either in two dimensions or in three dimensions. Another
package called Seaborn is also used in the generation of graphs. The first eight
chapters can be used by a novice student to gain a good expertise in Python
programming.
The second part of the book is four chapters long and it covers concepts of
optimization using Scipy that has a library of functions to perform optimiza-
tion. Several examples show the techniques used. Image and video processing
are implemented using OpenCV, a package especially designed to handle im-
ages and video. Machine learning is the topic of the last two chapters. Chapter
11 implements supervised and unsupervised learning using Keras and Tensor-
Flow. Chapter 12 makes use of neural networks to implement machine learning
algorithms. The version of Python used in the book examples was Python ver-
sion 3.12.
Finally, the authors wish thank the staff at Taylor and Francis for the
support to take this project to a happy ending. Special thanks Callum Fraser
and Mansi Kabra for the encouragement to complete the project.

David Báez-López
David Alfredo Báez Villegas

Cholula, Mexico, March 2024.


Author Biographies

David Báez-López obtained a B.S. in Physics in 1973 from the Universi-


dad Autonoma de Puebla, Mexico where he graduated with Honors. He also
attended the University of Arizona where he obtained the M.S. and Ph.D.
degrees, both in Electrical Engineering, in 1976 and 1979, respectively. From
1979 to 1984 he was a researcher at Department of Electronics at National
Institute for Astrophysics, Optics, and Electronics, located in Tonantzintla,
Puebla, Mexico, and he became Head of The Department of Electronics and
Graduate Coordinator. In 1985 he joined Universidad de las Americas-Puebla,
Mexico as a professor and in 1988 became Chairman of the Department of
Electronic Engineering until 1994. He was a member of the National Systems
of Researchers. He retired in 2015 and now is Academic Director in EDUPRO-
TEC A.C. in Puebla, Mexico, a non-governmental organization dedicated to
the improvement of mathematics learning, among other topics. He has au-
thored books on circuit simulation, MATLAB, Python, and Mathematica in
English and Spanish. A Chinese edition of a book on MATLAB was published
in 2017.

David Alfredo Báez Villegas is an electrical engineer with master’s degrees


in Project Management (École de Technologie Supérieure, Montréal) and Elec-
trical Engineering (Texas A&M University, Texas Tech University). He holds
a B. S. in Electronic Engineering from Universidad de las Americas-Puebla,
Mexico, and M.S in Electrical Engineering from Texas Tech University and
from Texas A&M University (TAMU). He worked as an assistant to the Office
of International Affairs at TAMU before moving to Montreal, Canada. Since
2015 he has been a consultant in project management.

xv
Chapter 1
Introduction to Python

1.1 What is the Python Programming Language


This book is about the Python programming language. This is a high-level
programming language. In April of 2024, Python ranked as the most used lan-
guage among many others such as C, C++, MATLAB, Java, and JavaScript,
just to mention a few.
When a program is written in any programming language, what the user
writes is called the source code. This code has to be converted to machine
language code, and for this purpose, a compiler is used. The result is a file
known as the object code. Finally, from this object code the compiler produces
the executable file and this file is independent of the language used.
Other programming languages compile the source code on an instruction-
by-instruction basis. These types of compilers are known as interpreters.
A task that the compiler does is to check the syntax of the source code.
If there are any errors, the compilation process indicates all the errors in the
source code. If there are no errors, the executable file is created. In the case of
the interpreters, an instruction is checked, and if there is an error, the process
is interrupted to correct the error. The disadvantage of an interpreter is that
if there are 10 errors in the program, then 10 compilations and 10 corrections
have to be made.
Python uses an interpreter to check the syntax. This fact makes the compi-
lation process slower as compared to other programming languages such as C,
C++, and Java, to name a few. However, it is possible to create an executable
file that runs faster as compared to the interpreter.

1.2 The Python Programming Language


Python was introduced in its version 0.9.0 in 1991. It was conceived by Guido
von Rossum. It is a high-level language and is open source. The most used
versions are Python 2 and Python 3. In this book Python 3 is used.

DOI: 10.1201/9781003222118-1 1
2 Python with Optimization, ...

FIGURE 1.1: Python’s IDLE.

Python is platform independent and that is an important advantage. A


Python program written in Windows can be used in Linux or Mac computers,
and vice versa. The only requirement for this is that the program is written
in the same Python version.
Python has a standard library of functions. This fact allows programmers
and designers a great deal of flexibility in producing very powerful code. In ad-
dition, users have developed modules and libraries, most of them open-source
and free. Those modules can be used to develop graphical interfaces, plotting,
develop financial, mathematical, and engineering applications, to name a few.

1.2.1 Downloading Python


Python can be downloaded from The Python Software Foundation page at
www.python.org. The version used in this book is the most recent version,
version 3.12 at the beginning of 2024. To install it, the instructions indicated
in Appendix A need to be followed. It is important to install on disk C: for
windows platforms. Once installed, it is ready to use.

1.2.2 The Python’s Integrated Development and Learning


Environment
Python has an Integrated Development and Learning Environment (IDLE),
where variables and functions are defined and executed. Also, results for scripts
and programs are displayed at the IDLE. Figure 1.1 shows the IDLE. In this
figure, the prompt is shown with the symbols >>>. It can be seen that the
Python version has a word length of 64 bits.
Introduction to Python 3

1.3 Book Organization


The book is designed to be read by students and programmers with no pre-
vious experience in Python programming. It can also be used by people with
previous knowledge of Python and who require a deeper knowledge of it. In
any case, the examples in this book as well as the exercises at the end of the
chapters help to improve the skills in Python programming. The book can
also be used by expert Python programmers to learn the topics on image and
video processing, optimization, and machine learning, topics that have a great
deal of applications.
Chapter 1 is introductory to Python. Operations are made from the IDLE,
also known as the Python shell. From there, readers learn to make simple pro-
grams, known as scripts. The user learns to use basic functions by importing
modules and libraries of functions.
Chapter 2 treats the concepts of conditionals and loops which serve to
control the flow of a program.
Chapters 3 and 4 give a treatment for data structures such as strings, lists,
dictionaries, tuples, vectors, and matrices.
Functions are the subject of Chapter 5. In this chapter the ways functions
can be defined is covered.
Data read and written to files is covered in Chapter 6.
Chapter 7 presents the Object-Oriented Programming (OOP) paradigm.
Data visualization is covered in Chapter 8 where matplotlib is used to
obtain high-quality plots.
Python can be used for image and video processing. This topic is covered
in Chapter 9.
Optimization using Python and Scipy can be used to optimize functions.
This is the topic of Chapter 10.
The last two Chapters, 11 and 12, are dedicated to the use of Python
in machine learning, which include neural networks using Scikit-Learn and
TensorFlow.

1.4 Algorithms
An algorithm is a way to solve a problem. Examples of algorithms in everyday
life can be found in cooking recipes, instructions to drive to a place, how
to assemble pre-built furniture, fixing a computer by a technician, etc. In
all of these examples, there is a sequence of steps to successfully complete
the required assignment. The last step in the design of the algorithm is the
4 Python with Optimization, ...

testing step to insure that the algorithm behaves as expected. A more accurate
definition of an algorithm is:

An algorithm is a precise finite sequence of steps to solve a prob-


lem and to achieve a result.

The necessary steps to implement an algorithm are:

1. Analysis of the problem.


2. Design of the algorithm to solve it.
3. Verification of the algorithm.
4. Implementation of the algorithm in some programming lan-
guage.
5. Testing of the algorithm with known data.

The word algorithm is to honor the Arab mathematician Al


Khuarismi who is believed to be the creator of the first algorithm
to obtain the roots of an equation.

1.5 Variables
Variables are data that can take on different values depending upon the algo-
rithm. Variables must have a name to identify them. The name is a string of
alphanumeric characters which must start with a letter. The name may have
numbers and underscores, either capital or lower-case letters. Also, the name
is unique; that is, no other variable can have the same name. In Python, a cap-
ital letter is different from a lower-case one; thus, the variable A1 is different
from a1.
In every programming language, there are reserved words called keywords
and cannot be used as variable names. Python 3 has 33 keywords. To display
them in the IDLE type:

>>> import keyword


>>> keyword.kwlist

and the result is:

[‘False’, ‘None’, ‘True’, ‘__peg_parser__’, ‘and’, ‘as’, ‘assert’, ‘async’,


‘await’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’,
‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’,
Introduction to Python 5

‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’,


‘yield’]

Names of variables must start with a letter and can only have
letters, numbers, and underscores.

1.5.1 Types of Variables


In Python, and in any other programming language, each variable has a cer-
tain type. The most used variable types in Python are floating-point, integer,
alphanumeric or text, and boolean.
A variable is a floating-point variable if it has a decimal point and an
integer part to the left of the decimal point, and a fractional part to the right
of the decimal point. The fractional part is known as the mantissa. Thus,
floating-point variables are of the form:

12345.987

A variable is an integer one if it does not have a fractional part; that is,
it only has an integer part. For example, a = 6, speed = 45, depth = -3 are
integer variables.
An alphanumeric variable or text variable is a string of alphanumeric
characters which may include letters, numbers, and any other character. These
variables are called strings and are enclosed in single or double quotes such
as “elephant”, ‘silver_coin’, ‘year 2025’. The last string includes a blank
space which is counted as a character.
A boolean variable is a variable that can only take the values True or
False. They are used whenever a decision is taken. Boolean variables are
named after George Boole, a British mathematician who invented what is
now called Boole’s algebra.
The type of the variable is assigned when the variable is defined. Thus,
the expression a = 3 defines an integer variable, b = -3.24 defines a floating-
point variable, c = True defines a boolean variable, and d = “animal” defines
a string.
Python is a dynamic-type language. This means that a variable of a cer-
tain type can be redefined in a program with another type. For example,
the variable d = “animal” can be redefined in the program as d = -0.32, a
floating-point variable, and later on redefined as d = True, a boolean variable.
To find out what is the type of a variable the type instruction is used. For
example, if variables a, b, c, and d are given by:

>>> a = 2
>>> b = 5.6
>>> c = True
>>> d = ‘ENIAC’
6 Python with Optimization, ...

The type of these variables is then:

>>> type(a)
<class ‘int’>
>>> type(b)
<class ‘float’>
>>> type(c)
<class ‘bool’>
>>> type(d)
<class ‘str’>

It can be seen that a is an integer variable, b is a floating-point one, c is a


boolean variable, and finally, d is a string.
The type of any variable can be changed by using the functions int
to change to integer, float to change to floating-point, bool to change to
boolean, and str to change to string.

Example 1.1 Change of type for a variable


The variable a = 9 has an integer type. The type of this variable can be
changed as:

>>> float(a)
1.0
>>> bool(a)
True
>>> str(a)
‘1’

1.5.2 Variable Assignment


The value for variables a1 and a2 are assigned as:

a1 = 4
a2 = 8

In this way, a1 has the value 4 and a2 has the value 8. It is said that the value
4 is assigned to a1 and the value 8 is assigned to a2. This process is called an
assignment. If now the following is done:

a1 = a2

Now a1 is assigned the value of a2; thus, the new value of a1 is 8. The old
value of 4 is lost. The value of a2 has been assigned to a1.
Introduction to Python 7

1.5.3 Basic Operations


Basic operations are implemented in a traditional way in the IDLE. Usually,
the result is assigned to a variable. For example:

>>> a = 3
>>> b = 5
>>> a + b

gives a result:

If a new variable is defined, the result is not automatically displayed. The user
must write the variable name at the Python prompt, as:

>>> c = a + b
>>> c

and the result is displayed:

1.6 Input and Output in Python


In the Python IDLE and in programs, the input of data is done using the
instruction input. This instruction reads data as strings of data. Thus, it is
natural to read in text. However, most of the data to be processed in Python
programs or scripts is numerical data and the strings read in must be con-
verted. This conversion can be done by changing the type of the strings. For
example, to read a floating-point number use:

x = input( )
x = float(x)

These two instructions can be combined in a single row by assigning the type
to the string read with the input instructions. For example:

x = float(input( ))

The argument of the input instruction can have a text between double or
single quotation marks to indicate users what data is being read, as in:
8 Python with Optimization, ...
TABLE 1.1: Format options
Symbol Output type Example
d,i Signed integer -123
u Unsigned integer 123
f Floating-point 123.45
e Scientific notation 1.2345 e+002
g Shortest representation between f and e 123.45
c Character ‘a’
s String “program”

>>> g = float(input(“Enter the value of gravity: ”))

Enter the value of gravity: 9.80

This indicates that the variable g is a floating-point one and its value is g =
9.8.
For the output of data, the instruction print can be used. To display the
value of g above, it can be done with:

print(g)

This instruction can also have a text in it as:

print(“The value of gravity is: ”, g)

A format can be added inside the instruction print. The format options
are shown in Table 1.1. A formatting instruction can be added inside a print
instruction as in this example for the gravity as:

>>> print(“The value of gravity is %0.3f: ”, g))

that displays:

The value of gravity is 9.800

The use of a print instruction provides a line feed. If there are two con-
secutive print instructions, the second print instruction is displayed in the
next line. For example,

>>> print(“The value of gravity is : ”)


>>> print(g)

produces:
Introduction to Python 9

The value of gravity is :


9.8

If it is desired to display in the same line, an end-line sign must be specified.


This end-line sign is end = “ ”. These instructions must be within a script.
For the variables a = 4, and b = -7 as:

print(“The value of a is: ”, a, end = “. ”)


print(“The value of b is: ”, b)

The data is displayed as:

The value of a is: 4. The value of b is: -7

Formatted string literals, called f-strings, can include the value of a


Python expression by using the prefix f and writing the expression between
braces. The last example can be written as

a = 4
b = -7
print(f‘The value of a is: {a}. The value of b is: {b}’)

which produces the same result.


Python uses the Unicode standard which is a standard representa-
tion for characters and strings. This lets Python programs work with all
these different possible characters. The Unicode standard is available at
https://www.unicode.org. For example, the number π, which is available in
the library math, has the Unicode \u03C0. To print out the value of the number
π the following is used:

from math import pi


>>> print(“The value of \u03C0 is %0.10f: ”, pi))

Libraries or modules are explained later in the chapter.

1.6.1 Escape Sequences

An escape sequence is a sequence of characters used inside a string. All escape


sequences consist of two or more characters and in every case the first character
is a backslash \. The following characters determine the interpretation of the
escape sequence. Some of the most used escape sequences are shown in Table
1.2.
10 Python with Optimization, ...
TABLE 1.2: Escape sequences
Escape sequence Meaning
\b Backspace
\f Formfeed Page Break
\n Newline (Line Feed)
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab
\’ Apostrophe
\\ Backslash

Example 1.2 Escape sequences


An example of escape sequences is in the use of the print instruction. Suppose
the variables a = 1, b = 2, and c = 3 are to be printed and separated by
the same distance using a horizontal tab escape sequence. Then, the print
instruction is:

>>> print(a, ‘\t’, b, ‘\t’, c)

and the data displayed is:

1 2 3

However, if the data is to be written in different rows, then a newline escape


sequence is used and the print instruction is:

>>> print(a, ‘\n’, b, ‘\n’, c)

and the result is:

1
2
3

1.7 Programs in Python


To perform complex computations a program is written in Python. This pro-
gram is stored in a separate file and run from the Python IDLE. Programs in
Python are also referred to as scripts. An example of a program is the script
to solve a first-degree equation of the form:
Introduction to Python 11

ax + b = c
that can be solved as
c−b
x=
a
This solution can be programmed1 as:

a = float(input(“Enter the value of coefficient a: ”))


b = float(input(“Enter the value of coefficient b: ”))
c = float(input(“Enter the value of coefficient c: ”))

x = (c - b)/a
print(“The solution of the first-degree equation is x = ”, x)

To create this script, in the menu bar in the Python IDLE select

File → New File

as shown in Figure 1.2. This opens a new window where the program can be
written. Before executing the script, it must be saved to a convenient location
with the name: Solution_first_degree_equation.py. To run this program,
in the menu bar of the program window, select Run → Run Module as shown
in Figure 1.3. The result is:

Enter the value of coefficient a: 2


Enter the value of coefficient b: 12
Enter the value of coefficient c: 16
The solution of the first-degree equation is x = 2.0

1.8 Comments in a Program


Comments in a program are of paramount importance because they help pro-
grammers and users to understand the code. They can be implemented at the
beginning and in between lines of code. Comments allow users to understand
the program and they can be used to more easily debug the program. Im-
portant characteristics of comments are: 1. They must be short giving precise
information about the purpose of the program. 2. They must add informative
value. There are single-line comments that start with a hash character # fol-
lowed by a comment. Every line of comments must start with a hash character.
There are comments that explain a line of code and they may be written after
1 Programs or scripts are enclosed in a box.
12 Python with Optimization, ...

FIGURE 1.2: Selecting a New File.

FIGURE 1.3: Selecting a New File.


Introduction to Python 13

the code starting with a hash character. Finally, there are multiple lines of
code. These lines of code start with three single or double quotation marks to
open the lines of code. Triple single or double quotation marks must close the
comment block. For the solution of the first-degree equation, comments may
be implemented as:

“ ‘This program finds the solution of a first-degree equation.


The equation is of the form ax + b = c.
The solution is of the form: x = (c - b)/a ”’

# The coefficients are read:


a = float(input(“Enter the value of coefficient a: ”))
b = float(input(“Enter the value of coefficient b: ”))
c = float(input(“Enter the value of coefficient c: ”))

# The solution is computed:


x = (c - b)/a

# The result is displayed:


print(“The solution of the first-degree equation is x = ”, x)

1.8.1 Operations with Integer and Floating-point Numbers


When one of the variables is an integer one and the other variable is a floating-
point number the result is a floating point number. For example, for a = 3.4
and b = 2, then the results are:

>>> a = 3.4
>>> b = 2
>>> x = a + b
>>> print(x)
5.4

1.9 Functions in Python


The concept of function is associated with a method to do something. The
first contact of a student with functions is with the trigonometric functions in
high school, but in real life there are many examples.
14 Python with Optimization, ...
TABLE 1.3: Built-in functions
Function Python Meaning
ab pow(a, b) Power
quotient of a/b a//b Obtains the integer part of a/b
residue of a/b a%b Obtains the residue of a/b
a/b divmod(a,b) Obtains quotient and residue of a/b
abs() abs(a) Absolute value of a
max() max(a, b) Maximum of a and b
bin() bin(a) Binary representation of a
hex() hex(a) Hexadecimal representation of a

In the Python IDLE, computations can be made and functions can be


run. As it was indicated before, the Python IDLE is ready to receive data
when the prompt appears. A list of some built-in basic functions available
in Python is shown in Table 1.3. This list is not exhaustive and a complete
list is available at https://docs.python.org/3/library/functions.html. Chapter
5 covers the instructions that let users create functions.

1.10 Modules and Libraries


In the basic Python shell, there are available basic functions. Although the
number of built-in functions is limited in Python, there are other modules or
libraries available to use them. Some of the modules available that are used
in the book are math, random, matplotlib, NumPy, SciPy, Scikit-Learn,
and TensorFlow. The modules have to be imported before being used. Each
module or library of functions has subsets that have to be imported as part
of the larger library. The modules or libraries will be referred to indistinctly
as modules or libraries. While some are available when Python is installed,
like the modules math and random, some other modules or libraries require an
installation as described in Appendix A. Once installed, the libraries can be
imported and used.
There are three ways to import a function. The three methods are de-
scribed here and to show them the square root function is used.

1. The square root function is available in the library math. To import a


library and use the square root function use:

import math
a = 3
b = math.sqrt(a)
Introduction to Python 15

In this case each and everyone of the functions available in the module
are imported. This makes the program larger.

2. Instead of importing the whole module, import only the square root
function. This is done with:

from math import sqrt


a = 3
b = sqrt(a)

This case is optimum because only the function required is imported. It


is not necessary to indicate that the function sqrt belongs to the library
math.

3. If several functions of a module are going to be used, then use:

from math import *

In this case, every function in the module is imported.

A common way to import modules or libraries is associating a shorter


name, for example, to import numpy the following can be used:

import numpy as np

and from there on use np, for example:

b = np.sqrt(a)

To learn which functions are available in a module or library, import the


module and then use dir(module_name). For example, for the library numpy:

>>> import numpy


>>> dir(numpy)

The option to display 92 lines is printed in the IDLE. Clicking on the option
displays all the functions available in the library numpy.
16 Python with Optimization, ...
TABLE 1.4: Arithmetic operators
Operation Operator Example Precedence
Addition + a + b 3
Subtraction - a - b 3
Multiplication * a*b 2
Division / a/b 2
Integer division // a//b 2
Power ax pow pow(a, x) 1

1.11 Operators
Operators indicate the type of operations that can be performed on the vari-
ables. There exist four types of operators:

1. Arithmetic.
2. Relational.
3. Logic.
4. Assignment.

1.11.1 Arithmetic Operators


Arithmetic operators require two arguments, although sometimes only one
argument is required. Table 1.4 gives a list of the arithmetic operators.
The result of an operation is of the same type as the type of the arguments
if both are of the same type. In case an operator is a floating-point one and
the other one is an integer, the result is a floating-point one.

1.11.2 Relational Operators


Relational operators, also called comparison operators, are used when a com-
parison must be done. They are extensively used in the next chapter. Table
1.5 gives a list of the relational operators. Relational operators are used to
compare and the comparison produces a boolean result.

TABLE 1.5: Relational operators


Operation Operator Example
Greater than > a >b
Less than < a <b
Greater than or equal to >= a>=b
Less than or equal to <= a<=b
Equal to == a == x
Different from != a != x
Introduction to Python 17
TABLE 1.6: Logical operators
Operation Operator Example Precedence
And and , & a and b, a & b 2
Or or, | a or b, a | b 4
Not not not a 1
Xor ∧ a∧b 3

Example 1.3 Relational operators


Given the variables a = 3 and b = 6, the following use of relational operators
produces the indicated results:

a < b → True
a > b → False
a == b → False
a != b → True

It can readily be seen that the these statements can only have a Boolean
result, namely, either True or False.

1.11.3 Logical Operators


The logical operators are used with boolean variables, also known as logical
variables. Boolean variables can only have the values True and False. Table
1.6 shows the main logical operators and their precedence.

Example 1.4 Logical Operators


Given the variables a, b, and c, the following operations can be done:

>>> a = True; b = True; c = False


>>> a & b
True

>>> a and b
True

>>> a and c
False

>>> a or c
True

>>> a|c
True
18 Python with Optimization, ...
TABLE 1.7: Assignment operators
Python Operator
a = b =
a = a + 1 a += 1
a = a − 1 a −= 1
a = a + b a += b
a = a − b a −= b
a = a ∗ b a ∗= b
a = a / b a /= b
a = a % b a %= b

>>> a ∧ b
False

>>> a ∧ c
True

It is convenient to use parenthesis and then first evaluate the parenthesis


and then the remaining operations using precedence. For example, if a =
False, b = True, and c = True, then

a & b | c

has the result True because a&b = False and False|c is True, but

a&(b|c)

is False because first (b|c) is computed and the result is True, but a&True is
False. Thus, using parenthesis alters the order and evaluation and the result.

1.11.4 Assignment Operators


Assignment operators assign or modify the value of a variable. Some very
useful assignment operators are shown in Table 1.7.

Example 1.5 Assignment operators


For the variables a = 5, b = 3, the following results are computed:

a += 1 → a = 6
a -= 1 → a = 4
a += b → a = 8
Introduction to Python 19

a -= b → a = 2
a ∗= b → a = 15
a /= b → a = 1.66667
a %= b → a = 2

1.12 Alphanumeric Variables


Alphanumeric variables in Python are known as strings. Strings are enclosed in
single or double quotation marks, for example, “dog”, ‘cat’, ‘2702’, ‘February
28’, ‘January_14’, ‘floating-point’. As can be seen, a string can contain
underscores, blank spaces, and any other character available. The type of a
string is str. Strings have already been used in the print and input instruc-
tions together with escape sequences. Strings are covered in Chapter 3 and
here only a few functions are covered.
The length of a string can be obtained with the function len(). This gives
the number of characters in the string including blank spaces. For example:

>>> len(‘February 28’)


11

The result is 11 because the blank space is also counted. Strings can be con-
catenated, or added, with the symbol +. For example, given the strings a =
‘cats’, b = ‘dogs’, c = ‘ ’, d = ‘and’, they can be added as:

>>> a + c + d + c + b
cats and dogs

>>> len(a + c + d + c + b)
13

The blank string is used for separation of the strings. The length of the string
is 13 because the two blank spaces are also counted.
Each character in a string has a position. The numbering of the position
may start from the left and in that case, the left-most character is in the
position 0, the next element to the right is in position 1, and so on. The last
element in a string with length n is in position n-1. If the position numbering
starts from the right, then the right-most element is in position -1, the next
element to the left is in position -2, and so on. The left-most element is in
position -n. The numbering of a string is depicted in Figure 1.4.
20 Python with Optimization, ...

FIGURE 1.4: Position of characters in a string.

1.13 Lists
Sometimes it is necessary to work with a set of data. In Python, a list is such
a set. A list of elements is enclosed in brackets and the elements of the list
can be of several types, for example, some elements can be integer ones, but
other elements can be floating-point elements, strings, or even lists. Elements
in a list must be separated by a comma. A list must have a finite number of
elements. As an example, a list of the first ten natural numbers is:

natural = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Another example is a list of friends with their age; thus, the list elements can
be strings and integer numbers:

>>> friends = [‘Pete’, 16, ‘Rose’, 15, ‘John’, 21, “Louis”, 19]

The number of elements of a list can be found with the function len() as was
the case for strings. Further, lists can be concatenated and multiplied by an
integer. For example,

>>> name = [“Tony”, 17]


>>> friends + name
[‘Pete’, 16, ‘Rose’, 15, ‘John’, 21, ‘Louis’, 19, ‘Tony’, 17]

>>> name*3
[‘Tony’, 17, ‘Tony’, 17, ‘Tony’, 17]
Introduction to Python 21

The instruction len is used to find out the number of the list name as:

>>> len(name)
2

because there are only two elements in the list name. But if it is desired to
find the number of elements in the first element of the list name, the result is:

>>> len(name[0])
4

because there are four elements in the string “Tony”.


The position of the elements in a list follows the same rules of strings.
In the list dogs = [“Cocker”, “Grayhound”, “Dalmatian”], “Cocker” is in the
position 0, , “Grayhound” is in the position 1, and “Dalmatian” is in the
position 2. Alternately, , “Dalmatian” is in the position -1, “Grayhound” is in
the position -2, and “Cocker” is in the position -3.

1.14 Dictionaries
Dictionaries are lists whose elements are pairs of variables. Each pair is com-
posed of a key and a value. The key and its value are separated by a colon.
The pairs are separated by commas. For example,

>>> dictionary1 = { ‘a’ : 23, ‘moon’ : ‘unique’, ‘x’ : 7.3 }

To print the value of a pair only the key is needed. To print the value of key
a in dictionary1 use:

>>> print(“dictionary1[‘a’]: ”, dictionary1[‘a’])


dictionary1[‘a’]:23

which is the correct result.

The length of a dictionary is obtained with the instruction len. For exam-
ple, consider the following dictionary whose length is going to be found:

>>> mat = { “Algebra”:[20, 30, 40] , “Arithmetic”:[17, 41] }


>>> len(mat)
2
22 Python with Optimization, ...

The answer is 2 because in the dictionary mat there are 2 pairs. Note that in
the dictionary mat the values are lists.
The list friends from the previous section can be better written as a
dictionary. This can be done with:

friends = {‘Pete’: 16, ‘Rose’: 15, ‘John’: 21, ‘Louis’: 19, ‘Tony’:
17}

Dictionaries are not ordered and, therefore, there is no position associated


with the pairs. Thus, as it was stated above, only the key is needed to display
a value in an element of a dictionary.

1.15 Tuples
A tuple is an ordered collection of elements. Tuples may or may not be enclosed
in parentheses. The elements of a tuple can be strings, integers, floating-point,
lists, dictionaries, or any combination of elements. The length of a tuple can be
found with the function len( ). For example, the tuple animals = (“cats”,
“dogs”, “birds”), the length can be found with:

>>> animals = (“cats”, “dogs”, “birds”)


>>> len(animals)
3

Concatenation and multiplication by an integer can also be done, as was


the case for strings, lists, and dictionaries.
The position of the elements in a tuple follows the same rules of strings.

1.16 Examples
This section presents three examples of small programs or scripts in Python.
The first example uses tuples to swap variables, the second example computes
simple and compound interest and the last example implements temperature
conversion. The scripts have to be written in a new file and run as described
in Section 1.7.

Example 1.6 Variable swap


Sometimes variables need to be swapped. Usually, the swap requires a tem-
porary variable. For example, to swap the variables x and y, the following is
done:
Introduction to Python 23

First, save the value of x in a temporary variable:

>>> temp = x

Second, store the variable y into variable x:

>>> x = y

Finally, store the temporary variable which has the value of x into y:

>>> y = temp

The final result is that the values of the variables x and y have been swapped.
There is another way to do the swapping using tuples and the whole swapping
is done with:

>>> (x, y) = (y, x)

For example, given the tuple (-7, 23), the values are swapped with:

>>> (x, y) = (-7, 23)


>>> (x, y) = (y, x)
>>> print(“The value of x is %d: ’%x)

The value of x is 23:

>>> print(“The value of y is %d: ’ %y)

The value of y is -7:

Thus, the swapping has been done.

Example 1.7 Temperature conversion


Conversion of temperatures in the three scales Celsius, Fahrenheit, and Kelvin
can be done using the conversion equations between them that are shown in
Table 1.8. The following script implements the conversion from Celsius to

TABLE 1.8: Conversion formulae


Celsius to Fahrenheit ◦ F = 9/5 (◦ C) + 32
Kelvin to Fahrenheit ◦ F = 9/5 (K - 273) + 32
Fahrenheit to Celsius ◦ C = 5/9 (◦ F - 32)
Celsius to Kelvin K = ◦ C + 273

Kelvin to Celsius C = K - 273
Fahrenheit to Kelvin K = 5/9 (◦ F - 32) + 273
24 Python with Optimization, ...

Fahrenheit. As an example, let the Celsius temperature be 45 ◦ C. The result


is:

Enter the temperature in Celsius degrees:


45
The equivalent Faherenheit temperature is 113.00:

The script is:

“ ‘Temperature conversion
This script converts between different temperature scales ”’

# START

# Read the temperature in Celsius degrees:


C = float(input(“Enter the temperature in Celsius degrees:\n”))

# Compute the conversion:


F = 1.8*C + 32

print(“The equivalent Faherenheit temperature is %0.2f” %F)

# END

Example 1.8 Simple and compound interest


It is desired to compute the simple and compound interest paid to an investor
at a fixed interest annual rate. The number of months n is divided by 12 to
obtain the number of years. The formulae for simple and compound interest
are:

simple interest = principal × n × interest rate

compound interest = principal × (1 + interest raten ) -1

To compute the power the function pow is used and it is available at the library
math that must be imported. The script is:
Introduction to Python 25

“ ‘Interest calculation.
This is Example 1.8. This program obtains the simple
and compound interest. ”’

# Import required libraries:


import math

principal = float(input(“Enter the principal:\n”))


interest_rate = float(input(“Enter the interest rate: \n”))
n = float(input(“Enter the number of months:\n”))

# Divide number of months by 12.


n = n/12

# Simple interest:
simple_interest = principal*n*interest_rate
compound_interest = principal*pow(1 + interest_rate, n) - 1

# Display results:
print(“Simple interest: %0.2f” % simple_interest)

print(“Compound interest: %0.2f” % compound_interest)

As an example consider a principal of $10,000.00, an annual interest rate


of 4%, and 18 months (1.5 years). Running the program the results are:

Enter the principal:


10000
Enter the interest rate:
4
Enter the number of months:
18
Simple interest: 600.00
Compound interest: 605.96

The function pow(a, x) has the meaning:


pow(a, x) = ax
26 Python with Optimization, ...
TABLE 1.9: Python instructions for Chapter 1
Instruction Description
# Comment line.
** Power.
“ ” String delimiter.
‘’ String delimiter.
“ ‘ ”’ Comments delimiter.
& Boolean AND.
| Boolean OR.
and AND instruction.
bool Boolean variable.
False False value of a boolean variable.
float Floating-point type variable.
import Import a module or library.
input Read a variable.
int Integer type variable.
len Length of a string, list, etc.
list List type variable.
math Mathematics module or library.
None None variable.
not NOT instruction.
or Boolean OR.
print Display a variable.
pow To obtain a number to the power of other number.
str String variable.
True True value of a boolean variable.
type Display the type of a variable.

1.17 Python Instructions for Chapter 1


The Instructions used in Chapter 1 are displayed in Table 1.9.

1.18 Conclusions
In this chapter the basic Python concepts such as variables, scripts, modules
or libraries, among many others, have been presented. Some examples show
how easy is to have a running program in Python. The Python IDLE has been
used but everything can be used in other platforms such as Jupyter or Spyder.
Introduction to Python 27

1.19 Exercises
1.1 What is displayed for the script presented below? Try to find the result
without running the script in the IDLE.

n = 4; k = 2
print(n)
print(-n)
print(n–)
print(n + k)
print(k)
print(n, k)
print(“ ”)
print(“n %3.2g” % n)
print(“n * n = %3.2g” % n*n)
print(n*n)
print(‘n’)
1.2 Indicate the output in the following segments of code:

a) x = 3
x = x + 2
print(x)

b) y = - 5.3
y = y*7.0
print(y)

c) x = 8; y = 3)
x = x%y
print(x)
1.3 Find the results for the following segments of code:

a) x = 3
x = x + 2
print(x)

b) y = - 5.3
y = y*7.0
print(y)

c) x = 8; y = 3
x = x % y;
print(x)
28 Python with Optimization, ...

1.4 The function getsizeof( ) from the library sys is used to obtain the
size of a variable. Using getsizeof( ) obtain the size of the variable “a” for

a) a = 2.0

b) a = 2

c) a = 20000

d) a = ‘2’

e) a = True

1.5 Indicate the result produced by the following code:

x = 42.0
y = 42.3 e-14
a = 42.3 e-14
b = −57.8
m = +87
n = +249
print(m*n)
print(x)
print(y)
print(a)
print(b)

1.6 Indicate the result produced by the following code:

a) x = 90.34
print(x)

b) z = 2
y = 7;
x = y + 2*z
print(“The result is: %5.2g” % x)

1.7 Say if the following code produces True or False:

x = 8; y = 5
a = 1; b = 2

a) x = 1 & a <= 2 | y < x

b) (x = 1 & a < texttt= 2) | y > x

c) x = 1 & (a <= 2 | y = x)
Introduction to Python 29

1.8 What is the result in the following code?

a = 2; b = 3
x = 8.3; y = -4.6; z = 2.3
print (“a*b = %5.2f” % a*b )
print(“a/b = % 5.2f” % a/b )
print(“x/y = % 5.2f” % x/y )
print(“a*z = %5.2f” % a*z )

1.9 What is the result in the following code?

c = ‘8’
r = 3
print(c*r)

1.10 Obtain the result in every case:

x = 7; y = 3

a. x % y

b. x = x + y

c. x += y

d. x =+ y

e. x *= y

f. x =* y

g. x %= y

1.11 Write a script to read the data: 7.0, 2.0, 8.3, 3.0, 5.6, 4.0 and
compute:
a. The maximum value.
b. The minimum value.
c. The addition.
d. The mean.
Chapter 2
Conditionals and Loops

2.1 Introduction
In real life, very often it is necessary to take a decision about what is the
next step in a given situation. The decision usually is taken depending upon
certain conditions in the situation. In the Python programming language, this
decision-making process also takes place, and the condition or conditions in
the process have to be evaluated. The decision-making instructions are known
as conditionals and they are studied in the first part of the chapter. The
second part of the chapter studies the instructions to repeat a process. These
instructions are called loops and they appear in iterative processes such as
those in numerical analysis, physics and finance, to name a few areas.
The chapter starts with the definition of a conditional in Python. Then, the
extension to nested conditionals is presented. Several examples show the use
of conditionals. Loops are introduced next. The two types of loops, for loops
and while loops are defined. Finally, examples using loops and conditionals
are presented.

2.2 Conditionals
Conditionals arise when an operation execution depends upon the true value
of a logic expression. For example, to take the square root of a number can only
be done if the number is positive. Thus, a conditional has to be implemented
to check that the number is positive.
The conditions in Python use the key word if in the following format:

if (logic expression):
Instructions
Next instructions

The key word if is followed by a logic expression and a colon. The


Instructions in the block must be indented by at least a single space but

DOI: 10.1201/9781003222118-2 30
Conditionals and Loops 31

they must have the same indentation. The indented instructions are part of
the if statement and they are executed if the logic expression is True. Oth-
erwise, if the logic expression is False, the Next instructions are executed.
The logic expression can be inside parenthesis but this is not compulsory and
they may be omitted.

A very common error is to omit the semicolon.

Example 2.1 Simple conditional using if

To obtain the quotient of two numbers, a condition that must be satisfied is


that the denominator must be different from 0. This condition can be checked
with an if conditional. A script to evaluate the quotient of two numbers must
first read in the numerator and denominator and assign them to variables a
and b:

a = float(input(“Enter numerator: ”))


b = float(input(“Enter denominator: ”))

Next, initialize a variable check to False and with a conditional change it to


True if the denominator is not equal to 0:

check = False
if (b != 0):
check = True

Now with other condition evaluate the quotient if check = False:

if (check):
c = a/b
print(“Result:”, c)

When the variable check is True, then it executes c = a/b and prints the
result. Otherwise, when a check is False it does not do anything.
The complete script is:

# This is script Example_2_1.py


# Logical Conditional.

a = float(input(“Enter numerator: ”))


b = float(input(“Enter denominator: ”))

# Script continues next page.


32 Python with Optimization, ...
TABLE 2.1: Relational Operators.
Operator Definition Example Traditional
Notation
> Greater than A >B A >B
>= Greater than or equal to A >= B A ≥ B
< Less than C < 4 C < 4
<= Less than or equal to X <= 8 X≤8
== Equal to X == Z X =Z
!= Different to a != 8.2 a = 8.2

# Script continued from previous page.

check = False
if (b != 0):
check = True
# End of conditional.

# Starts conditional.
if (check):
c = a/b
print(“Result:”, c)

The logic expression may use any of the relational operators shown in
Table 2.1. It can be seen that the equality comparison uses a double-equal
sign. Thus, a == b is comparing if a is equal to b. If the equality holds then
the logic expression is True. Otherwise, it is False. It is worth mentioning
that a = b is an assignment meaning that the value of b is assigned to a; thus,
it is not a comparison.

To compare equality a double equal sign == is required.

For example, consider the following example of correct and incorrect logic
expressions:

if(a == b): CORRECT It is a comparison.


if(a = b): incorrect It is an assignment. It is an error.
if(a == b) incorrect The colon is missing :
Conditionals and Loops 33
TABLE 2.2: Logic Connectives.
Operator Definition Example Precedence
not logical NOT not X 1
& logic AND A&B 2
and A and B 2
∧ exclusive OR A ∧ B 3
| logic OR A | B 4
or A or B 4

Composite logic expressions may also use the logic connectives from Table 2.2.
Some examples are:

a = 1; b = 2; c = 3
if( a < b ): True logic expression
if( a < b or c > 0): True logic expression
if( b > 1 and c == 4): False logic expression

Example 2.2 Solution of a first-degree equation


A first-degree equation is of the form ax + b = 0. The solution is given by
x = −b/a, which requires that a = 0. Thus, a conditional is required to check
this value. To solve the equation, the following steps are required:

• Read in a and b
• Check if a = 0
• Solve for x = b/a
• Display x

In Python the code is:

# This is script Example_2_2.py


# Code to solve a first-degree equation.
# START:

# Enter the coefficient values:


a = float(input(“Enter a: ”))
b = float(input(“Enter b: ”))

# Script continues at next page.


Random documents with unrelated
content Scribd suggests to you:
great inventors, and even the great reformers will not be ranked as
high as the great educators.

FOOTNOTE:
[1] It is an open question whether light corporal punishment should
not occasionally be permitted in the case of very young children who
have not yet arrived at the age of reason. In this case, at all events,
there is no danger that the permission will be abused. No one would
think of seriously hurting a very young child.
III
In order that a parent shall properly influence a child's character, it is
necessary for him to know what that character is, and what the
nature is of each fault with which he is dealing. I feel almost like
asking pardon for saying anything so self-evident. It seems like
saying that a physician who is called to a sick-bed, before beginning
to prescribe, should know the nature of the disease for which he is
prescribing, should not prescribe for one disease when he is dealing
with another.
I do not know enough about physicians to say whether such
mistakes ever happen among them; but that such egregious
mistakes do occur among parents all the time, I am sure. There are
many parents who never stop to ask before they punish—that is,
before they prescribe their moral remedies—what the nature of the
disease is with which their child is afflicted. They never take the
trouble to make a diagnosis of the case in order to treat it correctly.
There is perhaps not one parent in a thousand who has a clear idea
of the character of his child, or to whom it even so much as occurs
that he ought to have a clear conception of that character, a map of
it, a chart of it, laid out, as it were, in his mind. The trouble is that
attention is not usually called to this important matter, and I purpose
to make it the special subject of this address.

1. Obstinacy
I am prepared at the outset for the objection that the case against
parents has been overstated. There are parents who freely
acknowledge, "My child is obstinate; I know it has an obstinate
character." Others say, "My child, alas! is untruthful." Others again
declare, "My child is indolent."
But these symptoms are far too indeterminate to base upon them a
correct reformatory treatment. Such symptoms may be due to a
variety of causes, and not until we have discovered the underlying
cause in any given case can we be sure that we are following the
right method.
Take the case, for instance, of obstinacy; a child is told to do a
certain thing and it refuses. Now, here is a dilemma. How shall we
act? There are those who say: In such cases a child must be
chastised until it does what it is told. A gentleman who was present
here last Sunday had the kindness to send me during the week an
edition of John Wesley's sermons, and in this volume, in the sermon
on "Obedience to Parents," I read the following words: "Break the
will if you would not damn the child. I conjure you not to neglect,
not to delay this! Therefore (1) Let a child from a year old be taught
to fear the rod and to cry softly. In order to do this (2) Let him have
nothing he cries for, absolutely nothing, great or small, else you
undo your own work. At all events, from that age make him do as he
is bid, if you whip him ten times running to effect it. Let none
persuade you it is cruelty to do this: it is cruelty not to do it. Break
his will now, and his soul will live, and he will probably bless you to
all eternity."
But by following this line of treatment we may obtain a result the
very opposite of that which we intended. Obstinacy in many cases is
due to sensitiveness. There are some children as sensitive to
impressions as is that well-known flower which closes its quivering
leaves at the slightest touch. These sensitive children retreat into
themselves at the first sign of unfriendliness or aggression from
without. The reason why such a child does not obey its father's
command is not, perhaps, because it is unwilling to do as it is told,
but because of the stern face, the impatient gesture, the raised
voice with which the parent accompanies the command, and which
jars upon the child's feelings.
If such a parent, incensed at the child's disobedience, becomes still
more severe, raises his voice still more, he will only make matters
worse. The child will shrink from him still more and continue its
passive resistance. In this manner obstinacy, which was at first only
a passing spell, may become a fixed trait in the child's character.
To be sure, we should not, on the other hand, treat these sensitive
children only with caresses. In this way we encourage their
sensitiveness, whereas we should regard it as a weakness that
requires to be gradually but steadily overcome.
The middle way seems the best. Let the parent exact obedience
from the child by gentle firmness, by a firmness in which there shall
be no trace of passion, no heightened feeling, and with a gentleness
which, gentle as it may be, shall be at the same time unyielding. But
while obstinacy is sometimes due to softness of nature, it is at other
times due to the opposite—to hardness of nature, and according to
the case we should vary our treatment.
There are persons who, having once made up their minds to do a
thing, cannot be moved from their resolution by any amount of
persuasion. These hard natures, these concentrated wills, are bound
to have their way, no matter whom they injure, no matter what
stands in the way. Such persons—and we notice the beginnings of
this trait in children—need to be taught to respect the rights of
others. Their wills should occasionally be allowed to collide with the
wills of others, in order that they may discover that there are other
wills limiting theirs, and may learn the necessary lesson of
submission.
In yet other cases obstinacy is due to stupidity. Persons of weak
intelligence are apt to be suspicious. Not understanding the motives
of others, they distrust them; unwilling to follow the guidance of
others, they cling with a sort of desperation to their own purpose.
These cases may be treated by removing the cause of suspicion, by
patiently explaining one's motives where it is possible to do so, by
awakening confidence.

2. Untruthfulness
Again, let us take the fault of untruthfulness. One cannot sufficiently
commend the watchfulness of those parents who take alarm at the
slightest sign of falsehood in a child. A lie should always put us on
our guard. The arch fiend is justly called "the father of lies." The
habit of falsehood, when it has become settled, is the sure inlet to
worse vices.
At the same time not all falsehoods are equally culpable or equally
indicative of evil tendency, and we should have a care to
discriminate between the different causes of falsehood in the young
child, in order that we may pursue the proper treatment. Sometimes
falsehood is due to redundant imagination, especially in young
children who have not yet learned to distinguish between fact and
fancy. In such cases we may restrain the child's imagination by
directing its attention to the world of fact, by trying to interest it in
natural history and the like.
We should especially set the example of strict accuracy ourselves in
all our statements, no matter how unimportant they may be. For
instance, if we narrate certain occurrences in the presence of the
child, we should be careful to observe the exact order in which the
events occurred, and if we have made a mistake we should take
pains to correct ourselves, though the order of occurrence is really
immaterial. Precisely because it is immaterial we show by this means
how much we value accuracy even in little things.
Then, again, falsehood is often due to the desire for gain. Or it may
be due to fear. The child is afraid of the severity of the parent's
discipline. In that case we are to blame; we must relax our
discipline. We have no business to tempt the child into falsehood.
Again, untruthfulness is often due to mistaken sympathy, as we see
in the case of pupils in school, who will tell a falsehood to shield a
fellow pupil. In the worst cases falsehood is inspired by malice.
It may be said that the proper positive treatment for this fault is to
set the example of the strictest truthfulness ourselves, to avoid the
little falsehoods which we sometimes allow ourselves without
compunction, to show our disgust at a lie, to fill the child with a
sense of the baseness of lying, and above all to find out the direct
cause which has tempted the child in any given case. As a rule,
falsehood is only a means to an end; children do not tell untruths
because they like to tell them, but because they have some ulterior
end in view. Find out what that ulterior end is, and instead of
directing your attention only to the lie, penetrate to the motive that
has led the child into falsehood, and try to divert it from the bad
end. Thus you may extract the cause of its wrongdoing.

3. Laziness
Thirdly, let us consider the fault of laziness. Laziness is sometimes
due to physical causes. Nothing may be necessary but a change of
diet, exercise in the fresh air, etc., to cure the evil. Sometimes it is
the sign of a certain slow growth of the mind. There are fruits in the
garden of the gods that ripen slowly, and these fruits are often not
the least precious or the least beautiful when they finally have
matured. Sir Isaac Newton's mind was one of these slowly ripening
fruits. In school he was regarded as a dullard and his teachers had
small hopes of him.
Laziness, like other faults of character, sometimes disappears in the
process of growth. Just as at a certain period in the life of a youth or
maiden new faculties seem to develop, new passions arise, a new
life begins to stir in the heart, so at a certain period qualities with
which we had long been familiar, disappear of themselves.
We have very little light upon this subject, but the fact that a great
transformation of character sometimes does take place in children
without any perceptible cause is quite certain, and it may be offered
as a comforting reflection to those parents who are over-anxious on
account of the faults they detect in their children. But again, on the
other hand, laziness or untruthfulness or obstinacy may be a black
streak, coming to the surface out of the nethermost strata of moral
depravity, and, taken in connection with other traits, may justify the
most serious apprehension, and should then be a signal for
immediate measures of the most stringent sort.

4. Discovering Causes
I am thus led to the second branch of my subject. I have tried to
meet the objection of the parent who says, "I know the character of
my child; I know my child is obstinate," by replying, "If you only
know that your child is obstinate you know very little; you need to
know what are the causes of his obstinacy, and vary your treatment
accordingly." Or if any one says, "My child is untruthful," I reply,
"You need to find out what the cause is of this untruthfulness and
vary your treatment accordingly." Or again, in the case which we
have just considered, I have pointed out that laziness in a child may
have no serious meaning whatever or may give just cause for the
most serious alarm, according to the group of characteristic traits of
which it is one. On this point I wish to lay stress. If you desire to
obtain a correct impression of a human face, you do not look at the
eye by itself, then at the nose, then fix your attention on the cheeks
and the chin and the brow, but you regard all these features
together and view them in their relations to one another. Or let us
recur to the simile of the physician. What would you think of the
doctor who should judge the nature of a disease by some one
symptom which happened to obtrude itself, or should treat each
symptom as it appears separately, without endeavoring to reach the
occult cause which has given rise to the symptoms, of which they
are all but the outward manifestation?
And yet that is precisely the incredible mistake which every one of
us, I venture to say, is apt to make in the treatment of children's
characters. We judge of them by some one trait, as obstinacy, which
happens to obtrude itself on our attention, and we prescribe for
each symptom as it arises; we treat obstinacy by itself, and
untruthfulness and indolence separately, without endeavoring to get
at the underlying cause of all these symptoms. The point I desire to
make is that in the education of our children it is necessary not only
to study individual traits, but each trait in connection with the group
to which it belongs.
Take for an illustration the case last mentioned—that of laziness.
There is a well-known type group or group of characteristic traits, of
which laziness is one. The chief components of this group are the
following: The sense of shame is wanting, that is one trait. The will
is under the control of random impulses, good impulses mingle
helter-skelter with bad. There is an indisposition on the part of such
a child to prolonged exertion in any direction, even in the direction
of pleasure. That is perhaps the most dangerous trait of all.
If you try to deal, as people actually do, with each of these traits
separately, you will fail. If you try to influence the sense of shame,
you will meet with no response; if you disgrace such a child, you will
make it worse; if you whip it, you will harden it. If you attempt to
overcome indolence by the promise of rewards, that will be useless.
The child forgets promised rewards just as quickly as it forgets
threatened punishment.
This forgetfulness, this lack of coherency in its ideas, is particularly
characteristic. The ideas of such a child are imperfectly connected.
The ties between causes and their effects are feeble. The contents
of the child's mind are in a state of unstable equilibrium. There is no
point of fixity in its mental realm. And the cure for such a condition
is to establish fixity in the thoughts, to induce habits of industry and
application by steady, unrelaxing discipline, and especially by means
of manual training.
The immense value of mechanical labor as a means of moral
improvement has been appreciated until now only to a very
imperfect extent. Mechanical labor wisely directed secures mental
fixity because it concentrates the child's attention for days and often
for weeks upon a single task. Mechanical labor stimulates moral
pride by enabling the pupil to produce articles of value and giving
him in this way the sense of achievement. Mechanical labor also
overcomes indolence by compelling settled habits of industry,
whereby the random impulses of the will are brought under control.
The type group which we have just considered is one of the most
clearly marked and easily recognized. It is a type which we often
meet with among the so-called criminal classes, where its
characteristic features can be seen in exaggerated proportions.
Without attempting to analyze any additional types (a task of great
delicacy and difficulty), the truth that the underlying fault of
character is often unlike the symptoms which appear most
conspicuously on the surface may be further illustrated by the
following example. I have known of a person who made himself
obnoxious to his friends by his overbearing manners and apparent
arrogance. Casual observers condemned him on account of what
they believed to be his overweening self-confidence, and expressed
the opinion that his self-conceit ought to be broken down. But the
real trouble with him was not that he was too self-confident, but that
he had not self-confidence enough. His self-confidence needed to be
built up. He was overbearing in society because he did not trust
himself, because he was always afraid of not being able to hold his
own, and hence he exaggerated on the other side. Those who take
such a person to be in reality what he seems to be will never be able
to influence him. If we find such a trait in a child, and simply treat it
as if it were arrogant, we shall miss the mark entirely. We must find
the underlying principle of the character the occult cause of which
the surface symptoms are the effects.
Our knowledge of the great type groups is as yet extremely meager.
Psychology has yet to do its work in this direction, and books on
education give us but little help. But there are certain means by
which the task of investigation may possibly be assisted. One means
is the study of the plays of Shakespeare. That master mind has
created certain types of character which repay the closest analysis.
The study of the best biographies is a second means. The study of
the moral characteristics of the primitive races—a study which has
been begun by Herbert Spencer in his work on Descriptive Sociology,
and by Waitz in his Anthropologie der Naturvölker—is perhaps
another means; and honest introspection, when it shall have become
the rule among intelligent persons, instead of being the exception,
will probably be the best means.
I am afraid that some of my hearers, from having been over-
confident as educators in the beginning, may now have become
over-timid; from having said to themselves, "Why, of course we
know the characteristics of our children," may now, since the
difficulties of studying character have been explained, be disposed to
exclaim in a kind of despair, "Who can ever understand the character
of a single human being?" A perfect understanding of any human
being is indeed impossible. We do not perfectly know even those
who are nearest and dearest to us. But there are means of reaching
at least approximate results, so far as children are concerned, and a
few of these permit me to briefly summarize.
Try to win the confidence of the child so that it may disclose its inner
life to you. Children accept the benefactions of their parents as
unthinkingly as they breathe the air around them. Show them that
your care and untiring devotion must be deserved, not taken as a
matter of course. In this way you will deepen their attachment and
lead them to willingly open their hearts to you. At the same time
enter into the lesser concerns of their life. Be their comrades, their
counselors; stoop to them, let them cling to you.
Observe your children when they are at play, for it is then that they
throw off their reserve and show themselves as they are. Some
children, for instance, will not join a game unless they can be
leaders; is not that a sign of character? Some children will take an
unfair advantage at play, and justify themselves by saying, "It is only
in play." Some are persistent in a game while others tire of any
game after a little while. Others are sticklers for a strict observance
of the rules. Observe how your sons or daughters are regarded by
their companions; children are often wonderfully quick to detect one
another's faults.
Try to find out what the favorite pursuits and studies of your child
are, by what it is repelled, by what attracted, and to what it is
indifferent. Above all, keep a record of your child's development. Do
not shun the labor involved in this. You know very well that nothing
worth having can be obtained without labor, yet most parents are
unwilling to give sufficient time and attention to the education of
their children. Keep a record of the most significant words and acts
of the child. Thus after a while you may have a picture of the child's
inward condition before you, an assemblage of characteristic traits,
and by comparing one trait with another, you may find the clue to a
deeper understanding of its nature.
What I have said about children applies equally to ourselves. I
started out by saying that not one parent in a thousand knows his
child's character. I conclude by saying that not one man or woman in
a thousand knows his or her own character. We go through life
cherishing an unreal conception of ourselves which is often inspired
by vanity.
I am well aware that it is difficult to know oneself, but there are
helps in this direction also. We can look over our own past record,
we can honestly examine how we have acted in the leading crises of
our lives, we can summon our own characteristic traits before our
minds—the things that we like to dwell upon, and the things which
we would gladly blot out of our memories if we could—and by
comparing this trait with that, we may discover the springs by which
we have been moved. It is difficult to attain self-knowledge, but it is
imperative that we should try to attain it. The aim of our existence is
to improve our characters, and clearly we cannot improve them
unless we know them.
I have undertaken to grapple with a most difficult subject, but I shall
have accomplished the purpose which I had in mind if I have
awakened in you a deeper desire to ask yourselves, first, "What is
the character of my child?" and, second, "What is my own
character?" The most serious business of our lives is to try to find
the answers to these two questions.
THE AMERICAN HOME SERIES
NORMAN B. RICHARDSON, Editor

1. The Nation's Challenge to the Home


2. How One Real Mother Lives with Her Children
3. Parenthood and Heredity
4. The Roots of Disposition and Character
5. The First Year in a Baby's Life
6. Thumb-Sucking
7. The Education of the Baby Until It Is One Year Old
8. First Steps Towards Character
9. The Second and Third Years
10. The Education of the Child During the Second and Third Years
11. The Mother as Playfellow (Years One, Two, and Three)
12. The Problems of Temper
13. The Problems of Fighting
14. The Government of Young Children
15. The Punishment of Children
16. The Home Kindergarten
17. The Religious Nurture of a Little Child (Years Four and Five)
18. The Nervous Child
19. On Truth Telling and the Problem of Children's Lies
20. The Government of Children Between Six and Twelve
21. The Dramatic Instinct in Children
22. Dramatics in the Home
23. Table Talk in the Home
24. Sunday in the Home
25. A Year of Good Sundays
26. The Picture-Hour in the Home
27. Story-Telling in the Home
28. Music in the Home
29. Training in Thrift
30. "What to Say" in Telling the Story of Life's Renewal
31. Sex Discipline for Boys in the Home
32. Youth's Outlook Upon Life
33. Building for Womanhood
34. Rhythm and Recreation
35. The Home and Moving Pictures.
36. Worship in the Home.
37. The Use of Dolls in Child-Training.
Prices will be Furnished on Application
*** END OF THE PROJECT GUTENBERG EBOOK THE PUNISHMENT
OF CHILDREN ***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying
copyright royalties. Special rules, set forth in the General Terms of
Use part of this license, apply to copying and distributing Project
Gutenberg™ electronic works to protect the PROJECT GUTENBERG™
concept and trademark. Project Gutenberg is a registered trademark,
and may not be used if you charge for an eBook, except by following
the terms of the trademark license, including paying royalties for use
of the Project Gutenberg trademark. If you do not charge anything
for copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund
from the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law
in the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name associated
with the work. You can easily comply with the terms of this
agreement by keeping this work in the same format with its attached
full Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears,
or with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning
of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookbell.com

You might also like