Python Fundamentals – Introduction

This entire note series will learn how to build the skill set to solve real-life problems. However, before that, we will gain knowledge of the python programming language and its key concepts. We will cover how to represent knowledge with data structures, computational metaphors such as iteration and recursion, abstraction of procedures and data types, organize and modularize systems using object classes and methods, different algorithms, searching and sorting, and complexity of algorithms.

What does a computer do?

Fundamentally computers perform calculations, generate results, consume them later on, or store them somewhere. Here, somewhere refers to persistent and non-persistent storage such as SRAM and DRAM for non-persistent and Hard drive, or external storage devices for persistent storage.

The computer does mainly two kinds of calculations. First, the calculations built into the language are very low-level calculations such as addition, subtraction, multiplication, etc. These are called primitive calculation types and are always supported by programming languages. The second type is, using the primitive calculation types, programmers design and build new types of calculations, and computers will perform those calculations as these are built on top of primitive calculations.

The computer does not have a mind, and it knows how to calculate really quickly as possible. But in the end, we need to tell it what calculation it needs to do as computers only know what we tell them.

Types of Knowledge

There are two types of knowledge, declarative knowledge and imperative knowledge. Declarative knowledge is statements of fact or truth. Whereas imperative knowledge is a recipe or cookbook where all the steps are well defined.

For example, suppose we want to find the square root of a number x, such that y = \sqrt{x}, we can rewrite it as y*y = x, and this is the statement of fact to find the square root. So, it is declarative knowledge. But, on the other hand, the methods or recipe for deducing the square root of a number x is imperative knowledge. In computer programming, we usually have declarative knowledge, so we write the recipe, and the computer follows the recipe. 

A recipe is a sequence of simple steps or a flow of controls. In the sequence of steps,  there may be different parts such that sometimes we are making the decision, and sometimes we are continuously repeating few steps, and in the end, we determine that it is a time to stop. In the above example, when we encounter approximate or exact results, which satisfies the declarative knowledge, we stop there. This recipe is a kind of algorithm in computer science terms.

Computers are machines, so the question is how to capture a recipe in a mechanical process. It is done in two ways. The first way is a fixed program computer, and the hardware circuit is designed to realize the recipe. For example, calculators. The other way is a stored program computer, where the machine stores and executes instructions. The recipe is designed and stored in the form of instructions.

Basic Machine Architecture

This basic machine architecture is the heart of every computer system. It contains four main components, Main Memory, Arithmetic Logic Unit (ALU), and Control Unit as a part of Central Processing Unit (CPU) & Input and Output ports.

Basic Machine Architecture
Basic Machine Architecture
  • Main Memory: It keeps a sequence of instructions and a bunch of data.
  • Control Unit: It maintains the execution of instructions and manages ALU, Memory, and IO ports.
  • Arithmetic Logic Unit: It executes the primitive operations.
  • Input and Output are the interfaces through which data is entered, and results are outputted. 

For example, suppose we have a recipe, and with the help of the simplified stored-program computer, we want to execute the recipe or a set of instructions that will end up with some output. Assuming that the recipe is already loaded into the memory. The control unit fetches these instructions from memory, passes them to the ALU for doing the primitive operations, and intermediate results or data will be stored into the memory. Once the entire instructions are executed from the recipe, it outputs the result.

Stored Program Computer

The sequence of instructions stored inside the computer and built from a predefined set of primitive instructions such as arithmetic and logic, simple tests, moving data, etc. There is a special program (interpreter) that executes each instruction in order and uses tests to change the flow of control through the sequence, and when done, it stops.

Basic Primitives

Alan Turing showed that we could compute anything using the six primitives and the six primitives are move left, move right, read, write, scan, and do nothing. So using these six instructions and the piece of tape, he showed that we could computer anything.

The programming language designers came with a more convenient set of primitives to work with modern computer systems, using those six instructions. There are many different programming languages such as C, C++, Java, Python, Golang, Rust, etc. Using any of the programming languages, we instruct the machine to does the same for us. Anything computable in one language is computable in any other programming language, and this is because of the primitives as everything is built on top of these primitives. Different languages give different convenient features, and we select a programming language based on our requirements and convenience. 

Creating recipes

A programming language provides primitive operations, such as add, subtract, multiplication and shifting, etc. Using these primitives, different expressions are designed, which may be complex but must be legal combinations of existing primitives. These expressions and computations have values and meaning in a programming language. These are the building block of any programming language. 

