Introduction to Computing#
Course Objective#
Introduce computer programming via Python as a tool for solving engineering problems
Straddles between a full-fledged introductory computer programming course and a numerical methods course
Reference textbooks
Algorithmic Problem Solving with Python by John B. Schneider, Shira Lynn Broschat, and Jess Dahmen. 2013.
Foundations of Python Programming by the Runestone Interactive Project
Think Python: How to Think Like a Computer Scientist by Allan B. Downey
Python Programming and Numerical Methods: A Guide for Engineers and Scientists by Qingkai Kong, Timmy Siauw, and Alexandre Bayen. 2020.
Computers#
What is a computer?
A device or machine capable of processing instructions and performing arithmetic or logical operations
Note: before the invention of electronic computers, computers referred to humans that carried out repetitive arithmetic calculations
Mechanical Computers#
Antikythera mechanism
Used for predicting astronomical events
Considered the first known computer
Antikythera Mechanism
Source:
Using computation to decode the first known computer, Edmunds & Feeth, 2011
Difference and analytical engine
Invented by Charles Babbage
Difference engine is a mechanical calculator designed to tabulate logarithms and trigonometric functions
Analytical engine is designed to be a general-purpose mechanical computer that was never built
Tide-predicting machine – invented by Lord Kelvin
Slide rule
Differential analyzer
…
Tide Predicting Machine
Source:
Lord Kelvin and his analog computer, Allison Marsh, 2024
Electronic Computers#
Earlier computers using analog electric signals
Inflexible - need to be reprogrammed for solving a specific program
Modern computers use digital signals and operate on binary data (0’s and 1’s)
Bit = Binary Digit
Bits to Bytes conversion
8 |
Bits |
= 1 Byte |
1024 |
Bytes |
= 1 Kilobyte (KB) |
1024 |
KB |
= 1 Megabyte (MB) |
1024 |
MB |
= 1 Gigabyte (MB) |
1024 |
GB |
= 1 Terabyte (MB) |
Electronic computers do not need precision mechanisms like mechanical computers
1 KB = 1000 bytes is the SI convention; kibi, mebi, etc. were proposed but not commonly used
Digital Computers#
Fixed program computers
Perform only a set of tasks (e.g., calculators)
Stored program computers
General-purpose devices like PCs, smartphones, and servers
Modern general-purpose computers constitute:
Input and output devices: mouse, keyboard, printer, monitor
Memory: RAM, hard drive(s)
Processors: CPU(s) and GPU(s)
The basic architecture of a modern computer
Source:
Wikipedia.org, Kaphoot
Current CPUs consist billions of transistors that operate on digital signals
Transistor is the basic unit of computation
Replica of the first transistor
Source:
Wikipedia.org, Ragesoss
Metal-Oxide-Semiconductor Field Effect Transistor (MOSFET)
Source:
Wikipedia.org, Cyril BUTTAY
The transistors form digital circuits that perform logic and arithmetic operations on binary data given specific instructions (also in binary format)
CPUs understand only binary instructions
These binary instructions are referred to as machine code or machine language which is specific to the CPU architecture
Current CPUs pack ~10 billion transistors
Source:
igorslab.de, Igor Wallossek, 2023
Computer Programming#
A computer program (or simply program) is a sequence of instructions to the computer with the goal of automating a specific task
Examples: e-mail clients, word processors, browsers, engineering software
Computer programs are written in computer programming languages
Programming languages can be classified as:
Low, medium, and high-level languages
Low-level languages
Machine language, assembly language
Specific to the CPU architecture
“Medium-level” languages
Fortran, C, C++, Rust
Independent of the machine architecture; abstract away the architectural details
Human readable
Need a compiler program to convert the source code into executable machine code
High-level languages
Python, Matlab language, Mathematica language, Perl, Ruby, tkl
Human readable and independent of the machine architecture
Not compiled but instead use an interpreter to process one instruction at a time
Programs written in compiled languages are typically faster than those written in interpreted languages
Programming languages
General purpose languages
C, C++, Python
Applications such as browsers, games, communication tools
Scientific computing
Fortran, Matlab, Octave, Julia, R
Applications
Weather prediction
Computer-aided design (CAD)
Computer-aided engineering (CAE)
Simulation of solids and fluids
Data analysis and visualization
Image analysis
Signal processing
…
Programming paradigms
Procedural programming
Makes use of reusable procedures or subroutines that operate on given data to achieve specific tasks
Object-oriented programming
Uses objects to combine data and procedures (called methods) to do the same
…
Fortran and C are strictly procedural programming languages
Python and C++ support both procedural and object-oriented programming paradigms
Programming vs Spoken Languages#
Spoken languages
Differ in grammar and semantics (meaning of the words/phrases in a specific context)
May use different scripts and sounds
Programming languages
Differ in syntax (rules for providing instructions to the computer)
Semantics: usually have either a valid or invalid meaning for an expression
Spoken languages |
Programming languages |
---|---|
Words |
Keywords (typically plain English words) and mathematical operators |
Grammar |
Syntax |
Communication between people |
Communication with computers |
Flexible and evolve over time |
Rigid; new syntax and features are added |
Programming Languages#
All programming languages have some common features. They define:
Keywords, syntax, and semantics of the language
Data types and data structures
How these can be operated on and interact with each other
Standard data types
Numbers (integers, real, and complex)
Characters
Boolean (True or False)
Data structures – groupings of various data types
Lists, arrays, maps, strings
Trees, hashes, graphs
Objects
Combine data with procedures that operate on the stored data (referred to as methods)
A class defines, implements and enables instantiation of objects
C vs Python Programming Language#
The C programming language is a statically and weakly typed language
Static typing - the data type must be specified before execution at the compile time
Weak typing - allows type conversion
E.g., a character or real number can be converted to the integer type during execution
Can lead to unpredicatable behavior
To be specific, C is relatively weakly typed compared to certain languages
Data types have fixed size in memory
/* Simple C program */
#include <stdio.h>
int main()
{
float varPi = 3.14159;
printf("Hello World!\n");
printf("The value of pi is %f\n", varPi);
return 0;
}
Python is dynamically and strongly typed language
Dynamic typing - the data type is typically determined during execution
Strong typing - type conversion is typically not done unless explicitly requested
All data types are objects
Memory automatically allocated for integers and can be arbitrarily large
Real numbers behave the same way as
double
data type in C
# Simple Python program
varPi = 3.14159
print("Hello World!")
print("The value of pi is {}".format(varPi))
Hello World!
The value of pi is 3.14159
Python: Pros and Cons#
Advantages of Python over C (and other similar languages)
Simple syntax, highly readable, and easy to learn/use
Automatic memory management
Free and open source
Portable and extremely popular
Ranked No. 1 over C and C++ by TIOBE index and IEEE
Advantages of Python over C (and other similar languages)
- Extensive standard library and many extensions in the form of modules and packages
- Numerical analysis - numpy and scipy
- Plotting and visualization - matplotlib
- Math and physics - sympy_ and sagemath
- Image processing - scikit-image
- Data science, statistics, and machine learning - pandas, scikit-learn, PyTorch
- Astrophysics - astropy
- Bioinformatics - biopython
Disadvantages
Slow and less memory efficient compared to compiled languages like C, Fortran, and C++
Total | |||||
---|---|---|---|---|---|
Energy (J) | Time (ms) | Mb | |||
(c) C | 1.00 | (c) C | 1.00 | (c) Pascal | 1.00 |
(c) Rust | 1.03 | (c) Rust | 1.04 | (c) Go | 1.05 |
(c) C++ | 1.34 | (c) C++ | 1.56 | (c) C | 1.17 |
(c) Ada | 1.70 | (c) Ada | 1.85 | (c) Fortran | 1.24 |
(v) Java | 1.98 | (v) Java | 1.89 | (c) C++ | 1.34 |
(c) Pascal | 2.14 | (c) Chapel | 2.14 | (c) Ada | 1.47 |
(c) Chapel | 2.18 | (c) Go | 2.83 | (c) Rust | 1.54 |
(v) Lisp | 2.27 | (c) Pascal | 3.02 | (v) Lisp | 1.92 |
(c) Ocaml | 2.40 | (c) Ocaml | 3.09 | (c) Haskell | 2.45 |
(c) Fortran | 2.52 | (v) C# | 3.14 | (i) PHP | 2.57 |
(c) Swift | 2.79 | (v) Lisp | 3.40 | (c) Swift | 2.71 |
(c) Haskell | 3.10 | (c) Haskell | 3.55 | (i) Python | 2.80 |
(v) C# | 3.14 | (c) Swift | 4.20 | (c) Ocaml | 2.82 |
(c) Go | 3.23 | (c) Fortran | 4.20 | (v) C# | 2.85 |
(i) Dart | 3.83 | (v) F# | 6.30 | (i) Hack | 3.34 |
(v) F# | 4.13 | (i) JavaScript | 6.52 | (v) Racket | 3.52 |
(i) JavaScript | 4.45 | (i) Dart | 6.67 | (i) Ruby | 3.97 |
(v) Racket | 7.91 | (v) Racket | 11.27 | (c) Chapel | 4.00 |
(i) TypeScript | 21.50 | (i) Hack | 26.99 | (v) F# | 4.25 |
(i) Hack | 24.02 | (i) PHP | 27.64 | (i) JavaScript | 4.59 |
(i) PHP | 29.30 | (v) Erlang | 36.71 | (i) TypeScript | 4.69 |
(v) Erlang | 42.23 | (i) Jruby | 43.44 | (v) Java | 6.01 |
(i) Lua | 45.98 | (i) TypeScript | 46.20 | (i) Perl | 6.62 |
(i) Jruby | 46.54 | (i) Ruby | 59.34 | (i) Lua | 6.72 |
(i) Ruby | 69.91 | (i) Perl | 65.79 | (v) Erlang | 7.20 |
(i) Python | 75.88 | (i) Python | 71.90 | (i) Dart | 8.64 |
(i) Perl | 79.58 | (i) Lua | 82.91 | (i) Jruby | 19.84 |
Source: Ranking programming languages by energy efficiency, Pereira et al., 2021.
Working with Python#
To learn and write programs in Python, we need:
A Python interpreter
An integrated development environment (IDE)
A software tool that helps:
Create and edit programs
With syntax highlighting and code autocompletion
Debug programs to resolve unexpected/unintended behavior
A package manager
A program to obtain additional modules/packages as required