Srijan Report of Python
Srijan Report of Python
A
Practical Training Report
on
PYTHON LANGUAGE
Submitted in partial fulfillment for the award of degree of
BACHELOR OF TECHNOLOGY
In
Computer Science & Engineering
Submitted to:
Mr. Pradeep Jha
Head of Dept.
Department of Computer Science & Engineering
GLOBAL INSTITUTE OF TECHNOLOGY
JAIPUR (RAJASTHAN)-302022
SESSION: 2022-23
Session 2022-23 i
PYTHON FOR DATA SCIENCE
Certificate
Session 2022-23 ii
PYTHON FOR DATA SCIENCE
Acknowledgement
We are grateful to our respected Dr. I. C. Sharma, Principal GIT for guiding us during
Practical Training period
Without their support and timely guidance, the completion of our Practical Training
would have seemed a farfetched dream. In this respect we find ourselves lucky to have
mentors of such a great potential.
SRIJAN KUMAR
21EGJCS148
Abstract
Session 2022-23 iv
PYTHON FOR DATA SCIENCE
Table Of Contents
Certificate ..................................................................................................................................................... ii
Abstract ........................................................................................................................................................ iv
Table of Content......................................................................................................................................... v
Session 2022-23 v
PYTHON FOR DATA SCIENCE
Session 2022-23 vi
PYTHON FOR DATA SCIENCE
REFERENCES ............................................................................................................ 49
List of Figures
List of Tables
Session 2022-23 ix
PYTHON FOR DATA SCIENCE
Chapter - 1
Introduction
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.
Scripting languages are often interpreted (rather than compiled). Primitives are usually
the elementary tasks or API calls, and the language allows them to be combined into more
complex programs. Environments that can be automated through scripting include
software applications, web pages within a web browser, the shells of operating systems
(OS), embedded systems, as well as numerous games.
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).
Over six years ago, in December 1989, I was looking for a "hobby" programming project
that would keep me occupied during the week around Christmas. My office ... would be
closed, but I had a home Computer, and not much else on my hands. I decided to write an
interpreter for the new scripting language I had been thinking about lately: a descendant
of ABC that would appeal to Unix/C hackers. I chose Python as a working title for the
project, being in a slightly irreverent mood (and a big fan of Monty Python's Flying
Circus).
Easy to use.
High Level.
Dynamically Typed.
Extensible.
Portable.
Chapter – 2
If you don’t already have a copy of Python installed on your computer, you will need to
open up your Internet browser and go to the Python download page
(http://www.python.org/download/).
Now that you are on the download page, select which of the software builds you would
like to download. For the purposes of this article we will use the most up to date version
available (Python 3.4.1).
Once you have clicked on that, you will be taken to a page with a description of all the
new updates and features of 3.4.1, however, you can always read that while the download
is in process. Scroll to the bottom of the page till you find the “Download” section and
click on the link that says “download page.”
Now you will scroll all the way to the bottom of the page and find the “Windows x86
MSI installer.” If you want to download the 86-64 bit MSI, feel free to do so. We believe
that even if you have a 64-bit operating system installed on your computer, the 86-bit
MSI is preferable. We say this because it will still run well and sometimes, with the 64-
bit architectures, some of the compiled binaries and Python libraries don’t work well.
Once you have downloaded the Python MSI, simply navigate to the download location on
your computer, double clicking the file and pressing Run when the dialog box pops up.
If you are the only person who uses your computer, simply leave the “Install for all users”
option selected. If you have multiple accounts on your PC and don’t want to install it
across all accounts, select the “Install just for me” option then press “Next.”
If you want to change the install location, feel free to do so; however, it is best to leave it
as is and simply select next, Otherwise...
Scroll down in the window and find the “Add Python.exe to Path” and click on the small
red “x.” Choose the “Will be installed on local hard drive” option then press “Next.”
Now that you have completed the installation process, click on “Finish.
Begin by opening the start menu and typing in “environment” and select the option called
“Edit the system environment variables.”
Once you have the “Environment Variables” window open, direct your focus to the
bottom half. You will notice that it controls all the “System Variables” rather than just
this associated with your user. Click on “New…” to create a new variable for Python.
Simply enter a name for your Path and the code shown below. For the purposes of this
example we have installed Python 2.7.3, so we will call the path: “Pythonpath.” The
string that you will need to enter is: “C:\Python27\;C:\Python27\Scripts;”
Session 2022-23 P a g e | 10
PYTHON FOR DATA SCIENCE
Now that we have successfully completed the installation process and added our
“Environment Variable,” you are ready to create your first basic Python script. Let’s
begin by opening Python’s GUI by pressing “Start” and typing “Python” and selecting the
“IDLE (Python GUI).”
Session 2022-23 P a g e | 11
PYTHON FOR DATA SCIENCE
Once the GUI is open, we will begin by using the simplest directive possible. This is the
“print” directive which simply prints whatever you tell it to, into a new line. Start by
typing a print directive like the one shown in the image below or copy and paste this text
then press
Python’s traditional runtime execution model: source code you type is translated to byte
code, which is then run by the Python Virtual Machine. Your code is automatically
compiled, but then it is interpreted. Source code extension is .py. Byte code extension is
.pyc (compiled python code)
Session 2022-23 P a g e | 12
PYTHON FOR DATA SCIENCE
Chapter – 3
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 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.
Python has many native data types. Here are the important ones:
Numbers can be integers (1 and 2), floats (1.1 and 1.2), fractions (1/2 and 2/3), or even
complex numbers.
Session 2022-23 P a g e | 13
PYTHON FOR DATA SCIENCE
3.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.
3.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.
EXAMPLE:-
>>>”HELLO”
>>>”45678”
>>>”HEY Chad”
Session 2022-23 P a g e | 14
PYTHON FOR DATA SCIENCE
In Python, we create a string by putting quotes around text. For example, we could take
our otherwise useless.
• len("hello") 5 # size
Session 2022-23 P a g e | 15
PYTHON FOR DATA SCIENCE
> Greater that - True if left operand is greater than the x > y
right
< Less that - True if left operand is less than the right x<y
Session 2022-23 P a g e | 16
PYTHON FOR DATA SCIENCE
Session 2022-23 P a g e | 17
PYTHON FOR DATA SCIENCE
Chapter – 4
4.1 List
- Mutable.
4.2 Tuples
- Immutable in nature.
- No type restriction.
- Indexing and slicing, everything's same like that in strings and lists.
4.3 Dictionaries
Session 2022-23 P a g e | 18
PYTHON FOR DATA SCIENCE
4.4 Sets
Session 2022-23 P a g e | 19
PYTHON FOR DATA SCIENCE
Chapter – 5
5.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 use parentheses.
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, 6, 7 ); print "tup1[0]: ", tup1[0] print
"tup2[1:5]: ", tup2[1:5]
When the above code is executed, it produces the following result − tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
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 –
Session 2022-23 P a g e | 20
PYTHON FOR DATA SCIENCE
Session 2022-23 P a g e | 21
PYTHON FOR DATA SCIENCE
5.2 Lists
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.
Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so
on.
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 ];
print list[2]
Session 2022-23 P a g e | 22
PYTHON FOR DATA SCIENCE
Session 2022-23 P a g e | 23
PYTHON FOR DATA SCIENCE
Session 2022-23 P a g e | 24
PYTHON FOR DATA SCIENCE
Session 2022-23 P a g e | 25
PYTHON FOR DATA SCIENCE
Chapter – 6
6.1 Loops
Programming languages provide various control structures that allow for more
complicated execution paths
Session 2022-23 P a g e | 26
PYTHON FOR DATA SCIENCE
For Loop:
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Session 2022-23 P a g e | 27
PYTHON FOR DATA SCIENCE
While Loop:
>>> count = 0
>>while(count< 4):
count = count + 1
Session 2022-23 P a g e | 28
PYTHON FOR DATA SCIENCE
Session 2022-23 P a g e | 29
PYTHON FOR DATA SCIENCE
If Statement:
a=33
b=200
If b>a:
print(“b”)
If...Else Statement:
a=200
b=33
if b>a:
else:
Session 2022-23 P a g e | 30
PYTHON FOR DATA SCIENCE
Chapter – 7
Science
- Bioinformatics
System Administration
- Unix
- Web Logic
- Web Sphere
-CGI
Testing Scripts
Web development
Software development
Game Development
AI and ML
Desktop GUI
Session 2022-23 P a g e | 31
PYTHON FOR DATA SCIENCE
Operating Systems
• Google makes extensive use of Python in its web search system, and employs Python’s
creator.
• 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.
• It's free (open source) o Downloading and installing Python is free and easy o Source
code is easily accessible
• It's powerful o Dynamic typing o Built-in types and tools o Library utilities
• It's portable o Python runs virtually every major platform used today o As long as you
have a compatible Python interpreter installed, Python programs will run in exactly the
same manner, irrespective of platform.
Session 2022-23 P a g e | 32
PYTHON FOR DATA SCIENCE
Chapter – 8
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 return None
Session 2022-23 P a g e | 33
PYTHON FOR DATA SCIENCE
print str
return;
Session 2022-23 P a g e | 34
PYTHON FOR DATA SCIENCE
Chapter – 9
File Handling
Python too supports file handling and allows users to handle files i.e., to read and write
files, along with many other file handling options, to operate on files. The concept of
file handling has stretched over various other languages, but the implementation is
either complicated or lengthy, but like other concepts of Python, this concept here is
also easy and short. Python treats files differently as text or binary and this is important.
Each line of code includes a sequence of characters and they form a text file. Each line
of a file is terminated with a special character, called the EOL or End of Line characters
like comma {,} or newline character. It ends the current line and tells the interpreter a
new one has begun. Let’s start with the reading and writing files.
Before performing any operation on the file like reading or writing, first, we have to
open that file. For this, we should use Python’s inbuilt function open() but at the time of
opening, we have to specify the mode, which represents the purpose of the opening file.
f = open(filename, mode)
Session 2022-23 P a g e | 35
PYTHON FOR DATA SCIENCE
The open command will open the file in the read mode and the for loop will print each
line present in the file.
There is more than one way to read a file in Python. If you need to extract a string that
contains all characters in the file then we can use file.read(). The full code would work
like this:
Another way to read a file is to call a certain number of characters like in the following
code the interpreter will read the first five characters of stored data and return it as a
string:
Let’s see how to create a file and how to write mode works, so in order to
manipulate the file, write the following in your Python environment:
Session 2022-23 P a g e | 36
PYTHON FOR DATA SCIENCE
The close() command terminates all the resources in use and frees the
system of this particular program.
There are also various other commands in file handling that is used to handle various tasks
like:
rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.
It is designed to provide much cleaner syntax and exception handling when you are
working with code. That explains why it’s good practice to use them with a statement
where applicable. This is helpful because using this method any files opened will be closed
automatically after one is done, so auto-cleanup.
Example:
Session 2022-23 P a g e | 37
PYTHON FOR DATA SCIENCE
data = file.read()
# do something with data
We can also use the write function along with the with() function:
We can also split lines using file handling in Python. This splits the variable when space is
encountered. You can also split using any characters as we wish. Here is the code:
Session 2022-23 P a g e | 38
PYTHON FOR DATA SCIENCE
Chapter – 10
10.1 Class
A class is a user-defined blueprint or prototype from which objects are created. Classes
provide a means of bundling data and functionality together. Creating a new class creates a
new type of object, allowing new instances of that type to be made. Each class instance can
have attributes attached to it for maintaining its state. Class instances can also have methods
(defined by their class) for modifying their state.
To understand the need for creating a class in Python let’s consider an example, let’s say
you wanted to track the number of dogs that may have different attributes like breed, age. If
a list is used, the first element could be the dog’s breed while the second element could
represent its age. Let’s suppose there are 100 different dogs, then how would you know
which element is supposed to be which? What if you wanted to add other properties to these
dogs? This lacks organization and it’s the exact need for classes.
10.1.1 Class Definition Syntax:
class ClassName:
# Statement
Session 2022-23 P a g e | 39
PYTHON FOR DATA SCIENCE
Attributes are always public and can be accessed using the dot (.) operator. Eg.:
Myclass.Myattribute
10.1.4 Defining a class
# Python3 program to
# demonstrate defining
# a class
class Dog:
pass
In the above example, the class keyword indicates that you are creating a class followed by
the name of the class (Dog in this case).
Session 2022-23 P a g e | 40
PYTHON FOR DATA SCIENCE
When an object of a class is created, the class is said to be instantiated. All the instances
share the attributes and the behavior of the class. But the values of those attributes, i.e. the
state are unique for each object. A single class may have any number of instances.
Example:
Declaring an object
# Python3 program to
Session 2022-23 P a g e | 41
PYTHON FOR DATA SCIENCE
# demonstrate instantiating
# a class
class Dog:
# A simple class
# attribute
attr1 = "mammal"
attr2 = "dog"
# A sample method
def fun(self):
# Driver code
# Object instantiation
Rodger = Dog()
Session 2022-23 P a g e | 42
PYTHON FOR DATA SCIENCE
print(Rodger.attr1)
Rodger.fun()
Output:
mammal
I'm a mammal
I'm a dog
Class methods must have an extra first parameter in the method definition. We do not
give a value for this parameter when we call the method, Python provides it.
If we have a method that takes no arguments, then we still have to have one argument.
This is similar to this pointer in C++ and this reference in Java.
When we call a method of this object as myobject.method(arg1, arg2), this is automatically
converted by Python into MyClass.method(myobject, arg1, arg2) – this is all the special self
is about.
Session 2022-23 P a g e | 43
PYTHON FOR DATA SCIENCE
The __init__ method is similar to constructors in C++ and Java. Constructors are used to
initializing the object’s state. Like methods, a constructor also contains a collection of
statements(i.e. instructions) that are executed at the time of Object creation. It runs as soon
as an object of a class is instantiated. The method is useful to do any initialization you want
to do with your object.
# Sample Method
def say_hi(self):
print('Hello, my name is', self.name)
p = Person('Nikhil')
p.say_hi()
Output:
Hello, my name is Nikhil
Instance variables are for data, unique to each instance and class variables are for
attributes and methods shared by all instances of the class. Instance variables are variables
whose value is assigned inside a constructor or method with self whereas class variables
are variables whose value is assigned in the class.
Session 2022-23 P a g e | 44
PYTHON FOR DATA SCIENCE
class Dog:
# Class Variable
animal = 'dog'
# Instance Variable
self.breed = breed
self.color = color
print('Rodger details:')
print('Rodger is a', Rodger.animal)
print('Breed: ', Rodger.breed)
print('Color: ', Rodger.color)
Session 2022-23 P a g e | 45
PYTHON FOR DATA SCIENCE
print('\nBuzo details:')
print('Buzo is a', Buzo.animal)
print('Breed: ', Buzo.breed)
print('Color: ', Buzo.color)
Output:
Rodger details:
Rodger is a dog
Breed: Pug
Color: brown
Buzo details:
Buzo is a dog
Breed: Bulldog
Color: black
dog
Session 2022-23 P a g e | 46
PYTHON FOR DATA SCIENCE
class Dog:
# Class Variable
animal = 'dog'
# Instance Variable
self.breed = breed
# Driver Code
Rodger = Dog("pug")
Rodger.setColor("brown")
print(Rodger.getColor())
Output:
brown
Chapter – 11
Session 2022-23 P a g e | 47
PYTHON FOR DATA SCIENCE
Conclusion
I believe the trial has shown conclusively that it is both possible and desirable to use Python
as the principal teaching language:
o It appears to be quicker to learn and, in combination with its many libraries, this
offers the possibility of more rapid student development allowing the course to be
made more challenging and varied;
and most importantly, its clean syntax offers increased understanding and enjoyment for
student
Session 2022-23 P a g e | 48
PYTHON FOR DATA SCIENCE
References
1. www.python.org
2. www.geeksforgeeks.org
3. www.scribd.com
4. “GETTING STARTED WITH PYTHON” By Sumita Arora
5. www.slideshare.net
Session 2022-23 P a g e | 49