Basics of Python

What is Python: - Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido Van Rossum during 1985-1990. It is used for:

  • Web Development (Server-side)
  • Software Development
  • Mathematics
  • System Scripting

What can Python do?

  • Python can be used on a server to create web applications.
  • Python can be used alongside software to create workflows.
  • Python can connect to database systems. It can also read and modify files.
  • Python can be used to handle big data and perform complex mathematics.
  • Python can be used for rapid prototyping, or for production-ready software development.

Why Python?

  • Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
  • Python has a simple syntax similar to the English language.
  • Python has a syntax that allows developers to write programs with fewer lines than some other programming languages.
  • Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick
  • Python can be treated in a procedural way, an object-orientated way or a functional way.

Why Learn Python?

  • Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently whereas other languages use punctuation, and it has fewer syntactical constructions than other languages.
  • Python is a MUST for students and working professionals to become great Software Engineers especially when they are working in Web Development Domain.

Some of the key Advantages of Learning python:

  • Python is Interpreted – Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP.
  • Python is Interactive – You can actually sit at a Python prompt and interact with the interpreter directly to write your programs.
  • Python is Object-Oriented – Python supports the Object-Oriented style or technique of programming that encapsulates code within objects.
  • Python is a Beginner’s Language – python is a great language for beginner-level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games.

Why Python is Called Python?

When Guido van Rossum began implementing Python, he was also reading the published scripts from “Monty Python’s Flying Circus”, a BBC comedy series from the 1970s. Van Rossum thought he needed a name that was short, unique, and slightly mysterious, so he decided to call the language Python.

Python History and Versions: -

  • Python laid its foundation in the late 1980s.
  • The implementation of python was started in December 1989 by Guido Van Rossum at Centrum Wiskunde & Informatica (CWI) in the Netherlands a successor of a language called ABC.
  • In February 1991, Guido Van Rossum published the code (labeled version 0.9.0) to alt. sources.
  • In 1994, Python 1.0 was released with new features like lambda, map, filter, and reduce.
  • Python 2.0 added new features such as list comprehensions, garbage collection systems.
  • On December 3, 2008, Python 3.0 (also called “Py3K”) was released. It was designed to rectify the fundamental flaw of the language.
  • ABC programming language is said to be the predecessor of python language, which was capable of Exception Handling and interfacing with the Amoeba Operating System.

  Python Version List: -

  • Python 1.0 - January 1994
  • Python 1.5 - December 31, 1997
  • Python 1.6 - September 5, 2000
  • Python 2.0 - October 16, 2000
  • Python 2.1 - April 17, 2001
  • Python 2.2 - December 21, 2001
  • Python 2.3 - July 29, 2003
  • Python 2.4 - November 30, 2004
  • Python 2.5 - September 19, 2006
  • Python 2.6 - October 1, 2008
  • Python 2.7 - July 3, 2010
  • Python 3.0 - December 3, 2008
  • Python 3.1 - June 27, 2009
  • Python 3.2 - February 20, 2011
  • Python 3.3 - September 29, 2012
  • Python 3.4 - March 16, 2014
  • Python 3.5 - September 13, 2015
  • Python 3.6 - December 23, 2016
  • Python 3.7 - June 27, 2018
  • Python 3.8 - October 14, 2019
  • Python 3.9 - October 5, 2020
  • Python 3.10 - October 4, 2021

Python 2 Vs. Python 3

  • In most programming languages, whenever a new version releases, it supports the features and syntax of the existing version of the language, therefore, it is easier for the projects to switch to the newer version. However, in the case of Python, the two versions Python 2 and Python 3 are very much different from each other.
  • Python 2 uses print as a statement and used as print “something” to print some string on the console. On the other hand, python 3 uses print as a function and used as a print(“something”) to print something on the console
  • Python 2 uses the function raw_input() to accept the user’s input. It returns the string representing the value, which is typed by the user. To convert it into the integer, we need to use the int() function in Python. On the other hand, Python 3 uses the input() function which automatically interpreted the type of input entered by the user. However, we can cast this value to any type by using primitive function int(), str(),
  • In Python 2, the implicit string type is ASCII, whereas, in Python 3, the implicit string type is Unicode.
  • Python 3 doesn’t contain the xrange() function of Python 2. The xrange() is the variant of range() function which returns a xrange object that works similar to java iterator. The range() returns a list for example – the function range(0, 3) contains 0, 1, 2.
  • There is also a small change made in Exception handling in python 3. It defines a keyword as which is necessary to be used. We will discuss it in exception handling later.

