bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Modules, Regular Expressions and Frameworks:

In this tutorial, we will be discussing Python Modules and Python Regular Expressions. This part of the Python tutorial highlights the following content:

What is a Python module?

Before we start the discussion about Python modules, let me give you one practical example which will help you understand the use of modules in Python.
Python Modules example

Say, Leon, a software developer was given a task to develop one software. Leon worked very hard for 4 days on that project. And finally completing he realized there are some issues with the coding. So now, he needs to do debugging.

benefits of using Python Modules example 2
Since he wrote the whole code in one file, he is now stressed out with the fact that the code file has become very big and making changes in that will mess up everything.
benefits of using Python Modules example 1
To avoid this kind of scenarios, Leon decided to divide the project into different parts based on some features or components. Then he finished debugging without messing up rest of the codes.

Learn Python in 16 hrs from experts



Similarly, in Python, Modules are used to divide the code into the smaller parts. In this, you can group similar data which makes the program easier to understand the code. The module is a file which contains function, variables etc. Modules are processed with two new statements and one important built-in function which are:

  • import – Lets a client obtain a module as a whole.
  • from – Permits clients to fetch particular names from a module
  • reload – Gives a way to reload a code of module without stopping Python

Why do we use modules in Python?

 

  • Code reusability
  • System namespace partitioning
  • Implementing shared services or data

Importing a Module:

To import a module, import keyword is used.

Syntax

import <file_name1, file_name2….file_namen

e.g.

def intellipaat():
print “Hello intellipaat”
save this file as hello.py
import hello
hello.intellipaat()Output
Hello intellipaat en>
  • form…import is used to import particular attribute from a module.

Syntax

from  module_name import atr1,atr2,…atrn

  • To import whole module use following syntax

Syntax

from module_name import *

Built-in Modules

 

Function Description
ceil(n) Returns the next integer number of the given number
sqrt(n) Returns the Square root of the given number.
exp(n) Returns the natural logarithm e raised to the given number
floor(n) Returns the previous integer number of the given number.
log(n,baseto) Returns the natural logarithm of the number.
pow(baseto, exp) Returns base to raised to the exp power.
sin(n) Returns sine of the given radian.
cos(n) Returns cosine of the given radian.
tan(n) Returns tangent of the given radian.

Now that we have learnt about the modules in Python, let us discuss some Python regular expressions as well.

Regular Expressions:

Regular expressions are patterns that permit you to “match” various string values in a variety of ways. The module re provides regular expressions in Python.
A pattern is simply one or more characters that represent a set of possible match characters. In regular expression matching, you use a character (or set of characters) to represent the strings you want to match in the text.

Table – Regular Expression Characters In Python

 

Symbol  Meaning
. (period) Matches any character except the newline character.
^ (caret sign) Matches the start of any string.
$ (dollar sign) Matches the end of any string.
* (asterisk) Matches zero or more repetitions of a given regular expression.
? Matches zero or one of the previous regular expressions.
{} Used as either {m} where m means to match exactly “m” instances of the previous regular expression or {m,n} where n > m, meaning to match between m and n instances of the previous regular expression.
\ (backslash) Either a special character, such as one of the other regular expression characters(i.e., \* matches an asterisk) or one of the special regular expression sequences

Wish to Learn Python? Click Here

The match Function

It matches RE pattern to string with optional flags.

Syntax

re.match(pattern, string, flags=0)

Where pattern is a regular expression to be matched, 2nd parameter is a string that will be searched to match pattern at the starting of the string.
e.g.

import re
print re.match(“i”, “intellipaat”)
Output<_sre.SRE_Match object at 0x7f9cac95d78>

Python then outputs a line signifying that a new object i.e. sre.SRE type has been created. The hex number following it is the address at which it was created.

import reprint re.match(“b”, “intellipaat”)

Output

None

Special Sequence Characters

The six most important sequence characters are:

  • \d: Matches any decimal digit. This is really the same as writing [0-9], but is done so often that it has its own shortcut sequence.
  • \D: Matches any non-decimal digit. This is the set of all characters that are not in [0-9] and can be written as [^0-9]
  • \s: Matches any white space character. White space is normally defined as a space, carriage return, tab, and non-printable character. Basically, white space is what separates words in a given sentence.
  • \S: Matches any non white space character. This is simply the inverse of the \s sequence above.
  • \w: Matches any alphanumeric character. This is the set of all letters and numbers in both lower- and uppercase.
  • \W: Matches any non-alphanumeric character. This is the inverse of the \w sequence above.

Search Function

It searches for primary occurrence of RE pattern within string with optional flags.

Syntax

re.search(pattern, string, flags=0)

e.g.

m = re.search(‘\bopen\b’, ‘please open the door’)
print m
OutputNone

