100% found this document useful (1 vote)
220 views155 pages

Python Notes 2022

Python lists allow storing multiple elements in a single variable. A list contains elements separated by commas and enclosed in square brackets. Elements can be accessed via indexes and lists support common operations like append, pop, count, sort, reverse etc. Lists are mutable, meaning elements can be modified after the list is created. Common list methods make it easy to manipulate lists in Python programs.

Uploaded by

Anuj Vishwakarma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
220 views155 pages

Python Notes 2022

Python lists allow storing multiple elements in a single variable. A list contains elements separated by commas and enclosed in square brackets. Elements can be accessed via indexes and lists support common operations like append, pop, count, sort, reverse etc. Lists are mutable, meaning elements can be modified after the list is created. Common list methods make it easy to manipulate lists in Python programs.

Uploaded by

Anuj Vishwakarma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 155

HISTORY

Python was conceived in the


late 1980’s by Guido van
Rossum at Centrum Wiskunde
& Informatica (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.
Its implementation began in
December 1989. Finally
appeared in 20 February 1991.
Why Python was created?
 In late 1980s, Guido Van Rossum was working on the
Amoeba distributed operating system group. He
wanted to use an interpreted language like ABC (ABC
has simple easy-to-understand syntax) that could
access the Amoeba system calls. So, he decided to
create a language that was extensible. This led
to design of a new language which was later named
Python.
Why the name Python?
No. It wasn't named after a
dangerous snake. Rossum
was fan of a comedy series
from late seventies. The
name "Python" was adopted
from the same series
"Monty Python's Flying
Circus".
Release Dates of Different Versions
1. Python 1.0 (first standard release) --January 1994
2. Python 1.6 (Last minor version) -- September 5, 2000
3. Python 2.0 (Introduced list comprehensions) -- October 16, 2000
4. Python 2.7 (Last minor version) -- July 3, 2010
5. Python 3.0 (Emphasis on removing duplicative constructs
and module) -- December 3, 2008
6. Python 3.5 (Last updated version) – September 13, 2015
7. Python 3.6 -- 23 December 2016.
8. Python 3.7 -- 27 June 2018
9. Python 3.8 -- 14 October 2019.
10. Python 3.9 -- October 2020
11. Python 3.10 -- 14 January 2022
Applications of Python
Install and Run Python in
Windows
1. Go to Download Python page on the official site and click Download
Python 3.8.0 (You may see different version name).
2. When the download is completed, double-click the file and follow
the instructions to install it.
When Python is installed, a program called IDLE is also installed
along with it. It provides graphical user interface to work with
Python.
3. Open IDLE, copy the following code below and press enter.
print("Hello, World!")
4. To create a file in IDLE, go to File > New Window (Shortcut: Ctrl+N).
5. Write Python code (you can copy the code below for now) and save
(Shortcut: Ctrl+S) with .py file extension like: hello.py or your-first-
program.py
6. Go to Run > Run module (Shortcut: F5) and you can see the output.
Congratulations, you've successfully run your first Python program.
Python has two basic modes:
Interactive Mode (IDLE): Interactive mode, also known as
the REPL(read–eval–print loop) provides us with a quick way of
running blocks or a single line of Python code. The code executes via
the Python shell, which comes with Python installation. Interactive
mode is handy when you just want to execute basic Python commands.
The >>> indicates that the Python shell is ready to execute and
send your commands to the Python interpreter. The result is
immediately displayed on the Python shell as soon as the Python
interpreter interprets the command.
>>> print("Hello World")
Hello World
>>>
Script Mode
Script Mode : If you need to write a long piece of Python
code or your Python script spans multiple files, interactive
mode is not recommended. Script mode is the way to go
in such cases. In script mode, You write your code in a text
file then save it with a .py extension which stands for
"Python". Note that you can use any text editor for this,
including Sublime, Atom, notepad++, etc.
 If you are in the standard Python shell, you can click "File"
then choose "New" or simply hit "Ctrl + N" on your
keyboard to open a blank script in which you can write your
code. You can then press "Ctrl + S" to save it.
 After writing your code, you can run it by clicking "Run"
then "Run Module" or simply press F5.
Print function working
Print(“Welcome to python Learning”)
 The print function in Python displays text on the
screen. The string inside the parentheses must be
enclosed in single or double quotation marks.
Input function working
Python input() function is used to get input from the
user. It prompts for the user input and reads a line. After
reading data, it converts it into a string and returns that.
Example:

# for string
variable_name=input(“Enter your name”)
# for integer
variable_name=int(input(“Enter any number:”))
Basic Program code

OUTPUT
Python Character set
It encompasses the following:
 Lowercase English letters: a through z
 Uppercase English letters: A through Z
 Some punctuation and symbols: "$" and "!", to name a
couple
 Whitespace characters: an actual space (" "), as well as a
newline, carriage return, horizontal tab, vertical tab, and a
few others
 Some non-printable characters: characters such as
backspace, "\b", that can’t be printed literally in the way
that the letter A can
TOKENS
A token is the smallest individual unit in a python
program. All statements and instructions in a program
are built with tokens.
Type of Tokens:
TOKEN: Keywords
TOKEN: Identifier
Identifiers are user-defined names given to
different entities such as constants, variables,
structures, functions, module etc.
TOKEN: Literals / Values
 Literals are the constant values or the variable
values used in a Python code.
 Python support the following literals:
TOKEN: Punctuators
These are the symbols that used in Python to
organize the structures, statements, and
expressions.
Data types in Python
Data types are the classification or categorization of data items. It
represents the kind of value that tells what operations can be performed on
a particular data. Since everything is an object in Python programming,
data types are actually classes and variables are instance (object) of these
classes.
OPERATORS:
Python Operators in general are used to perform
operations on values and variables.
Comparison(Relational) Operators
Logical Operators
Bitwise Operators
In Python, bitwise operators are used to performing bitwise calculations
on integers. The integers are first converted into binary and then
operations are performed on bit by bit, hence the name bitwise operators.
Membership Operators
Identity Operator
Working of If Condition in Python
with Examples
Working of If ..else Condition in
Python with Examples
Working of elif Condition in Python
with Examples
Working of Nested..if Condition in
Python with Examples
What is while loop in Python?
 The while loop in Python is used to iterate over a block
of code as long as the test expression (condition) is
true.
 With the while loop we can execute a set of statements
as long as a condition is true. We generally use this
loop when we don't know the number of times to iterate
beforehand.
Syntax of while Loop in Python
Flowchart of while Loop
Example
Print i as long as i is less than 6:

OUTPUT
What is for loop in Python?
The for loop in Python is used to iterate over a sequence
(list, tuple, string, range function) or other iterable
objects. Iterating over a sequence is called traversal.
Syntax of for Loop
Flowchart of for Loop
Example: Python for Loop
Print each fruit in a fruit list:

OUTPUT
What is a Nested Loop in Python?
A nested loop is a loop inside the body of the outer
loop. The inner or outer loop can be any type, such as a
while loop or for loop. For example, the outer for loop can
contain a while loop and vice versa.
The break Statement
With the break statement we can stop the loop before it
has looped through all the items:

OUTPUT
What is String in Python?
A string is a sequence of characters. A character is simply
a symbol. For example, the English language has 26
characters. The Data Type of the text is str( String )
Creation of String
Strings are one of the most common data types in
Python. It is used for storing text. It can be created by
enclosing characters either in single quotation marks or
double quotation marks. It can be assigned to a variable
using = sign.
Access text from string
A character (also called element) of a string can be accessed with it's index
number. In Python, index number starts with 0 in forward direction and -1
in backward direction. The figure below describes the indexing concept of a
string.
What is List in Python?
Lists are used to store multiple items in a single variable.
A list in Python is used to store the sequence of various
types of data. Python lists are mutable type its mean we
can modify its element after it created.
Lists are enclosed in square brackets [ ] and each item is
separated by a comma.
List features
Accessing Values in Lists
Updating Lists
You can update single or multiple elements of lists by giving the slice on the left-
hand side of the assignment operator, and you can add to elements in a list with the
append() method. For example −
Traversing Lists : Two ways
Python List Methods
How to apply List Methods:
my_list = [3, 8, 1, 6, 0, 8, 4]

print(len(my_list))
my_list.append(9)
# Output: 7
print(my_list)
print(my_list.index(8))
# Output: [3, 8, 1, 6, 0, 8, 4, 9]
# Output: 2
my_list.pop(2)
print(my_list.count(8))
# Output: 1
my_list.sort()
my_list.extend([12,15,16])
print(my_list)
print(my_list)
# Output: [0, 1, 3, 4, 6, 8, 8]
# Output: [3, 8, 6, 0, 8, 4, 9, 12, 15, 16]
my_list.reverse()
new_list=my_list.copy()
print(my_list)
print(new_list)
# Output: [8, 8, 6, 4, 3, 1, 0]
# Output: [3, 8, 6, 0, 8, 4, 9, 12, 15, 16]
What is tuple in Python?
 Tuples are used to store multiple items in a single
variable.
 Tuple is one of 4 built-in data types in Python used to
store collections of data. A tuple is a collection which is
ordered and unchangeable.
 Tuples are written with round brackets.
The characteristics of a Python tuple
are:
1. Tuples are ordered, indexed collections of data.
Similar to string indices, the first value in the tuple
will have the index [0], the second value [1], and so on.
2. Tuples can store duplicate values.
3. Once data is assigned to a tuple, the values cannot be
changed.
4. Tuples allow you to store several data items in one
variable. You can choose to store only one kind of
data in a tuple or mix it up as needed.
How to Create and Use Tuples?
In Python, tuples are assigned by placing the values or
"elements" of data inside round brackets "()." Commas
must separate these items.
A tuple may have any number of values and the values
can be of any type.
Here are some examples of declared tuples:

You can also create an empty tuple by putting no values between


the brackets, like so:
Accessing Values in Tuple
TUPLE OPERATIONS
Tuple operation examples
Joining/ Concatenation
The + operator, concatenation operator is used to joining two tuples.

Repeating/ Replicate
You can use * operator to replicate a tuple a specified number of times.

Slicing the Tuples


Slicing refers to extracting a subpart of the mentioned sequence. Tuple-
slices are same as the List- slices.
Syntax: seq= T[start:stop]
Tuple operation examples
Comparing Tuples
You can compare two tuples using operators (>, >=, <, <=, !=). Python
compares individual elements of two tuples.

Unpacking Tuples
Packing- Creating tuples from a set of values.
Unpacking- Creating individual values from a tuple’s element.

.
Tuple operation examples
Deleting Tuples
The del statement is used to delete elements and objects. But, since tuples
are immutable as discussed above, individual item of a tuple cannot be
deleted.

But you can delete a complete tuple with del statement:

.
Tuple Methods or Function
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 −
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
Print("dict['Name']: ", dict['Name'])
Print("dict['Age']: ", dict['Age'])
When the above code is executed, it produces the
following result −
dict['Name']: Zara
dict['Age']: 7
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 −
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
 When the above code is executed, it produces the following
result −
dict['Age']: 8
dict['School']: DPS School
Dictionary Methods or Functions
How to use Methods
DIFFERENCES BETWEEN LIST, TUPLE & DICTIONARY
FUNCTION
 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
You can define functions to provide
the required functionality. Here are
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.
What are Python Modules?
Modules provide us with a way to share reusable
functions. A module is simply a “Python file” which
contains code we can reuse in multiple Python programs.
A module may contain functions, classes, lists, etc.
Modules in Python can be of two types:
 Built-in Modules.
 User-defined Modules.
Built-in Modules in Python
One of the many superpowers of Python is that it comes
with a “rich standard library”. This rich standard library
contains lots of built-in modules. Hence, it provides a lot
of reusable code.
To name a few, Python contains modules like “os”, “sys”,
“datetime”, “random”.
You can import and use any of the built-in modules
whenever you like in your program. (We’ll look at it
shortly.)
User-Defined Modules in Python
Another superpower of Python is that it lets you take
things in your own hands. You can create your own
functions and classes, put them inside modules and voila!
You can now include hundreds of lines of code into any
program just by writing a simple import statement.
To create a module, just put the code inside a .py file.
Let’s create one.
EXAMPLE
 The Python code for a module named aname normally
resides in a file named aname.py. Here's an example of
a simple module, support.py
def print_func( par ):
print "Hello : ", par
return
The import Statement
 You can use any Python source file as a module by
executing an import statement in some other Python
source file. The import has the following syntax −

import module1[, module2[,... moduleN]


When the interpreter encounters an import statement, it imports the module if
the module is present in the search path. A search path is a list of directories
that the interpreter searches before importing a module. For example, to
import the module support.py, you need to put the following command at the top of
the script −

 #!/usr/bin/python
# Import module support
import support
# Now you can call defined function that module as follows
support.print_func("Zara")
When the above code is executed, it produces the following result −
Hello : Zara
A module is loaded only once, regardless of the number of times it is
imported. This prevents the module execution from happening over
and over again if multiple imports occur.
The from...import Statement
 Python's from statement lets you import specific
attributes from a module into the current namespace.
The from...import has the following syntax −
from modname import name1[, name2[, ... nameN]]
 For example, to import the function fibonacci from the
module fib, use the following statement −
from fib import fibonacci
 This statement does not import the entire module fib into
the current namespace; it just introduces the item
fibonacci from the module fib into the global symbol table
of the importing module.
The from...import * Statement
 It is also possible to import all names from a module
into the current namespace by using the following
import statement −
from modname import *
This provides an easy way to import all the items from a module into the
current namespace; however, this statement should be used sparingly.
Locating Modules
 When you import a module, the Python interpreter
searches for the module in the following sequences −
 The current directory.
 If the module isn't found, Python then searches each
directory in the shell variable PYTHONPATH.
 If all else fails, Python checks the default path. On UNIX, this
default path is normally /usr/local/lib/python/.
 The module search path is stored in the system
module sys as the sys.pathvariable. The sys.path
variable contains the current directory,
PYTHONPATH, and the installation-dependent
default.
NUMPY ARRAYS
 Numpy which stands for Numerical Python which is
the core library for scientific computing in Python. It
provides a high-performance multidimensional array
object, and tools for working with these arrays.
 Arrays : A numpy array is a grid of values, all of the
same type, and is indexed by a tuple of nonnegative
integers. The number of dimensions is the rank of the
array; the shape of an array is a tuple of integers giving
the size of the array along each dimension.
What are NumPy and Pandas?
 Numpy is an open source Python library used for scientific
computing and provides a host of features that allow a
Python programmer to work with high-performance arrays
and matrices.
 In addition, pandas is a package for data manipulation that
uses the DataFrame objects from R (as well as different R
packages) in a Python environment.

 Both NumPy and pandas are often used together, as the


pandas library relies heavily on the NumPy array for the
implementation of pandas data objects and shares many of
its features. In addition, pandas builds upon functionality
provided by NumPy.
Operations using 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.
NumPy – A Replacement for MatLab
NumPy is often used along with packages like SciPy (Scientific
Python) and Mat−plotlib (plotting library). This combination is
widely used as a replacement for MatLab, a popular platform for
technical computing. However, Python alternative to MatLab is now
seen as a more modern and complete programming language.
NumPy – Environment(Installation)
 Standard Python distribution doesn't come bundled
with NumPy module. A lightweight alternative is to
install NumPy using popular Python package
installer, pip.
>>>pip install numpy

In order to use NumPy, you must import in your module


by using a statement like: You can use
>>>Import numpy as np any identifier
in place of np
NumPy arrays come in two forms:
 1-D (one dimensional)
arrays as Vectors (have
single row/column only).
Eg:- import numpy as np
a=np.array([1,2,3])
print(a)
 Multidimensional arrays
known as Matrices (have
multiple rows and
column).
Eg:-
a=np.array([(1,2,3),(4,5,6)]
)
print(a)
NUMPY - DATA TYPES
 bool_Boolean (True or False) stored as a byte.
 intc Identical to C int (normally int32 or int64)
 intp Integer used for indexing (same as C ssize_t; normally either int32 or
int64)
 int8 Byte (-128 to 127)
 int16 Integer (-32768 to 32767)
 int32 Integer (-2147483648 to 2147483647)
 int64 Integer (-9223372036854775808 to 9223372036854775807)
 uint8 Unsigned integer (0 to 255)
 Single hold 6 digit after decimal
 Double hold 12 digit after decimal
 float_ Shorthand for float64
 float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
 complex_ Shorthand for complex128
 complex64 Complex number, represented by two 32-bit floats (real and
imaginary components)
ndarray.shape
 This array attribute returns a tuple consisting of array
dimensions. It can also be used to resize the array.
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
Example 1 print (a.shape)
The output is as follows −
(2, 3) NumPy also
provides a
# this resizes the ndarray reshape function
import numpy as np to resize an array.
Example 2 a = np.array([[1,2,3],[4,5,6]])
Example:
a.shape = (3,2)
print (a)
b = a.reshape(3,2)
The output is as follows −
[[1, 2]
[3, 4]
[5, 6]]
NumPy - Array Creation
 numpy.empty
It creates an uninitialized array of specified shape and
dtype. It uses the following constructor −
numpy.empty(shape, dtype = float, order = 'C')
Parameter & Description
 Shape: Shape of an empty array in int or tuple of int
 Dtype : Desired output data type. Optional
 Order : 'C' for C-style row-major array, 'F' for FORTRAN style column-
major array [[22649312 1701344351]
import numpy as np
[1818321759 1885959276]
Example: x = np.empty([3,2], dtype = int) [16779776 156368896]]
print x Note − The elements in an array
show random values as they are
not initialized.
Arrays: Slices, Joins and Subsets
 Basic slicing is an extension of Python's basic concept
of slicing to n dimensions. A Python slice object is
constructed by giving start, stop,
and step parameters to the built-in slice function.
This slice object is passed to the array to extract a part
of array.
Example 1 :
import numpy as np
a = np.arange(10) Its output is as follows −
print (a[1:2]) [2 4 6]
Arrays: Slices
[2 4 6]
Example 2
import numpy as np
a = np.arange(10)
b = a[2:7:2]
print b [2 3 4 5 6 7 8 9]]
Example 4
# slice items starting from index
import numpy as np
a = np.arange(10) Its output is as follows −
print a[2:]
[[1 2 3]
Example 5 [3 4 5]
import numpy as np
[4 5 6]]
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print a Now we will slice the array
from
# slice items starting from index print 'Now we will slice the index
the array a[1:]
from the index a[1:]'
print a[1:] [[3 4 5]
[4 5 6]]
Arrays: Joins(Concatenation)
 Concatenation refers to joining. This function is used to join two or more arrays of the
same shape along a specified axis. The function takes the following parameters.
Syntax: numpy.concatenate((a1, a2, ...), axis)
Example:
import numpy as np
a = np.array([[1,2],[3,4]]) Its output is as follows −
print 'First array:' First array:
print a [[1 2]
print '\n' [3 4]]
b = np.array([[5,6],[7,8]]) Second array:
print 'Second array:' [[5 6]
print b [7 8]]
print '\n' Joining the two arrays along axis 0:
# both the arrays are of same dimensions [1 2]
[3 4]
print 'Joining the two arrays along axis 0:‘
[5 6]
print np.concatenate((a,b))
[7 8]]
print '\n' Joining the two arrays along axis 1:
print 'Joining the two arrays along axis 1:' [[1 2 5 6]
print np.concatenate((a,b),axis = 1) [3 4 7 8]]
Arrays: Subsets
 The issubset() method returns True if all elements of a
