Programming in Python
Programming in Python
Introduction to Python:
What does it mean by language?
Human beings use languages for communication. We use the languages like
English, Hindi, French, Bengali. These are the natural languages. These are called
as natural languages. So, we use these languages for communication.
Likewise, machines also have their languages. This is our computer system
that is also having its own language. And that language we call it as machine
language. And this machine works in number system that is binary number system
that is in the form of zeros and ones only.
What is programming?
Programming means a step-by-step procedure for solving any problem.
So a language that is used for writing the procedures which machines can
understand and execute them and machines do the processing so that languages
are nothing but programming languages.
Types of Programming Languages
Low-Level Language
Machine Language
i) The machine language that is in the form of only zeros and ones.
ii) Humans cannot understand this language that is zeros and ones
easily.
Assembly Language
High-Level Language
o General Purpose
o Special Purpose
o Scripting
Then later people thought that let us use English like languages for writing
the programs such languages are called as high-level languages.
We write the programs using C language, then we compile it and the
compiler will convert this C language program into machine language
code.
General Purpose
The language can be used for writing various types of programs like
desktop application, web application, cloud application, enterprise
application, mobile application, telecommunication application system
programming so on.
But a language which is used for writing any type of application is
called as general-purpose language.
he languages as C, C++, Java, C#, python. These are all high-level
languages which are useful for various purpose of programming
Special Purpose
There are some special purpose languages, like there are some
languages only for web development, there are some languages that are
only for mobile development, like Swift is the language or a Android
language. There are some languages only for gaming purpose, so such
languages are called as special purpose languages.
Example: JS, Android
Scripting
They are very easy to write means they will not have very strict rules of
programming. It is easy for the programmer to write down the code, so
those are usually called as scripting languages.
Python is a general-purpose language, but it is more like scripting
language, so it's easy to learn and develop the application.
High-Level Language
Compiler Based (C, C++)
Interpreter Based (JS, PHP)
Hybrid (Java, C#, Python, .Net)
The high-level languages are either compiler based or interpreter based or hybrid.
We write the programs using high level language. Machine cannot understand that
language, so this high-level language program code should be translated into
machine language code.
Who does the translation?
So, translation is done either by compiler or interpreter. So, it's done either by
compiler or interpreter. There are two methods of translation.
So, some programming languages uses only compiler some programming
languages, uses only interpreter, and some uses both. Some are hybrid languages,
which uses both compiler and interpreter.
Compiler VS Interpreter
Compiler:
i) Compiler takes the complete source program and generates complete
machine code program. It takes a file and it gives a new file that contains
a complete machine code.
ii) If there are no errors in your program, then only this translation is done
into machine code.
iii) Only one time translation is done.
Interpreter:
If these are the lines of JavaScript code, the interpreter will translate one line into
machine code. It will generate the machine code just for one line and also get it
executed at that time. Only then second line translate into machine code, get it
executed next line, convert it into machine code executed each line it will take
and convert into machine code and also it will get executed. So this is how
interpreter works.
Difference between Compiler and Interpreter
i) See, the compiler was not executing it. Compiler was giving complete
machine code. You execute whenever you want, but interpreter will directly
execute it on the machine.
ii) Interpreter will convert each line into machine code and getting executed and
separate file is not generated but compiler will generate two separate files.
iii) Compiler based languages are faster than interpreter-based languages because
in interpreter-based languages, every time translation is done, program is
translated into machine code and executed. So, we say it is done line by line.
Sometime the entire code is converted also, but always the translation is done.
iv) Interpreter languages are easier to write and run because they run inside the
runtime environment, which is more comfortable for the programmer to
develop such programs by taking the support of runtime environment.
How a Python Program Runs?
It will compile this source code into intermediate code called bytecode. it converts into
bytecode and it generates one hidden file. Actually, it's cannot be seen. So, the file name is
program (.pyc) so it was (.pyc) file but now it is compiled file.
When the compilation is done. Compiler will check if there are any errors in the
program. If there are no errors, then it will convert into machine code. Interpreter will not check
for the errors because already the bytecode is error free. So, what it has to do just it has to
translate this bytecode into machine code that is machine language. So, Python's interpreter
will translate bytecode into machine code and execute. When you run a program, it gets
compiled and then interpreted and executed by Python Virtual Machine that is PVM.
Python Features
Python provides many useful features which make it popular and valuable from
the other programming languages. It supports object-oriented programming,
procedural programming approaches and provides dynamic memory allocation.
We have listed below a few essential features.
2) Expressive Language
Python can perform complex tasks using a few lines of code. A simple example,
the hello world program you simply type print("Hello World"). It will take only
one line to execute, while Java or C takes multiple lines.
3) Interpreted Language
4) Cross-platform Language
Python can run equally on different platforms such as Windows, Linux, UNIX,
and Macintosh, etc. So, we can say that Python is a portable language. It enables
programmers to develop the software for several competing platforms by writing
a program only once.
6) Object-Oriented Language
7) Extensible
It implies that other languages such as C/C++ can be used to compile the code
and thus it can be used further in our Python code. It converts the program into
byte code, and any platform can use that byte code.
It provides a vast range of libraries for the various fields such as machine learning,
web developer, and also for the scripting. There are various machine learning
libraries, such as Tensor flow, Pandas, Numpy, Keras, and Pytorch, etc. Django,
flask, pyramids are the popular framework for Python web development.
Graphical User Interface is used for the developing Desktop application. PyQT5,
Tkinter, Kivy are the libraries which are used for developing the web application.
10) Integrated
It can be easily integrated with languages like C, C++, and JAVA, etc. Python
runs code line by line like C, C++ Java. It makes easy to debug the code.
11. Embeddable
The code of the other programming language can use in the Python source code.
We can use Python source code in another programming language as well. It can
embed other language into our code.