This output is occurred because the ‘\b’ escape sequence is treated as a special backspace character. Meta characters are those characters which include /.

>>> import re
>>> m = re.search(‘\\bopen\\b’, “please open the door”)
>>> print mOutput<_sre.SRE_Match object at 0x00A3F058>

Regular Expression Modifiers (Option Flags)

 

Modifier Description
re.I Performs case-insensitive matching.
re.L Interprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior (\b and \B).
re.M Makes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string).
re.S Makes a period (dot) match any character, including a newline.
re.U Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B.
re.X Allows “cuter” regular expression syntax.

Python Frameworks: (Popular GUI Frameworks)

There are several popular Python GUI toolkits. The most popular are as follows:

  • Qt
  • GTK
  • wxPython
  • Tkinter

1. Qt

Qt is one of the most popular libraries for cross-platform development. It is the main graphical library for the KDE desktop. There have several licenses available with the main ones being for commercial and open-source use. If your application is commercial i.e. closed-source then you must pay for a license to use it. If the application is open source then you can use the GPL license; however, you will be unable to commercialize your program without upgrading the license.

It contains a graphical layout utility which permits the user to drag & drop graphical widgets for quick design of the interface. On the other hand the developer can hand code the interface layout. It also supports non GUI features containing SQL database access, network support, XML parsing. It is written in C++ but python binding is available via PyQt.

2. GTK+

GTK stands for GIMP Tool Kit which is created for the development of the GIMP image software. GTK+ developed into the graphical library that controls the GNOME desktop. Widgets are basic building blocks of a GUI application.

GTK+ is open source under the GPL license. When Qt was selected to power the KDE desktop then Trolltech had a proprietary license for it. Because it goes against the free Software concept, GTK was created to permit people to use the GNOME desktop and GTK applications as an open source software.

GTK does not essentially have a built-in drag & drop interface however the wxGlade utility gives this functionality. The original GTK was written in C whereas GTK+ is written in C++ using OOP practices and PyGTK is the Python binding for GTK+.

3. wxPython

It is a cross-platform toolkit which is used for creating desktop GUI applications. It is a python implementation of wxWidgets that means programs of python interact with python wrappers of the fundamental C, C++ code. Using wxPython developers can create applications on Mac OS, Windows and on different Unix systems.
wxWidgets is a general purpose cross-platform C++ library. wxPython is an Open Source Software which is licensed under the GPL. Programs which are written with wxPython use the native widgets of an OS.

4. Tkinter

It is a Python binding to the Tk GUI toolkit. Tk is the default GUI library for Python development due to its addition in the core Python language. It is the original GUI library for the Tcl language. The Tkinter toolset is quite limited but it is easy to learn to make it build simple GUI programs in Python.

It has three built-in layout managers:

  • Pack geometry manager – Manage widgets in vertical and horizontal boxes
  • Grid geometry manager – Put widgets in a two-dimensional grid
  • Place geometry manager – Put widgets on their containers using absolute positioning.

JPython: A Combination of java and python

It is completely written in Java by Jim Hugunin. Programmers of python can use their python information to Java based development environments and also programmers of java can use the python scripting language as a way to control their Java systems and to test libraries etc.

The JPython installation includes a number of parts:

  • jpython: The equivalent of the Python program
  • jpythonc: It acquire a JPython program and then compiles it to Java class files. The resulting Java class files can be used as any Java class file. For example as servlets or as beans.
  • A set of modules that provide the JPython user with the huge majority of the modules in the standard Python library.

Using JPython is very similar to using Python:

~/book> jpython
JPython 1.0.3 on java1.2beta4
Copyright 1997–1998 Corporation for National Research Initiatives
>>> 2 + 3

Output
5

Conclusion:

This brings us to the end of this tutorial, where we have gone through the Fundamentals of Python, control flow statements, Loops and functions. We have also discussed different File Handling, Exception Handling methods. By the end of this tutorial, we have also gathered an idea about how to work with different modules. Further you can check our free Python interview questions by experts.

Python and its popularity:

Python and its popularity

Big companies like Netflix, IBM uses Python, Dropbox is also created in Python and hundreds of other big companies are also adapting Python. So, this is no surprise that Python has become one of the fastest-growing programming languages according to Stack Overflow. The ever-expanding applications of large-scale Data Science and Artificial Intelligence have become two of the most aspiring field in the 21st century.

To be part of these competitive yet two of the most lucrative fields, one must fulfill the requirement of having a well-rounded understanding of Python. This structured Python training course will help you get an elaborate understanding of Python.

Previous Next

Download Interview Questions asked by top MNCs in 2018?

Learn SQL in 16 hrs from experts



"0 Responses on Python Modules, Regular Expressions and Python Frameworks"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python Modules, Regular Expressions and Python Frameworks.