Aspects of Languages

We will see different aspects of programming language with the English language.

English LanguageProgramming Language
Words are primitive constructs.Numbers, Strings, Simple operators are the primitive constructs.
Using these primitive constructs, we create phrases and sentences. These sentences should be syntactically correct according to grammar rules. For example:
a syntactically correct sentence (Noun-Verb-Noun), the cat hugs boy. But this is not syntactically correct, like cat dog boy.
In the programming language, “hi”5 not syntactically correct but 2.3*5 is syntactically valid.
There is something called static semantics, it tells a sentence is syntactically valid and has meaning. For example, “I are hungry”, is syntactically valid but does not give any meaning so it is static semantically invalid.In the same way, 2.5+”hi” it is syntactically valid (operand – operator – operand) but static semantically invalid as adding a number with string does not give any meaning. However, 2.5 + 3.5 is static semantically correct.
In English, the meaning of semantics is associated with a syntactically correct string of symbols with no static semantic errors. Thus, an example can have many meanings. For example, flying planes can be dangerous.Programming languages have only one meaning. The same set of instructions should always do the same thing. But may not be what the programmer intended.

Where things go wrong

  • Syntactic errors: It is common and easily caught.
  • Static semantic errors: Some languages check for these before running the program, and it can cause unpredictable behavior.
  • No semantic errors but different meaning than what the programmer intended. For example, program crashes stop running, runs forever, answers different from expected.

Python Programs

The python program is a sequence of definitions and commands. Usually, definitions (syntax & static semantic) are evaluated, and commands (statements) are executed by a python interpreter in a shell. These statements are the kinds of programming logic or instructions to the interpreter to do something.

Objects

In python, everything is an object, and python programs manipulate these data objects. The objects have a type that defines the kinds of things or operations a program can do. For example, if an object is an integer type, we can do all the major operations with other objects of integer or float types. This gives flexibility to interpreters to check the definition before going to execute the statement. 

There are two types of objects, and these are as follows:

  • One is scalar objects and can’t be subdivided further. These are the basic type of objects and from which everything will be made. For example, primitive data types like integer, string, float, etc.
  • The other type of object is a non-scalar object, and these objects have an internal structure that can be accessed. For example, a list data type contains the same or different data objects. This list can be further divided into multiple objects.
a = 2.1
print(type(a))
# Output: <class 'float'>

print(int(a))
# Output: 2
# It changes the value from float to int

print(type(a))
# Output: <class 'float'>
# It does not change the object type

Expressions

Once you have objects, we can combine objects and operators to form expressions. And each expression has a value. So an expression evaluates to a value. The syntax for a simple expression as “<object> <operator> <object>”. For example, number1 + number1

Binding Variables and Values

An equal sign is an assignment of a value to a variable name. For example, (variable) pi = 3.14159(value). The value stored in computer memory and as assignment binds a name to a value. To retrieve the value associated with the name or variable by invoking the name by typing. pi.

Abstraction Expressions

We give names to values of expression to reuse names instead of values. It also provides flexibility to easier to change code later. Also, expressions are always on the right side, and variables are on the left with respect to an assignment operator. 

pi = 3.14
radius = 5.2
area = pi * (radius ** 2)

Changing Bindings

We can re-bind variable names using new assignment statements. However, by doing that, the previous value may still be there in memory, but we lost the handle for it.  Later on, the garbage collection process will free those kinds of memories.

For example, we can see from the above example code that the pi =3.14, radius = 5.2, and area = 84.90 are stored in memory. When we change the radius value from 5.2 to 4.2, the object allocated earlier for radius still exists in the memory. 

Changing Bindings
Changing Bindings

These variables are immutable objects. Therefore, once the value is assigned, it can’t be modified. It means, for modification, we need a new object, and the variable will be mapped with the new object. However, there are also mutable variables such as list, dictionary, set, etc. Usually, the data type which is built using customer classes is mutable. 

Summary

In this note, we have seen how does computer work, how to control the computer to make the work done from the programming point of view, what is an object in the python programming language, what is primitive operations and how it works, how it is used to compose different data objects.

License

This note is published under CC BY-NC-SA 4.0 license.

References

 230 total views,  1 views today

Scroll to Top
Scroll to Top
%d bloggers like this: