top
upGrad KnowledgeHut SkillFest Sale!

Search

Python Tutorial

Python is a general-purpose high-level language. Well this description applies to almost every other language a computer science student knows. So why has Python’s popularity risen tremendously in last few years? According to TIOBE (which stands for “The Importance Of Being Earnest”) programming community index, Python currently (as on September 2018) ranks third only behind Java and C. (ref: https://www.tiobe.com/tiobe-index/python/) The index is calculated taking various search engine results into account.Primary objective of any computer program is to automate a repetitive task. However, development cycle of some of the popular languages is extremely tedious and slow. This is where Python scores over them. Python is very easy to use. It has a very clean and uncomplicated syntax. Hence it will get the job done for you more quickly than most other languages.S:NoPython Tutorial Chapters - Summary1Python-IntroductionThe first chapter in this tutorial begins by emphasising the main features of Python programming language.  Python is simple to use, interpreter based, cross-platform, dynamically typed and extensible.Some important applications for which Python is used include data science, machine learning, image processing etc. IT giants such as Google and Facebook use Python in their applications and actively support the Python community.This chapter takes a brief look at the history of development of Python. Currently, two versions of Python (Python 2.7 and Python 3.7) are hosted on Python software foundation’s official website, although maintenance and support of Python 2.x is set to be discontinued after 2020.2Python - Getting StartedIt is possible to get acquainted with Python by trying any online interpreters such asPythonAnyWhere, Repl.it ,Trinket etc.To install Python software in your machine, download official distribution suitable for you from https://python.org. Installation instructions for Windows and Linux are explained in this chapter.Other alternate Python distributions are also available such as Anaconda and Canopy. A web based interface to Ipython kernel, called Jupyter Notebook, is extremely popular. It supports dynamic data visualization, and has the capability to export to PDF or HTML format.3Python - Basic SyntaxVarious programming elements such as keywords, functions and classes etc. are suitably named. This chapter explains rules of naming identifiers with suitable examples. Keywords are identifiers with predefined syntax.A statement contains one or more Python identifiers. Python interpreter treats one or more statements with uniform indent as a block. It may be a conditional block, loop, function or class.It is important to intersperse programming logic with comments. Different ways of using comments are explained in this chapter. 4Python - VariablesPython has a very unique data type system. Python’s built-in data types include numbers, sequences and mappings. In Python, a variable is just a label of object stored in computer’s memory. As a result, variable doesn’t need prior declaration. This chapter illustrates concepts of dynamic typing with illustrations.Python differentiates between objects on the basis of mutability. Number, string and tuple data types are immutable, while list and dictionary objects are mutable.This chapter also introduces built in input/output functions of Python. The input() and print() functions with their optional arguments are explained with the help of appropriate examples.5Python - NumbersThis chapter explains the representation of numeric data types – int, float and complex in Python. Literal representation of Hexadecimal and octal integers, and scientific notation of floats is also discussed.Python also has built-in functions for conversion of one numeric object to other. These are int(), float, complex() etc. Python’s library contains useful functions to be used with numbers. The abs() function can be used with integers, floats and complex numbers. Similarly built-in pow() and round() functions have been explained in this chapter with simple examples.6Python - StringsThis chapter takes a detailed look at characteristics of Python’s String data type which has an equivalent literal representation using single, double, triple quotation marks. Characters with special significance may be embedded in string by prefixing with ‘\’ known as escape character. Different escape character sequences are explained.This chapter also explains number of useful methods of built-in String class – such as changing cases, find and replace within string and checking if string is alphanumeric or made up of digits only.String formatting is an important aspect of displaying output. Old style formatting using C like formatting place holders such as %d, %f etc and use of format() function is explained with suitable examples.7Python - List And TupleAlong with String, Python’s built-in container types include List and Tuple. Even though they appear similar, there is a difference of mutability between them. Some functions such as len(), max() etc. can be used with both list and tuple. However, operations such as append(), update() etc. can be performed only on list and not tuple because the latter is immutable.All the sequence objects list, tuple and string are interconvertible. The functions list(), tuple() and str() are illustrated in this chapter.8Python - DictionaryDictionary is a mappings type built-in object. It is a collection of key-value pairs but it doesn’t follow order of index. This chapter describes how dictionary object is represented.A key in dictionary object has to be some immutable object whereas value part can be of any valid Python type.Various operations on dictionary object can be performed such as accessing and updating value of certain key, adding and deleting a k-v pair etc. The operations are performed with the help of methods of built-in dict class.Some methods of dict class return view objects. Keys(), values() and items() methods automatically refresh views of underlying dictionary objects.9Python - SetsPython’s Set data type is implementation of set as in set theory of mathematics. Like list/tuple, it is also a collection of items. However, items must be unique and their position is not defined by index.Set has a literal as well as constructor based representation using built-in set() function. Any iterable object(list, tuple or string) can be cast to set using the set() function.Set cannot contain any mutable object such as list as one of its items. However, set object is mutable hence addition/update/delete operation can be performed. Built-in set class defines methods for the purpose.This chapter explains how various set operations such as union, intersection are performed using respective methods in Set class.10Python - OperatorsOperators are symbols predefined in Python interpreter that perform various operations on Python objects. Well known arithmetic and logical operators as defined in Python are discussed in this chapter.In addition, Python has a large number of operators specifically defined to work with Python data types such as sequences, and mappings. This chapter also explains use of set operators corresponding to methods in Set class.Python also has identity, membership and bitwise operators. All these operators are explained in this chapter with suitable examples. 11Python - Conditional StatementsA program can contain a certain block of statements to be executed only if a certain conditional expression happens to be true. In this chapter we shall learn how to write a Python program that is able to choose different blocks of statements depending upon a conditional expression.Python uses if and else keywords to set up logical conditions. The elif keyword is also explained in this chapter, which is useful especially when there are a number of possibilities in a slabwise computation as explained with example in this chapter.12Python - LoopsThe term ‘loop’ is used to indicate that the program flow is directed towards any of the earlier statements in the sequence of instructions. This chapter describes importance and usage of while and for loops in Python.In Python, for loop is mainly used to traverse collection of items in a sequence and range object. Various examples of traversing string, list and range have been covered in this chapter.  Also explained in this chapter is using for loop to traverse dictionary objects using its view methods. Finally concept of nested loop is also covered in this chapter.It is often needed to control the behaviour of loops. Python’s loop control keywords break and continue have been dealt with in detail in this chapter.13Python - Built-In ModulesIn addition to built-in functions, Python library is bundled with a large number of modules, each containing a number of utility functions.In order to use a function from a module, it must be imported. In this chapter, frequently used functions from various modules are explained with examples.Firstly, directory management functions from OS module are described. Another important module is random module which provides utilities based on pseudo random number technique. Common arithmetic and trigonometric functions from math module are also explained.The sys module (containing system specific attributes and functions) and statistics module (where well known mean, mode and median functions are available) are covered in this chapter. Last but not the least, we take a look at useful functions from collections module that give modified representations of built-in containers like list and tuple.14Python - User Defined FunctionsFunction is an important component of structured development of program’s logic. In this chapter we shall learn to define a new function as per requirement and how to call it.Different features of defining and passing arguments to function are explained with suitable examples here. We shall learn how to make a function return value. We shall also understand local and global scope of variables in a function. Finally calling function by reference is also explained in this chapter.15Python - Functional ProgrammingThis chapter takes an overview of some important tools supporting functional programming paradigm. Lambda and recursive functions are very efficient. We shall also come to know about iterator and generator functions in this chapter.List comprehension technique is an attractive technique of processing iterables. In this chapter, list comprehension and related tools are dealt with in detail. You will also learn to use map, reduce and filter functions.Python library provides functools module that defines a number of functional programming tools.16Python - Custom ModulesJust as built-in modules in the library, programmer can develop his own modules containing one or more user defined functions/classes. In this chapter, we shall learn how to build a module and import functions from it.Each module has important attributes. We shall learn to use them in various programming situations. We also find out how to set the search path and reload a module dynamically.17Python - PackagesA directory/folder containing one or more related modules is called a package. In this chapter, we shall learn to construct a package and import functions from it.Later we shall build a package and install it for system wide use with the help of setuptools module. Process of publishing module on Python Package Index repository is also briefly described in this chapter.18Python - ExceptionsException is a runtime error that a Python interpreter encounters while executing instructions. Python library has a number of predefined Exception classes. In this chapter we learn frequently encountered exception types.It is mandatory to handle exceptions properly. In this chapter Python’s exception handling mechanism is explained using try, except and finally keywords.In addition to predefined exceptions, you can also define customized exception classes and how to raise their instances. Later in the chapter, another important concept of assertions is also dealt with. Python’s assert keyword is used for this purpose.19Python - FileIOComputer disk file stores data in a persistent manner. In this chapter, we will get acquainted with the built-in File object of Python. The open() function returns file object which represents a disk file and can perform read/write operations.Various file opening modes such as append and simultaneous read/write are explained with relevant examples. File object has an in-built iterator using which contents of file can be fetched. File operation may lead to exception. A section in this chapter deals with file related exception handling.We shall also learn to perform read/write operations on a binary file.20Python - CSVComma Separated values (CSV) format is commonly used by spreadsheets and databases. Python’s csv module possesses Reader and Writer classes as well as convenience functions to save Python data to file object in CSV format. This chapter gives account of these functions with examples.The CSV module also has the provision to store dictionary data in csv format including the keys as headers. This is especially useful to exchange data between database tables and files.21Python - Database ConnectivityThere are a number of relational database products in the market today. Python’s DB-API recommends standard set of functionality to be included in database connectivity modules for respective RDBMS products.This chapter starts with introduction to concepts of relational database and explains the basic SQL queries for CRUD operations with the help of SQLite and MySQL database.Python’s standard library include sqlite3 module which is DB-API compliant. This chapter shows how Python program can be used to perform SQL operations on SQLite database.For other types of databases, their respective modules have to be installed. Here, MySQL connectivity is explained by installing PyMySql module.22Python – TkinterMany graphic widget libraries have been ported to Python. Standard library’s bundle includes Tkinter package by default. In this chapter we take a detailed look at Tkinter, Python port to Tk toolkit from TCL.Starting with how to form a basic window, we shall learn to put various widgets like button, label, entry etc. In this chapter their layout management is also covered with place(), pack() and grid() methods.Event handling mechanism is also explained in this chapter with elaborate examples. There is a brief discussion on menu formation. Lastly we will learn to draw different shapes using Canvas widget.23Python - Object Oriented ProgrammingThis chapter opens with differentiating procedure oriented approach towards programming from object oriented approach. Everything in Python is an object. All built-in classes are derived from Object class. We shall learn how to build a user defined class and set up its objects.Later in this chapter, concept of constructor, instance attributes and methods is explained. Python has different philosophy towards data encapsulation. We shall see how Python mildly enforces access restriction by name mangling.Even though class may have getter/setters, Python’s built-in property() function is explained with examples to understand interface to instance variables defined in a class.24Python - DecoratorsDecorator in Python is a function whose argument is a function itself. This is because function is a first order object. This chapter shows how we can have a function with another function as argument and return type also as function.Decorator helps in extending behaviour of argument function without actually modifying it. This concept is further applied to built-in property() function and use it as @property decorator.Python class can have class and static attributes and methods. They are extended by @classmethod and @staticmethod built-in decorators.25Python - InheritanceIn this chapter, we take a look at Python’s implementation of principle of inheritance which is an important cornerstone of object oriented programming methodology.The principle of overriding is explained with the help of triangle and equilateral triangle classes. Python also supports multiple inheritance. However, Python doesn’t support ‘protected’ behaviour of instance attributes of parent class. Instead attributes with single underscore prefix emulate behaviour of protected access.26Python - Magic MethodsEvery Python class is derived from object class. This base class has a number of abstract methods having double underscore character as prefix and suffix. These methods are called magic methods. Other classes override these methods.In this chapter, magic methods used for object customization are discussed. They are __new__(), __repr__() and __str__(). Operator overloading technique is implemented by overriding respective magic method. For example, ‘+’ operator is overloaded by providing implementation of __add__() method. This chapter uses MyTime class to demonstrate operator overloading.27Python - RegexRegular Expressions is in important tool for searching, lexical analysis and input validation. This chapter explains functions defined in regex module of Python’s standard library.Raw strings are used by regex functions for searching and pattern matching. The module defines number of escape sequence patterns to be used. This chapter demonstrates searching for, matching, finding and compiling regex patterns.In the end typical use cases of regex such as matching domain names are explained.28Python - CGIThis chapter provides discussion of important features of Python’s implementation of CGI protocol. CGI enabled web servers such as Apache can serve output of Python scripts to client browsers. First of all this chapter describes how Apache server is configured for running *.py files as CGI script. Python library contains cgi and cgitb modules for the purpose. The web server receives data from client in the form of http get or post request and is populated as FieldStorage object.In this chapter, various attributes and methods of FieldStorage class have been used to demonstrate how data is sent from web client and result is redirected from server.29Python - Send EmailPython’s standard library is bundled with modules useful for handling various email protocols such as POP3 and IMAP. This chapter deals with SMTP protocol with the help of smtplib module.A suitable example is used to shows how we can use gmail – a very popular email service by configuring its smtp server to send email from a Python script.30Python - Object SerializationObject serialization is the process of converting state of an object into byte stream. It can be saved to a file object or can be transmitted to network stream. Deserialization means reconstruction of object from the stream.Python’s standard library has various modules for performing this process. In this chapter, we discuss pickle module which is Python’s own serialization format. Serialization functionality of marshal and shelve module is also explained. Finally, we shall learn how Python objects are represented in JSON (Java Object Notation) format with the help of json module.31Python - MultithreadingPython extends support for multithreaded applications through ‘threading’ module included in the standard distribution. In this chapter, we briefly explain principles of concurrent threads in a program. The threading module defines functions to initialize, start run and terminate a thread. We shall also see how a thread can be paused and resumed.This chapter takes a look at the concept of thread synchronization. The threading module provides lock object for acquiring exclusive control of a process and then releasing it for consumption by other threads.32Python - XMLXML (Extendible markup language) is a popular data exchange format. Python library ships with xml package. It supports xml processing using ElementTree, DOM and SAX APIs.In this chapter, the implementation of ElementTree API model is covered. With the help of a suitable example, we shall see how Python objects can be serialized in XML format and reconstructed back.33Python - Socket ModuleThe socket module defines functions for controlling communication between server client at hardware level using socket endpoints. This chapter takes a look at the functionality of 'socket' module that provides access to BSD socket interface.Two types of sockets are generally used. They are TCP and UDP sockets. This chapter focuses mainly on connection oriented TCP sockets. We shall learn how to set up a socket server and a client application.34Python - Data ClassesDataclasses module is latest addition to Python’s standard library. The @dataclass decorator helps in autogeneration of constructor and operator overloading methods in a class.Main features:Simplicity: ‘Simple is better than complex’. This is one of the guiding principles behind design philosophy of Python. The collection of software principles that influence the design philosophy of Python is illustrated by ‘Zen of Python’. They can be viewed by entering “import this” from Python prompt >>> import thisIn C/C++/Java etc. writing a simple ‘Hello World’ program can be very daunting for the beginner because of rather too elaborate syntax, clumsy use of curly brackets and the fact that the code should be compiled before execution. In contrast, Python instructions can be executed straight away, from the Python shell itself.Free and open source: Python software can be freely downloaded from its official website www.python.org. Its source code is also available; hence a large number of developers are actively involved in development and support of Python libraries for various applications. Many alternative distributions such as Anaconda and implementations such as Jython have come up as a result.Interpreter based: Python instructions are executed even from Python prompt just as OS commands. This helps in rapid prototyping of applications. Python scripts are not compiled. As a result, execution of Python program is slower than C/C++/Java programs. However it is possible to build a self executable from Python script using tools such as PyInstaller.Cross-platform: Python can be installed on all prominent operating system platforms such as Windows, Linux, MacOS etc. Python program developed on one OS environment runs seamlessly on others.Multi-paradigm: Python supports procedural, object oriented as well as functional programming styles.Compact: More often than not, Python code is smaller size as compared to equivalent code (performing same process) in C/C++/Java. This is mainly because of functional programming features such as list comprehension, map and reduce functions etc.Extensible: Standard distribution of Python is bundled with several built-in modules having functions ranging from statistics, regex, socket handling, serialization etc. However, customised modules can be added in the library. These modules may be written in C/C++/Java or Python.Dynamically typed: Python program doesn’t need prior declaration of name and type of variable, as is the case in C/C++/Java etc.Where is Python used?As mentioned before, Python is a general purpose language. Moreover, its extensibility makes it possible to use it for all kinds of applications. Some key application areas are listed below:Data science: The surge in popularity of Python in the recent past is primarily because of its application in data science. Although data science is a very broad term, it basically means analysis and visualization of large amount of data that is nowadays generated by widespread use of web and mobile applications as well as embedded products.Special-purpose libraries like Numpy, Pandas and Matplotlib etc. have become important tools for data scientists. Commercial as well as community distributions of Python in the form of Anaconda, Canopy and Activestate bundle all required libraries for numerical and scientific computing.Machine learning: This is another area in which Python is becoming popular. Special purpose Python libraries such as Scikit-Learn and TensorFlow have capability to predict trends based on past data regarding customer behaviour, stock market, electioneering etc.Image processing: Face detection and gesture recognition using opencv library and Python is another important application. Opencv is a c++ library, but has been ported to Python. This library has lot of extremely powerful image processing functions.GUI interface: Almost all popular GUI toolkits have their Python port. Standard distribution of Python is bundled with Tkinter. Qt library has been ported to python in the form of PyQt. So is GTK, OpenGL and others. Using these toolkits, attractive GUI interfaces can be designed.Data based applications: Almost all RDBMS products e.g. Oracle, MySQL, SQL Server etc. can be used as a back and for Python program. Python library has in-built support for SQLite. Python recommends a uniform DB-API standard based on which database interfacing libraries have been developed for each database type.Embedded systems and IoT: Another important area of Python application is in embedded systems. Raspberry Pi and Arduino based automation products, robotics, IoT, and kiosk applications are programmed in Python.Automation scripts: Python is a popular choice for writing scripts for scheduled jobs to be performed by servers. Python is also embedded in software products like Maya and Paintshop Pro to define macros.Web applications: Python based toolkits for rapid web application development are very popular. Examples are Django, Flask, Pyramid etc. These applications are hosted on WSGI compliant web servers such as Apache, IIS etc.Who uses Python?Many software giants have adopted Python as their main development platform. Some prominent ones are as follows:Google uses Python extensively in many of its applications. Its support is one of the main reasons behind the increasing popularity of Python. Google has contributed immensely by preparing documentations and other resources on Python. Google App Engine is a popular choice for hosting Python based web applications.Facebook, the social media company uses Python particularly in Facebook Ads API, infrastructure management and binary distribution etc.Instagram, a photo and video-sharing social networking platform also uses django framework which is written entirely in Python.Quora, a popular question and answer forum also uses Python frameworks such as django and Pylons.Dropbox, a cloud based storage system uses Python in its desktop client module. Guido Van Rossum, the man who is Python’s principle developer has been working with Dropbox for last few years.Youtube, American video-sharing company now a part of Google conglomerate relies substantially on Python for its functionality.HistoryAlthough Python is extremely popular amongst the modern generation of software professionals, it has a history of more than three decades. Guido Van Rossum, a Dutch programmer conceived and developed this language and published its early version in 1991. Python can be thought as a successor of another language named ABC. According to Rossum, he named his language after a popular BBC TV series titled ‘Monty Python’s Flying Circus’.Python is an open source language. It is released under GPL compatible PSF licence. Python Software Foundation (PSF) owns the copyright of Python.In October 2000, Python’s 2.0 version was released. It contained many features of functional programming paradigm. Since then, successive upgradations of Python 2.x have been published. Most current version in this sequence is Python 2.7.15.  PSF has announced that development, maintenance and support of Python 2.x will be discontinued after 2020 and users are encouraged to adapt Python 3 as early as possible.Python 3.0 was introduced in December 2008. This version was meant to be backward incompatible. However, many of its features have since been back-ported to Python 2.6.x and Python 2.7.x series. Python 3 comes with 2to3 utility to help Python 2.x users covert their code. Current version in this branch is Python 3.7.2
logo

Python Tutorial

Python - Introduction

Python is a general-purpose high-level language. Well this description applies to almost every other language a computer science student knows. So why has Python’s popularity risen tremendously in last few years? According to TIOBE (which stands for “The Importance Of Being Earnest”) programming community index, Python currently (as on September 2018) ranks third only behind Java and C. (ref: https://www.tiobe.com/tiobe-index/python/) The index is calculated taking various search engine results into account.

Primary objective of any computer program is to automate a repetitive task. However, development cycle of some of the popular languages is extremely tedious and slow. This is where Python scores over them. Python is very easy to use. It has a very clean and uncomplicated syntax. Hence it will get the job done for you more quickly than most other languages.

S:No
Python Tutorial Chapters - Summary
1

Python-Introduction

The first chapter in this tutorial begins by emphasising the main features of Python programming language.  Python is simple to use, interpreter based, cross-platform, dynamically typed and extensible.

Some important applications for which Python is used include data science, machine learning, image processing etc. IT giants such as Google and Facebook use Python in their applications and actively support the Python community.

This chapter takes a brief look at the history of development of Python. Currently, two versions of Python (Python 2.7 and Python 3.7) are hosted on Python software foundation’s official website, although maintenance and support of Python 2.x is set to be discontinued after 2020.


2

Python - Getting Started

It is possible to get acquainted with Python by trying any online interpreters such asPythonAnyWhere, Repl.it ,Trinket etc.

To install Python software in your machine, download official distribution suitable for you from https://python.org. Installation instructions for Windows and Linux are explained in this chapter.

Other alternate Python distributions are also available such as Anaconda and Canopy. A web based interface to Ipython kernel, called Jupyter Notebook, is extremely popular. It supports dynamic data visualization, and has the capability to export to PDF or HTML format.


3

Python - Basic Syntax

Various programming elements such as keywords, functions and classes etc. are suitably named. This chapter explains rules of naming identifiers with suitable examples. Keywords are identifiers with predefined syntax.

A statement contains one or more Python identifiers. Python interpreter treats one or more statements with uniform indent as a block. It may be a conditional block, loop, function or class.

It is important to intersperse programming logic with comments. Different ways of using comments are explained in this chapter. 

4

Python - Variables

Python has a very unique data type system. Python’s built-in data types include numbers, sequences and mappings. 

In Python, a variable is just a label of object stored in computer’s memory. As a result, variable doesn’t need prior declaration. This chapter illustrates concepts of dynamic typing with illustrations.

Python differentiates between objects on the basis of mutability. Number, string and tuple data types are immutable, while list and dictionary objects are mutable.

This chapter also introduces built in input/output functions of Python. The input() and print() functions with their optional arguments are explained with the help of appropriate examples.

5

Python - Numbers

This chapter explains the representation of numeric data types – int, float and complex in Python. Literal representation of Hexadecimal and octal integers, and scientific notation of floats is also discussed.

Python also has built-in functions for conversion of one numeric object to other. These are int(), float, complex() etc. 

Python’s library contains useful functions to be used with numbers. The abs() function can be used with integers, floats and complex numbers. Similarly built-in pow() and round() functions have been explained in this chapter with simple examples.

6

Python - Strings

This chapter takes a detailed look at characteristics of Python’s String data type which has an equivalent literal representation using single, double, triple quotation marks. Characters with special significance may be embedded in string by prefixing with ‘\’ known as escape character. Different escape character sequences are explained.

This chapter also explains number of useful methods of built-in String class – such as changing cases, find and replace within string and checking if string is alphanumeric or made up of digits only.

String formatting is an important aspect of displaying output. Old style formatting using C like formatting place holders such as %d, %f etc and use of format() function is explained with suitable examples.

7

Python - List And Tuple

Along with String, Python’s built-in container types include List and Tuple. Even though they appear similar, there is a difference of mutability between them. 

Some functions such as len(), max() etc. can be used with both list and tuple. However, operations such as append(), update() etc. can be performed only on list and not tuple because the latter is immutable.

All the sequence objects list, tuple and string are interconvertible. The functions list(), tuple() and str() are illustrated in this chapter.

8

Python - Dictionary

Dictionary is a mappings type built-in object. It is a collection of key-value pairs but it doesn’t follow order of index. This chapter describes how dictionary object is represented.

A key in dictionary object has to be some immutable object whereas value part can be of any valid Python type.

Various operations on dictionary object can be performed such as accessing and updating value of certain key, adding and deleting a k-v pair etc. The operations are performed with the help of methods of built-in dict class.

Some methods of dict class return view objects. Keys(), values() and items() methods automatically refresh views of underlying dictionary objects.

9

Python - Sets

Python’s Set data type is implementation of set as in set theory of mathematics. Like list/tuple, it is also a collection of items. However, items must be unique and their position is not defined by index.

Set has a literal as well as constructor based representation using built-in set() function. Any iterable object(list, tuple or string) can be cast to set using the set() function.

Set cannot contain any mutable object such as list as one of its items. However, set object is mutable hence addition/update/delete operation can be performed. Built-in set class defines methods for the purpose.

This chapter explains how various set operations such as union, intersection are performed using respective methods in Set class.

10

Python - Operators

Operators are symbols predefined in Python interpreter that perform various operations on Python objects. Well known arithmetic and logical operators as defined in Python are discussed in this chapter.

In addition, Python has a large number of operators specifically defined to work with Python data types such as sequences, and mappings. This chapter also explains use of set operators corresponding to methods in Set class.

Python also has identity, membership and bitwise operators. All these operators are explained in this chapter with suitable examples. 

11

Python - Conditional Statements

A program can contain a certain block of statements to be executed only if a certain conditional expression happens to be true. In this chapter we shall learn how to write a Python program that is able to choose different blocks of statements depending upon a conditional expression.

Python uses if and else keywords to set up logical conditions. The elif keyword is also explained in this chapter, which is useful especially when there are a number of possibilities in a slabwise computation as explained with example in this chapter.

12

Python - Loops

The term ‘loop’ is used to indicate that the program flow is directed towards any of the earlier statements in the sequence of instructions. This chapter describes importance and usage of while and for loops in Python.

In Python, for loop is mainly used to traverse collection of items in a sequence and range object. Various examples of traversing string, list and range have been covered in this chapter.  Also explained in this chapter is using for loop to traverse dictionary objects using its view methods. Finally concept of nested loop is also covered in this chapter.

It is often needed to control the behaviour of loops. Python’s loop control keywords break and continue have been dealt with in detail in this chapter.

13

Python - Built-In Modules

In addition to built-in functions, Python library is bundled with a large number of modules, each containing a number of utility functions.

In order to use a function from a module, it must be imported. In this chapter, frequently used functions from various modules are explained with examples.

Firstly, directory management functions from OS module are described. Another important module is random module which provides utilities based on pseudo random number technique. Common arithmetic and trigonometric functions from math module are also explained.

The sys module (containing system specific attributes and functions) and statistics module (where well known mean, mode and median functions are available) are covered in this chapter. Last but not the least, we take a look at useful functions from collections module that give modified representations of built-in containers like list and tuple.

14

Python - User Defined Functions

Function is an important component of structured development of program’s logic. In this chapter we shall learn to define a new function as per requirement and how to call it.

Different features of defining and passing arguments to function are explained with suitable examples here. We shall learn how to make a function return value. 

We shall also understand local and global scope of variables in a function. Finally calling function by reference is also explained in this chapter.

15

Python - Functional Programming

This chapter takes an overview of some important tools supporting functional programming paradigm. Lambda and recursive functions are very efficient. We shall also come to know about iterator and generator functions in this chapter.

List comprehension technique is an attractive technique of processing iterables. In this chapter, list comprehension and related tools are dealt with in detail. You will also learn to use map, reduce and filter functions.

Python library provides functools module that defines a number of functional programming tools.

16

Python - Custom Modules

Just as built-in modules in the library, programmer can develop his own modules containing one or more user defined functions/classes. In this chapter, we shall learn how to build a module and import functions from it.

Each module has important attributes. We shall learn to use them in various programming situations. We also find out how to set the search path and reload a module dynamically.

17

Python - Packages

A directory/folder containing one or more related modules is called a package. In this chapter, we shall learn to construct a package and import functions from it.

Later we shall build a package and install it for system wide use with the help of setuptools module. Process of publishing module on Python Package Index repository is also briefly described in this chapter.

18

Python - Exceptions

Exception is a runtime error that a Python interpreter encounters while executing instructions. Python library has a number of predefined Exception classes. In this chapter we learn frequently encountered exception types.

It is mandatory to handle exceptions properly. In this chapter Python’s exception handling mechanism is explained using try, except and finally keywords.

In addition to predefined exceptions, you can also define customized exception classes and how to raise their instances. Later in the chapter, another important concept of assertions is also dealt with. Python’s assert keyword is used for this purpose.

19

Python - FileIO

Computer disk file stores data in a persistent manner. In this chapter, we will get acquainted with the built-in File object of Python. The open() function returns file object which represents a disk file and can perform read/write operations.

Various file opening modes such as append and simultaneous read/write are explained with relevant examples. File object has an in-built iterator using which contents of file can be fetched. File operation may lead to exception. A section in this chapter deals with file related exception handling.

We shall also learn to perform read/write operations on a binary file.

20

Python - CSV

Comma Separated values (CSV) format is commonly used by spreadsheets and databases. Python’s csv module possesses Reader and Writer classes as well as convenience functions to save Python data to file object in CSV format. This chapter gives account of these functions with examples.

The CSV module also has the provision to store dictionary data in csv format including the keys as headers. This is especially useful to exchange data between database tables and files.

21

Python - Database Connectivity

There are a number of relational database products in the market today. Python’s DB-API recommends standard set of functionality to be included in database connectivity modules for respective RDBMS products.

This chapter starts with introduction to concepts of relational database and explains the basic SQL queries for CRUD operations with the help of SQLite and MySQL database.

Python’s standard library include sqlite3 module which is DB-API compliant. This chapter shows how Python program can be used to perform SQL operations on SQLite database.

For other types of databases, their respective modules have to be installed. Here, MySQL connectivity is explained by installing PyMySql module.

22

Python – Tkinter

Many graphic widget libraries have been ported to Python. Standard library’s bundle includes Tkinter package by default. In this chapter we take a detailed look at Tkinter, Python port to Tk toolkit from TCL.

Starting with how to form a basic window, we shall learn to put various widgets like button, label, entry etc. In this chapter their layout management is also covered with place(), pack() and grid() methods.

Event handling mechanism is also explained in this chapter with elaborate examples. There is a brief discussion on menu formation. Lastly we will learn to draw different shapes using Canvas widget.

23

Python - Object Oriented Programming

This chapter opens with differentiating procedure oriented approach towards programming from object oriented approach. Everything in Python is an object. All built-in classes are derived from Object class. We shall learn how to build a user defined class and set up its objects.

Later in this chapter, concept of constructor, instance attributes and methods is explained. Python has different philosophy towards data encapsulation. We shall see how Python mildly enforces access restriction by name mangling.

Even though class may have getter/setters, Python’s built-in property() function is explained with examples to understand interface to instance variables defined in a class.

24

Python - Decorators

Decorator in Python is a function whose argument is a function itself. This is because function is a first order object. This chapter shows how we can have a function with another function as argument and return type also as function.

Decorator helps in extending behaviour of argument function without actually modifying it. This concept is further applied to built-in property() function and use it as @property decorator.

Python class can have class and static attributes and methods. They are extended by @classmethod and @staticmethod built-in decorators.

25

Python - Inheritance

In this chapter, we take a look at Python’s implementation of principle of inheritance which is an important cornerstone of object oriented programming methodology.

The principle of overriding is explained with the help of triangle and equilateral triangle classes. Python also supports multiple inheritance. However, Python doesn’t support ‘protected’ behaviour of instance attributes of parent class. Instead attributes with single underscore prefix emulate behaviour of protected access.

26

Python - Magic Methods

Every Python class is derived from object class. This base class has a number of abstract methods having double underscore character as prefix and suffix. These methods are called magic methods. Other classes override these methods.

In this chapter, magic methods used for object customization are discussed. They are __new__(), __repr__() and __str__(). 

Operator overloading technique is implemented by overriding respective magic method. For example, ‘+’ operator is overloaded by providing implementation of __add__() method. This chapter uses MyTime class to demonstrate operator overloading.

27

Python - Regex

Regular Expressions is in important tool for searching, lexical analysis and input validation. This chapter explains functions defined in regex module of Python’s standard library.

Raw strings are used by regex functions for searching and pattern matching. The module defines number of escape sequence patterns to be used. This chapter demonstrates searching for, matching, finding and compiling regex patterns.

In the end typical use cases of regex such as matching domain names are explained.

28

Python - CGI

This chapter provides discussion of important features of Python’s implementation of CGI protocol. CGI enabled web servers such as Apache can serve output of Python scripts to client browsers. 

First of all this chapter describes how Apache server is configured for running *.py files as CGI script. Python library contains cgi and cgitb modules for the purpose. The web server receives data from client in the form of http get or post request and is populated as FieldStorage object.

In this chapter, various attributes and methods of FieldStorage class have been used to demonstrate how data is sent from web client and result is redirected from server.

29

Python - Send Email

Python’s standard library is bundled with modules useful for handling various email protocols such as POP3 and IMAP. This chapter deals with SMTP protocol with the help of smtplib module.

A suitable example is used to shows how we can use gmail – a very popular email service by configuring its smtp server to send email from a Python script.

30

Python - Object Serialization

Object serialization is the process of converting state of an object into byte stream. It can be saved to a file object or can be transmitted to network stream. Deserialization means reconstruction of object from the stream.

Python’s standard library has various modules for performing this process. In this chapter, we discuss pickle module which is Python’s own serialization format. Serialization functionality of marshal and shelve module is also explained. 

Finally, we shall learn how Python objects are represented in JSON (Java Object Notation) format with the help of json module.

31

Python - Multithreading

Python extends support for multithreaded applications through ‘threading’ module included in the standard distribution. 

In this chapter, we briefly explain principles of concurrent threads in a program. The threading module defines functions to initialize, start run and terminate a thread. We shall also see how a thread can be paused and resumed.

This chapter takes a look at the concept of thread synchronization. The threading module provides lock object for acquiring exclusive control of a process and then releasing it for consumption by other threads.

32

Python - XML

XML (Extendible markup language) is a popular data exchange format. Python library ships with xml package. It supports xml processing using ElementTree, DOM and SAX APIs.

In this chapter, the implementation of ElementTree API model is covered. With the help of a suitable example, we shall see how Python objects can be serialized in XML format and reconstructed back.

33

Python - Socket Module

The socket module defines functions for controlling communication between server client at hardware level using socket endpoints. This chapter takes a look at the functionality of 'socket' module that provides access to BSD socket interface.

Two types of sockets are generally used. They are TCP and UDP sockets. This chapter focuses mainly on connection oriented TCP sockets. We shall learn how to set up a socket server and a client application.

34

Python - Data Classes

Dataclasses module is latest addition to Python’s standard library. The @dataclass decorator helps in autogeneration of constructor and operator overloading methods in a class.

Main features:

  • Simplicity: ‘Simple is better than complex’. This is one of the guiding principles behind design philosophy of Python. The collection of software principles that influence the design philosophy of Python is illustrated by ‘Zen of Python’. They can be viewed by entering “import this” from Python prompt >>> import this

In C/C++/Java etc. writing a simple ‘Hello World’ program can be very daunting for the beginner because of rather too elaborate syntax, clumsy use of curly brackets and the fact that the code should be compiled before execution. In contrast, Python instructions can be executed straight away, from the Python shell itself.

  • Free and open source: Python software can be freely downloaded from its official website www.python.org. Its source code is also available; hence a large number of developers are actively involved in development and support of Python libraries for various applications. Many alternative distributions such as Anaconda and implementations such as Jython have come up as a result.
  • Interpreter based: Python instructions are executed even from Python prompt just as OS commands. This helps in rapid prototyping of applications. Python scripts are not compiled. As a result, execution of Python program is slower than C/C++/Java programs. However it is possible to build a self executable from Python script using tools such as PyInstaller.
  • Cross-platform: Python can be installed on all prominent operating system platforms such as Windows, Linux, MacOS etc. Python program developed on one OS environment runs seamlessly on others.
  • Multi-paradigm: Python supports procedural, object oriented as well as functional programming styles.
  • Compact: More often than not, Python code is smaller size as compared to equivalent code (performing same process) in C/C++/Java. This is mainly because of functional programming features such as list comprehension, map and reduce functions etc.
  • Extensible: Standard distribution of Python is bundled with several built-in modules having functions ranging from statistics, regex, socket handling, serialization etc. However, customised modules can be added in the library. These modules may be written in C/C++/Java or Python.
  • Dynamically typed: Python program doesn’t need prior declaration of name and type of variable, as is the case in C/C++/Java etc.

Where is Python used?

As mentioned before, Python is a general purpose language. Moreover, its extensibility makes it possible to use it for all kinds of applications. Some key application areas are listed below:

  • Data science: The surge in popularity of Python in the recent past is primarily because of its application in data science. Although data science is a very broad term, it basically means analysis and visualization of large amount of data that is nowadays generated by widespread use of web and mobile applications as well as embedded products.

Special-purpose libraries like Numpy, Pandas and Matplotlib etc. have become important tools for data scientists. Commercial as well as community distributions of Python in the form of Anaconda, Canopy and Activestate bundle all required libraries for numerical and scientific computing.

  • Machine learning: This is another area in which Python is becoming popular. Special purpose Python libraries such as Scikit-Learn and TensorFlow have capability to predict trends based on past data regarding customer behaviour, stock market, electioneering etc.
  • Image processing: Face detection and gesture recognition using opencv library and Python is another important application. Opencv is a c++ library, but has been ported to Python. This library has lot of extremely powerful image processing functions.
  • GUI interface: Almost all popular GUI toolkits have their Python port. Standard distribution of Python is bundled with Tkinter. Qt library has been ported to python in the form of PyQt. So is GTK, OpenGL and others. Using these toolkits, attractive GUI interfaces can be designed.
  • Data based applications: Almost all RDBMS products e.g. Oracle, MySQL, SQL Server etc. can be used as a back and for Python program. Python library has in-built support for SQLite. Python recommends a uniform DB-API standard based on which database interfacing libraries have been developed for each database type.
  • Embedded systems and IoT: Another important area of Python application is in embedded systems. Raspberry Pi and Arduino based automation products, robotics, IoT, and kiosk applications are programmed in Python.
  • Automation scripts: Python is a popular choice for writing scripts for scheduled jobs to be performed by servers. Python is also embedded in software products like Maya and Paintshop Pro to define macros.
  • Web applications: Python based toolkits for rapid web application development are very popular. Examples are Django, Flask, Pyramid etc. These applications are hosted on WSGI compliant web servers such as Apache, IIS etc.

Who uses Python?

Many software giants have adopted Python as their main development platform. Some prominent ones are as follows:

Google uses Python extensively in many of its applications. Its support is one of the main reasons behind the increasing popularity of Python. Google has contributed immensely by preparing documentations and other resources on Python. Google App Engine is a popular choice for hosting Python based web applications.

Facebook, the social media company uses Python particularly in Facebook Ads API, infrastructure management and binary distribution etc.

Instagram, a photo and video-sharing social networking platform also uses django framework which is written entirely in Python.

Quora, a popular question and answer forum also uses Python frameworks such as django and Pylons.

Dropbox, a cloud based storage system uses Python in its desktop client module. Guido Van Rossum, the man who is Python’s principle developer has been working with Dropbox for last few years.

Youtube, American video-sharing company now a part of Google conglomerate relies substantially on Python for its functionality.

History

Although Python is extremely popular amongst the modern generation of software professionals, it has a history of more than three decades. Guido Van Rossum, a Dutch programmer conceived and developed this language and published its early version in 1991. Python can be thought as a successor of another language named ABC. According to Rossum, he named his language after a popular BBC TV series titled ‘Monty Python’s Flying Circus.

Python is an open source language. It is released under GPL compatible PSF licence. Python Software Foundation (PSF) owns the copyright of Python.

In October 2000, Python’s 2.0 version was released. It contained many features of functional programming paradigm. Since then, successive upgradations of Python 2.x have been published. Most current version in this sequence is Python 2.7.15.  PSF has announced that development, maintenance and support of Python 2.x will be discontinued after 2020 and users are encouraged to adapt Python 3 as early as possible.

Python 3.0 was introduced in December 2008. This version was meant to be backward incompatible. However, many of its features have since been back-ported to Python 2.6.x and Python 2.7.x series. Python 3 comes with 2to3 utility to help Python 2.x users covert their code. Current version in this branch is Python 3.7.2

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Eula

This is my first time here. I am truly impressed to read all this in one place.

Jaypee Dela Cruz

Thank you for your wonderful codes and website, you helped me a lot especially in this socket module. Thank you again!

lucky verma

Thank you for taking the time to share your knowledge about using python to find the path! Your insight and guidance is greatly appreciated.

Pre Engineered Metal Building

Usually I by no means touch upon blogs however your article is so convincing that I by no means prevent myself to mention it here.

Pre Engineered Metal Building

Usually, I never touch upon blogs; however, your article is so convincing that I could not prevent myself from mentioning how nice it is written.

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses the .Net Framework. It utilizes the Common Language Interface (CLI) that describes the executable code as well as the runtime environment. C# can be used for various applications such as web applications, distributed applications, database applications, window applications etc.For greater understanding of this tutorial, a basic knowledge of object-oriented languages such as C++, Java etc. would be beneficial.
C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses ...

Read More