Philip Joyce
C and Python Applications
Embedding Python Code in C Programs, SQL Methods, and Python Sockets
1st ed.
Logo of the publisher
ISBN 978-1-4842-7773-7 e-ISBN 978-1-4842-7774-4
https://doi.org/10.1007/978-1-4842-7774-4
Philip Joyce 2022
Apress Standard
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
This Apress imprint is published by the registered company APress Media, LLC part of Springer Nature.
The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.
Introduction
The C and Python programming languages are important languages in many computer applications. This book will demonstrate how to use the C and Python languages to write applications in SQL. It will demonstrate how to embed a Python program within a C program. Finally, the reader will learn how to create Python socket programs which can communicate with each other on different computers (these are called sockets).
A basic familiarity with mathematics is assumed along with some experience of the basics of computer programs. The first two chapters review the basics of C and Python. The chapters following these are grouped into SQL techniques, embedded Python, and sockets applications. There are exercises in each chapter with answers and suggested code at the end of the book.
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484277737. For more detailed information, please visit http://www.apress.com/source-code.
Acknowledgments
Thanks to my wife, Anne, for her support, my son Michael, and my daughter Katharine. All three have mathematics degrees. Thanks to everyone on the Apress team who helped me with the publication of this, my third book.
Table of Contents
About the Author
Philip Joyce
has 28 years of experience as a software engineer, working on control of steel production, control of oil refineries, communications software (pre-Internet), office products (server software), and computer control of airports. He programs in Assembler, COBOL, Coral 66, C, and C++ with SQL. He served as a mentor to new graduates in the Ferranti Company. He obtained an MSc in computational physics (including augmented matrix techniques and Monte Carlo techniques using Fortran) from Salford University in 1996. He is also a chartered physicist and a member of the Institute of Physics (member of the Higher Education Group).
About the Technical Reviewer
Swathi Sutrave
is a self-professed tech geek. She has been a subject matter expert for several different programming languages, including Python, C, and SQL, for corporations, startups, and universities.
The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
P. Joyce C and Python Applications https://doi.org/10.1007/978-1-4842-7774-4_1
1. Python Programming
This is the first of two chapters in which youll review both Python and C programming languages. A basic understanding of computing and what programs are about is assumed although no prior knowledge of either Python or C is needed.
In this chapter, we will start with the basics of Python. This will include how items used in a program are stored in the computer, basic arithmetic formats, handling strings of characters, reading in data that the user can enter on the command line, etc. Then we will work up to file access on the computer, which will lead us up to industrial/commercial-level computing by the end of the book.
If you dont already have a Python development environment on your computer, you can download it and the Development Kit, free of charge, from www.python.org/downloads/ . Another way you can access Python is by using Visual Studio. Again, a version of this can be downloaded.
Definition of Variables
This section looks at the different types of store areas that are used in Python. We refer to these store areas as variables . The different types can be numbers (integers or decimals), characters, and different types of groups of these (strings, arrays, dictionaries, lists, or tuples).
In these examples, you can go to the command line and enter Python which starts up the Python environment and produces >>> as the prompt for you to enter Python code.
In Python, unlike C, you dont define the variable as a specific type. The different types are integer, floating point, character, string, etc. The type is assigned when you give the variable a value. So try the following code:
>>> a1 = 51
>>> print(type(a1))
We get the output
>>>
Here we are defining a variable called a1 and we are assigning the integer value 51 to it.
We then call the function print with the parameter type and a1 and we get the reply class int. type means that we want to display whether the variable is an integer, floating point, character, string, etc.
We can now exit the Python environment by typing quit().
We will now perform the same function from a program.
Create a file called typ1a.py.
Then enter the following two lines of Python code:
Now on the command line, enter python typ1a.py.
And you should get the output
which is the same as our first example.
This is just demonstrating the equivalence of the two methods.
Obviously, if you want to run a program with many lines of code and possibly run it many times, then having the code in a file is more efficient.
We can demonstrate different data types being stored in the same variable using the following code:
When we run this, we get
The 51 entered is an int. The 51.6 is a float (decimal) type, and 51 is a string.
We can make the results a little clearer if we use print(a1 is, type(a1)).