set are present in another set (passed as an argument).
If not, it returns False.
The syntax of issubset() is:
A.issubset(B)
Return Value from issubset()
 True if A is a subset of B
 False if A is not a subset of B
Arrays: How issubset() works?
 A = {1, 2, 3}
 B = {1, 2, 3, 4, 5}
 C = {1, 2, 4, 5}
 # Returns True
print(A.issubset(B)) When you run the program, the
output will be:
 # Returns False True
# B is not subset of A False
print(B.issubset(A)) False
 # Returns False True
print(A.issubset(C))
 # Returns True
print(C.issubset(B))
Arithmetic operation on 2D Arrays
 import numpy as np
 x = np.array([[1,2],[3,4]], dtype=np.float64)
 y = np.array([[5,6],[7,8]], dtype=np.float64)
 # Element wise sum; both produce the array
 # [[ 6.0 8.0]
 # [10.0 12.0]]
 print(x + y)
 print(np.add(x, y))
 # Element wise difference; both produce the array
 # [[-4.0 -4.0]
 # [-4.0 -4.0]]
 print(x - y)
 print(np.subtract(x, y))
Arithmetic operation on 2D Arrays
 # Elementwise product; both produce the array
 # [[ 5.0 12.0]
 # [21.0 32.0]]
 print(x * y)
 print(np.multiply(x, y))
 # Elementwise division; both produce the array
 # [[ 0.2 0.33333333]
 # [ 0.42857143 0.5 ]]
 print(x / y)
 print(np.divide(x, y))
 # Elementwise square root; produces the array
 # [[ 1. 1.41421356]
 # [ 1.73205081 2. ]]
 print(np.sqrt(x))
