Uploaded by vixacir238

industrial-training-report-inder

advertisement
lOMoARcPSD|34288278
Industrial Training Report Inder
B.tech (Dr. A.P.J. Abdul Kalam Technical University)
Studocu is not sponsored or endorsed by any college or university
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Industrial Training Report
On
PYTHON PROGRAMMING
Submitted in Partial Fulfillment of the Requirements for the Degree of
BACHELOR OF TECHNOLOGY
In
COMPUTER SCIENCE & ENGINEERING
Submitted By
INDER BARTHWAL
(University Roll no. 1709510017)
College Lo
Amazing Training Basket Pvt. Ltd. Plot no: A-40,
Unit No: - 207 & 301, 2nd & 3rd Floor, I-Thum Tower-A, Sector 62, Noida – 201309
Under the supervision of
CHARLES RUSSELL SEVERANCE
Charles Russell Severance
Submitted To: SHALU GUPTA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
MGM’s College of Engineering & Technology, Noida
1|Page
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
DECLARATION
I, Inder Barthwal (1709510017), hereby declare that the work which is being presented in the
project report “Python Programming” is the record of authentic work to be carried out by me
in the partial fulfilment for the award of degree “Bachelor of Technology in Computer Science
and Engineering”, this work has not been submitted to any other university/institute for the
award of any degree/diploma.
Inder Barthwal
(1709510017)
Shalu Gupta
(Project guide)
2|Page
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
CERTIFICATE
3|Page
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
ACKNOWLEDGEMENT
I have taken efforts in this project. However, it would not have been possible without the kind
support and help of many individuals and organizations. I would like to extend my sincere
thanks to all of them. I am highly indebted to Coursera and my college MGM CoET for their
guidance and constant supervision as well as for providing necessary information regarding the
course & also for their support in completing the course.
•
I would like to express my gratitude towards my parents & professor of University of
Michigan, Mr. Charles Russell Severance and my project guide Mrs. Shalu Gupta
for their kind co-operation and encouragement which help me in completion of this
course. I would like to express my special gratitude and thanks to industry persons for
giving me such attention and time.
4|Page
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
ABSTRACT
The objective of a practical training is to learn something about industries practically and to be
familiar with a working style of a technical worker to adjust simply according to industrial
environment. This report deals with the equipment their relation and their general operating
principle.
Python, an interpreted language which was created by Guido van Rossum, and released in
1991.The language supports both object oriented and procedural oriented approach. 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 Covers seven weeks industrial training in Python.
5|Page
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
TABLE OF CONTENT
DECLARATION………………………………………………………2
CERTIFICATE………………………………………………………...3
ACKNOWLEDGEMENT…………..…………………………………4
ABSTRACT……………………………………………………………5
1. INTRODUCTION
1.1. What is Python…………………………………………………7
1.2. History…………………………………………………………7
1.3. Python Feature…………………………………………………8
2. Operator……………………………………………………………9
3. Data Structure In Python
3.1. List……………………………………………………………16
3.2. Tuple…………………………………………………………19
3.3. Dictionary……………………………………………………21
4. Functions…………………………………………………………25
5. Hands On With Python………………………………………… 27
6. References……………………………………………………… 32
6|Page
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
INTRODUCTION
1. PYTHON
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python
is designed to be highly readable. It uses English keywords frequently where as other languages
use punctuation, and it has fewer syntactical constructions than other languages.
•
Python is Interpreted − Python is processed at runtime by the interpreter. You do not need
to compile your program before executing it. This is similar to PERL and PHP.
•
Python is Interactive − You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
•
Python is Object-Oriented − Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
•
Python is a Beginner's Language − Python is a great language for the beginner-level
programmers and supports the development of a wide range of applications from simple text
processing to WWW browsers to games.
2. History of Python
Python was developed by Guido van Rossum in the late eighties and early nineties at the
National Research Institute for Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68,
SmallTalk, and Unix shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the GNU General
Public License (GPL).
7|Page
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Python is now maintained by a core development team at the institute, although Guido van
Rossum still holds a vital role in directing its progress.
3. Python Features
Python's features include −
•
Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax.
This allows the student to pick up the language quickly.
•
Easy-to-read − Python code is more clearly defined and visible to the eyes.
•
Easy-to-maintain − Python's source code is fairly easy-to-maintain.
•
A broad standard library − Python's bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
•
Interactive Mode − Python has support for an interactive mode which allows interactive
testing and debugging of snippets of code.
•
Portable − Python can run on a wide variety of hardware platforms and has the same interface
on all platforms.
•
Extendable − You can add low-level modules to the Python interpreter. These modules
enable programmers to add to or customize their tools to be more efficient.
•
Databases − Python provides interfaces to all major commercial databases.
•
Scalable − Python provides a better structure and support for large programs than shell
scripting.
8|Page
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Python has a big list of good features
•
It supports functional and structured programming methods as well as OOP.
•
It can be used as a scripting language or can be compiled to byte-code for building large
applications.
•
It provides very high-level dynamic data types and supports dynamic type checking.
•
It supports automatic garbage collection.
•
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
OPERATORS
9|Page
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Arithmetic operators: Python arithmetic operators are used to perform addition, subtraction,
multiplication, and division. They act as basic mathematical operations.
Example:
x = 15
y=4
print('x + y =', x + y)
# Output: x + y = 19
print ('x - y =', x-y)
# Output: x - y = 11
print('x * y =', x*y)
# Output: x * y = 60
Assignment operators: Assignment operators are used in Python to assign values to variables.
a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on
the left.
There are various compound operators in Python like a += 5 that adds to the variable and later
assigns the same. It is equivalent to a = a + 5.
Comparison operators: Comparison operators are used to compare two values.
x = 10
y = 12
print('x > y is', x>y)
# Output: x > y is False
print('x < y is', x<y)
# Output: x < y is True
Logical operators: Comparison operators are used to compare values. It returns either True or
False according to the condition.
10 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Example:
x = True
y = False
print('x and y is', x and y)
#Output: x and y is False
print('x or y is', x or y)
#Output: x or y is True.
Identity operators: is and is not are the identity operators in Python. They are used to check
if two values (or variables) are located on the same part of the memory. Two variables that are
equal does not imply that they are identical.
x1 = 5
y1 = 5
print(x1 is not y1)
# Output: False
Membership Operators: in and not in are the membership operators in Python. They are used
to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.
x = 'Hello world'
y = {1:'a',2:'b'}
print('H' in x)
# Output: True
print(1 in y)
# Output: True
Bitwise Operators: Bitwise operators act on operands as if they were strings of binary digits.
They operate bit by bit, hence the name.
For example, 2 is 10 in binary and 7 is 111.
11 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Operator Precedence In Python
12 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Data Structure In Python
1. LIST
The list is a most versatile datatype available in Python which can be written as a list of commaseparated 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 –
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
Basic List Operations
Lists respond to the + and * operators much like strings; they mean concatenation and repetition
here too, except that the result is a new list, not a string.
Python Expression
Results
Description
len([1, 2, 3])
3
Length
[1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
Concatenation
['Hi!'] * 4
['Hi!', 'Hi!', 'Hi!', 'Hi!']
Repetition
3 in [1, 2, 3]
True
Membership
for x in [1, 2, 3]: print x,
123
Iteration
13 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
List Functions & Methods
Python includes the following list functions −
Sr.No.
1
Functions & Methods with Description
len(list)
Gives the total length of the list.
2
max(list)
Returns item from the list with max value.
3
min(list)
Returns item from the list with min value.
4
list(seq)
Converts a tuple into list.
5
list.append(obj)
Appends object obj to list
6
list.insert(index, obj)
Inserts object obj into list at offset index
7
list.reverse()
Reverses objects of list in place
8
list.sort()
Sorts objects of list
9
list.remove(obj)
Removes object obj from list
14 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
2. TUPLES
A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like
lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and
tuples 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 −
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d"
The empty tuple is written as two parentheses containing nothing −
tup1 = ( )
To write a tuple containing a single value you have to include a comma, even
though there is only one value −
tup1 = (50,)
Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.
Accessing Values in Tuples
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 –
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5)
print(tup1[0])
print(tup2[1:5])
When the above code is executed, it produces the following result −
physics
[2, 3, 4, 5]
Updating Tuples
Tuples are immutable which means you cannot update or change the values of tuple elements.
You are able to take portions of existing tuples to create new tuples as the following example
demonstrates –
tup1 = (12, 34.56)
tup2 = (‘abc’, ‘xyz’)
tup3 = tup1 + tup2
print(tup3)
result of above code is: (12, 34.56, 'abc', 'xyz')
15 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Delete Tuple Elements
Removing individual tuple elements is not possible. There is, of course, nothing wrong with
putting together another tuple with the undesired elements discarded.
To explicitly remove an entire tuple, just use the del statement. For example –
tup1 = ('physics', 'chemistry', 1997, 2000)
print(tup1)
del tup1
print (“After Deleting tup:”)
print tup1
Built-in Tuples Operations
Sr.No.
1
Function with Description
len(tuple)
Gives the total length of the tuple.
2
max(tuple)
Returns item from the tuple with max value.
3
min(tuple)
Returns item from the tuple with min value.
4
tuple(seq)
Converts a list into tuple.
16 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
3. DICTIONARY
Each key is separated from its value by a colon (:), the items are separated by commas, and the
whole thing is enclosed in curly braces. An empty dictionary without any items is written with
just two curly braces, like this: { }.
Keys are unique within a dictionary while values may not be. The values of a dictionary can be
of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
Accessing Values in Dictionary
To access dictionary elements, you can use the familiar square brackets along with the key to
obtain its value. Following is a simple example –
dict = {‘Name’:’Zara’,’Age’:7,’Class’:’First’}
print(dict[‘Name’])
Output is :Zara
Updating Dictionary
You can update a dictionary by adding a new entry or a key-value pair, modifying an existing
entry, or deleting an existing entry as shown below in the simple example −
dict = {‘Name’:’Zara’,’Age’:7,’Class’:’First’}
dict[‘Age’] = 8 #update existing entry
print(dict[‘Age’])
Output is :8
17 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Dictionary Functions & Methods
Python includes the following dictionary functions −
Function & Methods with Description
Sr.No.
1
len(dict)
Gives the total length of the dictionary. This would be equal to the number of items in the
dictionary.
2
str(dict)
Produces a printable string representation of a dictionary
3
type(variable)
Returns the type of the passed variable. If passed variable is dictionary, then it would return a
dictionary type.
4
dict.clear()
Removes all elements of dictionary dict
5
dict.copy()
Returns a shallow copy of dictionary dict
6
dict.get(key, default=None)
For key key, returns value or default if key not in dictionary
7
dict.update(dict2)
Adds dictionary dict2's key-values pairs to dict
8
dict.keys(),dict.values()
Returns list of dictionary dict's keys,dict’s value
18 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
FUNCTIONS IN PYTHON
A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing.
As you already know, Python gives you many built-in functions like print(), etc. but you can also
create your own functions. These functions are called user-defined functions.
Defining a Function
Simple rules to define a function in Python.
•
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 or docstring.
•
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 return None.
Syntax
def functionname( parameters ):
function_suite
return [expression]
Example
def printer(str):
print(str)
return
19 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Calling a Function
Defining a function gives it a name, specifies the parameters that are to be included in the function
and structures the blocks of code.
Once the basic structure of a function is finalized, you can execute it by calling it from another
function or directly from the Python prompt. Following is an example to call
the printer() function.
def printer(str):
print(str)
return
printer(“INDER”)
OUTPUT IS :INDER
Function Arguments
You can call a function by using the following types of formal arguments −
•
Required arguments
•
Keyword arguments
•
Default arguments
20 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
Numeric Matrix Processor
def operations():
while True:
user_choice = int(input("""1. Add matrices\n
2. Multiply matrix by a constant\n
3. Multiply matrices\n
4. Transpose matrix\n
5. Calculate a determinant\n
0. Exit\n
Your choice: """))
if user_choice == 1:
add_matrix()
elif user_choice == 2:
multiply_with_constant()
elif user_choice == 3:
matrix_multiplication()
elif user_choice == 4:
transpose_matrix()
elif user_choice == 5:
dimension = get_dimensions('Enter size of matrix:: ')
matrix = get_matrix(dimension[0], dimension[1], 'Enter matrix:
')
print("The result is: ")
print(calc_determinant(matrix))
elif user_choice == 0:
exit()
def get_matrix(rows, columns, input_text):
print(input_text)
matrix = []
for i in range(rows):
row = input().split()
matrix.append([])
for j in range(columns):
matrix[i].append(dataype(row[j]))
return matrix
def get_dimensions(input_text):
return [int(x) for x in input(input_text).split()]
def add_matrix():
21 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
dimension_1 = get_dimensions('Enter size of first matrix: ')
matrix_1 = get_matrix(dimension_1[0], dimension_1[1], 'Enter first matrix:
')
dimension_2 = get_dimensions('Enter size of second matrix: ')
matrix_2 = get_matrix(dimension_2[0], dimension_2[1], 'Enter second matrix
:')
if dimension_1 == dimension_2:
matrix_3 = []
for i in range(dimension_1[0]):
matrix_3.append([])
for j in range(dimension_1[1]):
matrix_3[i].append(matrix_1[i][j] + matrix_2[i][j])
print_matrix(matrix_3)
else:
print('ERROR')
def multiply_with_constant():
dimension = get_dimensions('Enter size of matrix:: ')
matrix = get_matrix(dimension[0], dimension[1], 'Enter matrix:')
constant = dataype(input('Enter constant: '))
matrix_result = []
for i in range(dimension[0]):
matrix_result.append([])
for j in range(dimension[1]):
matrix_result[i].append(matrix[i][j] * constant)
print_matrix(matrix_result)
def matrix_multiplication():
dimension_1 = get_dimensions('Enter size of first matrix: ')
matrix_1 = get_matrix(dimension_1[0], dimension_1[1], 'Enter first matrix:
')
dimension_2 = get_dimensions('Enter size of second matrix: ')
matrix_2 = get_matrix(dimension_2[0], dimension_2[1], 'Enter second matrix
:')
if dimension_1[1] == dimension_2[0]:
matrix_product = []
for i in range(dimension_1[0]):
matrix_product.append([])
for j in range(dimension_2[1]):
dot_product = 0
for k in range(dimension_2[0]):
dot_product += matrix_1[i][k] * matrix_2[k][j]
matrix_product[i].append(dot_product)
22 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
print_matrix(matrix_product)
def transpose_matrix():
functions = int(input('1. Main diagonal\n'
'2. Side diagonal\n'
'3. Vertical line\n'
'4. Horizontal line\n'
'Your choice: '))
if 1 <= functions <= 4:
dimension_1 = get_dimensions('Enter matrix size: ')
matrix = get_matrix(dimension_1[0], dimension_1[1], 'Enter matrix:')
if functions == 1:
main_daigonal(matrix)
elif functions == 2:
side_diagonal(matrix)
elif functions == 3:
vertical_line(matrix)
elif functions == 4:
horizontal_line(matrix)
def main_daigonal(matrix):
row = len(matrix)
column = len(matrix[0])
transposed_matrix = []
for i in range(row):
transposed_matrix.append([])
for j in range(column):
transposed_matrix[i].append(matrix[j][i])
print_matrix(transposed_matrix)
def side_diagonal(matrix):
row = len(matrix)
column = len(matrix[0])
transposed_matrix = []
for i in range(row):
row = []
for j in range(column):
row.insert(0, matrix[j][i])
transposed_matrix.insert(0, row)
print_matrix(transposed_matrix)
def vertical_line(matrix):
23 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
column = len(matrix[0])
transposed_matrix = []
for i in range(column):
transposed_matrix.append(matrix[i][::-1])
print_matrix(transposed_matrix)
def horizontal_line(matrix):
column = len(matrix[0])
transposed_matrix = []
for i in range(column):
transposed_matrix.insert(0, matrix[i])
print_matrix(transposed_matrix)
def calc_determinant(matrix):
n = len(matrix)
m = len(matrix[0])
det = 0
if n != m:
print("cannot be found")
else:
if n == 1:
det = matrix[0][0]
elif n == 2:
det = matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1]
else:
for j in range(m):
det += matrix[0][j] * calc_cofactor(matrix, 0, j)
return det
def calc_cofactor(matrix, row, col):
return pow((1), row + 1 + col + 1) * calc_determinant(get_minor(matrix, row, col))
def get_minor(matrix, a, b):
rows = len(matrix)
cols = len(matrix[0])
minor_matrix = []
for i in range(rows):
minor_matrix.append([])
for j in range(cols):
if i != a and j != b:
minor_matrix[i].append(matrix[i][j])
24 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
return [row for row in minor_matrix if row]
def print_matrix(matrix):
print('The result is:')
for i in matrix:
print(*i)
def dataype(number):
try:
int(number)
return int(number)
except ValueError:
try:
float(number)
return float(number)
except ValueError:
return None
operations()
OUTPUT IS :-
25 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
lOMoARcPSD|34288278
References:
•
HTTP://COURSERA.ORG
•
HTTP://HYPERSKILL.ORG
•
HTTP://PYTHON.ORG
•
HTTP://JAVATPOINT.ORG
26 | P a g e
Downloaded by grtt rthh (vixacir238@marksia.com)
Download