Characteristics of Python Programming

  • It supports functional and structured programming methods as well as OOP
  • It can be used as a scripting language or can be compiled to byte-code for building large applications.
  • It provides very high-level dynamic data types and supports dynamic type checking.
  • It supports automatic garbage collection.
  • It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.

Python Features: - Python provides many useful features which make it popular and valuable from the other programming languages. It supports OOP, procedural Programming approaches and provides dynamic memory allocation. We have listed below a few essential features.

  • Easy to Learn and Use – Python is easy to learn as compared to other programming languages. Its syntax is straightforward and much the same as the English language. There is no use of the semicolon or curly-bracket, the indentation defines the code block. It is the recommended programming language for beginners.
  • Expressive language – Python can perform complex tasks using a few lines of code. A simple example, the hello world program you simple type print(“Hello World”). It will take only one line to execute, while Java or C takes multiple lines.
  • Interpreted Language – Python is an interpreted language; it means the python program is executed one line at a time. The advantage of being interpreted language, it makes debugging easy and portable.
  • 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.
  • Free and Open Source – Python is freely available for everyone. It is freely available on its official website www.python.org. it has a large community across the world that is dedicatedly working towards making new python modules and functions. Anyone can contribute to the Python community. The open-source means, “Anyone can download its source code without paying any penny.”
  • Object-Oriented Language – Python supports object-oriented language and concepts of classes and objects come into existence. It supports inheritance, polymorphism, and encapsulation, etc. The object-oriented procedure helps programmers to write reusable code and develop applications in less code.
  • 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.
  • Large Standard Library – it provides a vast range of libraries for various fields such as machine learning, web development, and also for 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.
  • GUI Programming Support – Graphical User Interface is used for the developing Desktop application. PyQt5, Tkinter, kivy are the libraries that are used for developing the web application.
  • 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 it easy to debug the code.
  • 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 another language into our code.
  • Dynamic Memory Allocation – In python, we don’t need to specify the data type of the variable. Then we assign some value to the variable, it automatically allocates the memory to the variable at run time. Suppose we are assigned integer value 15 to x, then we don’t need to write int x = 15. Just write x = 15.