We instead use the dot function to compute inner products of vectors, to multiply
a vector by a matrix, and to multiply matrices. dot is available both as a function in
the numpy module and as an instance method of array objects:
 import numpy as np
 x = np.array([[1,2],[3,4]])
 y = np.array([[5,6],[7,8]])
 v = np.array([9,10])
 w = np.array([11, 12])
 # Inner product of vectors; both produce 219
 print(v.dot(w))
 print(np.dot(v, w))
 # Matrix / vector product; both produce the rank 1 array [29 67]
 print(x.dot(v))
 print(np.dot(x, v))
 # Matrix / matrix product; both produce the rank 2 array
 # [[19 22] # [43 50]]
 print(x.dot(y))
 print(np.dot(x, y))
NumPy Covariance Function
 Covariance provides the a measure of strength of
correlation between two variable or more set of variables.
The covariance matrix element Cij is the covariance of xi
and xj. The element Cii is the variance of xi.
 Syntax: numpy.cov(m, y=None, rowvar=True, bias=False,
ddof=None, fweights=None, aweights=None)
 Example:
 # Python code to demonstrate the
 # use of numpy.cov
 import numpy as np
 x = np.array([[0, 3, 4], [1, 2, 4], [3, 4, 5]])
 print("Shape of array:\n", np.shape(x))
 print("Covarinace matrix of x:\n", np.cov(x))
