Python 101 - Basics

in Python

Python 101 : Basics

Computers can perform multiple tasks because they can be programmed, which means they are not designed to play a unique activity, but to do whatever is expected from the installed programs.

A program is a set of instructions that the computer must follow to perform a task. Programming languages have been developed to specify what actions must be performed by the program.

Python is one of the easiest programming languages to learn; in fact it is used in many professional environments (business, health, army, government, university, entertainment and data science). It has been developed in the early 90s by Guido Van Rossum, with the objective to perform repetitive tasks in a simpler way. It has spread significantly in scientific and academic applications, thanks to his syntax, simpler than other popular languages one (like Java, C and C ++), which makes it easy to learn. Today, Python is used by Google, NASA, Youtube, many software companies that produce video games, the New York Stock Exchange and many others.

Before starting with Python syntax, let’s have a look at programming basic concepts (it’s necessary to know some basic information on how a computer works).

1 – Hardware and data storage

The term hardware refers to all the components of a computer, of which the most important are:

  • CPU: The central processing unit is the most important component of a computer, without which it could not run the software.
  • RAM: The random access memory can be considered the working area of the computer, i.e. the place in which the program is stored while it is running. RAM is a volatile memory used to temporarily store the data while working on them.
  • HDD: The hard disk drive is a type of memory that can hold data for long periods of time, even when the computer is not powered.

The computer memory is divided into small memory units called bytes. A byte is a portion of memory to store a letter of the alphabet or a small integer. Each byte is divided into eight memory locations known as bits, acronym for binary digit. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit of memory in many computer architectures. The bits are microscopic electrical components that may contain a positive or negative charge; normally, the positive charge is considered as the on state of the bit, while the negative charge as the off state. When storing a data in a byte, the computer sets a sequence of on/off states to represent that data. A single bit can hold only one of two values: 0 for off state or 1 for on state.

All stored data on a computer are converted into a sequence of 0s and 1s.

In this system, which is defined binary system, the position of each digit has a proper value.

The table below shows the values of bit/bytes.

Number of bits

Number of values

2 raised to the power

Unit

1

2

2^1

Bit (0/1)

2

4

2^2

4

16

2^4

8

256

2^8

Byte (one character)

16

65.536

2^16

More meaningful information are obtained by combining consecutive bits into larger units. Computers are sometimes classified by the number of bits they can process at one time or by the number of bits they use to represent addresses. Graphics are also often described by the number of bits used to represent each dot. A 1-bit image is monochrome, an 8-bit image supports 256 colors or grayscale, and a 24- or 32-bit graphic supports true color. Large amounts of memory are indicated in terms of kilobytes, megabytes, gigabytes, terabytes, etc.

The amount of the values in the positions where the bit is on determine the value of the number written with binary system. In the following table for example, the values of the state on are 1, 4, 8, 16 and 128: their sum is 157. Then the value of the binary number 10011101 is exactly 157.

1

0

0

1

1

1

0

1

2^7

2^6

2^5

2^4

2^3

2^2

2^1

2^0

128

64

32

16

8

4

2

1

When all the bits in a byte are set to 0 (off), the byte value is 0, while when all are set to 1 (on), the byte value is 255, which is the highest value that a byte can have. To store numbers greater than 255 computers must use more than one byte.

Any data stored on a computer must be in the form of binary number, including characters such as letters or punctuation marks. One of the most widely used standard for encoding of alphabet letters and other symbols, for a total of 128 numeric codes, is the ASCII (American Standard Code for Information Interchange), invented in the early 60s and used by almost all computer manufacturers. However, this standard has limitations because it defines only 128 characters, and that’s why in the 90s was introduced the Unicode standard, compatible with ASCII, but with a much more extensive coding system.

2 – How programs work

The CPU, as seen before, is the most important component of a computer and is designed to carry out a series of rudimentary instructions: read/write memory, arithmetic operations (addition, subtraction, multiplication, division) and logic operations (comparison data). The CPU then performs simple operations on the data, not independently but with the aid of a program.

A program is nothing more than a list of commands which perform operations to the CPU.

