What is Python?
Python is a general purpose,interpreted and high-level programming language.It is object-oriented and dynamically typed programming language.Dynamically typed means that variables are not required to be declared before using them.Instead you directly assign value to a variable.It is called general purpose programming language because of its dynamic nature.Unlike other programming languages such as C++ ,Python has easy to read syntax and use more English keywords which makes it easy to understand Python programs.It can be used for different types of programming tasks such as web applications,desktop applications,gaming robotics,AI.
- Python has following features:Open-source It is open source and can be used freely even for commercial purspose.You can also contribute to the source code of python
- Portability It is supported on all the major OS such as Microsoft Windows, Linux,Mac OS.This is because it uses bytecode which is machine independent.
- Object-oriented It can be used to write both object oriented and strcutured applications.
- It is the official programming language of Raspberry Pi
- Simple Code code written in Python is much shorter than program written in other programming languages.Also code written in Python is much easier for the developers to understand.
- Large collection of standard libraries Python has good collection of standard modules(300).Apart from standard modules there are lots of Open Source Frameworks and toolkits for all of the major programming tasks.These includes :
NumPy and SciPy for numerical computations
Requests for networking
Scrapy for webscrapping
BeautifulSoup for HTML parsing
NLTK for natural language processing
History of Python
First version of Python was released more than 25 years back.It is influenced by ABC and Modula.Python 3(also called “Py3K”)was released in 2008 and addresses shortcomings of the language.
Python 3 is not backwards compatible with earlier versions of Python.
Applications of Python
- Desktop applications
- Web applications Django is a open source web application framework for Python
- Scientific and computational applications
- Machine Learning and data analysis
You can also use Python for:
- quickly developing Prototypes
- natural language processing
- Database programming
- network programming
Is Python compiled?
Python is first compiled ,syntax is verified and then interpreted.When you execute python source code(.py file) following happens:
- Compiled Compilation of source code into bytecode This is intermediate language which is supposed to be executed by Python Virtual Machine(PVM).Bytecode is machine independent. Bytecode is intermediate language as it is executed by the interpreter one line at a time
- interpreted Execution of bytecocde by interpreter Each line is executed at a time by the interpreter.
On compiling Python source code ,bytecode is generated in a file having extension .pyc.
When you invoke the Python interpreter by giving the following command:
$ python file_name.py
then the above 2 steps happen together ,behind the scenes,bytecode is generated and program is executed.Developer never needs to know about the compliation and interpretation steps.
Whenever you run a Python program ,the Python compiler will check if Python bytecode exists.It determines the existence of Bytecode by checking a file with extension .pyc.
If such a file exists then it will execute the bytecode contained in that file.If the .pyc does not exists then it will compile the source code in .py file to create the .pyc file with bytecode.The bytecode file created is dependent upon the specific implementation such as IronPython or JPython.It then passes it to Python virtual machine(PVM).PVM executes(interprets) the bytecode one line at a time.
There has different run times available for different platforms and written in different languages.Some of the important ones are:
- CPython standard version CPython is written in C.Includes only the basic tools and does not perform code optimization.
- Jython executing Python on Java JVM and written in Java.
- IronPython executing on .NET CLR and written in C#.IronPython is used for integrating Python with .NET applications.
Developing Python application
Before you start developing Python programs you need to install Python.You can download Python from the following path https://www.python.org/downloads/.This will install python interpreter.Python interpreter is used for executing Python code.
You can either work with interpreter in interactive mode or give file consisting of Python commands..It can operate in one of the two modes:
- Interactive Mode(REPL) executing one command at a time.Here it displays three greater-than signs (>>>) in windows.It means it is ready to execute the next command.
- Script files Executing code in a “.py” file consisting of Python commands.
Launching the Python Interpreter
When you install Python it will be installed in a directory such as Program Files\Python\Python36-32\ .This directory consists of Python.exe which is the Python interpreter.To launch Python interpreter you have to either navigate to this directory in command prompt or add it to the Path variable.If you included the path to Python.exe in environment variable then you can launch it by simply opening command prompt and typing:
exiting the Python Interpreter
Like other programming languages Python has some programming constructs which are useful when developing applications.Some of the main language elements are:
Modules and Packages Module defines a specific functionality.Programs are composed of multiple modules implementing different functionalities.For example in a Game application there can be modules for Characters,Scores,UI. Module is a namespace which can consist of modules as well as other packages.You can use another module in a module using the import command as:
Classes consists of functions and variables.Class is a template which is used to create an object.
Types Python is dynamically typed language.So you don’t need to define the variable type before using it.You just assign a value to a variable.For example to declare an integer variable you can use the following syntax:
Lists Lists are extensively used in Python.List can contain different types of variables.You can declare and items to a list as:
listObj = 
Garbage Collection and Reference counting Python automatically manages memory using Garbage collection and Reference counting.Reference counting is the means by which Python tracks the active references in the application.Garbage collector is automatically invoked but can also be manually invoked if required.