Correlation
Correlation values range between -1 and 1.
There are two key components of a correlation value:
 magnitude – The larger the magnitude (closer to 1 or -1), the
stronger the correlation
 sign – If negative, there is an inverse correlation. If positive,
there is a regular correlation.
Positive Correlation
 Let’s take a look at a positive correlation. Numpy implements
a corrcoef() function that returns a matrix of correlations of x
with x, x with y, y with x and y with y. We’re interested in the
values of correlation of x with y (so position (1, 0) or (0, 1)).
Negative Correlation
 What happens to our correlation figure if we invert the
correlation such that an increase in x results in a decrease in y?
Example
 Import numpy as np
 a=np.array([1,2,3,4,5])
 b=np.array([3,4,0,-1,-3])
 Correlation=np.corrcoef(a,b)
 Print(correlation)
OUTPUT
array([[1. , -0.93299621]
[-0.93299621, 1. ]])
Linear Regression
 Linear regression is a method used to find a relationship
between a dependent variable and a set of independent
variables. In its simplest form it consist of fitting a
function y=w.x+b to observed data, where y is the
dependent variable, x the independent, w the weight
matrix and b the bias.
 Example:
x = np.array([1,2,3,4,5,6,7,8]) print the results:
y = np.array([15,32,66,45,90,153,170,200]) OUTPUT1
Syntax: [ 27.27380952 -
Print(np.polyfit(x,y,1) 26.35714286]
Where ‘1’ represent two constant values
NumPy some FUNCTIONS
PANDAS INTRODUCTION
 Pandas is an open-source Python Library providing
high-performance data manipulation and analysis tool
using its powerful data structures. The name Pandas is
derived from the word Panel Data – an Econometrics
from Multidimensional data.
 In 2008, developer Wes McKinney started developing
pandas when in need of high performance, flexible
tool for analysis of data.
KEY FEATURES OF PANDAS
 Fast and efficient DataFrame object with default and
customized indexing.
 Tools for loading data into in-memory data objects from
different file formats.
 Data alignment and integrated handling of missing data.
 Reshaping and pivoting of data sets.
 Label-based slicing, indexing and subsetting of large data
sets.
 Columns from a data structure can be deleted or inserted.
 Group by data for aggregation and transformations.
 High performance merging and joining of data.
 Time Series functionality.
PANDAS INSTALLATION
 Standard Python distribution doesn't come bundled with Pandas module. A
lightweight alternative is to install NumPy using popular Python package
installer, pip.
pip install pandas
If you install Anaconda Python package, Pandas will be installed by default
with the following −
Windows :
 Anaconda (from https://www.continuum.io) is a free Python distribution for
SciPy stack. It is also available for Linux and Mac.
 Canopy (https://www.enthought.com/products/canopy/) is available as free as
well as commercial distribution with full SciPy stack for Windows, Linux and
Mac.
 Python (x,y) is a free Python distribution with SciPy stack and Spyder IDE for
Windows OS. (Downloadable from http://python-xy.github.io/)
PANDAS DATA STRUCTURE
 Pandas deals with the following three data structures −
 Series
 DataFrame
 Panel
DATA STRUCTURE DIMENSIONS DESCRIPTION

Series 1 1-D labeled homogeneous array, size


immutable

Data Frames 2 General 2-D labeled, size-mutable


tabular structure with potentially
heterogeneously typed columns.

Panel 3 General 3D labeled, size-mutable array.


PANDAS SERIES
 Series is a one-dimensional labeled array capable of holding data of any type
(integer, string, float, python objects, etc.). The axis labels are collectively
called index.
 A pandas Series can be created using the following constructor −
pandas.Series( data, index, dtype, copy)
SN PARAMETER DESCRIPTION

1 data data takes various forms like ndarray, list, constants


2 index Index values must be unique and hashable, same length
as data. Default np.arrange(n) if no index is passed.

3 dtype dtype is for data type. If None, data type will be inferred

4 copy Copy data. Default False


Create an Empty Series
 A basic series, which can be created is an Empty Series.
Example:
#import the pandas library and aliasing as pd
import pandas as pd
s = pd.Series()
print s
Its output is as follows −
Series([], dtype: float64)
Create a Series from ndarray
 If data is an ndarray, then index passed must be of the
same length. If no index is passed, then by default index
will be range(n) where n is array length, i.e.,
[0,1,2,3…. range(len(array))-1].
Example :
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data) OR s = pd.Series(data,index=[100,101,102,103])
print (s)
0 a 100 a
Its output is as follows − 1 b OR 101 b
2 c 102 c
3 d 103 d
Create a Series from dict
 A dict can be passed as input and if no index is specified,
then the dictionary keys are taken in a sorted order to
construct index. If index is passed, the values in data
corresponding to the labels in the index will be pulled out.
 Example :
#import the pandas library and aliasing as pd
import pandas as pd
OUTPUT
import numpy as np
a 0.0
data = {'a' : 0., 'b' : 1., 'c' : 2.} b 1.0
s = pd.Series(data) c 2.0
print (s)
Accessing Data from Series with
Position
 Data in the series can be accessed similar to that in
an ndarray.
 Retrieve the first element. As we already know, the
counting starts from zero for the array, which means
the first element is stored at zeroth position and so on.
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) #retrieve the first element
print s[0]
Also retrieve
data using slice
S[1:3]
Series Attributes
PANDAS DATA FRAME
 Pandas DataFrame is two-dimensional size-mutable,
potentially heterogeneous tabular data structure with
labeled axes (rows and columns).
 A Data frame is a two-dimensional data structure, i.e.,
data is aligned in a tabular fashion in rows and
columns. Pandas DataFrame consists of three principal
components, the data, rows, and columns.
Creating a Pandas DataFrame
 In the real world, a Pandas DataFrame will be created by loading
the datasets from existing storage, storage can be SQL Database,
CSV file, and Excel file. Pandas DataFrame can be created from
the lists, dictionary, and from a list of dictionary etc.
 Creating a dataframe using List: DataFrame can be created using
a single list or a list of lists.
# import pandas as pd OUTPUT
import pandas as pd
# list of strings
lst = ['Geeks', 'For', 'Geeks', 'is',
'portal', 'for', 'Geeks']
# Calling DataFrame constructor on list
df = pd.DataFrame(lst)
print(df)
 Creating DataFrame from dict of ndarray/lists:
To create DataFrame from dict of narray/list, all the narray must be of same
length. If index is passed then the length index should be equal to the length of
arrays. If no index is passed, then by default, index will be range(n) where n is
the array length.
# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
OUTPUT
import pandas as pd

# intialise data of lists.


data = {'Name':['Tom', 'nick', 'krish', 'jack'],
'Age':[20, 21, 19, 18]}

# Create DataFrame
df = pd.DataFrame(data)

# Print the output.


print(df)
Column Selection
# Import pandas package
import pandas as pd
OUTPUT
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}

# Convert the dictionary into DataFrame


df = pd.DataFrame(data)

# select two columns


print(df[['Name', 'Qualification']])
Column Addition
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b',
OUTPUT
'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a',
'b', 'c', 'd'])}
df = pd.DataFrame(d)
# Adding a new column to an existing
DataFrame object with column label by
passing new series
print ("Adding a new column by passing as
Series:")
df['three']=pd.Series([10,20,30],index=['a','b
','c'])
OUTPUT
print df
print ("Adding a new column using the
existing columns in DataFrame:")
df['four']=df['one']+df['three']
print df
Deleting Columns
 Drop a column by name:
 Lets see an example of how to drop a column by name
in python pandas
# drop a column based on name
df.drop('Age',axis=1)
# drop a column based on column index
df.drop(df.columns[2],axis=1)
# delete a column
del df['Age']
Dataframe – Functions(max & min)
The max() and min() function find out the minimum or maximum
value respectively from a given set of data(dataframe).
 Max()
# get the maximum values of all the column in dataframe
df.max()
# get the maximum value of the column 'Age‘
df['Age'].max()
 Min()
# get the minimum values of all the column in dataframe
df.min()
# get the minimum value of the column 'Age‘
df['Age'].min()
Dataframe – Functions(Mean)
Mean Function in python pandas is used to calculate the arithmetic mean of
a given set of numbers, mean of a data frame ,mean of column and mean of rows

Mean of the dataframe:


# mean of the dataframe
 df.mean()
Column Mean of the dataframe:
# column mean of the dataframe
 df.mean(axis=0)
Row Mean of the dataframe:
# Row mean of the dataframe
 df.mean(axis=1)
Calculate the mean of the specific Column
# mean of the specific column
 df.loc[:,"Score1"].mean()
Dataframe – Functions(Median)
Median Function in python pandas is used to calculate the
median or middle value of a given set of numbers, Median of
a data frame, median of column and median of rows
Median of the dataframe:
# median of the dataframe
df.median()
Column Median of the dataframe:
# column median of the dataframe
df.median(axis=0)
Calculate the median of the specific Column
# median of the specific column
df.loc[:,"Score1"].median()
Dataframe – Functions(Mode)
Mode Function in python pandas is used to calculate the
mode or most repeated value of a given set of numbers.
Mode of a data frame, mode of column and mode of rows:
# mode of the dataframe
df.mode()
# column mode of the dataframe
df.mode(axis=0)
# Row mode of the dataframe
df.mode(axis=1)
# mode of the specific column
df.loc[:,"Score1"].mode()
Dataframe – Functions(Pivot-Table)
 One of the most common tasks in data science is to
manipulate the data frame we have to a specific format. For
example, sometime we may want to take data frame with fewer
columns, say in long format, summarize and convert into a
data frame with multiple columns, i.e. a wide data frame.
SYNTAX:
df.pivot_table(index=“field_name”,columns=“field_name",values=“fieldname”)
# simple example with pivot_table
pd.pivot_table(df, values='lifeExp', columns='continent')
continent Africa Americas Asia Europe Oceania
lifeExp 48.86533 64.658737 60.064903 71.903686 74.326208
 # pivot table example with three columns
pd.pivot_table(df1, values='lifeExp', index=['year'], columns='continent')
Dataframe – Functions(Sorting)
 How to sort a dataframe in python pandas by ascending order and by
descending order on multiple columns with an example for each . our
focus on this exercise will be on
 how to sort a pandas dataframe in python by Ascending and
Descending
 how to sort a python pandas dataframe by single column
 how to sort a pandas dataframe by multiple columns.
 Examples:
# sort the pandas dataframe by ascending value of single column
df.sort_values(by='Score')
# sort the pandas dataframe by descending value of single column
df.sort_values(by='Score',ascending=0)
# sort the pandas dataframe by multiple columns
df.sort_values(by=['Age', 'Score'],ascending=[True,False])
Dataframe – Functions(aggregation)
 aggregation (such as min, max, sum, count, etc.) and
grouping. Both are very commonly used methods in
analytics and data science projects
Pandas Data Aggregation #1: .count()
df.count()
Pandas Data Aggregation #2: .sum()
df.sum()
Function: Quartile()
Pandas dataframe.quantile() function return values at the given
quantile over requested axis, a numpy.percentile.
# importing pandas as pd
import pandas as pd

# Creating the dataframe


df = pd.DataFrame({"A":[1, 5, 3, 4, 2],
"B":[3, 2, 4, 3, 4],
"C":[2, 2, 7, 3, 4],
"D":[4, 3, 6, 12, 7]})

# Print the dataframe


df
# find the product over the index axis
df.quantile(.2, axis = 0)
Function: Var()
 Variance Function in python pandas is used to calculate
variance of a given set of numbers, Variance of a data
frame, Variance of column and Variance of rows, let’s see an
example of each. We need to use the packa
# calculate variance
import numpy as np
print(np.var([1,9,5,6,8,7]))
print(np.var([4,-11,-5,16,5,7,9])) OUTPUT
2.82842712475
Or 8.97881103594
# variance of the dataframe
df.var()
Creating Histogram
 Histograms are a powerful tool for analyzing the distribution of
data. Plots like histograms that characterize the distribution of
individual variables or features are vital for data analysis because
they help reveal patterns in the input data. Pandas DataFrames
that contain our data come pre-equipped with methods for
creating histograms, making preparation and presentation easy.
df.hist(column="Test_1", bins=5) # Plotting 5 bins
df.hist(column=["Test_1", "Test_2"]) # Plot specific columns
 Modifying Histogram Bin Sizes
The bins, or bars of the histogram plot, can be adjusted using the bins option.
This option can be tuned depending on the data, to see either subtle distribution
trends, or broader phenomena. Which bin size to use heavily depends on the
data set you use, therefore it is important to know how to change this
parameter. The default number of bars is 10.
Function Application
 To apply your own or another library’s functions to
Pandas objects, you should be aware of the three
important methods. The methods have been discussed
below. The appropriate method to use depends on
whether your function expects to operate on an entire
DataFrame, row- or column-wise, or element wise.
 Table wise Function Application: pipe()
 Row or Column Wise Function Application: apply()
 Element wise Function Application: applymap()
Function Application: pipe()
 Pipe() function performs the custom operation for the entire dataframe. In below
example we will using pipe() Function to add value 2 to the entire dataframe:
Import pandas as pd
import numpy as np
import math

# own function
def adder(adder1,adder2):
return adder1+adder2

#Create a Dictionary of series


d = {'Score_Math':pd.Series([66,57,75,44,31,67,85,33,42,62,51,47]),
'Score_Science':pd.Series([89,87,67,55,47,72,76,79,44,92,93,69])}

df = pd.DataFrame(d)
print df
print df.pipe(adder,2)
Row or Column Wise Function Application:
apply()
 apply() function performs the custom operation for either
row wise or column wise . In below example we will be
using apply() Function to find the mean of values across
rows and mean of values across columns:
import pandas as pd
import numpy as np
import math
#Create a DataFrame
d = {'Score_Math':pd.Series([66,57,75,44,31,67,85,33,42,62,51,47]),
'Score_Science':pd.Series([89,87,67,55,47,72,76,79,44,92,93,69])}

df = pd.DataFrame(d)
print df
print df.apply(np.mean,axis=1)
Element wise Function Application
in python pandas: applymap()
applymap() Function performs the specified operation
for all the elements the dataframe. we will be using the
same dataframe to depict example of applymap()
Function. We will be multiplying the all the elements
of dataframe by 2 as shown below:
import pandas as pd
import numpy as np
import math
# applymap() Function
print df.applymap(lambda x:x*2)
Matplotlib.pyplot
matplotlib.pyplot is a collection of command style functions that make
matplotlib work like MATLAB. Each pyplot function makes some change to a
figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a
plotting area, decorates the plot with labels, etc.
Example:
import matplotlib.pyplot as plt
plt.plot(x,y, color=“Green”)
plt.title(‘Graph Name')
plt.xlim(two values)
plt.xticks(x,[‘A’,’B’,’C’,’D’])
plt.yticks(x,[‘A’,’B’,’C’,’D’])
plt.legend(loc=‘upper left’)
plt.xlabel(‘x-axis name')
plt.ylabel(‘y-axis name')
plt.show()
LINE CHART
 A line chart or line graph is a type of chart which
displays information as a series of data points called
‘markers’ connected by straight line segments. It is a
basic type of chart common in many fields.
 A line chart is often used to visualize a trend in data
over intervals of time – a time series – thus the line is
often drawn chronologically. In these cases they are
known as run charts.
 SYNTAX:
plt.plot(x,y, color=red, marker =‘*’, markersize=5,
markeredgecolor=‘red’)
LINE CHART RESULT
BAR CHART
A bar chart or bar graph is a chart or graph that presents
categorical data with rectangular bars with heights or
lengths proportional to the values that they represent.
The bars can be plotted vertically or horizontally.
A bar graph shows comparisons among discrete
categories. One axis of the chart shows the specific
categories being compared, and the other axis represents
a measured value
SYNTAX:
plt.bar(x,y, width=0.5, color=‘red’ or ‘r’,align='center',
alpha=0.5)
BAR CHART RESULT
SCATTER CHART
Scatter plots are used to plot data points on horizontal and
vertical axis in the attempt to show how much one variable
is affected by another.
Each row in the data table is represented by a marker the
position depends on its values in the columns set on the X
and Y axes.
SYNTAX:
plt.scatter(x,y, s=None, c=None, marker=None)
x,y : Data
s : marker size
c : marker color
marker : Marker style like *,o
SCATTER CHART RESULT
PIE CHART
 A Pie Chart can only display one series of data. Pie
charts show the size of items (called wedge) in one
data series, proportional to the sum of the items. The
data points in a pie chart are shown as a percentage of
the whole pie.
 SYNTAX:
plt.pie(data, sizes, explode=explode, labels=labels,
autopct='%1.1f%%', shadow=True, startangle=90)
PIE CHART RESULT
HISTOGRAM CHART
 A histogram is a great tool for quickly assessing a probability distribution that is intuitively
understood by almost any audience.
 Python offers a handful of different options for building and plotting histograms. Most people
know a histogram by its graphical representation, which is similar to a bar graph:
 SYNTAX:
plt.hist(x,bins=None, cumulative=False, histtype=‘bar’, align=‘mid’, orientation=‘vertical’)

I. X : data
II. Bins : an integer is given, bins + 1 bin edges are calculated and returned,
consistent
III. Cumulative : If True, then a histogram is computed where each bin gives the counts
in that bin plus all bins for smaller values. The last bin gives the total number of
datapoints .
I. Histtype : {'bar', 'barstacked', 'step', 'stepfilled'}, optional.
II. Align : {'left', 'mid', 'right'}, optional
III. Orientation : {'horizontal', 'vertical'}, optional
BOX PLOT
 Boxplots are a measure of how well distributed the data
in a data set is. It divides the data set into three
quartiles.
 This graph represents :
1) Minimum,
2) Maximum,
3) Median,
4) Lower Quartile
5) Upper Quartile
Example:
plt.boxplot(x, vert=False, notch=True, showmeans=True, showbox=True)
BOX PLOT RESULT

You might also like