Programming with Python for Engineers

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

An interactive book introducing Python to engineers and engineering students. This book is intended to be an accompanying textbook for teaching programming to science and engineering students with no prior programming expertise. This endeavour requires a delicate balance between providing details on computers & programming in a complete manner and the programming needs of science and engineering disciplines. With the hopes of providing a suitable balance, the book uses Python as the programming language, since it is easy to learn and program. Moreover, for keeping the balance, the book is formed of three parts: • Part I: The Basics of Computers and Computing: The book starts with what computation is, introduces both the present-day hardware and software infrastructure on which programming is performed and introduces the spectrum of programming languages. • Part II: Programming with Python: The second part starts with the basic building blocks of Python programming and continues with providing the ground formation for solving a problem in to Python. Since almost all science and engineering libraries in Python are written with an object-oriented approach, a gentle introduction to this concept is also provided in this part. • Part III: Using Python for Science and Engineering Problems: The last part of the book is dedicated to practical and powerful tools that are widely used by various science and engineering disciplines. These tools provide functionalities for reading and writing data from/to files, working with data (using e.g. algebraic, numerical or statistical computations) and plotting data. These tools are then utilized in example problems and applications at the end of the book. This is an ‘interactive’ book with a rather ‘minimalist’ approach: Some details or specialized subjects are not emphasized and instead, direct interaction with examples and problems are encouraged. Therefore, rather than being a ‘complete reference manual’, this book is a ‘first things first’ and ‘hands on’ book. The pointers to skipped details will be provided by links in the book. Bearing this in mind, the reader is strongly encouraged to read and interact all contents of the book thoroughly. The book’s interactivity is thanks to Jupyter notebook. Therefore, the book differs from a conventional book by providing some dynamic content. This content can appear in audio-visual form as well as some applets (small applications) embedded in the book. It is also possible that the book asks the the reader to complete/write a piece of Python program, run it, and inspect the result, from time to time. The reader is encouraged to complete these minor tasks. Such tasks and interactions are of great assistance in gaining acquaintance with Python and building up a self-confidence in solving problems with Python.

Author(s): Sinan Kalkan, Onur Tolga Sehitoglu, Gokturk Ucoluk
Publisher: Independently Published
Year: 2022

Language: English
Pages: 259

Preface
Basic Computer Organization
The von Neumann Architecture
John von Neumann
Components of the von Neumann Architecture
The Memory
The CPU
The Fetch-Decode-Execute Cycle
The Stored Program Concept
Pros and Cons of the von Neuman Architecture
Peripherals of a computer
The running of a computer
Start up Process
The Operating System
Important Concepts
Further Reading
Exercises
A Broad Look at Programming and Programming Languages
How do we solve problems with programs?
Algorithm
How to write algorithms
How to compare algorithms
Data Representation
The World of Programming Languages
Low-level Languages
High-level Languages
Implementing with a High-level Language: Interpreter vs. Compiler
Programming-language Paradigms
Introducing Python
Important Concepts
Further Reading
Exercise
Representation of Data
Representing integers
Sign-Magnitude Notation
Two’s Complement Representation
Why does Two’s Complement work?
Benefits of the Two’s Complement Representation
PRACTICE TIME
Representing real numbers
The IEEE754 Representation
Information loss in floating-point representations
PRACTICE TIME
Numbers in Python
Representing text
Characters
Strings
Containers
Representing truth values (Booleans)
Important Concepts
Further Reading
Exercises
Dive into Python
Basic Data
Numbers in Python
Boolean Values
Container data (str, tuple, list, dict, set)
Accessing elements in sequential containers
Useful operations common to containers
String
List and tuple
Dictionary
Set
Expressions
Arithmetic, Logic, Container and Comparison Operations
Exercise
Evaluating Expressions
Implicit and Explicit Type Conversion
Basic Statements
Assignment Statement and Variables
Variables & Aliasing
Naming variables
Other Basic Statements
Compound Statements
Basic actions for interacting with the environment
Actions for input
Actions for output
Actions that are ignored
Comments
Pass statements
Actions and data packaged in libraries
Providing your actions to the interpreter
Directly interacting with the interpreter
Writing actions in a file (script)
Writing your actions as libraries (modules)
Important Concepts
Further Reading
Exercises
Conditional and Repetitive Execution
Conditional execution
if statement
Exercise
Nested if statements
Practice
Conditional expression
Repetitive execution
while statement
Examples with while statement
for statement
Examples with for statement
continue and break statements
Set and list comprehension
Important Concepts
Further Reading
Exercises
Functions
Why define functions?
Defining functions
Passing parameters to functions
Default Parameters
Scope of variables
Higher-order functions
Functions in programming vs. functions in Mathematics
Recursion
Function Examples
Programming Style
Important Concepts
Further Reading
Exercises
A Gentle Introduction to Object-Oriented Programming
Properties of Object-Oriented Programming
Encapsulation
Inheritance
Polymorphism
Basic OOP in Python
The Class Syntax
Special Methods/Operator Overloading
Example 1: Counter
Example 2: Rational Number
Inheritance with Python
Interactive Example: A Simple Shape Drawing Program
Useful Short Notes on Python’s OOP
Widely-used Member Functions of Containers
Important Concepts
Further Reading
Exercises
File Handling
First Example
Files and Sequential Access
Data Conversion and Parsing
Accessing Text Files Line by Line
Termination of Input
Example: Processing CSV Files
Formatting Files
Binary Files
Note on Files, Directory Organization and Paths
List of File Class Member Functions
Important Concepts
Further Reading
Exercises
Error Handling and Debugging
Types of Errors
Syntax Errors
Type errors
Run-Time Errors
Logical Errors
How to Work with Errors
Program with Care
Place Controls in Your Code
Handle Exceptions
Write verification code and raise exceptions
Debug Your Code
Write Test Cases
Debugging
Debugging Using Debugging Outputs
Handle the Exception to Get More Information
Use Python Debugger
Important Concepts
Further Reading
Scientific and Engineering Libraries
Numerical Computing with NumPy
Arrays and Their Basic Properties
Working with Arrays
Linear Algebra with NumPy
Why Use NumPy? Efficiency Benefits
Scientific Computing with SciPy
Data handling & analysis with Pandas
Supported File Formats
Data Frames
Accessing Data with DataFrames
Modifying Data with DataFrames
Analyzing Data with DataFrames
Presenting Data in DataFrames
Plotting data with Matplotlib
Parts of a Figure
Preparing your Data for Plotting
Drawing Single Plots
Drawing Multiple Plots in a Figure
Changing elements of a plot
Important Concepts
Further Reading
Exercises
An Application: Approximation and Optimization
Approximating Functions with Taylor Series
Taylor Series Example in Python
Finding the Roots of a Function
Newton’s Method for Finding the Roots
Misc Details on Newton’s Method for the Curious
Newton’s Method in Python
Newton’s Method in SciPy
Finding a Minimum of Functions
Newton’s Method for Finding the Minimum of a Function
Misc Details for the Curious
Newton’s Method in Python
Newton’s Method for Finding Minima in SciPy
Important Concepts
Further Reading
Exercises
An Application: Solving a Simple Regression Problem
Introduction
Why is regression important?
The form of the function
Least-Squares Regression
Linear Regression with SciPy
Create Artificial Data
Download and Visualize Data
Fit a Linear Function with SciPy
Analyze the Solution
Non-linear Regression with SciPy
Create Artificial Data
Download and Visualize Data
Fitting a Non-linear Function with SciPy
Analyzing the Solution
Important Concepts
Further Reading
Exercises