Limitations of Python: - According to many surveys, Python is one of the most widely used programming languages of 2015. In addition to being simple and easy to learn, Python enables developers to express concepts without writing longer lines of code. The simple syntax and readable code make it easier for developers to maintain applications written in python. Python also has a robust standard library, which enables web programmers to accomplish common programming tasks like

  • Web service implementation. String operations, operating system interfaces management, and working with internet protocols without writing additional code.
  • They can use specific frameworks to use Python for developing – web applications, desktop GUI applications, and scientific and numeric applications, and cross-platform mobile apps.
  • But python. Like other programming languages, has several shortcomings. It’s essential for programmers to know some of the major limitations of the python programming language.
  • Performance and speed – Many studies have proved that python is slower than other modern programming languages like Java and C++. So the developers have to frequently explore ways to enhance the python application’s speed. However, they have a number of options to make the applications written in python run faster. For instance, the developers can create a custom runtime, and use it instead of the default runtime of the programming language. Likewise, they can rewrite the existing python code to take advantage of the existing execution speed.
  • Incompatibility of Two Versions – Beginners often find it to pick and learn the right version of Python. Officially, python 2.x is described as legacy, whereas Python 3.x is described as current and futuristic. But both versions of the programming language have been updated on a regular basis. Also, a large percentage of programmers still prefer python 2 to python 3. There are also a number of popular frameworks and libraries that support only Python 2.
  • Application portability – python is a high-level programming language. So developers use interpreters to convert python code into code understandable by the operating system. They need to install specific version of python interpreter on the operating system to run the python application on that platform. The shortcoming does not allow programmers to use python for developing cross-platform applications. Also, they could not port the application from one platform to another smoothly.
  • Requires Additional Testing – python is a dynamically typed programming language. It does not require programmers to define the type of a variable while declaring it. The feature makes it easier for programmers to write code freely. But a number of critical bugs or defects emerge at the time of compilation as the variable type are not defined explicitly. So the developers must perform a number of tests additionally to identify and fix the bugs during runtime.
  • Lacks web development capabilities – many programmers prefer using python as a scripting language to build web applications rapidly. But python does not come with built-in web development capabilities. Also, the standard implementation of python does not boost the web applications’’ performance across multiple browsers. That is why, the python developers have to use a number of python web frameworks additionally to effectuate web application development. However, they have option to choose from serveral full-stack web frameworks for python including Django, TurboGear, web2py, Reahland and Zope2.
  • Weak in Mobile Computing – the steady decline in mobile web usage has made it essential for modern businesses to launch mobile apps. Often developers have to write mobile apps in a particular programming language according to the targeted mobile platform. For instance, they need to write iOS app in either Objective-C or Swift. Likewise, mobile apps for Android need to be written in java. But the developers cannot use python directly for developing mobile apps by targeting any popular mobile platforms. They have to use frameworks like Kivy to build cross-platform mobile apps using Python.
  • Depending on Third-Party Frameworks and Libraries – python lacks a number of features provided by other modern programming languages. So the programmers have to use a number of third-party frameworks and tools to build web applications and mobile apps in python. However, they need to use open-source frameworks and libraries to avoid increasing project overheads. The cost factor restricts the developers from availing of the advanced features and functionality provided by commercial frameworks.
  • No Option to Embed Block Comments – Nowadays, developers are required to make the application code readable and maintainable. While writing code, programmers frequently deactivate a specific section or block of code by using block comments. Unlike other modern programming languages, python does not support block comments. Hence, the programmers have to assess the quality of the code either by writing comments for each line of code or removing a specific section of code at the time of execution. The lack of block comments support requires programmers to put additional time and effort to assess the quality of python code.
  • Many Python Modules Lack Adequate Support – Python is supported by a large and active community. The members of the python community regularly share new packages or modules to make it easier for programmers to add functionality to the application. But developers often complain that the quality of individual python modules or packages differs. Some of these packages lack adequate support and are not updated regularly. Hence, the programmers have to do some initial research to pick the right packages or modules.
  • Does not Provide Prebuilt Statistical Models and Tests – Many developers prefer using Python for developing custom statistical and big data applications. But developers need to use additional statistical and data analysis packages to write statistical applications more efficiently. Also, they have to use specific libraries like Pygal, Seaborn, and Bokeh to accomplish data visualization. They cannot make the Python application present and analyze huge volumes of data without using these libraries and tools.