The CPU can only understand instructions that have a machine language written binary structure (eg. 10011101). For each rudimentary instruction there’s a corresponding machine language instruction and the set of these is called CPU instruction set. The programs, which contain thousands of instructions, are stored on mass storage (HDD) and are copied to the main memory (RAM) when the CPU has to execute them. If the CPU executes the instruction of a program it starts a process called fetch-decode-execute cycle:

  • Fetch: The first step the CPU carries out is to fetch some data and instructions (program) from main memory then store them in its own internal temporary memory areas. These memory areas are called registers.
  • Decode: The second step is for the CPU to make sense of the instruction it has just fetched. The CPU decodes the instruction and prepares various areas within the chip in readiness of the next step.
  • Execute: This is the part of the cycle when data processing actually takes place. The result of this processing is stored in yet another register. Once the execute stage is complete, the CPU sets itself up to begin another cycle once more.

Because computers can only execute programs written in machine language (binary system), developers have created other alternative languages more easy to understood, called assembly languages. This type of language does not use binary numbers for instructions but short mnemonic character sequences (eg. add to sum or mul to multiply). The programs in assembly language, however, can not be executed directly by the CPU, and it needs a special program known as assembler that translates them into machine language.

Figure - Assembler

However, the assembly language is still very difficult to use, so in the 50s were born the first high-level languages that greatly simplify programming. Each high-level language has its own set of pre-defined words (keywords) that the programmer must use to write a program, each of which has a precise meaning and can not be used for any other purpose.

The following table shows some of the keywords for Python.

and

del

from

None

True

as

elif

global

nonlocal

try

assert

else

if

not

while

break

except

import

or

with

class

False

in

pass

yield

continue

finally

is

raise

def

for

lambda

return

High-level programming languages contain, in addition to keywords, operators which carry out various operations on the data (arithmetic, logic, etc.). Each language has its own syntax: a set of rules that must be followed strictly when writing a program that set out how keywords, operators and various punctuation marks can be used in the program itself. To be read by the CPU, the high-level languages must be translated into machine language, through the use of a compiler or interpreter.

A compiler is a program that translates a code written in a high-level language into a program written in machine language.

Figure - Compiler

An interpreter is a program that translates and executes instructions of code written in high-level language. The instructions are executed immediately. This process is repeated for each high-level instruction.

Figure - Interpreter

The instructions written in a high-level language program are called source code (or even code or source). If the code contains a syntax error, however, the translation does not take place. A syntax error is an error caused by a keyword poorly written, a punctuation mark missed or an incorrect use of operators; programmers must be aware of this in order for the code to be translated.

In Python, the interpreter can be used in two modes: interactive and script. In the first case the interpreter waits for typed statement, executes it and waits for another; in the second the interpreter reads the contents of a file containing instructions in Python, with a .py extension, and then executes each instruction of the program in the correct order. The Python interpreter can be started from the command line of the operating system or through an IDE (Python 102: IDE), which is a single program that provides all the tools needed to write, run and test the code.

3 – Software development process

As previously seen, developers use Python to create programs. However, creating a program is a process much more complex than writing a code. The process of creating a program that functions properly usually takes several steps, illustrated in the following figure:

Figure - Program Development Cycle

This is called software development process and consists of five stages:

  • Design the program: before writing the code developers need to carefully plan a program.
  • Write the code: at this stage the programmer writes the code using the syntax rules that determine how to use keywords, operators, and punctuation marks.
  • Correct Syntax errors: if the program contains a syntax error, the interpreter displays an error message indicating what error it is. When all the typos and syntax are corrected, the developer can compile the program translating it into machine language code.
  • Test the program: when the code is in an executable form, it is tested to determine if there are logical errors. These errors do not prevent the execution of the program but can generate wrong results.
  • Resolve logical errors: at this stage developers debug the code to correct logical errors. It may happen that the programmer discovers that it is better to change the original structure of the program and, in this case, the development cycle begins again.

The design phase is certainly the most important one because it determines what are the foundations of the program. This phase can be described in two steps:

  • Requirements analysis: that is critical to the failure of a software project. The requirements should be documented, actionable, measurable, testable, traceable, related to identified business needs or opportunities, and defined to a level of detail sufficient for system design.
  • Creating an algorithm: using previous templates, flowcharts and pseudocode to identify the problem write a draft solution of it.

To sum up, computer science is necessary to learn how computers think; it teaches you how to think algorithmically and how to solve problems. As seen before the process to develop a program can be summarised into three steps: input (what is the problem and what is required to solve it), algorithm (the instructions to solve the problem) and output (the finished code of the program).

Book – Interesting Reads

 

Leave a comment

Comment