Python Data Science Wilkinson CH
Python Data Science Wilkinson CH
Now, let's run Python on the command prompt. Write the command
Python; it may show error. This is because the path has not been set.
In order to set the path of Python, right-click on "My PC" and select
Properties → select Advanced → select Environment Variables.
New Path in the user variable section will be added
Write PATH as the variable name and set the path to the installation
directory of Python.
When the path is set, we can now run Python on our local system. Restart
CMD, and type Python again. Finally, Python interpreter will be opened
where we can execute the Python statements.
The least demanding approach to introduce things in the Command line is
utilizing the apt-get application's install functionality. You need to type
apt-get install. If the extra exists, apt-get will discover and submit it.
Unfortunately, the version of apt-get on your server isn't the latest one, so
as an initial step, update it with this Command:
sudo apt-get update
Through this command, you can update the version and can run the
installation process.
CHAPTER: 2 Python Functions and File
Handling
Python functions and file handling are the most important part of Python
for data science. Without using these functionalities, no data scientist can
achieve results. They are easy to understand codes that can be called up
anywhere in the main Python code.
2.1 Functions in Python
Python functions are highly useful small block of code that can be called
to run a specific function. They are used in programs to perform special
roles. Basically, they are unique statements that are enclosed by {}. They
can be called as many times as required.
Advantage of Python functions
Here are some major advantages of Python functions:
• They avoid repetition of code. With a single statement, the whole
function can be called. It saves a lot of time.
• Their reusability is a very attractive feature. It can be called a number
of times in a program.
• Through these functions, a large program can be divided into multiple
functions. It enhances the usage.
Functions of Python
There are many functions in Python programming language. They can be
called from interpreter package to use in any program. Without these
functions, this language has no attraction for the software community.
Nowadays, they are being used across the world to perform major
programming tasks related to data science and other projects.
Result/ Output:
[5] is True
No-value is False
These results are defining ‘how function’ works.
Results:
' Hi Python.'
These results are defining ‘how function’ works.
Result:
True
False
These results are defining ‘how function’ works.
For Example
Let’s evaluate this function from this syntax elaboration of function.
String1= "Python Data Science"
#string1 with encode ‘utf-8'
Array1= bytearray(string, ‘utf-8')
Print(array1)
Result:
bytearray(b'Python Data Science')
These results are defining ‘how function’ works.
Result/Output :
245
363.790
35
These results are defining ‘how function’ works.
listIter = iter(list)
# prints '6'
print(next(listIter))
# prints '7'
print(next(listIter))
# prints '8'
print(next(listIter))
# prints '9'
print(next(listIter))
Result/Output:
6
7
8
9
These results are defining ‘how function’ works.
Result:
localsJunior: {}
localsSenior: {'present': True}
These results are defining ‘how function’ works.
Result / Output:
<map object at 0x7fb04a6bec18>
These results are defining ‘how function’ works.
Result / Output :
21 John John@xyz
These results are defining ‘how function’ works.
Result/ Output:
(6, 0)
These results are defining ‘how function’ works.
Result/ Output:
[(0, 4), (1, 5), (2, 6)]
These results are defining ‘how function’ works.
The dict() Function of Python
It returns a dictionary. This function generates three types of dictionary:
Empty Dictionary: When there is no argument passed.
Identical Key-value pair Dictionary: When there is a potential argument
given.
Keyword and Value added Dictionary: When there is a keyword argument.
For Example
Let’s evaluate this function from this syntax elaboration of function.
X = dict()
Y = dict(c=4,d=5)
print(result)
print(result2)
Result/ Output:
{} #empty dictionary
{'c': 4, 'd':5} #dictionary with values
These results are defining ‘how function’ works.
Output :
221
1000.25
These results are defining ‘how function’ works.
Result/ Output:
0x2
0x70
These results are defining ‘how function’ works.
Result/Output :
59696771728
66864236539
19945047867
These results are defining ‘how function’ works.
#X is number
#Y is item
Result/ Output :
128
16
42
These results are defining ‘how function’ works.
Result/ Output :
Int val : 15 15 15
These results are defining ‘how function’ works.
# Negative a, Positive b
print(pow(-2, 3))
# Positive a, Negative b (x**-y)
print(pow(2, -3))
# Negative a, Negative b
print(pow(-2, -3))
Result/ Output :
8
8
These results are defining ‘how function’ works.
Results / Output:
Python Data Science
a=7
a=7=b
These results are defining ‘how function’ works.
Result/ Output :
[10,11]
These results are defining ‘how function’ works.
List = [1, 2, 7, 5]
print(list(reversed(List)))
Result/ Output:
['n', 'o', 'h', 't', ‘y','P']
These results are defining ‘how function’ works.
The round() Function of Python
This function is mostly used when there are decimals in the list of
numbers. Let’s look at this example to understand this function.
For Example:
Let’s evaluate this function from this syntax elaboration of function.
print(round(8))
print(round(10.4))
print(round(6.6))
Result/ Output:
8
10
7
These results are defining ‘how function’ works.
Result/ Output:
'6'
These results are defining ‘how function’ works.
Result/ Output :
a= ()
b= (2, 8, 10)
a= ('P', 'y', 't', 'h','o','n')
a= (4, 5)
These results are defining ‘how function’ works.
InstanceOfPython = Python()
print(type(InstanceOfPython))
Result/ Output:
<class 'X'>
<class 'Y'>
<class '__main__.Python'>
These results are defining ‘how function’ works.
Result/ Output:
{'b': 9, 'a': 7}
These results are defining ‘how function’ works.
numericalList = [4,5, 6]
stringList = ['four', 'five', 'six']
X = zip()
XList = list(X)
print(XList)
X= zip(numberList, stringList)
XSet = set(result)
print(XSet)
Naming of Identifier
Factors are the situation of identifiers. An identifier is used to perceive the
literals used in the program. The standards to name an identifier are given
below.
• The essential character of the variable must be a letter or underscore (
_).
• Every one of the characters beside the essential character may be a
letter arranged by lower-case(a-z), promoted (A-Z), underscored, or a
digit (0-9).
• The identifier name must not contain any void zone, or extraordinary
characters (Ex: ! @, #, %, ^, and, *).
• The identifier name must not resemble any catchphrase portrayed in
Python syntax.
• They are case sensitive. For example, my name, and My name isn't
recognized as the same.
• Instances of considerable identifiers: a123, _n, n_9, etc.
• Instances of invalid identifiers: 1a, n%4, n 9, etc.
Multiple Assignments
Python enables doling out an incentive to numerous variables in a solitary
explanation, which is otherwise called multiple assignments. It can be
applied in two different ways: either by doling out a solitary incentive to
various variables, or relegating various qualities to numerous variables.
Example:
a=b=c=60
print
print z
Output :
>>>
60
60
60
>>>
Example:
x,y,z=21,25,45
print x
print y
print z
Output:
>>>
21
25
45
>>>
Arithmetic operators
These operators are used for specific arithmetic operations to get results.
Two operands are taken, and activity through an operator is performed
resulting in some desired value.
Here is some very important arithmetic operators used in Python.
Detailed Description
ADDITION +
It is to perform addition or sum function between two operands. E.g. if x
= 25, y = 15 => x+y = 40
SUBTRACTION -
It subtracts 2nd operand from the 1st operand. E.g. if x = 40, y = 10 => x -
y = 30
DIVISION /
It divides the 1st operand by 2nd operand, and gives quotient. e.g. if x =
20, y = 2 => x/y = 10
MULTIPLICATION *
It performs a multiplication operation. For example, if x = 30, y = 10 => x
* y = 300
REMAINDER %
It performs the operation of division and gets remainder. For example, if
x=30, y=10, x/y=0
Output:
<type 'int'>
<type 'str'>
<type 'float'>
Numbers
Number stores numeric values. Python generated number objects
whenever a number is given to a variable. For example:
1. a = 3 , b = 5 #a and b are number objects
Four different types of numeric data are supported by Python.
int (signed integers like 12, 22, 39, etc.)
long (long integers used for a relatively higher range of values like
908800L, -0x19292L, etc.)
float (float is used to store floating-point like 1.7, 9.1902, 151.2, etc.)
Complex (complex numbers like 12.14j, 2.0 + 12.3j, etc.)
Python allows to use a lower-case L to be used with long integers. But we
must ensure that always an upper-case L is used for clarity.
A complex number consists of an ordered pair, i.e., a + ib, where a and b
denote the real and imaginary parts respectively).
String
String can be described as the sequence of characters that are represented
in the quotation marks. Also, single, double, or triple quotes can be used to
define a string.
String handling is a simple, and clear task, since there are many in-built
functions and operators provided in Python.
For string handling, the operator + is used to concatenate two strings as the
operation "hello"+" Mr. Davir" returns "hello Mr. David".
The operator * is known as a repetition operator as the operation "Python"
*2 returns "Python Python ".
String handling in Python is illustrated in following example
Example:
string1 = 'hello Mr. David'
string2 = ' how are you'
print (string1[0:2]) #printing first two character using slice operator
print (string1[4]) #printing 4th character of the string
print (string1*2) #printing the string twice
print (string1 + str2) #printing the concatenation of string1 and string2
Output:
he
o
hello Mr. David hello Mr. David
hello Mr. David how are you
String Operators
+
Operator ‘Addition’ is used to join the strings in a program.
*
Operator with symbol ‘Multiplication’ is for the generation of multiple
copies of the same string to perform a function.
[]
Slice Operator makes available the sub-strings of a specified string.
[:]
This range slice operator performs function of getting characters.
In
This membership operator returns value against the presence of specific
sub-string in the main string.
%
It is employed to perform string formatting.
Lists
Lists are identical to arrays in C. But the list can contain data of various
types. The stored items in the list are separated with a comma (,) and
enclosed within square brackets [].
Slice [:] operators can be employed for accessing the list's. The
concatenation operator (+) and repetition operator (*) work with the list in
a similar way as they were working with the strings.
Example:
l = [1.5, "Hi", "Python", 2]
print (l[3:]);
print (l[0:2]);
print (l);
print (l + l);
print (l * 3);
Output:
[2]
[1.5, 'Hi']
[1.5, 'Hi', 'Python', 2]
[1.5, 'Hi', 'Python', 2, 1.5, 'Hi', 'Python', 2]
[1.5, 'Hi', 'Python', 2, 1.5, 'Hi', 'Python', 2, 1.5, 'Hi', 'Python', 2]
Tuple
It is identical to the list in a lot of ways. Similar to lists, tuples also
possess the collection of the items of various data types. The items of the
tuple are segregated with a comma (,) and enclosed in parentheses ().
A tuple can't modify the size and value of the items of a tuple.
Example:
t = ("Hi", "Python world", 4)
print (t[1:]);
print (t[0:1]);
print (t);
print (t + t);
print (t * 3);
print (type(t))
t[2] = "hi";
Output:
('Python world', 4)
('Hi',)
('Hi', 'Python world', 4)
('Hi', 'Python world', 4, 'Hi', 'Python world', 2)
('hi', 'Python world', 4, 'Hi', 'Python world', 4, 'Hi', 'Python world', 4)
<type 'tuple'>
Traceback (most recent call last):
File "main.py", line 8, in <module>
t[2] = "Hello";
TypeError: 'tuple' object does not support assignment
Chapter 4: Python Regular Expressions,
Statements, Loops
Python regular expressions, statements and loops are the totality of Python
programming. All of these functions, methods, statements and loops play a
vital role in building an effective program for data analysis in Python.
There are number of reasons behind the addition of these operation
runners in the libraries of Python. Let’s discuss the importance and
functionalities of these programs.
4.1 Python Regular Expressions
The regular expression (regex) works to analyze the pattern in a string.
There are a number of regex functionalities that can be imported to bring
into use. For importing these functions, we can use the command: import
re.
a = int(input("Enter a? "));
b = int(input("Enter b? "));
c = int(input("Enter c? "));
if a>b and a>c:
print("a is largest");
if b>a and b>c:
print("b is largest");
if c>a and c>b:
print("c is largest");
Output:
Enter a? 100
Enter b? 120
Enter c? 130
c is largest
The if-else statement
The if-else statement provides an else block joined with the if statement
that is executed in the false case of the condition. When the condition is
true, then the if-block is executed. Otherwise, the else-block is executed.
Syntax is as follows
if condition:
#block of statements
else:
#another block of statements (else-block)
Example:
age = int (input("Enter the age? "))
if age>=18:
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");
Output:
Enter your age? 90
You are eligible to vote !!
Example 2:
num = int(input("enter the number?"))
if num%2 == 0:
print("Number is even...")
else:
print("Number is odd...")
Output:
Enter the number?10
Number is even
The elif statement
This statement helps to run multiple level of conditions. It must have if-
an-if ladder to perform the program. It works only by taking up series of
‘True’ conditions.
Syntax is as follows
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
Python break statement
The break statement has a unique importance in Python loop
programming. It shifts the execution pattern on the next lines by breaking
up the loop from the previous codes. With simple syntax, it gives back
control to the required loops in the same large program.
Syntax is break
Python continue Statement
This statement brings control of programing to the start of loop. It skips
the rest of codes, and execution comes back to the beginning. It has an
important role in skipping and executing specific conditions.
Syntax is as follows
#loop statements
continue;
#the code to be skipped
Example 1:
i = 0;
while i!=10:
print("%d"%i);
continue;
i=i+1;
Output:
infinite loop
Example 2:
x=1; #initializing a local variable
#starting a loop from 10 to 20
for x in range(1,10):
if x==15:
continue;
print("%d"%i);
Output:
10
11
12
13
14
16
17
18
19
20
Python Pass Statement
This statement is a non-executable part of the program. It appears to
justify syntax, but provides only null operation. It is sometimes used
when the code is not a part of program, but written somewhere outside the
program.
Syntax is as follow: Pass
Example:
For a in [1,2,3,4,5]:
if a==4:
pass
print "pass when value is",a
print a,
Output:
>>>
1 2 3 Pass when value is 4
45
>>>
The import statement in Python
This is the most valuable statement in Python programming language. It
makes possible the access of one module’s functionality to another.
Without the import statement, Python can’t perform up to the mark level.
Syntax of ‘import statement’
import module
Example:
import doc;
first name = input("input the first name?")
doc.displayMsg(first name)
Output:
Input the first name? John
Hi John
4.3 Loops in Python
Programming is all about flow of commands and functions over and again.
Most of the time, the same code has to be repeated several times to get
results, which is common practice in the general programming world. To
make this easy for data scientists and programmers, there are many loops
that are used by professionals to save time and keep the syntax easy to
understand. These loops repeat the required code multiple times with only
a small block of code. In Python, these loops are necessary to build up
predictive models and data analysis.
Syntax is as follows
for iterating_var in sequence:
statement(s)
Example:
i=1;
num = int(input("Enter a number:"));
for i in range(1,11):
print("%d X %d = %d"%(num,i,num*i));
Output:
Enter a number:10
10 X 1 = 10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100
Nested for loop in Python
It is about nesting a ‘for loop’ inside a ‘for loop’ to execute it multiple
times.
Syntax is as follows
for iterating_var1 in sequence:
for iterating_var2 in sequence:
#block of statements
#Other statements
Example:
n = int(input("enter number of rows"))
i,j=0,0
for i in range(0,n):
print()
for j in range(0,i+1):
print("*",end="")
Output:
Enter the number of rows? 6
*
**
***
****
*****
******
Tasks on Stack:
Addition – It increases the size of stack.
Cancellation – It is used to decrease the size of stack.
Traversing - It involves visiting every component of the stack.
Qualities:
• Insertion request of the stack is saved.
• Helpful for parsing the activities.
• Duplicacy is permitted.
Code
# Code to demonstrate Implementation of
# stack using list
y= ["Python-language", "Csharp", "Androidnew"]
y.push("Javaflash")
y.push("C++lang")
print(y)
print(y.pop())
print(y)
print(y.pop())
print(y)
Output:
['Python-language', 'Csharp', 'Androidnew', 'Javaflash', 'C++lang']
C++lang
['Python-language', 'Csharp', 'Androidnew', 'Javaflash']
Javaflash
['Python-language', 'Csharp', 'Androidnew']
Queue Attributes
First-in-First-Out (FIFO) principle allows queue to have elements from
both ends. It is open to get in and let go of components.
Basic functionalities in queue:
enqueue – For adding elements.
dequeue – For removing elements from queue.
Qualities
• Insertion request of the queue is protected.
• Duplicacy is permitted.
• Valuable for parsing CPU task activities.
Code
import queue
# Queue is created as an object 'L'
L = queue.Queue(maxsize=10)
# Data is inserted in 'L' at the end using put()
L.put(9)
L.put(6)
L.put(7)
L.put(4)
# get() takes data from
# from the head
# of the Queue
print(L.get())
print(L.get())
print(L.get())
print(L.get())
Output:
9
6
7
4
Command line arguments in Python
Python focuses to provide command lines for input parameters that are
passed to elements in order to execute functions.
By using getopt module, this operation is executed.
The getopt module of Python
It is very similar to other programming languages. It is used to pass inputs
through command lines to get options from the user. It allows a user to
input options.
Python Assert Keyword
These keywords inform the programmer about the realities of running the
program. It works with conditional commands. When the condition
doesn’t get fulfilled, it declines with the display of an assertive message
on the screen e.g. “no data is available”. AssertionErrors are used to define
the program properly.
Why Assertion?
It is a highly recommended debugging tool. It keeps the user aware about
codes on each step. If some lines of codes have errors or mistakes, it alerts
the user with message.
Syntax
assert condition, error_message(optional)
Example:
def avg(scores):
assert len(scores) != 0,"The List is empty."
return sum(scores)/len(scores)
scoresb = [67,59,86,75,92]
print("The Average of scoresb:",avg(scoresb))
scores1 = []
print("The Average of scoresa:",avg(scoresa))
Output:
The Average of scores2: 75.8
AssertionError: The List is empty.
Chapter 6: Python Modules, Exceptions and
Arrays
Python modules, exceptions and arrays are an integral part of object-
oriented Python programming language. In data science, we use them from
time to time to have a better understanding with the usage of code in a
logical way. These programming methods are also used in other
programming languages, and are a popular framework because of their
usage to transform the complexities of programming into simple coding.
Let’s discuss them one by one.
6.1 Python Modules
Python modules are programs that have programming codes in Python.
They contain all variables, classes and functions of this unique language.
They enable the programmer to organize codes in a proper format that is
logically valid. They can be imported to use the functionality of one
module for another.
Example:
Now here a module named as file.py will be generated which contains a
function func that has a code to print some message on the console.
So let’s generate it file.py.
#displayMsg prints a message to the name.
def displayMsg(name)
print("Hi "+name);
Now it is required to add this module into the main module to call the
method displayMsg() defined in the module named file.
Loading the module in our Python code
In order to utilize the functionality of Python code, the module is loaded.
Python provides two types of statements as defined below.
1. The import statement
2. The from-import statement
Python Standard Library- Built-in Modules
There is an unlimited pool of Python Built-in Modules. We will discuss
some of the most important modules. These are:
• random
• statistics
• math
• datetime
• csv
To import any of them, use this syntax:
Import[module_name]
eg. Import random
Random module in Python
This module is used to generate numbers. By using the command
random(), we can generate float numbers. The range of these float
numbers lies between 0.0 and 1.0.
Here are some important random functions used in random module:
The Function random.randint()
It is for random integers.
The Function random.randrange()
It is for randomly selected elements.
The Function random.choice()
It is for randomly selected elements from non-empty.
The Statistics module of Python
It is a very useful module of Python. It provides numerical data after
performing statistics functions.
Here is a list of some very commonly used functions of this module:
The mean() function
It performs arithmetic mean of the list.
For Example:
import statistics
datalist = [5, 2, 7, 4, 2, 6, 8]
a= statistics.mean(datalist)
print("The Mean will be:", a)
Output:
The Mean will be: 4.857142857142857
The median() function
It gives middle value of the list.
Example:
import statistics
dataset = [4, -5, 6, 6, 9, 4, 5, -2]
print("Median of data-set is : % s "
% (statistics.median(dataset)))
Output:
Median of data-set is: 4.5
The mode() function
It provides common data from the list.
Example:
import statistics
datasets =[2, 4, 7, 7, 2, 2, 3, 6, 6, 8]
print("Calculated Mode % s" % (statistics.mode(datasets)))
Output:
Calculated Mode 2
The stdev() function
It calculates the standard deviation.
Example:
import statistics
sample = [7, 8, 9, 10, 11]
print("Standard Deviations of sample data is % s "
% (statistics.stdev(sample)))
Output:
Standard Deviation of sample data is 1.5811388300841898
The median_low()
The median_low function is used to return the low median of numeric data
in the list.
Example:
import statistics
# simple list of a set of integers
set1 = [4, 6, 2, 5, 7, 7]
# Print low median of the data-set
print ("data-set Low median is % s "
% (statistics.median_low(set1)))
Output:
Low median of the data-set is 5
median_high()
The median_high () function is employed to calculate the high median of
numeric data in the list.
Example:
import statistics
# list of set of the integers
dataset = [2, 1, 7, 6, 1, 9]
print("High median of data-set is %s "
% (statistics.median_high(dataset)))
Output:
High median of the data-set is 6
The math module of Python
This module contains the mathematical functions to perform every
mathematical calculation.
Here are two constants as well:
Pie (n): A well-known mathematical constant and is defined as the ratio
of circumstance to the diameter of a circle. Its value is
3.141592653589793.
Euler's number (e): It is the base of the natural logarithmic, and its value
is 2.718281828459045.
A few math modules which are given below:
The math.log10() function
It calculates base1 0 logarithm of the number.
Example:
im port math
x=13 # small value of of x
print('log10(x) is :', math.log10(x))
Output:
log10(x) is : 1.1139433523068367
The math.sqrt() function
It calculates the root of the number.
Example:
import math
x = 20
y = 14
z = 17.8995
print('sqrt of 20 is ', math.sqrt(x))
print('sqrt of 14 is ', math.sqrt(y))
print('sqrt of 17.8995 is ', math.sqrt(z))
Output:
sqrt of 20 is 4.47213595499958
sqrt of 14 is 3.7416573867739413
sqrt of 17.8995 is 4.230780069916185
The math.expm1() function
This method calculates e raised to the power of any number minus 1. e is
the base of natural logarithm.
The math.cos() function
It calculates cosine of any number in radians.
Example:
import math
angleInDegree = 60
angleInRadian = math.radians(angleInDegree)
print('Given angle :', angleInRadian)
print('cos(x) is :', math.cos(angleInRadian))
Output:
Given angle : 1.0471975511965976
cos(x) is : 0.5000000000000001
The math.sin() function
It calculates the sine of any number, in radians.
Example:
import math
angleInDegree = 60
angleInRadian = math.radians(angleInDegree)
print('Given angle :', angleInRadian)
print('sin(x) is :', math.sin(angleInRadian))
Output:
Given angle: 1.0471975511965976
sin(x) is: 0.8660254037844386
The math.tan() function
It returns the tangent of any number, in radians.
Example:
import math
angleInDegree = 60
angleInRadian = math.radians(angleInDegree)
print('Given angle :', angleInRadian)
print('tan(x) is :', math.tan(angleInRadian))
Output:
Given angle : 1.0471975511965976
tan(x) is : 1.7320508075688767
The sys module of Python
This module provides access to system-specific functions. It changes the
Python Runtime Environment to enable the user to get variables and
parameters.
Need to import sys function
First, there is a need to import the sys module in the program before
starting the use of functions.
The sys.modules’ function
These functions perform some really important tasks on system in Python
programming.
• Function of sys.argv: For arguments
• Function of sys.base_prefix: For startup
• Function of sys.byteorder : To get byterorder.
• Function of sys.maxsize : To get large integer.
• Function of sys.path : To set path.
• Function of sys.stdin : To restore files.
• Function of sys.getrefcount : To get reference count of an object.
• Fun tion of sys.exit : To exit from Python command prompt.
• Function of sys executable : Locate the Python in system.
• sys.platform: To identify Platform.
The Collection Module of Python
This module plays an important role, as it collects major data formats or
data structures, such as list, dictionary, set, and tuple. It improves the
functionality of the current version of Python. It is defined as a container
that is employed to conserve collections of data, for example, list.
The function of namedtuple() in Collection Module
It produces a tuple object without causing an issue with indexing.
Examples:
John = ('John', 25, 'Male')
print (John)
Output:
('John', 25, 'Male')
OrderedDict() function
It generates dictionary object with key that can overwrite data inside.
Example:
import collections
d1=collections.OrderedDict()
d1['A']=15
d1['C']=20
d1['B']=25
d1['D']=30
for k,v in d1.items():
print (k,v)
Output:
A 15
C 20
B 25
D 30
Functin defaultdict()
It produces an object similar to dictionary.
Example:
from collections import defaultdict
number = defaultdict(int)
number['one'] = 1
number['two'] = 2
print(number['three'])
Output:
0
Counter() function
It counts the hasbale objects after reviewing the elements of list.
Example:
A = Counter()
Xlist = [1,2,3,4,5,7,8,5,9,6,10]
Counter(Xlist)
Counter({1:5,2:4})
Ylist = [1,2,4,7,5,1,6,7,6,9,1]
c = Counter(Ylist)
print(A[1])
Result:
3
The function deque()
It facilitates addition and removal of elements from both ends.
For Example:
from collections import deque
list = ["x","y","z"]
deq = deque(list)
print(deq)
Output:
deque(['x', 'y', 'z'])
Python OS Module
Python OS module provides functions utilized for interacting with the
operating system and also obtains related data about it. The OS comes
under Python's standard utility modulesPython OS module which allows
you to work with the files, documents and directories. Some of OS module
functions are as follows:
os.name
It provides the name of the operating system module it imports.
It can register 'posix', 'nt', 'os2', 'ce', 'java' and 'riscos'.
Example:
import os
print(os.name)
Output:
posix
os.getcwd()
It restores the Current Working Directory (CWD) of the file.
Example:
import os
print(os.getcwd())
Output:
C:\Users\Python\Desktop\ModuleOS
os.error
The functions in this module define the OS level errors in case of invalid
file names and path.
Example:
import os
filename1 = 'PythonData.txt'
f = open(filename1, 'rU')
text = f.read()
f.close()
print('Difficult read: ' + filename1)
Output:
Difficult read: PythonData.txt
os.popen()
It opens a file, and it gives back a fileobject that contains connection with
pipe.
The datetime Module
It is an imported module that allows you to create date and time objects. It
works to conduct many functions related to date and time.
Let’s understand it through an example:
Example:
import datetime;
#returns the current datetime object
print(datetime.datetime.now())
Output:
2018-12-18 16:16:45.462778
Python read csv file
The Comma Separated values (CSV) File
It is a simple file format that arranges tabular data. It is used to store data
in tabular form ora spreadsheet that can be exchanged when needed. It is
in a Microsoft excel supported data form.
The CSV Module Functions in Python
This module helps in reading/writing CSV files. It takes the data from
columns and stores it to use in the future.
• The function csv.field_size_limit - To maximize field size.
• The function csv.reader – To read information or data from a csv
file.
• The function csv.writer – To write the information or data to a csv
file
These functions have a major role in CSV module.
6.2 The Exceptions in Python
Exceptions are actually interruptions that stops the running program. They
are mistakes or errors in the code. In Python, these are handled differently.
The Common Exceptions in Python
Here are some common exceptions that may occur in Python. Every
Python programmer is very familiar with these errors or exceptions.
• The exception of ZeroDivisionError: when a number is divided by
zero.
• The exception of NameError: when a name is not found.
• The exception of IndentationError: when incorrect indentation is
given.
• The exception of IOError: when Input Output operation fails.
• The exception of EOFError: when the end of the file is reached,
and still operations are being performed.
Unhandled Exceptions
Example:
x= int(input("Enter a:"))
y = int(input("Enter b:"))
z= a/b;
print("x/y = %d"%c)
print("Hello I am a teacher")
Output:
Enter a:10
Enter b:0
Traceback (most recent call last):
File "exception-test.py", line 3, in <module>
c = a/b;
ZeroDivisionError: division by zero
The finally block
It is used to run a code before the try statement.
Syntax
try:
# block of code
# this may throw an exception
finally:
# block of code
# this will always be executed
Example:
try:
fileptr = open("file.txt","r")
try:
fileptr.write("Hi I am good")
finally:
fileptr.close()
print("file closed")
except:
print("Error")
Output:
file closed
Error
The Exception Raising in Python
The raise clause in Python is used to raise an exception.
Syntax
Raise exception_class,<value>
The Custom Exception in Python
It enables programmers to generate exceptions that have already been
launched with the program.
Example:
class ErrorInCode(Exception):
def __init__(self, data):
self.data = data
def __str__(self):
return repr(self.data)
try:
raise ErrorInCode(2000)
except ErrorInCode as ae:
print("Received error:", ae.data)
Output:
Received error: 2000
6.3 Python Arrays
Array is a set of elements that are used to work on specific data values. It
is advanced level programming that allows users multiple functionality
over data structures. Through arrays, code can be simplified, therefore
saving a lot of time.
Array Element - Data element stored in array.
Array Index - Position of an element.
Array Representation:
The declaration of array can be done in many different ways.
• Array Index starts with 0.
• Element can be located with the help of its index number.
• The length of the array defines the storage capacity of the elements.
Array operations in Python:
Some of the basic operations in an array are given below:
• Traverse – To print all the elements one by one.
• Insertion – Addition of element in Index.
• Deletion – Deletion of element at index.
• Search – To search the element.
• Update - To update an element at the given index.
Array Generation
array import *
MyarrayName = array(typecode, [initializers])
Accessing array elements
The array elements accessibility can be ensured by using the respective
indices of those elements.
import array as arr
a = arr.array('i', [1, 3, 5, 87])
print("First element:", a[0])
print("Second element:", a[1])
print("Second last element:", a[-1])
Output:
First element: 1
Second element: 3
Second last element: 8
Arrays are changeable, and elements can be changed in similar to lists.
A combination of arrays makes the process speedy and saves time. The
array can reduce the code's size.
Deletion can be done by using the del statement in Python.
The length of an array can be described as the number of elements in an
array. It returns an integer value that is equal to the total number of the
elements present in that array.
Syntax
len(array_name)
Example:
a=arr.array('d',[1.2 , 2.2 ,3.2,3,6,7.8])
b=arr.array('d',[4.5,8.6])
c=arr.array('d')
c=a+b
print("Array c = ",c)
Output:
Array c= array('d', [1.2, 2.2, 3.2, 3.6, 7.8, 4.5, 8.6])
Example:
import array as arr
x = arr.array('i', [5, 10, 15, 20])
print("First element:", x[0])
print("Second element:", x[1])
print("Second last element:", x[-1])
Output:
First element: 5
Second element: 10
Second last element: 15
Chapter 7: Python Data Science Libraries and
General Libraries
In the previous chapters, we discussed the important concepts of Python,
such as data structures, built-in functions, variable, exceptions, methods,
for loops and if statements. Now, we will study the modules and packages
of Python that is important for any project.
Python programming and data science are integral to one another. Python
is an unbelievable language for data science and the individuals who need
to begin in the field of data science. It bolsters countless cluster libraries
and systems to give a decision for working with data science in a spotless
and productive manner. The different systems and libraries accompany a
particular reason for use, and should be picked by your prerequisite.
7.1 Python Data Science Libraries
A Python library is a gathering of capacities and techniques that aid in
finishing explicit assignments. There are highly advanced libraries
employed by developers for various tasks. In the beginning, Data Science
and Python was considered unsuitable for each other, and now Python is
very much connected with statistics, machine learning, and predictive
analytics, as well as simple data analytics tasks. It is getting more
accessible and useful day-by-day, as it is an open-source language. There
are millions of data scientists who are enriching the language with tools
through advanced coding. Now, there are highly advanced packages and
libraries that data scientists are using for multiple data analysis tasks.
A brief description of some of the best Python libraries is given below
Numpy
NumPy is a very crucial Python library implied for logical registering. It
accompanies support for an amazing N-dimensional exhibit item and
broadcasting capacities.
Additionally, NumPy offers Fourier changes, arbitrary number capacities,
and devices for coordinating C/C++ and Fortran code. Having a working
understanding of NumPy is obligatory for full stack developers associated
with AI ventures utilizing Python.
Numpy is the most fundamental, and a fantastic bundle, for working with
information in Python. On the off chance that you are getting down to
business on information investigation or Machine learning ventures, at
that point, having a strong comprehension of numpy is required.
Different bundles for information investigation (like pandas) is based on
numpy and the scikit-learn package that is utilized to assemble AI
applications.
What does numpy provide?
At the center, numpy gives the phenomenal ndarray objects; short for n-
dimensional clusters. In a 'ndarray' object, otherwise known as 'exhibit',
you can store numerous things of similar information. It is the offices
around the exhibit object that makes numpy advantageous for performing
math and information controls.
Salient Features
· It is a very interactive library and it’s easy to use.
· Mathematical problems are solved with ease.
Pandas
In Python, we use two-dimensional tables to analyze data, like in SQL or
Excel. Initially, Python didn't have this feature. But that's why Pandas is so
famous. Without a doubt, Pandas is the "SQL of Python. " In short, Pandas
is the library that will help us to handle two-dimensional data tables in
Python. In many ways, it's similar to SQL, though.
The Pandas' library is not exclusively a focal segment of the information
science toolbox, yet it is utilized for different libraries in that
accumulation.
Pandas is based on the NumPy bundle, which means a great deal of the
structure of NumPy is utilized or duplicated in Pandas. Information in
pandas is frequently used to bolster factual examination in SciPy, plotting
capacities from Matplotlib, and machine learning calculations in Scikit-
learn.
Jupyter Notebooks offer a decent situation for utilizing pandas to do
information investigation and demonstrating, yet pandas can likewise be
used in content tools.
Jupyter Notebooks enable us to execute code in a specific cell instead of
running the whole record. This spares a lot of time when working with
enormous datasets and complex changes. Scratchpad, likewise, gives a
simple method to imagine pandas' DataFrames and plots.
Pandas is prominently known for giving information outlines in Python.
This is a fantastic library for information examination, contrasted with
other explicit dialects like R. By utilizing Pandas, it's simpler to deal with
missing information, bolsters working with contrastingly filed information
assembled from numerous various assets, and supports programmed
information arrangement. It also provides devices information
examination and information structures, like consolidating, molding or
cutting datasets, and it is additionally exceptionally viable in working with
information identified with time arrangement. The function works by
giving hearty apparatuses to stacking information from Excel, level
documents, databases, and a quick HDF5 group.
Utilizing the Pandas library makes it simpler and instinctive for
developers to work with named or social data. It offers expressive, quick,
and adaptable data structures. Pandas fills in as the essential elevated level
structure for doing genuine data examination utilizing Python.
One of the most prominent and dominant features of Pandas is to interpret
complex data activities utilizing negligible directions. Also, the AI library
has no shortage of worked in techniques for consolidating, separating, and
gathering data. It additionally
highlights time-arrangement usefulness.
Salient Features:
• Operations of custom type can be completed easily.
• Data manipulation becomes simpler and easier.
• When employed with other Python libraries and tools ,it
gives excellent results.
The Matplotlib
Matplotlib is a two-dimensional plotting library with extraordinary
representation modules for the Python programming language. It is
equipped for delivering top-notch figures in various printed version
organizations and intelligent cross-stage conditions. Besides being utilized
in Python shell, Python contents, and IPython shell, Matplotlib can
likewise be utilized in:
· Jupyter Notebook
· Web application servers
· GUI toolboxes; GTK+, Tkinter, Qt, and wxPython
As indicated by the official site of Matplotlib, the Python library attempts
to "make simple things simple and hard things conceivable." The 2D
plotting Python library permits producing bar graphs, mistake diagrams,
histograms, plots, scatterplots, etc. with fewer lines of code.
Probably the best advantage is that it permits visual access to enormous
measures of information in effectively absorbable visuals. Matplotlib
comprises of a few plots, like line, bar, disperse, and histograms.
Matplotlib represents a Mathematical Plotting Library in Python. It is a
library that is for the most part utilized for information representation,
including 3D plots, histograms, picture plots, scatterplots, bar graphs, and
power spectra. It includes bright highlights for zooming and searching for
gold in various printed copy designs. It bolsters practically all systems, for
example, Windows, Mac, and Linux. This library also fills in as an
augmentation for the NumPy library. Matplotlib has a module pyplot that
is utilized in representations, which is frequently contrasted with
MATLAB.
These libraries are the best for amateurs to begin information science
utilizing the Python programming language. There are numerous other
Python libraries accessible. For example, NLTK for standard language
preparing, Pattern for web mining, Theano for profound learning. IPython
and Scrapy for web scratching. Also, Mlpy and Statsmodels; the sky is the
limit from there. Be that as it may, for novices beginning wihh information
science in Python, it is an absolute necessity to be knowledgeable about
the top libraries.
Salient Features
· It has handy properties, font properties, line styles, etc. through an
object-oriented interface.
· Scatter's Legend
· MATLAB interface for simple plotting of data.
· It has secondary x/y axis support to represent 2-dimmensions.
· It is supports many operating systems.
Scikit-Learn
Scikit-learn gives a scope of administered and solo learning calculations
by means of a predictable interface in Python. It is authorized under a
lenient rearranged BSD permit and is dispersed under numerous Linux
disseminations, empowering scholastic and business use. The library is
based upon the SciPy (Scientific Python) that must be introduced before
you can utilize scikit-learn.
There are a few Python libraries that give a strong execution to the scope
of machine learning calculations. Outstanding amongst others is Scikit-
Learn, a bundle that gives proficient adaptations of countless basic
calculations. Scikit-Learn is described as a perfect, uniform, and
streamlined API, is extremely helpful and has complete online
documentation. One advantage is the consistency. Once you comprehend
the fundamental use and language structure of Scikit-Learn for one model,
changing to another model or calculation is very direct.
Undoubtedly, the fanciest things in Python are Machine Learning and
Prescient Investigation. Also, the best library for that is Scikit-Learn,
which essentially characterizes itself as "Machine Learning in Python."
Scikit-Learn has a few techniques, fundamentally covering all that you
may require in the initial couple of long periods of your information
profession: relapse strategies, characterization strategies, and bunching,
model approval and model determination.
This prevalent library is utilized for AI in information science with
different order, relapse and grouping calculations. It offers help with
vector machines, innocent Bayes, angle boosting, and sensible relapse.
SciKit is intended to interoperate with SciPy and NumPy.
Salient Features
· Capability to extract features from images and text
· Can be utilized again in several contexts
Scipy
There is scipy library and scipy stack. The vast majority of the libraries
and bundles are a piece of the Scipy stack (for logical processing in
Python). One of these parts is the Scipy library, which gives proficient
answers for numerical schedules (the math stuff behind AI models). These
include incorporation, introduction, improvement, and so forth. Scipy
gives scientific strategies to do the unpredictable AI forms in Scikit-learn.
It is an open-source library utilized for registering different modules, for
example, picture preparing, joining, insertion, unique capacities,
enhancements, straight variable based math, Fourier Transform, grouping,
and numerous different undertakings. This library is utilized with NumPy
to perform proficient numerical calculations.
Salient Features
· Comfortably handles mathematical operations.
· Provides effective and efficient numerical routines, such as numerical
integration and optimization, using sub-modules.
· Supports signal processing.
TensorFlow
Anyone engaged with AI machine learning tasks utilizing Python must
have knowledge of TensorFlow. Created by Google, it is an open-source
representative math library for numerical calculations utilizing
information stream diagrams. The scientific activities in a normal
TensorFlow information stream diagram are spoken to by the chart hubs.
The chart edges speak to the multidimensional information exhibits, a.k.a.
tensors, that stream between the diagram hubs.
TensorFlow parades an adaptable design. It enables Python engineers to
convey calculations to one or numerous CPUs or GPUs in a work area, cell
phone, or server, without the need for revising code. All libraries made in
TensorFlow are written in C and C++. Broadly utilized Google items, like
Google Photos and Google Voice Search, are constructed utilizing
TensorFlow. The library has a convoluted front-end for Python. The Python
code will get accumulated and after that executed on TensorFlow.
Salient Features
· Allows preparing various neural systems and numerous GPUs, making
models exceptionally productive for enormous scale frameworks.
· Easily trainable on CPU and GPU for disseminated figuring.
· Flexibility in its operability, which means TensorFlow offers the choice
of taking out the parts that you need and leaving what you don't.
· Great level of network and designer support.
· Unlike other information science Python libraries, TensorFlow improves
the way toward imagining every single piece of the diagram.
Keras
It is recognized as one of the coolest AI (Algorithm) Python libraries.
Keras offers a simpler instrument for communicating neural systems. It
also features extraordinary utilities for accumulating models, preparing
datasets, imagining charts, and significantly more. Written in Python,
Keras can keep running over CNTK, TensorFlow, and Theano. The Python
AI library is created with an essential spotlight on permitting quick
experimentation. All Keras models are compact.
Contrasted with other Python AI libraries, Keras is moderate. This is
because of the way that it makes a computational diagram utilizing the
backend framework first, and after that uses the equivalent to perform
activities. Keras is extremely expressive and adaptable for doing creative
research.
Salient Features
· Being totally Python-based makes it simple to troubleshoot and
investigate.
· Modular in nature.
· Neural system models can be joined for growing increasingly complex
models.
· Runs easily on both CPU and GPU.
· Supports practically all models of a neural system, including
convolutional, inserting, completely associated, pooling, and repetitive.
Seaborn
Fundamentally an information perception library for Python, Seaborn is
based over the Matplotlib library. Additionally, it is firmly incorporated
with Pandas information structures. The Python information perception
library offers an abnormal state interface for drawing appealing factual
charts.
The primary point of Seaborn is to make representation an imperative
piece of investigating and getting information. Its dataset-arranged
plotting capacities work on exhibits and information edges containing
entire datasets. The library is perfect for inspecting connections among
numerous factors. Seaborn makes all the significant semantic mapping and
measurable collections for creating educational plots. The Python
information representation library also has devices for picking shading
palettes that guide you in uncovering designs in a dataset.
Salient Features
· Automatic estimation; the plotting of direct relapse models.
· Comfortable perspectives on the general structure of complex datasets.
· Eases building complex representations, utilizing abnormal state
deliberations for organizing multi-plot matrices.
· Options for picturing bivariate or univariate disseminations.
· Specialized support for utilizing clear cut factors.
Natural Language Toolkit (NLTK)
Valuable for common language preparing and design acknowledgment
undertakings. It can be utilized to create intellectual models, tokenization,
labeling, thinking and different assignments helpful to AI applications
Salient Features
• Comes with a linguistic structure tagger.
• Supports lexical assessment.
7.2 Python General Libraries
Python is named as a "batteries-included programming language." This
essentially implies it accompanies various pre-packaged libraries. In any
case, there are an abundance of different libraries accessible for the
translated, abnormal state, universally useful programming language.
Among different elements adding to the prevalence of Python, having a
humongous gathering of libraries is a noteworthy one. The more libraries
and bundles a programming language has available to it, the more assorted
use-cases it can have.
Requests
One of the most prominent general Python libraries is Requests that make
HTTP demand less difficult and increasingly human-accommodating.
Authorized under the Apache2 permit and written in Python, Requests is
the true standard utilized by engineers for making HTTP demands in
Python.
Notwithstanding utilizing the Requests library for sending HTTP
solicitations to a server, it also permits including structure information,
content, header, multi-part documents, and so forth with them. With the
library, designers need not to add a question to the URL or structure
encode the POST information physically.
The Requests library abstracts the various complexities of making HTTP
demands in a basic API so designers can concentrate more on
communicating with administrations. The library offers authority support
for Python 2.7, 3.4 or more and works incredibly well on PyPy, as well.
Salient Features:
· Allows multipart record transfers and spilling downloads.
· Automatic substance disentangling and programmed decompression.
· Browser-style SSL confirmation.
· Features can be modified and improved according to prerequisites.
· Keep-Alive and Connection Pooling Supports international domains and
URLs.
Pillow
Python Imaging Library or PIL is a free Python library that adds a picture
preparing capacity to the Python interpreter. In basic terms, PIL permits
controlling and opening, and different picture records organized in Python.
Made by Alex Clark and Contributors, Pillow is a fork of the PIL library.
Notwithstanding offering incredible picture handling abilities, Pillow
offers powerful inward portrayal and broad record organization support.
The center Python library is intended to offer quick access to information.
Salient Features:
• Effective investigating bolster utilizing the show()
strategy.
• Ideal for group handling applications.
• Identifies and peruses a huge scope of picture document
designs.
• Offers BitmapImage, PhotoImage, and Window DIB
interfaces.
• Supports discretionary relative changes, shading space
transformations, separating with a lot of implicit convolution
parts, picture resizing and turning, and point activities.
• The histogram technique permits hauling a few
measurements out of a picture, and can be utilized for
programmed upgrade and worldwide factual investigation.
Scrapy
Scrapy is a free and open-source Python structure that is broadly utilized
for web scratching and various different assignments, including
mechanized testing and information mining. At first, Scrapy was created
for web scratching but has advanced to satisfy different purposes. The
library offers a quick and abnormal state strategy for creeping sites and
separating organized information from website pages.
Written in Python, Scrapy is works around bugs that are essentially
independent crawlers, which are given a lot of guidelines. Complying with
the DRY standard, Scrapy makes it simpler to assemble and scale
undeniable web slithering undertakings.
Salient Features:
• Easy to compose a bug to slither a site and concentrate information.
• Follows the DRY rule.
• Offers a web-slithering shell that enables engineers to test a site's
conduct.
• Supports sending out scratched information utilizing the direction
line.
Tkinte
When utilized with Tkinter, Python offers a simple and quick path for
making GUI applications. It is considered the standard GUI library for the
Python programming language. It offers an amazing item situated
interface for the Tk GUI toolbox. Making a GUI application utilizing
Tkinter is simple. You can simply pursue these basic advances:
· Import Tkinter
· Create the primary window for the GUI application; a work in progress
· Add at least one Tkinter Widget
· Enter the headliner circle for making a move for every client activated
occasion.
Tkinter is Graphical User Interface (GUI) library that has powerful
modules to create a user interface.
Salient Features:
• Comes with a scope of gadgets that help geometry the
executive’s strategies.
• Eases creating GUI applications.
• Supports a powerful object-situated interface.
Six
Owing to the fact that it’s the simplest Python library, Six is an amazing
Python library that is intended to smooth out the contrasts between
different Python 2 and Python 3. Six is used for supporting codebases that
can work on both Python 2 and Python 3 without the need for adjustments.
The Six libraries is super-simple to utilize on account of it being offered
as a solitary Python document. Consequently, it is absurdly simple to
duplicate the library into a Python venture. The name Six reflects (Python)
2 x (Python) 3.
Salient Features:
· Simple utility capacities for making Python code perfect with Python 2
and Python 3.
· Supports each adaptation since Python 2.6.
· Easy to use because its contained in a solitary Python document.
Pygame
Pygame is a free and open-source Python library that is intended for
achieving sight and sound application improvements in Python,
particularly two-dimensional gaming ventures. Thus, it is generally
utilized by both beginner and expert Python game engineers. Pygame
utilizes the SDL (Simple DirectMedia Layer) library. Like the SDL library,
Pygame library is profoundly convenient and subsequently offers help for
a wide number of stages and working frameworks.
It is conceivable to port applications created utilizing Pygame on Android-
fueled gadgets, as well as cell phones and tablets. For this very reason,
pgs4a (Pygame subset for Android) should be utilized.
Salient Features:
· Doesn't request OpenGL.
· Simple for utilizing multi-center CPUs.
· No GUI required for utilizing every single accessible capacity.
· Provides support for a wide scope of stages and working frameworks.
· Simple to utilize.
· Uses Assembly code and advanced C code for actualizing center
capacities.
Bokeh
An instinctive portrayal library for the Python programming language,
Bokeh grants imagining data in a stunning and critical course inside
contemporary web programs. The data portrayal library encourages the
creation of dashboards, data applications, and keen plots.
Despite offering brief and lovely improvement of versatile plans, the
Bokeh library extends its capacity with tip top knowledge over spilling or
tremendous datasets.
Salient Features:
• Authentic plots with clear headings can be built easily
without complexity.
• Bokeh portrayals can be successfully introduced into two
of the most standard Python frameworks: Django and Flask.
• Capable of making dazzling and natural data recognitions
Multiple language ties (Julia, Lua, Python, and R).
Asyncio
This library is used for composing simultaneous code utilizing the
async/anticipates grammar by the developers. In larger part, the asyncio
library is perfect for IO-bound and elevated level organized system code.
Asyncio has been utilized for different Python nonconcurrent frameworks
that offer database association libraries, circulated undertaking lines, elite
system and web servers, and significantly more. The library accompanies
various elevated level and low-level APIs.
Salient Features
• Implementation of protocols by employing transport.
• Codes are simple and easy.
• Helps in generation of various loops.
7.3 Python Data Science Frameworks
Python frameworks provide a great utility to developers because they are
considered a necessary time saving tools. They allow software engineers
to deliver products quicker by providing a ready-made structure for
application development, and by reducing the number of code.
Frameworks enable developers to be quick and responsive for the
development of applications. They also allow the software engineers to
reduce the number of codes employed.
Types of Python Frameworks
Full-Stack Framework
Full stack framework gives developers the utility of a one stop solution.
These are as follows:
• Django
• Pyramid
• Turbo gears
• Web2py
• Cubicweb
• Giotto
• Pylon
Django
Django is one of the most exceptional and adaptable Python frameworks
used. This full-stack, open-source framework focuses on decreasing the
improvement of web application time. It achieves this through an open
source system. The system is continually releasing new modules and code
to unravel the methodology.
Django has multiple modules with an arrangement to access outside
libraries’ functions. It is very popular framework because of its large
quantity of functions. Programmers prefer to use this framework in their
programs as it is really supportive. They wish to promote it among the
programmers specialists to improve its open-source libraries innovation
and access.
Salient Features
• Large amount of readily available libraries.
• Web servers get support and assistance with it.
• Allows mandatory URL routing.
TurboGears
This framework is also open source and expects to make web application
progression a much smoother and faster process.
The framework relies upon Ruby on the Rails and was built using the
model-see controller plan. It empowers creators to re-reason business
basics across stages and abatement the proportion of made code.
Creators would like to release a "negligible mode" later on, which will fill
in as a smaller scale framework. This stripped-down variation will enable
experts to build direct programming quickly, and save time and cash.
Salient Features:
• Function decorators: All features are implemented.
• Has command-line.
• Integration with MochiKit JavaScript library.
• Supports Multi-databases.
• Architecture MVC-style.
Pyrami d
Pyramid is an ultra-versatile, lightweight Python framework. Developers
as a rule use Pyramid to get basic web applications completely operational
as quickly as possible.
The marketing behind Pyramid implies the framework is "the starting
close to nothing, complete gigantic, stay finished framework." It functions
as shown by the standard of control, which makes it a mind blowing
elective for experienced specialists.
Salient Features:
• Versatility in authorization.
• Gives decorators for functions.
• Built in renderers available.
CubicWeb
Designed and generated by Logilab, CubicWeb is an allowed to-utilize,
semantic, open-source, Python-based web system. In view of the
information model, CubicWeb requires the equivalent characterized so as
to build up a useful application.
Not at all like other famous Python structures that utilization separate
perspectives and models; has CubicWeb utilized block. Various 3D shapes
are then consolidated for making an occasion with the assistance of a
database, a web server, and some design documents.
Salient features:
• Support OWL (Web Ontology Language) and RDF
(Resource Description Framework).
• Components are reusable.
• Security work processes.
• Simplifies information related questions with RQL
(Relational Query Language).
• Support for numerous databases.
Giotto
In light of the Model View Controller design, Giotto is an application
system for Python. So as to permit website specialists, web engineers, and
framework administrator’s to work autonomously, Giotto isolates Model,
View, and Controller components.
Giotto incorporates controller modules that empower clients to make
applications over the web, IRC (Internet Relay Chat), and order line.
Salient Feature s :
• Automatic URL steering.
• Database steadiness with SQLAlchemy.
• Extremely concise code.
• Functional CRUD designs.
• Generic models and perspectives.
• Inbuilt reserve with help for Memcache and Redis
(Available API for expanding support for different motors).
• Jinja2 for HTML layouts (API accessible for supporting
other format motors).
Arch
Pythons Framework is an open-source Python-put together structure that
concentrations with respect to the quick improvement of utilizations. The
structure is planned by joining probably the best components and
properties of dialects including Perl, Python, and Ruby.
It is accessible although in support mode. A few designers still utilize the
Pylons system because of its capacity to offer an exceptionally adaptable
structure for web improvement. To advance reusability, the full-stack
structure utilizes WSGI (Web Server Gateway Interface).
Salient features:
• HTML form validation and generation.
• Routes.
• Text-based templating.
• URL dispatch.
• URL mapping dependent on Routes setup by means of
WebHelpers.
Micro frameworks
Miniaturized scale frameworks don’t give extra functionalities and
highlights. For example, database deliberation layer, structure approval,
and explicit apparatuses and libraries. Developers utilizing a miniaturized
scale framework includes many codes, and extra necessities, such as:
• Flask
• Bottle
• Cherrypy
• Dash
• Falcon
• Hug
• Morepath
• Pycnic
Flask
It allows the developers to make a secure web application establishment
from where it turns into a potential to utilize any expansions required. The
miniaturized scale framework is perfect with Google App Engine. Tried by
the Sinatra Ruby framework, the miniaturized scale framework requires
Jinja2 layout and Werkzeug WSGI toolbox. Flask is versatile for clients
given its lightweight and measured structure.
Notable Features:
• Built-in quick debugger.
• Inbuilt advancement server.
• Jinja2 templating.
• Support for connecting any ORM.
Bottle
Bottle creates a source record for each application utilizing it. Aside from
the Python Standard Library, Bottle doesn't demonstrate conditions
required for making little web applications.
Out of the numerous preferences of utilizing Bottle, the real one is that it
enables developers to work near the equipment. Notwithstanding building
short-sighted individual use applications, Bottle is an adept fit for learning
the association of web frameworks and prototyping.
Salient Features:
• Adapter support for outsider format motors and
WSGI/HTTP servers
• Plugin support for various databases
• Gives demand dispatching courses having URL-parameter
support
CherryPy
CherryPy is a remarkable open-source, object-oriented Python framework.
Any CherryPy-controlled web application is a free Python application with
its very own embedded multi-hung web server and continues running on
any OS with assistance for Python.
There is no prerequisite for an Apache server for running applications
made using CherryPy. The little scale framework allows the developer(s)
to use any advancement for data and templating.
Salient features
• A number of out-of-the-case instruments for affirmation,
saving, encoding, sessions, static substance, and significantly
more
• A versatile understood module framework
• Consideration, profiling, and testing is done with the help
of in-built support.
• Offers straightforwardness for running different HTTP
servers simultaneously
• It has a robust structure framework
Dash
Run is an open-source Python-based structure for structure insightful
applications based on the web. This framework is ideal for data analysts
that aren't into the mechanics of web improvement.
Salient Features:
• No standard code for starting
• Customization is of high level
• It contains support of plugins.
• it hase a simple interface for tying UI controls, including
dropdowns, outlines, and sliders
• URL coordinating (Dash Deployment Server)
Falcon
Falcon is a widely used Python structure across the world. It is micro-
framework that enables HTTP and REST models for licensed Python
programmers.
As indicated by the benchmark test-driven by Sanic, Falcon can manage
more requests than all other micro-frameworks. The Python framework
intends to have 100% code incorporation. Bird of prey is used by
tremendous players like LinkedIn, OpenStack, and RackSpace.
Salient Features:
▪ An extensible, incredibly streamlined code
base.
▪ DRY requesting planning through middleware
sections and catches.
▪ Extra speed help with Cython support.
▪ Unit testing by means of WSGI assistants and
ridicules
Hug
The Hug is intended to enable Python engineers to build up an API. The
Python structure streamlines API improvement through multiple methods
for offering various interfaces. It is marked as the quickest web structure
for Python 3.
Whether you are doing neighborhood advancement or over HTTP or using
the CLI, Hug gives you a chance to finish application improvements
rapidly and effectively. To take execution to the next level, Hug devours
assets just when required and uses Cython for arrangement.
MorePath
It is marked as the "Too Powered Python Web Framework,". MorePath
guarantees insignificant arrangement impression. It is planned explicitly
for getting the vast majority of the run of the mill go through cases and
running ASAP, including the regular Python data structures being initiated
into RESTful Web Services.
The micro framework, MorePath, is a genuinely adaptable model-driven
web system.
Salient features:
• All perspectives are conventional.
• Comes with all the essential apparatuses to create restful web
administrations
• Creating conventional UIs is as basic as subclassing
• Extensible with a straightforward, lucid, and general expansion and
abrogate instrument
• Flexible, straightforward, and amazing authorizations
Pycnic
Pycnic is an object orriented micro framework accepted to be the quickest
for structure JSON-based APIs. The little, independent, and streamlined
for JSON-based APIs system can hold its ground well among enormous
players. Since Pycnic makes only the Web APIs, it has a negligible
impression and in this manner, it is quick.
Salient features:
• Built-in blunder dealing with
• Capable of taking care of JSON-based solicitations
• Handles routing
3. Asynchronous Framework
An asynchronous framework is a microframework that permits for
handling a broad set of concurrent connections. Usually, an asynchronous
framework made for Python utilizes the programming language’s asyncio
library.
• Sanic
• Tornado
• Growler
Tornado
The Tornado is an open-source Python system and a non-concurrent
organizing library. It has multiple features that focus on authentication and
authorization processes. While settling the C10k issue (which intends to
deal with 10k associations at some random time), the unique structure
utilizes a non-blocking system I/O.
The Python system was initially made for an organization called
FriendFeed, which was procured by Facebook in 2009. The Tornado is
considered as a perfect device for structure applications requesting
superior and a few thousand simultaneous clients.
Salient Features
• Permits the implementation of 3rd-party authentication and
authorization schemes
• Provides high-quality output
• Real-time services
• Supports translation and localization
• User authentication support
• It has Web Templating
Growler
Aspired by the NodeJS and the Express/Connect systems, Growler is a
small-scale web structure composed on the Python's asyncio library. In
contrast to other ordinary Python systems, demands in Growler aren't
taken care of in the structure.
A top decision among Python systems for effectively and rapidly
actualizing complex applications, Growler was initially created to figure
out how to utilize asyncio library at its most reduced levels.
Salient Features
• Easy to use to montor the flow of program
• Supportive to open source packages
• Syntax of code is clean as it uses decorators
AIOHTTP
It is a dominant Python framework that has unique features: async and
awaits. It uses asyncio library, that’s why it is known as an asynchronous
framework. It is both a server and client framework.
Salient Features
• Allows effectively building the views
• Middle-wares support
• Pluggable routing
• Best Signals
CHAPTER 8: Python Interpreters, Compilers,
IDEs and Text Editor
Python interpreters, compilers, IDEs and Text Editor play a mandatory
role in Python programming. It has multiple applications to execute major
complex calculations in a very simplified method.
8.1 Python Interpreters
In Python, many interpreters work to align, manipulate and refine the
programming codes. Python is employed and executed in different ways.
Python programming is carried out with the help of a large quantity of
interpreters. This high-level programming language is very easy to
understand and execute.
It is depicted as a program that executes the guidelines written as codes.
Execution is done directly so it can be said there is no need for the
guidelines to be put into any programming software.
The following is a list of best interpreters used in Python programming
language:
Interpreter- CPython:
It supports up to 3.7 Version of Python. CPython is the commonly
available interpreter of Python language. It provides an outside capacity
for many software.
CPython can be named a compiler.
It is very supportive to all platforms and provides a smooth experience to
all users. This interpreter is famous because of the high demands of the
software engineers, professionals, and computer language experts.
Interpreter- IronPython
IronPython is one of the most utilized interpreter of the Python language.
It was generated by Jim Hugunin and was responsible for its upgrading to
Version 1.0 that got released in 2006. After Version 1.0, it has been
maintained by Microsoft. IronPython has numerous features, with the
most prominent one is that it is completely written in C language. Most of
the codes are automatically generated with the help of a code generator
that is written in Python.
IronPython interpreter has affiliation with two libraries: Python and .NET
framework. It possesses tools that directly attach it with visual studio.
This feature of IronPython is quite a unique one, and due to this, it is
highly demanded by program developers as it gives them the utility of
visual studio as well. The console of Python is also very interactive.
Moreover, it allows dynamic ways to interpret other languages
Interpreter- Jython
Jython is an interpreter that was formerly called JPython. Jython is
implemented on the platform of Java. Jython was developed in late 1990's
to change C with Java for enhanced performance. Jython contains
excellent specifications and features. It has the function of dynamic and
static compiling that allows software engineers to perform multiple tasks.
Program in Jython utilizes Java scripts and modules rather than using the
modules of Python.
Another salient feature of Jython is that it links the Python database with
Java Virtual Machine.
Jython permits the users to import any Java class, like Python module.
Developers can write codes first in Java and then transform it to Python.
Due to this ability, it is considered as one of the top choices of developers
throughout the world.
Interpreter-PyPy
Pypy is very quick and is used as an alternative for Python language. It
was created in 2002. Its primary feature is that its closely related to
CPython in context to execution and display. Python latest version is
speedier than CPython. One primary reason for that is CPython acts only
as an interpreter, and PyPy can also be utilized as a compiler. It is more
flexible, versatile, and efficient than CPython, and supports many codes
for Python language as well as other languages. Pypy also gives support to
dynamic languages. That's why It is favorite to all programmers.
Interpreter- Stackless Python
Stackless Python is another efficient type of interpreter. It was released in
1998. It supports up to Python Version 3.7. It avoids using C stack.
Stackless Python has a predominant feature of micro-threads. The feature
allows avoiding the burden of the overhead associated with the standard
operating system threads. Stackless Python assists with communication
channels and routine tasks scheduling. Stackless Python is used in the
programming of games. Various Python libraries also utilize it. Most of
the Stackless Python features have resemblance with Pypy, as well.
Facebook
Facebook is keen in utilizing Python in their Production Engineering
Department.
Instagram
Instagram’s engineering team revealed in 2016 that the world's most
massive deployment of the Django web framework driven by them is
completely written in Python.
Netflix
Netflix utilizes Python in a very similar manner to Spotify, depending on
the language to power its data analysis on the server-side.
Dropbox
This cloud-based storage system employs Python in its desktop client.