Applications of Python: - Python is known for its general-purpose nature that makes it applicable in almost every domain of software development. Python makes its presence in every emerging field. It is the fastest-growing programming language and can develop any application.

  • Web Application – we can use python to develop web applications. It provides libraries to handle internet protocols such as HTML and XML, JSON, Email processing, requests, beautiful soup, Feedparser, etc. One of the python web-framework named Django is used on Instagram. Python provides many useful frameworks, and these are given below:
    • Django and Pyramid framework (Use for heavy applications)
    • Flask and Bottle (Micro-framework)
    • Plone and Django CMS (Advance Content management)
  • Desktop GUI Applications – The GUI stands for Graphical User Interface, which provides a smooth interaction to any application. Python provides a Tk GUI library to develop a user interface. Some popular GUI libraries are given below.
    • Tkinter or Tk
    • wxWidgetM
    • kivy (used for writing multitouch applications)
    • PyQt or Pyside
  • Console-based Application – Console-based applications run from the command-line or shell. These applications are computer programs which are used commands to execute. This kind of application was more popular in the old generation of computers. Python can develop this kind of application very effectively. It is famous for having REPL, which means the Read-Eval-Print Loop that makes it the most suitable language for command-line applications. Python provides many free library or module which helps to build command-line apps. The necessary IO libraries are used to read and write. It helps to parse arguments and create console help text out-of-the-box. There are also advanced libraries that can develop independent console apps.
  • Software Development – Python is useful for the software development process. It works as a support language and can be used to build control and management, testing, etc.
    • SCons is used to build control.
    • Buildbot and Apache Gumps are used for automated continuous compilation and testing.
    • Round or Trac for bug tracking and project management.
  • Scientific and Numeric – this is the era of Artificial intelligence where the machine can perform the task the same as the human. Python language is the most suitable language for Artificial intelligence or machine learning. It consists of many scientific and mathematical libraries, which makes it easy to solve complex calculations. Implementing machine learning algorithms require complex mathematical calculation. Python has many libraries for scientific and numeric such as Numpy, Pandas, Scipy, Scikit-learn, etc. if you have some basic knowledge of python, you need to import libraries on the top of the code. A few popular frameworks of machine libraries are given below.
    • SciPy
    • Scikit-learn
    • NumPy
    • Pandas
    • Matplotlib
  • Business Applications – Business Applications differ from standard applications. E-Commerce and ERP are an example of business applications. This kind of application requires extensively, scalability and readability, and Python provides all these features. Oddo is an example of the all-in-one Python-based application which offers a range of business applications. Python provides a Tryton platform which is used to develop the business application.
  • Audio or Video-based Applications – Python is flexible to perform multiple tasks and can be used to create multimedia applications. Some multimedia applications which are made by using python are TimePlayer, cplay, etc. A few multimedia libraries are given below.
    • Gstreamer
    • Pyglet
    • QT Phonon
  • 3D CAD Applications – The CAD (Computer-aided design) is used to design engineering-related architecture. It is used to develop the 3D representation of a part of a system. Python can create a 3D CAD application by using the following functionalities.
    • Fandango (Popular)
    • CAMVOX
    • HeeksCNC
    • AnyCAD
    • RCAM
  • Enterprise Applications – Python can be used to create applications that can be used within an Enterprise or an Organization. Some real-time applications are OpenERP, Tryton, Picalo, etc.
  • Image Processing Application – Python contains many libraries that are used to work with the image. The image can be manipulated according to our requirements. Some libraries of image processing are given below.
    • OpenCV
    • Pillow
    • SimpleITK
  • In this topic, we have described all types of applications where python plays an essential role in the development of these applications.

Download & Install Python

    1.    Download Python – to start, go to ww.python.org/downloads (the following page will appear in your browser.) and then click on the button to download the latest version of python.

    2.    The following process will appear in your browser: - ‘python-3.10.0.exe’ will appear.

    3.    Double-click the icon labeling the file ‘python-3.10.0-amd64.exe’ that you just downloaded

    4.    A Security Warning pop-up window will appear. Click Run/yes to continue.

    5.    A python 3.10.0 (64-bit) setup pop-up window will appear.

    6.    Ensure that the Install launcher for all users (recommended) and the Add Python 3.10.0 to PATH checkboxes at the bottom are checked.

       Note: if the Python Installer finds an earlier version of python installed on your computer, the Install Now (and the checkboxes will not appear)

    7.    You can now start the installation of python by clicking on Install Now / Customize Installation

    8.    Click the Yes button. – A new python 3.10.0 (64-bit) Setup pop-up window will appear with a setup progress message and a progress bar.

        Note: After a short period of time, your setup would be completed: - Congrats, you just install python for Windows!

    9.    Click the Close button. – Python should now be installed.

Python – Environment Setup: - Python is available on a wide variety of platforms including Linux and Mac OS X. Let’s understand how to set up our Python environment

Local Environment Setup: - Open a terminal window and type ‘python’ to find out if it is already installed and which version is installed

