Professional Documents
Culture Documents
Ilovepdf - Merged (3) - Pagenumber
Ilovepdf - Merged (3) - Pagenumber
Session: -2021-22
SUBMITTED BY:
Nirmal Mali
18EAREC058
1
CERTIFICATE
This is to certify that the INDUSTRIAL TRAINING report entitled “PYTHON (BASIC
& ADVANCE)” is submitted by NIRMAL MALI (18EAREC058), Final Year VII
semester in partial fulfillment of the degree of Bachelor of Technology in Electronics &
Communication Engineering of Rajasthan Technical University, Kota during the academic
year 2021-22. The work has been found satisfactory and is approved for submission.
(TRAINING INCHARGE)
1
2
2
3
CANDIDATE’S DECLARATION
I hereby declare that the work, which is being presented in the Industrial training
Report, entitled “PYTHON (BASIC & ADVANCE)” is in partial fulfillment for the
requirement of the course of “Bachelor of Technology” in Deptt. Of Electronics &
Communication Engineering and submitted to the Department for a record of my own
studies carried under the guidance of Er. MANISH KUMAR GUPTA, Department of
Electronics & Communication Engineering, Arya College of Engineering & I.T. Jaipur.
I have not submitted the matter presented in the report anywhere for the requirement of
any other course.
NIRMAL MALI
18EAREC058
Arya College of Engineering & I.T. ,Kukas (Jaipur)
(TRAINING INCHARGE)
3
4
ACKNOWLEDGEMENT
I would like to extend my sincere gratitude to Mr. Manish Kumar Gupta (Seminar
Coordinator, Deptt. Of ECE) for this opportunity he had provided me. He has been a
great source of inspiration for all of us and his experience and knowledge have helped
us in learning and giving this seminar shape it was assumed.
I am indebted to Dr. Rahul Shrivastava (Head of ECE Department), for providing all
required resources for the completion of the seminar.
I express my thanks to all staff members and friends for all the help and coordination
and to the authors of references and other literature referred to in this seminar.
Last but not least, I am very much thankful to my parents who guided me in every step
which I took.
NIRMAL MALI
18EAREC058
4
5
ABSTRACT
The objective of a practical training is to learn something about industries practically and
to be familiar with the working style of a technical worker to adjust simply according to
the industrial environment . This report deals with the equipment , their relation, and
their general operating principle . Python , an interpreted language which was developed
by Guido van Rossum came into implementation in 1989. The language supports both
object- oriented and procedure - oriented approaches . Python is designed to be a highly
extensible language .
Python works on the principle of " there is only one obvious way to do a task " rather
than " there is more than one way to solve a particular problem " . Python is very easy to
learn and implement .
The simpler syntax , uncomplicated semantics and approach with which Python has
been developed makes it very easier to learn . A large number of python
implementations and extensions have been developed since its inception . Training
Cover provides both six weeks as well as six months industrial training in Python . Python
is divided into two parts as " Core Python " and " Advance Python " .of Accordingly , all
the basic and advanced topics are discussed in both of the modules
5
6
Table Of Contents
Chapter - 1 9
Introduction To Python 9
1.1 Python 9
1.2 Scripting Language 9
1.3 Object-Oriented Programming Language 9
1.4 History 10
1.5 Behind the scene of Python 10
Chapter - 2 11
Data Type in Python 11
2.1 Data Types 11
2.2 Variables 11
2.3 Strings 12
2.4 Creating Strings 12
2.5 Python Operators 13
Arithmetic Operator 13
Comparison Operator 14
Logical Operator 15
Chapter - 3 16
Tuples and Lists 16
3.1 Tuples 16
3.2 Accessing Values in Tuples 16
3.3 Basic Tuples Operations 17
3.4 Built-in Tuple Functions 17
3.5 List 18
3.6 Accessing Values in Lists: 19
3. 7 Basic List Operation 20
3. 8 Built-in List Functions & Methods: 20
Chapter - 4 23
Loops 23
4.1 Loops 23
4. 2 Examples: 24
4. 3 Conditional Statements: 25
4. 4 Functions 27
Chapter - 5 29
Scope of Python 29
5.1 What Can We do With Python? 29
5.2 Who Uses Python Today? 29
5.3 Why Do People Use Python? 30
7
Chapter - 6 31
Numpy 31
6.1 Introduction to Numpy 31
6.2 Operations using NumPy 31
6.3 Numpy -the ndarray Object 31
6.4 Operations on Numpy array 32
6.4.1 Aithmatic Operation 32
6.4.2 Transposing 34
6.4.3 Sorting 34
6.4.4 Concatenating Arrays 34
6.4.5 Reshaping Arrays 35
6.4.6 Reshaping Arrays 35
6.5 Indexing and Slicing of NumPy array 36
6.5.1 Indexing 36
6.5.2 Slicing 37
Chapter - 7 38
Pandas Library 38
7.1 Introduction to Pandas 38
7.2 Data Structures in Pandas 38
7.2.1 Series 39
7.2.1.1 Creation of Series 39
7.2.1.2 Indexing of Series 40
7.2.1.3 Slicing of Series 40
7.2.2 DataFrame 41
7.2.2.1 Creation of DataFrame 42
7.2.2.2 Indexing of DataFrame 42
7.3 Advantages of Pandas 43
Chapter - 8 44
Final Project - OpenMF 44
8.1 Project Introduction 44
8.2 Project Architecture 44
8.3 Project Components 44
8.3 Database design diagram 45
8.4 Project Demo 45
8.5 Technology Used 46
8
FIGURE INDEX
9
Chapter - 1
Introduction To Python
1.1 Python
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. Its
design philosophy emphasizes code readability, and its syntax allows programmers to express concepts
in fewer lines of code than would be possible in languages such as C++ or Java. The language provides
constructs intended to enable clear programs on both a small and large scale.
Python supports multiple programming paradigms, including object-oriented, imperative, and
functional programming or procedural styles. It features a dynamic type system and automatic memory
management and has a large and comprehensive standard library. Python interpreters are available for
installation on many operating systems, allowing Python code execution on a wide variety of systems.
10
can access and often modify the data fields of the object with which they are associated (objects have a
notion of "this" or "self").
In OO programming, computer programs are designed by making them out of objects that interact
with one another. There is significant diversity in object-oriented programming, but most popular
languages are class-based, meaning that objects are instances of classes, which typically also
determines their type.
1.4 History
Python was conceived in the late 1980s, and its implementation was started in December 1989 by
Guido van Rossum at CWI in the Netherlands as a successor to the ABC language (itself inspired by
SETL) capable of exception handling and interfacing with the Amoeba operating system. Van Rossum
is Python's principal author, and his continuing central role in deciding the direction of Python is
reflected in the title given to him by the Python community, benevolent dictator for life (BDFL).
“Python is an experiment in how much freedom programmers need. Too much freedom and nobody
can read another's code; too little and expressiveness is endangered.”
10
11
Chapter - 2
Data types determine whether an object can do something, or whether it just would not make sense.
Other programming languages often determine whether an operation makes sense for an object by
making sure the object can never be stored somewhere where the operation will be performed on the
object (this type of system is called static typing). Python does not do that. Instead, it stores the type of
an object with the object, and checks when the operation is performed whether that operation makes
sense for that object (this is called dynamic typing).
Python has many native data types. Here are the important ones:
2.2 Variables
Variables are nothing but reserved memory locations to store values. This means that when you create
a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored
in the reserved memory. Therefore, by assigning different data types to variables, you can store
integers, decimals or characters in these variables.
Ex:
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
11
12
name = "John" # A string
2.3 Strings
In programming terms, we usually call text a string. When you think of a string as a collection of
letters, the term makes sense.
All the letters, numbers, and symbols in this book could be a string.
For that matter, your name could be a string, and so could your address.
In Python, we create a string by putting quotes around the text. For example, we could take our
otherwise useless
• len("hello") 5 # size
12
13
2.5 Python Operators
Arithmetic Operator
+2
13
14
** Exponent - left operand raised to the power of x**y (x to the power
right y)
Comparison Operator
> Greater that - True if the left operand is greater than the right x>y
< Less that - True if the left operand is less than the right x<y
> Greater than or equal to - True if the left operand is greater than or x >=
= equal to the right y
< Less than or equal to - True if the left operand is less than or equal to x <=
= the right y
14
15
Logical Operator
15
16
Chapter - 3
3.1 Tuples
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The
differences between tuples and lists are, the tuples cannot be changed, unlike lists and tuples that use
parentheses, whereas lists use square brackets.
Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these
comma-separated values between parentheses also. For example −
tup2 = (1, 2, 3, 4, 5 );
To access values in tuple, use the square brackets for slicing along with the index or indices to obtain
value available at that index. For example −
tup2 = (1, 2, 3, 4, 5, 6, 7 );
tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
16
17
3.3 Basic Tuples Operations
Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here
too, except that the result is a new tuple, not a string. In fact, tuples respond to all of the general
sequence operations we used on strings in the prior chapter −
17
18
1 cmp(tuple1, tuple2) Compares elements of both tuples.
3.5 List
The list is a most versatile datatype available in Python which can be written as a list of
comma-separated values (items) between square brackets. Important thing about a list is that items in a
list need not be of the same type.
Creating a list is as simple as putting different comma-separated values between square brackets. For
example −
list2 = [1, 2, 3, 4, 5 ];
Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so on.
18
19
3.6 Accessing Values in Lists:
To access values in lists, use the square brackets for slicing along with the index or indices to obtain
value available at that index. For example −
list2 = [1, 2, 3, 4, 5, 6, 7 ];
Output:list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
print list[2]
list[2] = 2001;
print list[2]
print list1
del list1[2];
19
20
print list1
20
21
1 cmp(list1, list2) Compares elements of both lists.
21
22
5 list.insert(index, obj) Inserts object obj into list at offset index
22
23
Chapter - 4
Loops
4.1 Loops
Programming languages provide various control structures that allow for more complicated execution
paths.
A loop statement allows us to execute a statement or group of statements multiple times. The following
diagram illustrates a loop statement −
Python programming language provides the following types of loops to handle looping requirements.
23
24
while loop Repeats a statement or group of statements while a given
condition is TRUE. It tests the condition before executing
the loop body.
nested loops You can use one or more loops inside any another while, for,
or do..while loop.
4. 2 Examples:
For Loop:
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
While Loop:
>>> count = 0
24
25
>>> while (count < 4):
count = count + 1
4. 3 Conditional Statements:
Decision making is the anticipation of conditions occurring while execution of the program and
specifying actions taken according to the conditions.
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcomes. You
need to determine which action to take and which statements to execute if the outcome is TRUE or
FALSE otherwise.
25
26
Python programming language provides the following types of decision making statements. Click the
following links to check their detail.
Statement Description
nested if statements You can use one if or else if statement inside another if
or else if statement(s).
Example:
If Statement:
print “TX
TX
If...Else Statement:
print “TX”
26
27
else:
If...Else...If Statement:
else:
print “Imposter!”
4. 4 Functions
Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses. You can also define
parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation string of the
function.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an expression to the caller.
A return statement with no arguments is the same as a returof n None.
Syntex:
"function_docstring"
function_suite
27
28
return [expression]
Example:
print str
return
print str
return;
28
29
Chapter - 5
Scope of Python
Science
- Bioinformatics
System Administration
-Unix
-Web logic
-Web sphere
-CGI
Testing scripts
● System programming
● Graphical User Interface Programming
● Internet Scripting
● Component Integration
● Database Programming
● Gaming, Images, XML , Robot and more
29
30
● Intel, Cisco, Hewlett-Packard, Seagate, Qualcomm, and IBM use Python for hardware testing.
● ESRI uses Python as an end-user customization tool for its popular GIS mapping products.
● The YouTube video sharing service is largely written in Python.
seem to be these:
· Python is object-oriented
· Indentation
· It's powerful
o Dynamic typing
o Library utilities
· It's portable
30
31
Chapter - 6
Numpy
Using Numpy a developer can perform the following operations: Mathematical and logical operations
on arrays. Fourier transforms and routines for shape manipulation. Operations related to linear
algebra. NumPy has in-built functions for linear algebra and random number generation.
The most important object defined in NumPy is an N-dimensional array type called ndarray. It
describes the collection of items of the same type. Items in the collection can be accessed using a
zero-based index. Every item in an ndarray takes the same size of the block in the memory. Each
element in ndarray is an object of the data-type object (called dtype). Any item extracted from ndarray
object (by slicing) is represented by a Python object the t of one of array scalar types. The following
diagram shows a relationship between ndarray, data type object (dtype) and array scalar type:
31
32
An instance of ndarray class can be constructed by different array creation routines described later in
the tutorial. The basic ndarray is created using an array function in NumPy as follows:
>> numpy.array
It creates an ndarray from any object exposing array interface, or from any method that returns an
array.
Arithmetic operations on NumPy arrays are fast and simple. When we perform a basic arithmetic
operation like addition, subtraction, multiplication, division etc. on two arrays, the operation is done on
each corresponding pair of elements. For instance, adding two arrays will result in the first element in
the first array to be added to the first element in the second array, and so on. Consider the following
element-wise operations on two arrays:
32
33
>>> array2 = np.array([[10,20],[15,12]])
#Element-wise addition of two matrices
>>> array1 + array2
array([[13, 26], [19, 14]])
#Subtraction
>>> array1 - array2
array([[ -7, -14], [-11, -10]])
#Multiplication
>>> array1 * array2
array([[ 30, 120], [ 60, 24]])
#Matrix Multiplication
>>> array1 @ array2
array([[120, 132], [ 70, 104]])
#Exponentiation
>>> array1 ** 3
array([[ 27, 216], [ 64, 8]], dtype=int32)
#Division
>>> array2 / array1
array([[3.33333333, 3.33333333], [3.75 , 6. ]])
It is important to note that for element-wise operations, size of both arrays must be same. That is,
array1.shape must be equal to array2.shape.
33
34
6.4.2 Transposing
Transposing an array turns its rows into columns and columns into rows just like matrices in
mathematics.
#Transpose
>>> array3 = np.array([[10,-7,0, 20], [-5,1,200,40],[30,1,-1,4]])
>>> array3
array([[ 10, -7, 0, 20], [ -5, 1, 200, 40], [ 30, 1, -1, 4]])
6.4.3 Sorting
Sorting is to arrange the elements of an array in hierarchical order either ascending or descending. By
default, numpy does sorting in ascending order.
Concatenation means joining two or more arrays. Concatenating 1-D arrays means appending the
sequences one after another. NumPy.concatenate().
function can be used to concatenate two or more 2-D arrays either row-wise or column-wise. All the
dimensions of the arrays to be concatenated must match exactly except for the dimension or axis along
which they need to be joined. Any mismatch in the dimensions results in an error. By default, the
concatenation of the arrays happens along axis=0.
34
35
>>> array1
array([[ 10, 20], [-30, 40]])
>>> array2
array([[0, 0, 0], [0, 0, 0]])
>>> array1.shape
(2, 2)
>>> array2.shape
(2, 3)
We can modify the shape of an array using the reshape() function. Reshaping an array cannot be used
to change the total number of elements in the array. Attempting to change the number of elements in
the array using reshape() results in an error.
>>> array3 = np.arange(10,22)
>>> array3
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
We can split an array into two or more subarrays. numpy.split() splits an array along the specified axis.
We cthe an either specify sequence of index values where an array is to be split; or we can specify an
integer N, that indicates the number of equal parts in which the array is to be split, as parameter(s) to
the NumPy.split() function. By default, NumPy.split() splits along axis = 0. Consider the array given
below:
>>> array4
array([[ 10, -7, 0, 20], [ -5, 1, 200, 40], [ 30, 1, -1, 4], [ 1, 2, 0, 4], [ 0, 1, 0, 2]])
# [1,3] indicate the row indices on which
# to split the array
>>> first, second, third = numpy split(array4, [1, 3])
35
36
# array4 is split on the first row and
# stored on the sub-array first
>>> first array([[10, -7, 0, 20]])
6.5.1 Indexing
For 2-D arrays indexing for both dimensions start from 0, and each element is referenced through two
indexes i and j, where I represent the row number and j represents the column number.
Consider the above Table showing marks obtained by students in three different subjects. Let us create
an array called marks to store marks given in three subjects for four students given in this table. As
there are 4 students (i.e. 4 rows) and 3 subjects (i.e. 3 columns), the array will be called marks[4][3].
This array can store 4*3 = 12 elements. Here, marks[i,j] refers to the element at (i+1)th row and (j+1)th
36
37
column because the index values start at 0. Thus marks[3,1] is the element in the 4th row and second
column which is 72 (marks of Prasad in English).
# accesses the element in the 1st row in
# the 3rd column
>>> marks[0,2] 56
>>> marks [0,4]
index Out of Bound "Index Error".
Index 4 is out of bounds for axis with size 3
6.5.2 Slicing
Sometimes we need to extract part of an array. This is done through slicing. We can define which part
of the array to be sliced by specifying the start and end index values using [start: end] along with the
array name.
>>> array8
array([-2, 2, 6, 10, 14, 18, 22])
37
38
Chapter - 7
Pandas Library
Python libraries contain a collection of built-in modules that allow us to perform many actions without
writing detailed programs for it. Each library in Python contains a large number of modules that one
can import and use. NumPy, Pandas, and Matplotlib are three well-established Python libraries for
scientific and analytical use. These libraries allow us to manipulate, transform and visualize data easily
and efficiently. NumPy, which stands for ‘Numerical Python’, is a library. I. It is a package that can be
used for numerical data analysis and scientific computing. NumPy uses a multidimensional array
object and has functions and tools for working with these arrays. Elements of an array stay together in
memory, hence, they can be quickly accessed. PANDAS (PANel DAta) is a high-level data
manipulation tool used for analyzing data. It is very easy to import and export data using the Pandas
library which has a very rich set of functions. It is built on packages like NumPy and Matplotlib and
gives us a single, convenient place to do most of our data analysis and visualization work. Pandas have
three important data structures, namely – Series, DataFrame, and Panel to make the process of
analyzing data organized, effective and efficient.
38
39
7.2.1 Series
A Series is a one-dimensional array containing a sequence of values of any data type (int, float, list,
string, etc) which by default have numeric data labels starting from zero. The data label associated with
a particular value is called its index. We can also assign values of other data types as indexes. We can
imagine a Pandas Series as a column in a spreadsheet. An example of a series containing names of
students is given below:
There are different ways in which a series can be created in Pandas. To create or use series, we first
need to import the Pandas library.
A Series can be created using scalar values as shown in the example below:
Observe that output is shown in two columns - the index is on the left and the data value is on the right.
If we do not explicitly specify an index for the data values while creating a series, then by default
indices range from 0 through N – 1. Here N is the number of data elements. We can also assign
user-defined labels to the index and use them to access elements of a Series. The following example
has a numeric index in random order.
39
40
Here, data values Kavi, Shyam, and Ravi have index values 3, 5, and 1, respectively. We can also use
letters or strings as indices, for example,
Indexing in Series is similar to that for NumPy arrays and is used to access elements in a series.
Indexes are of two types: positional index and labeled index. The positional index takes an integer
value that corresponds to its position in the series starting from 0, whereas the labeled index takes any
user-defined label as an index.
The following example shows the usage of the positional index for accessing a value from a Series.
Here, the value 30 is displayed for the positional index 2. When labels are specified, we can use labels
as indices while selecting values from a Series, as shown below. Here, the value 3 is displayed for the
labelled index Mar.
>>> seriesMnths = pd.Series([2,3,4],index=["Feb ","Mar","Apr"])
>>> seriesMnths["Mar"]
3I
Sometimes, we may need to extract a part of a series. This can be done through slicing. This is similar
to slicing used with NumPy arrays. We can define which part of the series is to be sliced by specifying
40
41
the start and end parameters [start: end] with the series name. When we indices for slicing, the value at
the endindex position is excluded, i.e., only (end - start) number of data values of the series are
extracted. Consider the following series seriesCapCntry:
As we can see in the above output, only data values at indices 1 and 2 are displayed. If labeled indexes
are used for slicing, then the value at the end index label is also included in the output, for example,
7.2.2 DataFrame
Sometimes we need to work on multiple columns at a time, i.e., we need to process the tabular data.
For example, the result of a class, items in a restaurant’s menu, reservation chart of a train, etc. Pandas
store such tabular data using a DataFrame. A DataFrame is a two-dimensional labeled data structure
like a table of MySQL. It contains rows and columns and therefore has both a row and column index.
Each column can have a different type of value such as numeric, string, boolean, etc., as in tables of a
database.
41
42
7.2.2.1 Creation of DataFrame
There is a number of ways to create a DataFrame. Some of them are listed in this section.
Consider the following three NumPy ndarrays. Let us create a simple DataFrame without any column
labels, using a single ndarray:
We can use slicing to select a subset of rows and/or columns from a DataFrame. To retrieve a set of
rows, slicing can be used with row labels. For example:
42
43
Here, the rows with labels Maths and Science are displayed. Note that in DataFrames slicing is
inclusive of the end values. We may use a slice of labels with a column name to access values of those
rows in that column only. For example, the following statement displays the rows with the label Maths
and Science, and the column with the label Arnab:
We may use a slice of labels with a slice of column names to access values of those rows and columns:
1. Data representation
2. Less writing and more work done
3. An extensive set of features
4. Efficiently handles large data
5. Makes data flexible and customizable
6. Fast as build upon NumPy.
43
44