Python 1.1 - All about programming world
Before diving directly into python, let’s first understand
some concepts about what are programs, why do we write programs, what is a programming
language, why do we need it and all the basic concepts about programming world.
No matter what programming language you are learning, you must know these
things too. These topics can also be asked in job interviews, written exams,
viva etc. You are now going to learn a programming language, then as a
programmer you must know these basic topics too. So let’s start.
All about programming world
We all use popular apps like Google, YouTube, Elyments, Facebook, Instagram, Twitter and millions of other applications. Have you ever thought about how these applications are made? Let me tell you that all these applications are made by writing programs. And to write programs, we need a programming language. So a programming language helps programmers to write programs. Programs can be created for computers, smartphones, tablets and for all the devices that have a processor and memory. Why processor and memory? Because programs are executed by a processor and the processor uses memory to store program’s data. When we say memory, it means we are talking about RAM because a processor can only access RAM. RAM stands for ‘random access memory’ which means a CPU (processor) randomly accesses the RAM to store and retrieve the data. So some examples of programming languages are C, C++, JAVA, Python etc. By using these languages, we can write programs to create applications. So programs are first written by programmers to create applications. These programs are then executed by the processor or CPU (Central processing Unit). Then finally the processor gives us the result. To achieve this result, a programmer writes programs.
Question – Why do
you need to learn programming language?
Answer – The present era is digital age where everything happens on computer and mobile. Everybody in the world wants to earn money to lead a life. For this, people do different jobs. One of those jobs, is that of a programmer or developer. A programming job is a job that the world always needs. A programming job is such a job that has never taken off till date. This world needed programmers and always be. You can say about programmers that “Sky is not the limit”. I think this explanation is enough for you that why should you learn programming.
Now we know that a
processor can execute only machine code. Let’s understand some other important
terms and the other forms of a code -
Program
A program is a set
of instructions/statements or commands that is created by a programmer to get
the machine (computer) or any other electronic or digital device to do some
work. Remember that the device must have a processor so that our programs can
be executed. Programs are always written in a programming language (low level
or high level language). We will write programs in a high level language
(python).
Programming language
So you want to write
programs for a machine? Have you thought about where and how to write programs?
Here comes the programming language. A programming language is used to write
programs. It provides an environment for programmers to write programs easily. By
using programming language, a programmer can write programs in English
language. Also, a programming language knows how to communicate with computer
(processor). There are two types of programming languages – low level programming
language and high level programming language which we’ll cover later.
Source code
The code/program
written by a programmer is called source code. It is generally written in a
high level language (Because nowadays low level language is used with little to
no). A source code is written in plain text using a human readable language
such as English. For example, if you have written a program in python and saved
the file named “sample.py” then this file will be called Source code since it
is written by programmer himself with a high level language.
Object code
If you come from C/C++ background, then you would know what an object code is. And if you are not from C/C++, then just read and move on to the next topic. We get the object code after compiling (compiling means executing the program) a C or C++ program. So a C/C++ compiler generates object code. An object code is a file that has an extension “.o” or “.obj”. Object code is often called machine code but it is not a complete machine code. The processor cannot execute an object code so the object code is converted into machine code (.exe file in Windows OS) by a program called Linker. Since object code is the first step to become a machine code, hence it is also called a machine code. But again Object code is slightly different from machine code.
Note: In C/C++, we get the ‘.exe’ file in only windows machine (Windows OS). When a C/C++ source code is compiled in another machine such as Linux and Macintosh then we don’t get an ‘exe’ file (because Linux and Mac do not understand ‘exe’ files) instead we get a file which can be executed by Linux and Mac OS. Such file doesn’t have any specific extension. So the thing is, C and C++ programming languages are also machine independent and portable but the code (.exe file in windows OS and other files in Linux and Mac OS) which is generated by compiler is machine dependent.
Let’s clear your doubt by taking an example. Suppose you are
a developer and you created a program in C/C++ language for a customer. Let’s
assume that you used Windows OS to develop the program so at the end you will
get an ‘exe’ file since Linker will generate ‘exe’ file in Windows machine.
This ‘exe’ file can only be run on Windows machine and not on Linux, Mac or other
machine. In order to run the file on Linux and Mac, you have to compile the
source code again in Linux and Mac so that C/C++ could generate the compatible
files for these operating systems. The conclusion is that you have to compile
the source code every time for different machine in C/C++ languages.
Machine code
A Processor cannot execute the source code, object code or any other code. It can only understand and execute the machine code. Machine code is the code that is directly executed by a processor. A machine code contains only 0s and 1s since computers work only with 0s and 1s (binary language). So machine code is also called binary code or binary language. It does not matter in which high level language (C, C++, Python, Java etc.) we are creating the program, at the end, the machine code will be generated and executed by the processor so we can get the output/result.
Note: We (programmers) will write the source code
(Source code is in English language), but the processor cannot execute the
source code because a processor can understand only and only machine code
(Machine code is in the language of 0s and 1s). So the source code will be
converted into machine code. In this process of conversion, there may be other
forms of code such as Object code in C/C++ programming. In C/C++ language we
get the object code before the machine code. Another form of code is Byte code
that we will get before machine code in Python programming language.
If you have no C/C++
background, then you may have some difficulty to understand object code but
don’t worry about it and move on.
Byte code
As we have understood that when a C/C++ source code is compiled then we get the object code, and then the object code is converted into machine code by Linker. Here compiling means executing or running the source code. When a Python source code is compiled then we get an intermediate code called Byte code. Python does not generate object code. Byte code is a file that will be produced by Python Compiler and then the Byte code will be executed by Interpreter (PVM) to generate the machine code so processor could execute it. We will understand compiler, interpreter and PVM very shortly. Since each instruction is of one Byte in Byte code, hence it is called the Byte code. The advantage of the Byte code is that the Byte code is machine independent which means that the Byte code can be executed on any Machine in the world, no matter if the machine is running on Windows, Linux/Unix or Mac. We don’t need to compile the Python source code every time for different machine. We just need the Byte code. This is not possible with the object code in C/C++. So the final code generated by C/C++ is machine dependent (.exe file for windows OS) since ‘.exe’ files generally run on Windows OS and code generated by Python compiler (Byte code) is machine independent.
Everything clear? As
we can see the difference between a C code and a Python code. We will discuss
about PVM soon.
Till now we
understood that we will write source code but a machine/computer cannot
understand it, it only understands machine code so there must be a way to
convert a source code into a machine code. And this task is done by “Language
translator”.
Translator
We cannot convert
the source code into machine code by ourselves. This work can only be done by a
software. That software is called a translator. So a programming language
translator is a software/program that converts a source code into machine code.
There are 3 types of language translators exist – Compiler, Assembler and
Interpreter. Different
programming languages use different translators with their benefits and
drawbacks. Some programming languages use a mixture. For example, Python uses
mixture of Compiler and Interpreter both. When you use any programming language
then you don’t need to exclusively use/download a translator, it comes with
that language.
So far we have
discussed about concepts of programming language. Now let’s talk about types of
programming languages –
As you can see there
are many drawbacks of using a low level language, that is why the world uses
high level language to create applications. But it does not mean that low level
languages are not used at all. Assembly language is used to develop hardware
related applications such as device drivers and high level languages are used
to develop user friendly applications.
As you know that source code cannot directly be executed by computer so first we need to convert it into machine code so that computer(processor) can execute it and give the final result. And to convert the source code, we have three “Translators” that will do this task.
Comments
Post a Comment