For us, programming (or coding) is about instructing a computer machine to operate using a programming language written in lines in simple text files or entered at a command prompt.
With it, you can:
Reproduce results in a highly automated way
Without having to manually redo many steps each time
Run routines with variations
Varying input data
Varying parameter values
Build analysis routines which are not available in standard software 'out of the box’
Build workflows or interfaces that are easy for others to use
value = 2
answer = 4 + value
if answer > 0 :
print (answer)
Code is written in text files called the source code (sometimes "src"). In Python's case, each command is on a new line.
The code has a specific syntax along with keywords and operators that have specific meanings for the computer. Here if is a control flow keyword: it controls the flow of the program as it runs based on some condition.
One or more files (often written by different people) work together to make a program.
Why Python?
It can be run on the most common Operating Systems
It has a thriving community of users that produce quality reusable code.
It is built into a wide range of software as a scripting language.
Knowledge of Python is highly desired by a wide range of employers.
Learning about it in depth will teach you about other languages.
Computing history
The first computers were people that did calculations
For example: Katherine Johnson who helped with space exploration
Machines were developed to take on these calculation tasks
Modern computers are electronic and use and store charge and circuits to store and process data
Data which represents things is separated from program instructions that are applied to read and change data
Data and instructions are binary in that they are effectively encoded using a series of zeros and ones, each digit either a 0 or a 1 is known as a bit
Bits are grouped together into Bytes – standard sized sets of bits
32 bit machines are generally working with Bytes of length 32
Binary
Binary encoding can be used to represent different states and each state could represent a number or something else
With 1 bit there are 2 states
With 2 bits there are 22 = 4 states
With 4 bits there are 24 = 16 states
With 8 bits there are 28 = 256 states
With 16 bits there are 216 = 65,536 states
With 32 bits there are 232 = 4,294,967,296 states
With 64 bits there are 264 = 18,446,744,073,709,551,616 states
Programming language types
Low Level Languages
First generation (Machine Code) languages are basic instructions that allowed the state of a computer to be set from a small list of possible states, and essentially allowed data to be read, deleted and written and for the state of the computer to be changed.
Second generation (Assembler) languages allowed computers to do a lot more
They are ‘Low Level’ as they operate close to the hardware, the memory and data processing units.
Programming language types
High Level Languages
Third generation languages first arrived in the 1950s
Programs are:
Written in a much more human readable form
Compiled or interpreted into Machine Code before or as they are run
Compiled languages
Programs are precompiled to check syntax and can be distributed as compiled (non-human readable) executables
Examples: Fortran C++ Java
Interpreted languages
Programs are distributed as source code (human readable) form and are converted into machine code each time they are run
Examples: Python Ruby Javascript
Imperative vs declarative
Imperative Languages are traditionally the most popular. They are used to form sets of statements that are read in sequence from top to bottom, which change the state of the computer (for example, the data held).
value = 2
answer = 4 + value
print (answer)
State is held in spaces inside the computer memory with label attached so we can talk about them: so-called "variables". For example, the label "value" above is assigned the number "2". The label "answer" is assigned the evaluation of the expression 4 + value.
Control flow
Control flow determines the sequence of instructions run by a program.
By default, the program normally runs the next line of code working from top to bottom.
In the past GOTO statements were commonly used to change from this default
A GOTO statement was to the effect of ordering the program to continue to run from a part of the program as specified
Modern languages use procedure/methods/functions/subroutines calls in place of GOTO
For loops and conditional statements also alter the normal top to bottom flow.
text = input("Type a number to add to four")
value = int(text)
answer = 4 + value
print (answer)
Here input is a procedure that displays some text and returns something typed by the user, int takes this typed text and returns a number, and print is code that prints stuff on screen and returns nothing.
Declarative Languages
Declarative Languages usually describe the end point first and the program works to get there.
One subset of these are Functional Languages where one passes one procedure to another in ever more complex nesting in order to calculate some end result defined by the first layer of nesting.
Generally stateful elements like variables are discouraged; the code is constructed from expressions rather than full statements.
print (
4 + int(
input(
"Type a number to add to four"
)
)
)
Generally stateful elements like variables are discouraged; the code is constructed from expressions rather than full statements.
Python
Functional languages are increasingly popular as everything is contained within the nesting; this means it can be split up easily so different nests run on different processors on machines with more than one processor (which is usual now)
Python is a third generation imperative language, but with functional bits.
Many languages are evolving to support both the declarative and imperative approaches.
Object Oriented Languages
Object Oriented (or sometimes in the UK “Orientated”) Languages
Help to divide code over multiple files.
Objects are instances of classes
Classes define the type of an Object
Classes are used to create instances which are the objects
A class could define what a bicycle is
An instance of the bicycle could be my bicycle, or your bicycle, or any other bicycle
Objects have state in that they hold data and can do specific things with it.
For instance, I might be able to ask the object representing my bicycle where it is, or whether it is locked, or what the front tyre pressure is.
Objects can pass data on to other objects and hold references to other objects so long as the class definition allows for this.
The "dot operator" (" . ") is used to say "look inside this object and find this code (in this case a procedure).
Python is a third generation, imperative, procedural, object oriented language.
Scripting languages vs System Programming Languages
Languages can finally be split between:
Systems (or Application) Programming Languages:
used for developing full systems, and...
Scripting Languages, which:
tie together other software (glue languages),
are used inside other applications for
simple programming (extension languages), or
are used to manage systems (control languages).
Generally scripting languages are interpreted (which helps with running inside other software) and hide away complicated code making them easy to learn, but traditionally reducing the ability to do complex jobs and making code had to optimise for speed.
Again, the two are converging: Python is traditionally a scripting language, but gives good access to complicated underlying computing, and undergoes continual optimisation. Much of the time it invisibly calls native code written in C, a very efficient language.
Brief History of Python
Developed initially by Guido van Rossum.
Who worked as part of the ABC language development team in the 1980s: a teaching language with a syntax that encouraged clarity, conciseness, and easy data structure use, taking a lot from a language called SETL (SET Language) developed in the late 1960s. Also influenced by Modula; Amoeba; C++.
Developed Python as a successor; first release 1991. Named after Monty Python.
Python 2 released in 2000: added a few elements of modernisation and opened out development to a full community effort.
Python 3 released in 2008: not back-compatible with Python 2. Python 2 end of life set now at 2020.
We are now on Python 3.9.
Language change
Generally language designers go to extreme lengths to ensure backward-compatibility (i.e. old code still works in the new version).
However language features may be deprecated (scheduled to be removed in a future version).
There is lots of legacy code (old but vital code) hanging around, and people need time (often decades; and significant funding) to revise it.
What's in a program?
Variables : labels attached to data (including text and other bits of code)
Delimiters: things like "()" that structure the code.
Operators: things like "+" that act with variables.
Keywords: words that can only be used to mean specific things (like the word "if" and can't be used for variable names.
Expressions: combinations of variables, keywords, and operators that evaluate to a value.
These are built up into statements telling the computer what to do.
What's in a program?
Statements are structured into programs using:
Line breaks: in Python, each new line is generally a new statement.
Control flow keywords: keywords (like “if”) that control the flow of the code.
Procedures: separate parts of a program that can be called and that run and may return results (or nothing) to the code that calls them.
Classes: code organised into separate bits and that does a specific job.
Libraries: a collection of different objects doing similar jobs.
The Python Philosophy
Python development aimed to keep the language simple and not aim for perfection as “good enough” is often just that.
Also ideas were borrowed from elsewhere whenever it made sense.
Resulted in a language which is, to reuse author Luciano Ramalho’s phrase: “Clear, Concise, and Effective”.