Setting Path in Python

    1.    Right-click on My Computer and click on properties.

    2.    Click on the Advanced System setting

    3.    Click on Environment Variable tab

    4.    Click on the new tab of user variables.

    5.    Write path in a variable name

    6.    Copy the path of python folder “click on address bar than Right click to copy the path”

    7.    Paste path of Python in variable value

    8.    Click on the OK button

    9.    Click again on the OK button


Python Environment Variable
  • PYTHONPATH – it has a role similar to PATH, this variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.
  • PYTHONSTARTUP – It contains the path of an initialization file containing a Python source. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH
  • PYTHONCASEOK – It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.
  • PYTHONHOME – it is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.

Running Python with the first program: - Interactive Interpreter

  • You can start Python from Unix, DOS, or any other system that provides you a command-line interpreter or shell window.
  • Enter python the command line.
  • Start coding right away in the interactive interpreter

Run a Code in Python

  • You can run code in Python via the Python IDLE.
  • A quick way to find your Python IDLE on windows is by clicking on the Start menu you should then see the IDLE

  • Once you click on the python IDLE, you would then see the shell screen:

  • Click on File and then select New File “alternatively, you may use the keyboard shortcut of Ctrl+N”

  • You would now see the following “untitled” box, where you can type your Python code:

  • For example, type/copy the command below. This command will print the famous expression of “Hello World”
  • This is how the command would look like in the “untitled” box:

  • Press F5 on your keyboard. You will then get the following message to save your code:

  • Choose a location where the Python file will be saved on your computer. You’ll also need to type a name for your file. For example, I named the file as “Test.py”

  • Once you’re done, press Save, and you would then see the “Hello World” expression printed on your Python Shell:

Python Interactive Help Feature

  • Many times we need to look into the python documentation for some help on functions, modules, etc.
  • Python provides a help function that gives us these needed results.
  • Python help() function is used to get help related to the object passed during the call.
  • It takes an optional parameter and returns help information.
  • If no argument is given, it shows the Python help console.
  • It internally calls python’s help function.
  • The help() method calls the built-in Python help system. “The syntax of help() is: help(object)”
  • Try these on python shell.
  • If a string is passed as an argument, the name of a module, function, class, method, keyword, or documentation topic, and a help page are printed.
  • If the string is passed as an argument, the given string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed.

Python Differences from other Languages

  • As you probably know, python is a high-level programming language with multiple paradigm features such as Object-oriented, interpreted, imperative, reflective, procedural, and functional.
  • Apart from these features, it is designed in such a way that the codes written in python are highly readable and take fewer lines for code completion as compared to other programming languages such as Java, C++, Php, etc.
  • This is one of the major reasons behind its heavy usage in the IT sector that including web development, GUI Development, Scientific and Numeric Computing, Software Development, and System Administration.
  • We have compared the codes written in python and programming languages like Php, Java, C++, etc. to get a clear picture of how we can reduce efforts in writing the same codes using Python.
  • Let’s a have look at a simple “Hello World” program written in python and other programming languages.
  • “Hello World” Program in C++
  • “Hello World” program in C
  • “Hello World” program in java
  • “Hello World” program in PHP
  • “Hello World” program in python

Python Vs. Java
  • Program run time
    • Python program runs slower than java
    • Java program runs faster than python
  • Program development time
    • Takes less time than Java
    • Takes more time for developing the same program
  • Length of the code
    • Python code is 3 – 5 times shorter than java code
    • Java code is longer than Python
  • Type
    • Python is dynamically typed
    • Java is not dynamically typed
Python Vs. JavaScript
  • Object-based
    • A python is an Object-based programming language
    • JavaScript is also an object-based scripting language
  • Type
    • python can be used as a scripting language as well as a programming language
    • JavaScript is only used for the scripting language
Python Vs. C++
  • Length of code
    • Python code is 5 – 10 times shorter than C++ code
    • C++ code is 5 – 10 times longer than Python code
  • OOPS
    • Python is a pure OOPS programming language
    • C++ is not a pure OOPS programming language

Comments

Popular posts from this blog

Modules

Control Structures

Classes and Objects