Python Notes
Python Notes
2 PYTHON PROGRAMMING
Unit-1
Data Types:
1|Page
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
In Python, numeric data type represent the data which has numeric value. Numeric value can be
integer, floating number or even complex numbers. These values are defined
as int, float and complex class in Python.
Integers – This value is represented by int class. It contains positive or negative whole
numbers (without fraction or decimal). In Python there is no limit to how long an integer
value can be.
Float – This value is represented by float class. It is a real number with floating point
representation. It is specified by a decimal point. Optionally, the character e or E followed by
a positive or negative integer may be appended to specify scientific notation.
Complex Numbers – Complex number is represented by complex class. It is specified as (real
part) + (imaginary part)j. For example – 2+3j
Example:
# Python program to demonstrate numeric value
a=5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
Output:
Type of a: <class 'int'>
In Python, sequence is the ordered collection of similar or different data types. Sequences
allows to store multiple values in an organized and efficient fashion. There are several sequence
types in Python –
String
List
Tuple
1) String
In Python, Strings are arrays of bytes representing Unicode characters. A string is a collection of
one or more characters put in a single quote, double-quote or triple quote. In python there is no
character data type, a character is a string of length one. It is represented by str class.
2) List
Lists are just like the arrays, declared in other languages which is a ordered collection of data. It
is very flexible as the items in a list do not need to be of the same type
3) Tuple
2|P a ge
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
tuple is also an ordered collection of Python objects. The only difference between tuple and list
is that tuples are immutable i.e. tuples cannot be modified after it is created. It is represented
by tuple class.
Boolean
Data type with one of the two built-in values, True or False. Boolean objects that are equal to
True are truthy (true), and those equal to False are falsy (false). But non-Boolean objects can be
evaluated in Boolean context as well and determined to be true or false. It is denoted by the
class bool.
Set
In Python, Set is an unordered collection of data type that is iterable, mutable and has no
duplicate elements. The order of elements in a set is undefined though it may consist of various
elements.
Dictionary
Dictionary in Python is an unordered collection of data values, used to store data values like a
map, which unlike other Data Types that hold only single value as an element, Dictionary
holds key:value pair. Key-value is provided in the dictionary to make it more optimized. Each
key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a
‘comma’.
Keywords:
Python keywords are special reserved words that have specific meanings and purposes and can’t
be used for anything but those specific purposes. In Python we have 35 keywords:
This is a logical operator it returns true if both the operands are true else
1 and return false.
This is also a logical operator it returns true if anyone operand is true else
2 Or return false.
3|P a ge
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
This is again a logical operator it returns True if the operand is false else
3 not return false.
Else is used with if and elif conditional statement the else block is executed
6 else if the given condition is not true.
4|P a ge
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
This function is used for debugging purposes. Usually used to check the
19 assert correctness of code
This is a special constant used to denote a null value or avoid. It’s important
to remember, 0, any empty container(e.g empty list) do not compute to
30 None None
Variables:
5|P a ge
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
• Variable is nothing but, the value that we assign for a letter or word.
• variables are a storage placeholder for texts and
numbers.
• Python is dynamically typed, which means that you don't have to
• declare what type each variable is.
Rules for variables:
• A variable name must start with a letter or the underscore character.
• A variable name cannot start with a number.
• A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ ).
• Variable names are case-sensitive (age, Age and AGE are three different variables).
• Example: x = “python”
y = “programming”
print(x+y)
//output = “python programming”
Identifiers:
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate
one entity from another.
Rules for writing identifiers
1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0
to 9) or an underscore _. Names like myClass, var_1 and print_this_to_screen, all are valid
example.
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is a valid name.
3. Keywords cannot be used as identifiers
Number Types: int, float, complex
Python includes three numeric types to represent numbers: integers, float, and complex
number.
Integer:
In Python, integers are zero, positive or negative whole numbers without a fractional part and
having unlimited precision, e.g. 0, 100, -10. The followings are valid integer literals in Python.
Integers can be binary, octal, and hexadecimal values.
Example:
>>> 0b11011000 # binary
216
>>> 0o12 # octal
10
>>> 0x12 # hexadecimal
15
All integer literals or variables are objects of the int class. Use the type() method to get the
class name
Binary
A number having 0b with eight digits in the combination of 0 and 1 represent the binary
numbers in Python. For example, 0b11011000 is a binary number equivalent to integer 216.
6|Page
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
>>> x=0b11011000
>>> x
216
>>> x=0b_1101_1000
>>> x
216
>>> type(x)
<class 'int'>
Octal:
A number having 0o or 0O as prefix represents an octal number. For example, 0O12 is
equivalent to integer 10.
>>> x=0o12
>>> x
10
>>> type(x)
<class 'int'>
Hexadecimal
A number with 0x or 0X as prefix represents hexadecimal number. For example, 0x12 is
equivalent to integer 18.
>>> x=0x12
>>> x
18
>>> type(x)
<class 'int'>
Floating Point
In Python, floating point numbers (float) are positive and negative real numbers with a
fractional part denoted by the decimal symbol . or the scientific notation E or e, e.g. 1234.56,
3.142, -1.55, 0.23
>>> f=1.2
>>> f
1.2
>>> type(f)
<class 'float'>
Floats can be separated by the underscore _, e.g. 123_42.222_013 is a valid float.
>>> f=123_42.222_013
>>> f
12342.222013
Floats has the maximum size depends on your system. The float beyond its maximum size
referred as "inf", "Inf", "INFINITY", or "infinity". Float 2e400 will be considered as infinity for
most systems.
>>> f=2e400
>>> f
7|Page
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
inf
Complex Number
A complex number is a number with real and imaginary components. For example, 5 + 6j is a
complex number where 5 is the real component and 6 multiplied by j is an imaginary
component.
Example:
>>> a=5+2j
>>> a
(5+2j)
>>> type(a)
<class 'complex'>
Collections Data Types: Tuples, Lists, Sets, dictionaries, Iterating and copying collections
List:
A list is a collection which is ordered and changeable. In Python lists are written with square
brackets.
• Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
• Output: ['apple', 'banana', 'cherry']
List Methods:
append(): The append() method appends an element to the end of the list
Syntax: list.append(elmnt)
Example:
fruits = ['apple', 'banana', 'cherry']
fruits.append("orange")
print(fruits)
Output: [‘apple’,’banana’,’cherry’,’orange’]
Clear(): The clear() method removes all the elements from a list
Syntax: list.clear()
Example:
fruits = ['apple', 'banana', 'cherry', 'orange']
fruits.clear()
print(fruits)
Output: []
Copy(): The copy() method returns a copy of the specified list
Syntax: list.copy()
Example:
fruits = ["apple", "banana", "cherry"]
x = fruits.copy()
print(x)
Output: [‘apple’,’banana’,’cherry’]
Count: The count() method returns the number of elements with the specified value.
Syntax: list.count(value)
8|P a ge
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example:
fruits = [1, 4, 2, 9, 7, 8, 9, 3, 1]
x = fruits.count(9)
print(x)
Output: 2
Extend(): The extend() method adds the specified list elements (or any iterable) to the end of the
current list
Syntax: list.extend(iterable)
Example:
fruits = ['apple', 'banana', 'cherry']
points = (1, 4, 5, 9)
fruits.extend(points)
print(fruits)
Output: ['apple', 'banana', 'cherry', 1, 4, 5, 9]
Index():
Returns the index of the first element with the specified value
Syntax: list.index(elmnt)
Example: What is the position of the value "cherry":
fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
Output: 2
Insert( ): The insert() method inserts the specified value at the specified position
Syntax: list.insert(pos, elmnt)
Example:
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
print(fruits)
Output: [‘apple’,’orange’,’banana’,’cherry’]
Pop( ): The pop() method removes the element at the specified position
Syntax: list.pop(pos)
Example:
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)
Output: ['apple','cherry']
Remove(): The remove() method removes the first occurrence of the element with the specified
value
Syntax: list.remove(elmnt)
Example:
fruits = ['apple', 'banana', 'cherry']
fruits.remove("banana")
print(fruits)
Output: [‘apple’,’cherry’]
Sort(): The sort() method sorts the list ascending by default.
Syntax: list.sort(reverse=True|False, key=myFunc)
9|Page
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example:
cars = ['Ford', 'BMW', 'Volvo']
cars.sort()
print(cars)
Output: ['BMW', 'Ford', 'Volvo']
Concatenate():
The concatenate() method used to add two lists
Example:
list1=[2,3,4]
list2 = [6,7,8]
list3 = list1 + list2
print(list3)
Output: [2,3,4,6,7,8]
Tuple:
A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round
brackets
Example:
Create a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Output: ('apple', 'banana', 'cherry')
Tuple Methods:
Count( ): The count() method returns the number of times a specified value appears in the tuple.
Syntax: tuple.count(value)
Example:
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.count(5)
print(x)
Output: 2
Index( ): The index() method finds the first occurrence of the specified value
The index() method raises an exception if the value is not found.
Syntax: tuple.index(value)
Example:
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.index(8)
print(x)
Output: 3
Dictionary:
10 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example:
Create and print a dictionary:
thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964}
print(thisdict)
Output: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
Accessing Items:
You can access the items of a dictionary by referring to its key name, inside square brackets
Dictionary Methods:
Clear( ): The clear() method removes all the elements from a dictionary
Syntax: dictionary.clear()
Example:
car = {"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.clear()
print(car)
Output:{}
Items( ):
The items() method returns a view object. The view
object contains the key-value pairs of the dictionary, as tuples in a list
Syntax: dictionary.items()
Example:
car = {"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x)
Output: dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
Popitem(): The popitem() method removes the item that was last inserted into the dictionary
11 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Setdefault(): The setdefault() method returns the value of the item with the specified key.
If key exists no effect,if not it assigns the same value.
Syntax: dictionary.setdefault(keyname, value)
Example:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.setdefault(“model”, “Bronco”)
print(x)
y = car.setdefault(“place”,”India”)
print(y)
print(car)
Output: Mustang
India
{“brand”:”Ford”, ”model”:”Mustang”,”year”: 1964,”place”:”India”}
Set:
A set is a collection which is unordered and un indexed. The set list is unordered, meaning: the
items will appear in a random order.
12 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Set Methods:
Add(): The add() method adds an element to the set.
Syntax: set.add(elmnt)
Example:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
Output: {'orange', 'cherry', 'banana', 'apple'}
Update(): The update() method updates the current set, by adding items from another set
Syntax: set.update(set)
Example: x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.update(y)
print(x)
13 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
a = {1,2,3}
b = {1,2,3,4,5}
a.issubset(b)
Output: True
issuperset():returns True if all items set y are present in set x
Syntax: set.issuperset(set)
Example:
x = {1,2,3,4,5,6}
y = {2,4,6}
x.issuperset(y)
Output: True
Deep Copy:
Deep copy is a process in which the copying process occurs recursively. It means first
constructing a new collection object and then recursively populating it with copies of the child
objects found in the original. In case of deep copy, a copy of object is copied in other object. It
means that any changes made to a copy of object do not reflect in the original object. In
python, this is implemented using “deepcopy()” function.
Deep copy
15 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example:
import copy
# initializing list 1
li1 = [1, 2, [3,5], 4]
print("\r")
# Change is reflected in l2
print ("The new list of elements after deep copying ")
for i in range(0,len( li1)):
print (li2[i],end=" ")
print("\r")
# Change is NOT reflected in original list
# as it is a deep copy
print ("The original elements after deep copying")
for i in range(0,len( li1)):
print (li1[i],end=" ")
Output:
The original elements before deep copying
1 2 [3, 5] 4
The new list of elements after deep copying
1 2 [7, 5] 4
The original elements after deep copying
1 2 [3, 5] 4
16 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Shallow copy
A shallow copy means constructing a new collection object and then populating it with
references to the child objects found in the original. The copying process does not recurse and
therefore won’t create copies of the child objects themselves. In case of shallow copy, a
reference of object is copied in other object. It means that any changes made to a copy of
object do reflect in the original object. In python, this is implemented using “copy()” function.
Example:
import copy
# initializing list 1
li1 = [1, 2, [3,5], 4]
print("\r")
17 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
The original elements before shallow copying
1 2 [3, 5] 4
The original elements after shallow copying
1 2 [7, 5] 4
Strings:
In Python, Strings are arrays of bytes representing Unicode characters. However, Python does
not have a character data type, a single character is simply a string with a length of 1. Square
brackets can be used to access elements of the string.
Creating a String
Strings in Python can be created using single quotes or double quotes or even triple quotes.
# Python Program for
# Creation of String
# Creating a String
# with single Quotes
String1 = 'Welcome to the Geeks World'
print("String with the use of Single Quotes: ")
print(String1)
# Creating a String
# with double Quotes
String1 = "I'm a Geek"
print("\nString with the use of Double Quotes: ")
print(String1)
# Creating a String
# with triple Quotes
String1 = '''I'm a Geek and I live in a world of "Geeks"'''
print("\nString with the use of Triple Quotes: ")
print(String1)
18 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
String with the use of Single Quotes:
Welcome to the Live World
Slicing
Specify the start index and the end index, separated by a colon, to return a part of the string.
Example
b = "Hello, World!"
print(b[2:5])
Output:
llo
19 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
By leaving out the start index, the range will start at the first character:
Example
b = "Hello, World!"
print(b[:5])
Output:
Hello
By leaving out the end index, the range will go to the end:
Example
Get the characters from position 2, and all the way to the end:
b = "Hello, World!"
print(b[2:])
Output:
llo, World!
Negative Indexing
Use negative indexes to start the slice from the end of the string:
Example
b = "Hello, World!"
print(b[-5:-2])
20 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
Orl
Modify Strings
Python has a set of built-in methods that you can use on strings.
Upper Case
Example
a = "Hello, World!"
print(a.upper())
Output:
HELLO, WORLD!
Lower Case
Example
a = "Hello, World!"
print(a.lower())
Output:
hello, world!
Remove Whitespace
Whitespace is the space before and/or after the actual text, and very often you want to remove
this space.
21 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example
The strip() method removes any whitespace from the beginning or the end:
Output:
Hello, World!
Replace String
Example
a = "Hello, World!"
print(a.replace("H", "J"))
Output:
Jello, World!
Split String
The split() method returns a list where the text between the specified separator becomes the list
items.
Example
The split() method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
Output:
String Concatenation
22 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example
a = "Hello"
b = "World"
c=a+b
print(c)
Output:
HelloWorld
Example
a = "Hello"
b = "World"
c=a+""+b
print(c)
Output:
Hello World
Escape Character
An example of an illegal character is a double quote inside a string that is surrounded by double
quotes:
Example
The escape character allows you to use double quotes when you normally would not be allowed:
Escape Characters
23 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Code Result
\\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f Form Feed
24 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
String methods:
Description
Method
endswith() Returns true if the string ends with the specified value
find() Searches the string for a specified value and returns the position of where it
was found
index() Searches the string for a specified value and returns the position of where it
was found
isalpha() Returns True if all characters in the string are in the alphabet
islower() Returns True if all characters in the string are lower case
25 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
isupper() Returns True if all characters in the string are upper case
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it
was found
rindex() Searches the string for a specified value and returns the last position of where it
was found
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
split() Splits the string at the specified separator, and returns a list
26 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
startswith() Returns true if the string starts with the specified value
swapcase() Swaps cases, lower case becomes upper case and vice versa
zfill() Fills the string with a specified number of 0 values at the beginning
Unit-2:
Python Control Structures, Functions and OOP:Control Structures and Functions: Conditional
Branching, Looping, Exception Handling, Custom Fuctions
Python Library Modules: random, math, time, os, shutil, sys, glob, re, statistics,creating a
custom module Object Oriented Programming: Object Oriented Concepts and Terminology,
Custom Classes, Attributes and Methods, Inheritance and Polymorphism, Using Properties to
Control Attribute Access
File Handling: Writing and Reading Binary Data, Writing and Parsing Text Files
27 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Decisions in a program are used when the program has conditional choices to execute a code
block.
.
Python provides various types of conditional statements:
Statement Description
if Statements It consists of a Boolean expression which
results are either TRUE or FALSE, followed
by one or more statements.
if else Statements It also contains a Boolean expression. The if
the statement is followed by an optional else
statement & if the expression results in
FALSE, then else statement gets executed. It
is also called alternative execution in which
there are two possibilities of the condition
determined in which any one of them will get
executed.
Nested Statements We can implement if statement and or if-else
statement inside another if or if - else
statement. Here more than one if conditions
are applied & there can be more than one if
within elif.
Python Conditional Statements
If Statement
The decision-making structures can be recognized and understood using flowcharts.
Figure - If condition Flowchart:
28 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Syntax :
if expression:
#execute your code
Example :
a = 15
if a > 10:
print("a is greater")
Output
a is greater
ifelse Statements
Figure - If else condition Flowchart:
Syntax :
if expression:
#execute your code
else:
#execute your code
Source Code
a = 15
b = 20
if a > b:
print("a is greater")
else:
print("b is greater")
Output
29 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
b is greater
elif - is a keyword used in Python replacement of else if to place another condition in the program.
This is called chained conditional.
Figure - elif condition Flowchart:
Syntax :
if expression:
#execute your code
elif expression:
#execute your code
else:
#execute your code
Example :
a = 15
b = 15
if a > b:
print("a is greater")
elif a == b:
print("both are equal")
else:
30 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
print("b is greater")
Output :
both are equal
Single Statement Condition
If the block of an executable statement of if - clause contains only a single line, programmers can
write it on the same line as a header statement.
Example
a = 15
Loops
In programming, loops are a sequence of instructions that does a specific set of instructions or
tasks based on some conditions and continue the tasks until it reaches certain conditions.
Loop Description
for Loop This is traditionally used when programmers had a piece of code and
wanted to repeat that 'n' number of times.
while Loop The loop gets repeated until the specific Boolean condition is met.
Nested Loops Programmers can use one loop inside another; i.e., they can use for loop
inside while or vice - versa or for loop inside for loop or while inside while.
Python Loops
For Loop
31 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Syntax :
Example 1
Source Code
OUTPUT
Loop execution 0
Loop execution 1
Loop execution 2
32 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example 2
Source Code
OUTPUT
While Loop
The graphical representation of the logic behind while looping is shown below:
Figure - while loop Flowchart:
33 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Syntax
while expression:
Example
Source Code
OUTPUT
34 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Nested Loops
Syntax
Example
Source Code
OUTPUT
1*1=1
1*2=2
2*1=2
2*2=4
35 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
3*1=3
3*2=6
4*1=4
4*2=8
5*1=5
5 * 2 = 10
These statements are used to change execution from its normal sequence.
Python supports three types of loop control statements:
Python Loop Control Statements
Break statement It is used to exit a while loop or a for a loop. It terminates the looping
& transfers execution to the statement next to the loop.
Continue statement It causes the looping to skip the rest part of its body & start re-
testing its condition.
Break Statement :
Syntax
Break
Source Code
count = 0
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
break
OUTPUT
Continue Statement
Syntax :
continue
Example
Source Code
for x in range(10):
#check whether x is even
if x % 2 == 0:
continue
print (x)
OUTPUT
37 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Pass Statement
Syntax
Pass
Source Code
if letter == 'P':
pass
OUTPUT
38 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Source Code
if letter == 'h':
pass
OUTPUT
39 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Source Code
if letter == 'm':
pass
OUTPUT
40 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Python Functions
A function is a block of code which only runs when it is called.You can pass
data, known as parameters, into a function.
A function can return data as a result.
Creating a Function
Example
def my_function():
print("Hello from a function")
Calling a Function
To call a function, use the function name followed by parenthesis:
Example
Source Code
def my_function():
print("Hello from a function")
my_function()
Output
41 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Arguments
Arguments are specified after the function name, inside the parentheses. You can add as many
arguments as you want, just separate them with a comma
Source Code
def my_function(fname):
print(fname + " Sirivennela")
my_function("Seetha")
my_function("Rama")
my_function("Shastry")
Output
Number of Arguments
By default, a function must be called with the correct number of arguments. Meaning that if
your function expects 2 arguments, you have to call the functionwith 2 arguments, not more,
and not less.
Example
This function expects 2 arguments, and gets 2 arguments:
Source Code
def my_function(fname, lname):
print(fname + " " + lname)
42 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
If you do not know how many arguments that will be passed into your function,add a * before
the parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access theitems
accordingly:
Example
If the number of arguments is unknown, add a * before the parameter name:
Source Code
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("one", "Two", "three")
Output
Keyword Arguments
You can also send arguments with the key = value syntax.This way the
order of the arguments does not matter.
Source Code
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "hi1", child2 = "hello2", child3 = "hii3")
Output
43 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
If you do not know how many keyword arguments that will be passed into yourfunction, add
two asterisk: ** before the parameter name in the function definition.
This way the function will receive a dictionary of arguments, and can access theitems
accordingly:
Source Code
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = "lilly", lname = "jasmine")
Output
The following example shows how to use a default parameter value.If we call the
function without argument, it uses the default value:
Example
Source Code
def my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India") my_function()
my_function("Brazil")
Output
44 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
You can send any data types of argument to a function (string, number, list, dictionary etc.), and
it will be treated as the same data type inside the function.
Source Code
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
Output
Recursion
Python also accepts function recursion, which meansdefined function can call itself
Source Code
def tri_recursion(k):
result = k + tri_recursion(k - 1)
print(result)
else:
result = 0 return
result
print("\n\nRecursion Example Results")
45 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
tri_recursion(6)
Python Exception
Whenever an exception occurs, the program stops the execution, and thus the further code is not
executed. Therefore, an exception is the run-time errors that are unable to handle to Python
script. An exception is a Python object that represents an error
Python provides a way to handle the exception so that the code can be executed without any
interruption. If we do not handle the exception, the interpreter doesn't execute all the code that
exists after the exception.
Python has many built-in exceptions that enable our program to run without interruption and
give the output. These exceptions are given below:
Common Exceptions
Python provides the number of built-in exceptions, but here we are describing the common
standard exceptions. A list of common exceptions that can be thrown from a standard Python
program is given below.
If the Python program contains suspicious code that may throw the exception, we must place
that code in the try block. The try block must be followed with the except statement, which
contains a block of code that will be executed if there is some exception in the try block.
46 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Syntax
try:
#block of code
except Exception1:
#block of code
except Exception2:
#block of code
#other code
Example:
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
except:
print("Can't divide with zero")
Output:
Enter a:10
Enter b:0
Can't divide with zero
The syntax to use the else statement with the try-except statement is given below.
47 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
try:
#block of code
except Exception1:
#block of code
else:
#this code executes if no except block is executed
Example
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
print("a/b = %d"%c)
# Using Exception with except statement. If we print(Exception) it will return exception class
except Exception:
print("can't divide by zero")
print(Exception)
else:
print("Hi I am else block")
Output:
48 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Enter a:10
Enter b:0
can't divide by zero
<class 'Exception'>
Python provides the flexibility not to specify the name of exception with the exception
statement.
Example
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b;
print("a/b = %d"%c)
except:
print("can't divide by zero")
else:
print("Hi I am else block")
We can use the exception variable with the except statement. It is used by using the as keyword.
this object will return the cause of the exception. Consider the following example:
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
print("a/b = %d"%c)
# Using exception object with the except statement
except Exception as e:
print("can't divide by zero")
print(e)
else:
print("Hi I am else block")
Output:
49 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Enter a:10
Enter b:0
can't divide by zero
division by zero
The Python random module functions depend on a pseudo-random number generator function
random(), which generates the float number between 0.0 and 1.0.
There are different types of functions used in a random module which is given below:
random.random():This function generates a random float number between 0.0 and 1.0.
Example:
import random
Output:
random.shuffle():
randrange(beg,end,step):
This function is used to generate a number within the range specified in its argument. It accepts
three arguments, beginning number, last number, and step, which is used to skip a number in the
range. Consider the following example.
50 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
import random
Output:
random.seed():
This function is used to apply on the particular random number with the seed argument. It returns
the mapper value. Consider the following example.
import random
# between 0 and 1
print(random.random())
Output:
Python math module is defined as the most famous mathematical functions, which includes
trigonometric functions, representation functions, logarithmic functions, etc. Furthermore, it also
defines two mathematical constants, i.e., Pie and Euler number, etc.
51 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Pie (n): It is a well-known mathematical constant and defined as the ratio of circumstance to the
diameter of a circle. Its value is 3.141592653589793.
Euler's number(e): It is defined as the base of the natural logarithmic, and its value is
2.718281828459045.
math.log():
This method returns the natural logarithm of a given number. It is calculated to the base e.
Example:
import math
Output:
math.log10():
This method returns base 10 logarithm of the given number and called the standard logarithm.
Example
import math
Output:
log10(x) is : 1.1139433523068367
math.exp():
This method returns a floating-point number after raising e to the given number.
Example
52 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
import math
Output:
math.pow(x,y):
This method returns the power of the x corresponding to the value of y. If value of x is negative or
y is not integer value than it raises a ValueError.
Example
import math
number = math.pow(10,2)
Output:
math.floor(x):
This method returns the floor value of the x. It returns the less than or equal value to x.
Example:
import math
number = math.floor(10.25201)
Output:
53 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
math.ceil(x):
This method returns the ceil value of the x. It returns the greater than or equal value to x.
import math
number = math.ceil(10.25201)
Output:
math.fabs(x):
Example:
import math
number = math.fabs(10.001)
Output:
math.factorial():
This method returns the factorial of the given number x. If x is not integral, it raises a ValueError.
Example
import math
number = math.factorial(7)
Output:
54 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
PYTHON OS MODULE:
Python OS module provides the facility to establish the interaction between the user and the
operating system. It offers many useful OS functions that are used to perform OS-based tasks and
get related information about operating system.The OS comes under Python's standard utility
modules. This module offers a portable way of using operating system dependent functionality.
The Python OS module lets us work with the files and directories.To work with the OS module, we
need to import the OS module.
import os
os.name():
This function provides the name of the operating system module that it imports.
Example
import os
print(os.name)
Output:
nt
os.mkdir():
The os.mkdir() function is used to create new directory. Consider the following example.
import os
os.mkdir("d:\\newdir") :It will create the new directory to the path in the string argument of the
function in the D drive named folder newdir.
os.getcwd():
55 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example
import os
print(os.getcwd())
Output:
C:\Users\Python\Desktop\ModuleOS
os.chdir():
The os module provides the chdir() function to change the current working directory.
import os
os.rmdir():
The rmdir() function removes the specified directory with an absolute or related path. First, we
have to change the current working directory and remove the folder.
Example
import os
# It will throw a Permission error; that's why we have to change the current working directory.
os.rmdir("d:\\newdir")
os.chdir("..")
os.rmdir("newdir")
os.error():
The os.error() function defines the OS level errors. It raises OSError in case of invalid or
inaccessible file names and path etc.
Example
import os
56 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
try:
filename = 'Python.txt'
f = open(filename, 'rU')
text = f.read()
f.close()
except IOError:
Output:
os.popen():
This function opens a file or from the command specified, and it returns a file object which is
connected to a pipe.
Example
import os
fd = "python.txt"
file.write("This is awesome")
file.close()
57 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
text = file.read()
print(text)
file.write("This is awesome")
Output:
This is awesome
os.close():
Example
import os
fr = "Python1.txt"
text = file.read()
print(text)
os.close(file)
Output:
58 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
os.rename():
A file or directory can be renamed by using the function os.rename(). A user can rename the file if
it has privilege to change the file.
Example
import os
fd = "python.txt"
os.rename(fd,'Python1.txt')
os.rename(fd,'Python1.txt')
Output:
os.rename(fd,'Python1.txt')
FileNotFoundError: [Errno 2]
The python sys module provides functions and variables which are used to manipulate different
parts of the Python Runtime Environment. It lets us access system-specific parameters and
functions.
import sys
First, we have to import the sys module in our program before running any functions.
sys.modules:This function provides the name of the existing python modules which have been
imported.
sys.argv:This function returns a list of command line arguments passed to a Python script. The
name of the script is always the item at index 0, and the rest of the arguments are stored at
subsequent indices.
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
sys.base_prefix:It is set up during Python startup, before site.py is run, to the same value as
prefix.
sys.path:This function shows the PYTHONPATH set in the current system. It is an environment
variable that is a search path for all the python modules.
sys.stdin:It is an object that contains the original values of stdin at the start of the program and
used during finalization. It can restore the files.
sys.exit:This function is used to exit from either the Python console or command prompt, and also
used to exit from the program in case of an exception.
sys executable:The value of this function is the absolute path to a Python interpreter. It is useful
for knowing where python is installed on someone else machine.
sys.platform:This value of this function is used to identify the platform on which we are working.
Python statistics module provides the functions to mathematical statistics of numeric data. There
are some popular statistical functions defined in this module.
mean() function: The mean() function is used to calculate the arithmetic mean of the numbers in
the list.
Example:
import statistics
datasets = [5, 2, 7, 4, 2, 6, 8]
x = statistics.mean(datasets)
60 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
print("Mean is :", x)
Output:
Mean is : 4.857142857142857
median() function
:The median() function is used to return the middle value of the numeric data in the list.
Example
import statistics
# random data-set
% (statistics.median(datasets)))
Output:
mode() function:
The mode() function returns the most common data that occurs in the list.
Example
import statistics
dataset =[2, 4, 7, 7, 2, 2, 3, 6, 6, 8]
61 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
Calculated Mode 2
stdev() function:
The stdev() function is used to calculate the standard deviation on a given sample which is
available in the form of the list.
Example
import statistics
% (statistics.stdev(sample)))
Output:
median_low():
The median_low function is used to return the low median of numeric data in the list.
Example
import statistics
set1 = [4, 6, 2, 5, 7, 7]
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
% (statistics.median_low(set1)))
Output:
median_high():
The median_high function is used to return the high median of numeric data in the list.
Example:
import statistics
dataset = [2, 1, 7, 6, 1, 9]
% (statistics.median_high(dataset)))
Output:
SHUTIL MODULE:
Shutil module offers high-level operation on a file like a copy, create, and remote operation on the
file. It comes under Python’s standard utility modules. This module helps in automating the
process of copying and removal of files and directories.
shutil.copy() method in Python is used to copy the content of the source file to the destination
file or directory. It also preserves the file’s permission mode but other metadata of the file like the
file’s creation and modification times is not preserved.
The source must represent a file but the destination can be a file or a directory. If the destination
is a directory then the file will be copied into the destination using the base filename from the
source. Also, the destination must be writable. If the destination is a file and already exists then it
will be replaced with the source file otherwise a new file will be created.
Syntax: shutil.copy(source, destination, *, follow_symlinks = True)
Parameter:
source: A string representing the path of the source file.
destination: A string representing the path of the destination file or directory.
63 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
follow_symlinks (optional) : The default value of this parameter is True. If it is False and
source represents a symbolic link then destination will be created as a symbolic link.
Return Type: This method returns a string which represents the path of newly created file.
Example
source = "path/main.py"
destination ="path/main2.py"
Output:
Destination path: path/main2.py
shutil.copy2() method in Python is used to copy the content of the source file to the destination
file or directory. This method is identical to shutil.copy() method but it also tries to preserve the
file’s metadata.
Syntax: shutil.copy2(source, destination, *, follow_symlinks = True)
Parameter:
source: A string representing the path of the source file.
destination: A string representing the path of the destination file or directory.
follow_symlinks (optional) : The default value of this parameter is True. If it is False and
source represents a symbolic link then it attempts to copy all metadata from the source
symbolic link to the newly-created destination symbolic link. This functionality is platform
dependent.
Return Type: This method returns a string which represents the path of newly created file.
64 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# importing os module
import os
# path
path = 'csv/'
# Source path
source = "csv/main.py"
# Destination path
destination = "csv/gfg/check.txt"
65 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
[‘archive (2)’, ‘c.jpg’, ‘c.PNG’, ‘Capture.PNG’, ‘cc.jpg’, ‘check.zip’, ‘cv.csv’, ‘d.png’, ‘Done! Terms And
Conditions Generator – The Fastest Free Terms and Conditions Generator!.pdf’, ‘file1.csv’, ‘gfg’,
‘haarcascade_frontalface_alt2.xml’, ‘log_transformed.jpg’, ‘main.py’, ‘nba.csv’, ‘new_gfg.png’,
‘r.gif’, ‘Result -_ Terms and Conditions are Ready!.pdf’, ‘rockyou.txt’, ‘sample.txt’]
Metadata: os.stat_result(st_mode=33206, st_ino=2251799814202896, st_dev=1689971230, st_nlink=1,
st_uid=0, st_gid=0, st_size=1916, st_atime=1612953710, st_mtime=1612613202, st_ctime=1612522940)
shutil.copyfile() method in Python is used to copy the content of the source file to the destination
file. The metadata of the file is not copied. Source and destination must represent a file and
destination must be writable. If the destination already exists then it will be replaced with the
source file otherwise a new file will be created.
If source and destination represent the same file then SameFileError exception will be raised.
Parameter:
source: A string representing the path of the source file.
destination: A string representing the path of the destination file.
follow_symlinks (optional) : The default value of this parameter is True. If False and
source represents a symbolic link then a new symbolic link will be created instead of
copying the file.
Return Type: This method returns a string which represents the path of newly created file.
# Source path
source = "csv/main.py"
66 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# Destination path
destination = "csv/gfg/main_2.py"
Output:
Destination path: csv/gfg/main_2.py
shutil.copytree() method recursively copies an entire directory tree rooted at source (src) to the
destination directory. The destination directory, named by (dst) must not already exist. It will be
created during copying.
Syntax:
shutil.copytree(src, dst, symlinks = False, ignore = None, copy_function = copy2,
igonre_dangling_symlinks = False)
Parameters:
src: A string representing the path of the source directory.
dest: A string representing the path of the destination.
symlinks (optional) : This parameter accepts True or False, depending on which the metadata of the
original links or linked links will be copied to the new tree.
ignore (optional) : If ignore is given, it must be a callable that will receive as its arguments the
directory being visited by copytree(), and a list of its contents, as returned by os.listdir().
copy_function (optional): The default value of this parameter is copy2. We can use other copy
function like copy() for this parameter.
igonre_dangling_symlinks (optional) : This parameter value when set to True is used to put a silence
on the exception raised if the file pointed by the symlink doesn’t exist.
Return Value: This method returns a string which represents the path of newly created directory.
# path
path = 'C:/Users/ksaty/csv/gfg'
67 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
print(os.listdir(path))
# Source path
src = 'C:/Users/ksaty/csv/gfg'
# Destination path
dest = 'C:/Users/ksaty/csv/gfg/dest'
Output:
import shutil
import os
68 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# location
location = "csv/gfg/"
# directory
dir = "dest"
# path
path = os.path.join(location, dir)
# removing directory
shutil.rmtree(path)
Finding files
shutil.which() method tells the path to an executable application that would be run if the
given cmd was called. This method can be used to find a file on a computer which is present on
the PATH.
Syntax: shutil.which(cmd, mode = os.F_OK | os.X_OK, path = None)
Parameters:
cmd: A string representing the file.
mode: This parameter specifies mode by which method should execute. os.F_OK tests existence of
the path and os.X_OK Checks if path can be executed or we can say mode determines if the file exists
and executable.
path: This parameter specifies the path to be used, if no path is specified then the results of
os.environ() are used
Return Value: This method returns the path to an executable application
# file search
cmd = 'anaconda'
# Print result
print(locate)
Output:
D:\Installation_bulk\Scripts\anaconda.EXE
69 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Python has a module named time to handle time-related tasks. To use functions defined in the
module, we need to import the module first. Here's how:
import time
import time
seconds = time.time()
print("Seconds since epoch =", seconds)
Python time.ctime()
The time.ctime() function takes seconds passed since epoch as an argument and returns a string
representing local time.
import time
Python time.sleep()
The sleep() function suspends (delays) execution of the current thread for the given number of
seconds.
import time
70 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
time.struct_time Class
Several functions in the time module such as gmtime(), asctime() etc. either
take time.struct_time object as an argument or return it.
1 tm_mon 1, 2, ..., 12
2 tm_mday 1, 2, ..., 31
3 tm_hour 0, 1, ..., 23
4 tm_min 0, 1, ..., 59
5 tm_sec 0, 1, ..., 61
8 tm_isdst 0, 1 or -1
The values (elements) of the time.struct_time object are accessible using both indices and
attributes.
Python time.localtime()
The localtime() function takes the number of seconds passed since epoch as an argument and
returns struct_time in local time.
71 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
import time
result = time.localtime(1545925769)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)
Python time.gmtime()
The gmtime() function takes the number of seconds passed since epoch as an argument and
returns struct_time in UTC.
import time
result = time.gmtime(1545925769)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)
year = 2018
tm_hour = 8
With the help of the Python glob module, we can search for all the path names which are looking
for files matching a specific pattern (which is defined by us). The specified pattern for file
matching is defined according to the rules dictated by the Unix shell. The result obtained by
following these rules for a specific pattern file matching is returned in the arbitrary order in the
output of the program. While using the file matching pattern, we have to fulfil some requirements
of the glob module because the module can travel through the list of the files at some location in
our local disk.
72 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
In Python, we have several functions which we can use to list down the files that match with the
specific pattern which we have defined inside the function in a program. With the help of these
functions, we can get the result list of the files which will match the given pattern in the specified
folder in an arbitrary order in the output.
1. fnmatch()
2. scandir()
3. path.expandvars()
4. path.expanduser()
The first two functions present in the above-given list, i.e., fnmatch.fnmatch() and os.scandir()
function, is actually used to perform the pattern matching task and not by invoking the sub-shell
in the Python. These two functions perform the pattern matching task and get the list of all
filenames and that too in arbitrary order
Rules of Pattern
We have to follow a specific set of rules while defining the pattern for the filename pattern
matching functions in the glob module.
Following are set of rules for the pattern that we define inside the glob module's pattern
matching functions:
o We have to follow all the standard set of rules of the UNIX path expansion in the pattern
matching.
o The path we define inside the pattern should be either absolute or relative, and we can't
define any unclear path inside the pattern.
o The special characters allowed inside the pattern are only two wild-cards, i.e., '*, ?' and the
normal characters that can be expressed inside the pattern are expressed in [].
o The rules of the pattern for glob module functions are applied to the filename segment
(which is provided in the functions), and it stops at the path separator, i.e., '/' of the files.
.
GLOB PYTHON MODULE:
1. iglob()
2. glob()
3. escape()
1. iglob() Function: The iglob() function of the glob module is very helpful in yielding the arbitrary
values of the list of files in the output. We can create a Python generator with the iglob() method.
We can use the Python generator created by the glob module to list down the files under a given
directory. This function also returns an iterator when called, and the iterator returned by it yields
the values (list of files) without storing all of the filenames simultaneously.
Syntax:
1. iglob(pathname, *, recursive=False)
As we can see in the syntax of iglob() function, it takes a total of three parameters in it, which can
be defined as given below:
(i) pathname: The pathname parameter is the optional parameter of the function, and we can
even leave it while we are working on the file directory that is the same as where our Python is
73 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
installed. We have to define the pathname from where we have to collect the list of files that
following a similar pattern (which is also defined inside the function).
(ii) recursive: It is also an optional parameter for the iglob() function, and it takes only bool values
(true or false) in it. The recursive parameter is used to set if the function is following the recursive
approach for finding file names or not.
(iii) '*': This is the mandatory parameter of the iglob() function as here we have to define the
pattern for which the iglob() function will collect the file names and list them down in the output.
The pattern we define inside the iglob() function (such as the extension of file) for the pattern
matching should start with the '*' symbol.
Now, let's use this iglob() function in an example program so that we can understand its
implementation and function in a better way.
Example :
:
1. # Import glob module in the program
2. import glob as gb
3. # Initialize a variable
4. inVar = gb.iglob("*.py") # Set Pattern in iglob() function
5. # Returning class type of variable
6. print(type(inVar))
7. # Printing list of names of all files that matched the pattern
8. print("List of the all the files in the directory having extension .py: ")
9. for py in inVar:
10. print(py)
Output:
<class 'generator'>
List of the all the files in the directory having extension .py:
adding.py
changing.py
code#1.py
code#2.py
code-3.py
code-4.py
code.py
code37.py
code_5.py
code_6.py
configuring.py
2. glob() Function: With the help of the glob() function, we can also get the list of files that
matching a specific pattern (We have to define that specific pattern inside the function). The list
returned by the glob() function will be a string that should contain a path specification according
to the path we have defined inside the function. The string or iterator for glob() function actually
returns the same value as returned by the iglob() function without actually storing these values
(filenames) in it.
Syntax:
74 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example
# Import glob module in the program
import glob as gb
# Initialize a variable
genVar = gb.glob("*.py") # Set Pattern in glob() function
# Printing list of names of all files that matched the pattern
print("List of the all the files in the directory having extension .py: ")
for py in genVar:
print(py)
Output:
List of the all the files in the directory having extension .py:
adding.py
changing.py
code#1.py
code#2.py
code-3.py
code-4.py
code.py
code37.py
code_5.py
code_6.py
configuring.py
.
3. escape() Function: The escape() becomes very impactful as it allows us to escape the given
character sequence, which we defined in the function. The escape() function is very handy for
locating files that having certain characters (as we will define in the function) in their file names. It
will match the sequence by matching an arbitrary literal string in the file names with that special
character in them.
Syntax:
1. >> escape(pathname)
Example
# Import glob module in the program
import glob as gb
# Initialize a variable
charSeq = "-_#"
print("Following is the list of filenames that match the special character sequence of escape fu
nction: ")
# Using nested for loop to get the filenames
for splChar in charSeq:
# Pathname for the glob() function
escSet = "*" + gb.escape(splChar) + "*" + ".py"
# Printing list of filenames with glob() function
for py in (gb.glob(escSet)):
75 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
print(py)
Output:
Following is the list of filenames that match the special character sequence of escape function:
code-3.py
code-4.py
code_5.py
code_6.py
code#1.py
code#2.py
The regular expressions can be defined as the sequence of characters which are used to search
for a pattern in a string. The module re provides the support to use regex in the python program.
The re module throws an exception if there is some error while using the regular expression.
The re module must be imported to use the regex functionalities in python.
1. import re
Regex Functions
The following regex functions are used in the python.
SN Function Description
1 match This method matches the regex pattern in the string with the optional flag. It
returns true if a match is found in the string otherwise it returns false.
2 search This method returns the match object if there is a match found in the string.
3 findall It returns a list that contains all the matches of a pattern in the string.
4 split Returns a list in which the string has been split in each match.
A regular expression can be formed by using the mix of meta-characters, special sequences, and
sets.
Meta-Characters
76 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Special Sequences
Special sequences are the sequences containing \ followed by one of the characters.
Character Description
\A It returns a match if the specified characters are present at the beginning of the string.
\b It returns a match if the specified characters are present at the beginning or the end of
the string.
\B It returns a match if the specified characters are present at the beginning of the string
but not at the end.
\S It returns a match if the string doesn't contain any white space character.
77 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
\Z Returns a match if the specified characters are at the end of the string.
Sets
A set is a group of characters given inside a pair of square brackets. It represents the special
meaning.
SN Set Description
1 [arn] Returns a match if the string contains any of the specified characters in the set.
2 [a-n] Returns a match if the string contains any of the characters between a to n.
3 [^arn] Returns a match if the string contains the characters except a, r, and n.
4 [0123] Returns a match if the string contains any of the specified digits.
5 [0-9] Returns a match if the string contains any digit between 0 and 9.
6 [0-5][0-9] Returns a match if the string contains any digit between 00 and 59.
10 [a-zA-Z] Returns a match if the string contains any alphabet (lower-case or upper-case).
There are the following methods associated with the Match object.
1. span(): It returns the tuple containing the starting and end position of the match.
2. string(): It returns a string passed into the function.
3. group(): The part of the string is returned where the match is found.
Example
import re
print(matches.span())
print(matches.group())
78 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
print(matches.string)
Output:
(0, 3)
How
How are you. How is everything
Class − A user-defined prototype for an object that defines a set of attributes that
characterize any object of the class. The attributes are data members (class variables and
instance variables) and methods, accessed via dot notation.
Class variable − A variable that is shared by all instances of a class. Class variables are
defined within a class but outside any of the class's methods. Class variables are not used
as frequently as instance variables are.
Data member − A class variable or instance variable that holds data associated with a class
and its objects.
Function overloading − The assignment of more than one behavior to a particular
function. The operation performed varies by the types of objects or arguments involved.
Instance variable − A variable that is defined inside a method and belongs only to the
current instance of a class.
Inheritance − The transfer of the characteristics of a class to other classes that are derived
from it.
Instance − An individual object of a certain class. An object obj that belongs to a class
Circle, for example, is an instance of the class Circle.
Instantiation − The creation of an instance of a class.
Method − A special kind of function that is defined in a class definition.
Object − A unique instance of a data structure that's defined by its class. An object
comprises both data members (class variables and instance variables) and methods.
Operator overloading − The assignment of more than one function to a particular
operator.
79 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Attributes of a class are function objects that define corresponding methods of its
instances. They are used to implement access controls of the classes.Attributes of a class can
also be accessed using the following built-in methods and functions :
1. getattr() – This function is used to access the attribute of object.
2. hasattr() – This function is used to check if an attribute exist or not.
3. setattr() – This function is used to set an attribute. If the attribute does not exist,
then it would be created.
4. delattr() – This function is used to delete an attribute. If you are accessing the
attribute after deleting it raises error “class has no attribute”.
# sets an attribute
setattr(e1,'height',152)
152
Static methods : A static method is a method[member function] that don’t use argument self at
all. To declare a static method, proceed it with the statement “@staticmethod”.
80 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
9
Accessing attributes and methods of one class in another class
Accessing attributes and methods of one class in another class is done by passing the object of
one class to another.
Explained with the example given below :
class ClassA():
def __init__(self):
self.var1 = 1
self.var2 = 2
def methodA(self):
self.var1 = self.var1 + self.var2
return self.var1
class ClassB(ClassA):
def __init__(self, class_a):
self.var1 = class_a.var1
self.var2 = class_a.var2
object1 = ClassA()
# updates the value of var1
81 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
summ = object1.methodA()
3
3INHERITANCE AND POLYMORPHISM:
Inheritance is a mechanism which allows us to create a new class - known as child class - that
is based upon an existing class - the parent class, by adding new attributes and methods on top of
the existing class. When you do so, the child class inherits attributes and methods of the parent
class.
By using inheritance, we can abstract out common properties to a general Shape class (parent
class) and then we can create child classes such as Rectangle, Triangle and Circle that inherits
from the Shape class. A child class class inherits all the attributes and methods from it's parent
class, but it can also
class ParentClass:
# body of ParentClass
# method1
# method2
class ChildClass(ParentClass):
# body of ChildClass
# method 1
# method 2
Example:
It creates a class named Shape, which contains attributes and methods common to all shapes,
then it creates two child classes Rectangle and Triangle which contains attributes and methods
specific to them only.
1 import math
82 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
2
3 class Shape:
4
5 def __init__(self, color='black', filled=False):
6 self.__color = color
7 self.__filled = filled
8
9 def get_color(self):
10 return self.__color
11
12 def set_color(self, color):
13 self.__color = color
14
15 def get_filled(self):
16 return self.__filled
17
18 def set_filled(self, filled):
19 self.__filled = filled
20
21
22 class Rectangle(Shape):
23
24 def __init__(self, length, breadth):
25 super().__init__()
26 self.__length = length
27 self.__breadth = breadth
28
29 def get_length(self):
30 return self.__length
31
32 def set_length(self, length):
33 self.__length = length
34
35 def get_breadth(self):
36 return self.__breadth
37
38 def set_breadth(self, breadth):
39 self.__breadth = breadth
40
41 def get_area(self):
42 return self.__length * self.__breadth
43
44 def get_perimeter(self):
45 return 2 * (self.__length + self.__breadth)
46
83 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
47
48 class Circle(Shape):
49 def __init__(self, radius):
50 super().__init__()
51 self.__radius = radius
52
53 def get_radius(self):
54 return self.__radius
55
56 def set_radius(self, radius):
57 self.__radius = radius
58
59 def get_area(self):
60 return math.pi * self.__radius ** 2
61
62 def get_perimeter(self):
63 return 2 * math.pi * self.__radius
64
65
66r1 = Rectangle(10.5, 2.5)
67
68 print("Area of rectangle r1:", r1.get_area())
69 print("Perimeter of rectangle r1:", r1.get_perimeter())
70 print("Color of rectangle r1:", r1.get_color())
71 print("Is rectangle r1 filled ? ", r1.get_filled())
72 r1.set_filled(True)
73 print("Is rectangle r1 filled ? ", r1.get_filled())
74 r1.set_color("orange")
75 print("Color of rectangle r1:", r1.get_color())
76
77 c1 = Circle(12)
78
79 print("\nArea of circle c1:", format(c1.get_area(), "0.2f"))
80 print("Perimeter of circle c1:", format(c1.get_perimeter(), "0.2f"))
81 print("Color of circle c1:", c1.get_color())
82 print("Is circle c1 filled ? ", c1.get_filled())
83 c1.set_filled(True)
84 print("Is circle c1 filled ? ", c1.get_filled())
85 c1.set_color("blue")
86print("Color of circle c1:", c1.get_color())
Output:
84 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Multiple Inheritance
Python allows us to derive a class from several classes at once, this is known as Multiple
Inheritance. Its general format is:
Class ParentClass_1:
1 # body of ParentClass_1
2
3 Class ParentClass_2:
4 # body of ParentClass_2
5
6 Class ParentClass_3:
7 # body of ParentClass_1
8
9 Class ChildClass(ParentClass_1, ParentClass_2, ParentClass_3):
10 # body of ChildClass
11
python101/Chapter-16/multiple_inheritance.py
1 class A:
2 def explore(self):
3 print("explore() method called")
85 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
4
5 class B:
6 def search(self):
7 print("search() method called")
8
9 class C:
10 def discover(self):
11 print("discover() method called")
12
13 class D(A, B, C):
14 def test(self):
15 print("test() method called")
16
17
18 d_obj = D()
19 d_obj.explore()
20 d_obj.search()
21 d_obj.discover()
22 d_obj.test()
Output:
Polymorphism means the ability to take various forms. In Python, Polymorphism allows us to
define methods in the child class with the same name as defined in their parent class.
As we know, a child class inherits all the methods from the parent class. However, you will
encounter situations where the method inherited from the parent class doesn't quite fit into the
child class. In such cases, you will have to re-implement method in the child class. This process is
known as Method Overriding.
In you have overridden a method in child class, then the version of the method will be called
based upon the the type of the object used to call it. If a child class object is used to call an
overridden method then the child class version of the method is called. On the other hand, if
parent class object is used to call an overridden method, then the parent class version of the
method is called.
86 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example:
class A:
1 def explore(self):
2 print("explore() method from class A")
3
4 class B(A):
5 def explore(self):
6 print("explore() method from class B")
7
8
9 b_obj = B()
10 a_obj = A()
11
12 b_obj.explore()
13 a_obj.explore()
14
Output:
FILE HANDLING:
Python provides inbuilt functions for creating, writing and reading files. There are two types of
files that can be handled in python, normal text files and binary files (written in binary language,
0s and 1s).
Text files: In this type of file, Each line of text is terminated with a special character
called EOL (End of Line), which is the new line character (‘\n’) in python by default.
Binary files: In this type of file, there is no terminator for a line and the data is stored
after converting it into machine understandable binary language.
In this article, we will be focusing on opening, closing, reading, and writing data in a text file.
87 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Access modes govern the type of operations possible in the opened file. It refers to how the file
will be used once its opened. These modes also define the location of the File Handle in the file.
File handle is like a cursor, which defines from where the data has to be read or written in the file.
There are 6 access modes in python.
1. Read Only (‘r’) : Open text file for reading. The handle is positioned at the beginning of
the file. If the file does not exists, raises I/O error. This is also the default mode in which
file is opened.
2. Read and Write (‘r+’) : Open the file for reading and writing. The handle is positioned at
the beginning of the file. Raises I/O error if the file does not exists.
3. Write Only (‘w’) : Open the file for writing. For existing file, the data is truncated and
over-written. The handle is positioned at the beginning of the file. Creates the file if the
file does not exists.
4. Write and Read (‘w+’) : Open the file for reading and writing. For existing file, data is
truncated and over-written. The handle is positioned at the beginning of the file.
5. Append Only (‘a’) : Open the file for writing. The file is created if it does not exist. The
handle is positioned at the end of the file. The data being written will be inserted at the
end, after the existing data.
6. Append and Read (‘a+’) : Open the file for reading and writing. The file is created if it
does not exist. The handle is positioned at the end of the file. The data being written
will be inserted at the end, after the existing data.
Opening a File
File_object = open(r"File_Name","Access_Mode")
The file should exist in the same directory as the python program file else, full address of the file
should be written on place of filename.
file1 = open("MyFile.txt","a")
file2 = open(r"D:\Text\MyFile2.txt","w+")
Here, file1 is created as object for MyFile1 and file2 as object for MyFile2
Closing a file
88 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
close() function closes the file and frees the memory space acquired by that file. It is used at the
time when the file is no longer needed or if it is to be opened in a different file mode.
File_object.close()
file1 = open("MyFile.txt","a")
file1.close()
Writing to a file
There are two ways to write in a file.
1. write() : Inserts the string str1 in a single line in the text file.
File_object.write(str1)
2. writelines() : For a list of string elements, each string is inserted in the text file.Used to
insert multiple strings at a single time.
File_object.writelines(L) for L = [str1, str2, str3]
Reading from a file
There are three ways to read data from a text file.
1. read() : Returns the read bytes in form of a string. Reads n bytes, if no n specified,
reads the entire file.
File_object.read([n])
2. readline() : Reads a line of the file and returns in form of a string.For specified n, reads
at most n bytes. However, does not reads more than one line, even if n exceeds the
length of the line.
File_object.readline([n])
3. readlines() : Reads all the lines and return them as each line a string element in a list.
File_object.readlines()
89 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
file1 = open("myfile.txt","r+")
file1.seek(0)
file1.seek(0)
file1.seek(0)
# readlines function
print("Output of Readlines function is ")
print(file1.readlines())
print()
file1.close()
Output:
Output of Read function is
Hello
This is Delhi
This is Paris
This is London
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Appending to a file
file1 = open("myfile.txt","w")
file1.writelines(L)
file1.close()
# Append-adds at last
file1.write("Today \n")
file1.close()
file1 = open("myfile.txt","r")
print(file1.readlines())
print()
91 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
file1.close()
# Write-Overwrites
file1.write("Tomorrow \n")
file1.close()
file1 = open("myfile.txt","r")
print(file1.readlines())
print()
file1.close()
Output:
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']
Unit-3
NumPy Arrays and Vectorized Computation: NumPy arrays, Array creation, Indexing and slicing,
Fancy indexing, Numerical operations on arrays, Array functions, Data processing using arrays,
Loading and saving data, Saving an array, Loading an array, Linear algebra with NumPy, NumPy
random numbers
Numpy Arrays:
Numpy is the core library for scientific computing in Python. It provides a high-performance
multidimensional array object, and tools for working with these arrays. If you are already familiar
with MATLAB, you might find this tutorial useful to get started with Numpy.
92 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
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.
We can initialize numpy arrays from nested Python lists, and access elements using square
brackets:
import numpy as np
import numpy as np
93 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# [ 0.68744134 0.87236687]]"
Array indexing
Slicing: Similar to Python lists, numpy arrays can be sliced. Since arrays may be multidimensional,
you must specify a slice for each dimension of the array:
import numpy as np
# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[2 3]
# [6 7]]
b = a[:2, 1:3]
You can also mix integer indexing with slice indexing. However, doing so will yield an array of
lower rank than the original array. Note that this is quite different from the way that MATLAB
handles array slicing:
import numpy as np
# Two ways of accessing the data in the middle row of the array.
94 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# When using integer array indexing, you can reuse the same
# element from the source array:
print(a[[0, 0], [1, 1]]) # Prints "[2 2]"
One useful trick with integer array indexing is selecting or mutating one element from each row
of a matrix:
import numpy as np
95 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
96 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Slicing in python is used for accessing parts of a sequence. The slice object is used to slice a given
sequence or any object. We use slicing when we require a part of a string and not the complete
string.
Syntax:
string[start : end : step]
Fancy Indexing:
Fancy indexing is conceptually simple: it means passing an array of indices to access multiple array
elements at once. For example, consider the following array:
import numpy as np
rand = np.random.RandomState(42)
x = rand.randint(100, size=10)
print(x)
[51 92 14 71 60 20 82 86 74 74]
Suppose we want to access three different elements. We could do it like this:
In [2]:
In [3]:
ind = [3, 7, 4]
97 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
x[ind]
Out[3]:
In [4]:
array([[71, 86],
[60, 20]])
Fancy indexing also works in multiple dimensions. Consider the following array:
In [5]:
X = np.arange(12).reshape((3, 4))
X
Out[5]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
Like with standard indexing, the first index refers to the row, and the second to the column:
In [6]:
array([ 2, 5, 11])
Notice that the first value in the result is X[0, 2], the second is X[1, 1], and the third is X[2, 3]. The
pairing of indices in fancy indexing follows all the broadcasting rules that were mentioned
in Computation on Arrays: Broadcasting. So, for example, if we combine a column vector and a
row vector within the indices, we get a two-dimensional result:
In [7]:
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Out[7]:
array([[ 2, 1, 3],
[ 6, 5, 7],
[10, 9, 11]])
Here, each row value is matched with each column vector, exactly as we saw in broadcasting of
arithmetic operations. For example:
In [8]:
array([[0, 0, 0],
[2, 1, 3],
[4, 2, 6]])
It is always important to remember with fancy indexing that the return value reflects
the broadcasted shape of the indices, rather than the shape of the array being indexed.
Example
import numpy as np
a = np.arange(9, dtype = np.float_).reshape(3,3)
99 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Second array:
[10 10 10]
ARRAY FUNCTIONS:
Array functions in python are defined as functions that will have arrays as parameters to the
function and perform set of instructions to perform a particular task on input parameters to
achieve a particular task is called array functions in python. Array functions will take an array as an
100 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
argument, it will be implemented in such a way that it will operate on a different number of
objects.
For example, we can define a function that will compute the average of an array of integers or
float data types. Python by default takes arguments passed to the program will be stored in
1. array(dataype, valuelist)
The above function, an array is used to create an array in python which accepts parameters as
data type and value-list where data type is the type of the value-list like integer, decimal, float,
Example:
Code:
a = array(‘i’,[1,2,3,4])
print(a)
The above example will create an array of ‘integer’ data type with values 1, 2, 3, 4 as its elements.
101 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
2. insert(pos, value)
The above function, insert() is used to insert an element to the array at a specific index or
position. It will take pos, and value as its parameters where pos variables tell the position and
import numpy as np
a = np.array([1, 3, 5, 7])
np.savetxt('test1.txt', a, fmt='%d')
a2 = np.loadtxt('test1.txt', dtype=int)
print(a == a2)
Output:
.
SAVING AND ARRAY:
Saving a numpy array stores it in a file and allows future programs to utilize it.
102 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
U S E numpy.save() T O S A V E A N A R R A Y
loaded_array = np.load("sample.npy")
print(loaded_array)
OUTPUT
[[1 2 3]
[4 5 6]]
LOADING AN ARRAY:
NumPy Linear Algebra
Numpy provides the following functions to perform the different algebraic calculations on the
input data.
SN Function Definition
numpy.dot() function
103 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
This function is used to return the dot product of the two matrices. It is similar to the matrix
multiplication. Consider the following example.
Example
1. import numpy as np
2. a = np.array([[100,200],[23,12]])
3. b = np.array([[10,20],[12,21]])
4. dot = np.dot(a,b)
5. print(dot)
Output:
[[3400 6200]
[ 374 712]]
Random number does NOT mean a different number every time. Random means something that
can not be predicted logically.
Computers work on programs, and programs are definitive set of instructions. So it means there
must be some algorithm to generate a random number as well.
If there is a program to generate random number it can be predicted, thus it is not truly random.
Random numbers generated through a generation algorithm are called pseudo random.
Example
104 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
x = random.randint(100)
print(x)
OUTPUT:
91
Unit-4
Data Analysis with Pandas: An overview of the Pandas package, The Pandas data structureSeries,
The DataFrame, The Essential Basic Functionality: Reindexing and altering labels , Head and tail,
Binary operations, Functional statistics , Function application Sorting, Indexing and selecting data,
Computational tools, Working with Missing Data, Advanced Uses of Pandas for Data Analysis -
Hierarchical indexing, The Panel data
Panda
pandas is a Python package providing fast, flexible, and expressive data structures designed to
make working with “relational” or “labeled” data both easy and intuitive. It aims to be the
fundamental high-level building block for doing practical, real-world data analysis in Python.
Additionally, it has the broader goal of becoming the most powerful and flexible open source
data analysis/manipulation tool available in any language. It is already well on its way toward this
goal.
105 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
pandas is built on top of NumPy and is intended to integrate well within a scientific computing
environment with many other 3rd party libraries.
Easy handling of missing data (represented as NaN) in floating point as well as non-
floating point data
Size mutability: columns can be inserted and deleted from DataFrame and higher
dimensional objects
Automatic and explicit data alignment: objects can be explicitly aligned to a set of
labels, or the user can simply ignore the labels and let Series, DataFrame, etc.
automatically align the data for you in computations
Powerful, flexible group by functionality to perform split-apply-combine operations
on data sets, for both aggregating and transforming data
Make it easy to convert ragged, differently-indexed data in other Python and
NumPy data structures into DataFrame objects
Intelligent label-based slicing, fancy indexing, and subsetting of large data sets
Intuitive merging and joining data sets
Flexible reshaping and pivoting of data sets
Hierarchical labeling of axes (possible to have multiple labels per tick)
Robust IO tools for loading data from flat files (CSV and delimited), Excel files,
databases, and saving / loading data from the ultrafast HDF5 format
Time series-specific functionality: date range generation and frequency conversion,
moving window statistics, date shifting, and lagging.
Data structures
Series
106 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Series is a one-dimensional labeled array capable of holding any data type (integers, strings,
floating point numbers, Python objects, etc.). The axis labels are collectively referred to as
the index. The basic method to create a Series is to call:
a Python dict
an ndarray
a scalar value
The passed index is a list of axis labels. Thus, this separates into a few cases depending on
what data is:
From ndarray
If data is an ndarray, index must be the same length as data. If no index is passed, one will be
created having values [0, ..., len(data) - 1].
In [4]: s
Out[4]:
a 0.469112
b -0.282863
c -1.509059
d -1.135632
e 1.212112
dtype: float64
In [5]: s.index
Out[5]: Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
In [6]: pd.Series(np.random.randn(5))
Out[6]:
0 -0.173215
1 0.119209
2 -1.044236
3 -0.861849
4 -2.104569
dtype: float64
107 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
pandas supports non-unique index values. If an operation that does not support duplicate index
values is attempted, an exception will be raised at that time.
From dict
In [8]: pd.Series(d)
Out[8]:
b 1
a 0
c 2
dtype: int64
If an index is passed, the values in data corresponding to the labels in the index will be pulled out.
In [10]: pd.Series(d)
Out[10]:
a 0.0
b 1.0
c 2.0
dtype: float64
NaN (not a number) is the standard missing data marker used in pandas.
If data is a scalar value, an index must be provided. The value will be repeated to match the length
of index.
108 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Series is ndarray-like
Series acts very similarly to a ndarray, and is a valid argument to most NumPy functions. However,
operations such as slicing will also slice the index.
In [13]: s[0]
Out[13]: 0.4691122999071863
In [14]: s[:3]
Out[14]:
a 0.469112
b -0.282863
c -1.509059
dtype: float64
In [17]: np.exp(s)
Out[17]:
a 1.598575
b 0.753623
c 0.221118
d 0.321219
109 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
e 3.360575
dtype: float64
In [18]: s.dtype
Out[18]: dtype('float64')
This is often a NumPy dtype. However, pandas and 3rd-party libraries extend NumPy’s type
system in a few places, in which case the dtype would be an ExtensionDtype. Some examples
within pandas are Categorical data and Nullable integer data type. See dtypes for more.
In [19]: s.array
Out[19]:
<PandasArray>
[ 0.4691122999071863, -0.2828633443286633, -1.5090585031735124,
-1.1356323710171934, 1.2121120250208506]
Length: 5, dtype: float64
Accessing the array can be useful when you need to do some operation without the index (to
disable automatic alignment, for example).
While Series is ndarray-like, if you need an actual ndarray, then use Series.to_numpy().
In [20]: s.to_numpy()
Out[20]: array([ 0.4691, -0.2829, -1.5091, -1.1356, 1.2121])
Even if the Series is backed by a ExtensionArray, Series.to_numpy() will return a NumPy ndarray.
Series is dict-like
A Series is like a fixed-size dict in that you can get and set values by index label:
110 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
In [21]: s["a"]
Out[21]: 0.4691122999071863
In [23]: s
Out[23]:
a 0.469112
b -0.282863
c -1.509059
d -1.135632
e 12.000000
dtype: float64
In [24]: "e" in s
Out[24]: True
In [25]: "f" in s
Out[25]: False
>>> s["f"]
KeyError: 'f'
Using the get method, a missing label will return None or specified default:
In [26]: s.get("f")
When working with raw NumPy arrays, looping through value-by-value is usually not necessary.
The same is true when working with Series in pandas. Series can also be passed into most NumPy
methods expecting an ndarray.
In [28]: s + s
Out[28]:
111 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
a 0.938225
b -0.565727
c -3.018117
d -2.271265
e 24.000000
dtype: float64
In [29]: s * 2
Out[29]:
a 0.938225
b -0.565727
c -3.018117
d -2.271265
e 24.000000
dtype: float64
In [30]: np.exp(s)
Out[30]:
a 1.598575
b 0.753623
c 0.221118
d 0.321219
e 162754.791419
dtype: float64
A key difference between Series and ndarray is that operations between Series automatically
align the data based on label. Thus, you can write computations without giving consideration to
whether the Series involved have the same labels.
Name attribute
112 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
In [33]: s
Out[33]:
0 -0.494929
1 1.071804
2 0.721555
3 -0.706771
4 -1.039575
Name: something, dtype: float64
In [34]: s.name
Out[34]: 'something'
The Series name will be assigned automatically in many cases, in particular when taking 1D slices
of DataFrame as you will see below.
In [35]: s2 = s.rename("different")
In [36]: s2.name
Out[36]: 'different'
DataFrame
DataFrame is a 2-dimensional labeled data structure with columns of potentially different types.
You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the
most commonly used pandas object. Like Series, DataFrame accepts many different kinds of
input:
113 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
If axis labels are not passed, they will be constructed from the input data based on common sense
rules.
The resulting index will be the union of the indexes of the various Series. If there are any nested
dicts, these will first be converted to Series. If no columns are passed, the columns will be the
ordered list of dict keys.
In [37]: d = {
....: "one": pd.Series([1.0, 2.0, 3.0], index=["a", "b", "c"]),
....: "two": pd.Series([1.0, 2.0, 3.0, 4.0], index=["a", "b", "c", "d"]),
....: }
....:
In [38]: df = pd.DataFrame(d)
In [39]: df
Out[39]:
one two
a 1.0 1.0
b 2.0 2.0
c 3.0 3.0
d NaN 4.0
114 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
The ndarrays must all be the same length. If an index is passed, it must clearly also be the same
length as the arrays. If no index is passed, the result will be range(n), where n is the array length.
In [44]: d = {"one": [1.0, 2.0, 3.0, 4.0], "two": [4.0, 3.0, 2.0, 1.0]}
In [45]: pd.DataFrame(d)
Out[45]:
one two
0 1.0 4.0
1 2.0 3.0
2 3.0 2.0
3 4.0 1.0
In [49]: pd.DataFrame(data)
Out[49]:
A B C
0 1 2.0 b'Hello'
1 2 3.0 b'World'
115 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
C A B
0 b'Hello' 1 2.0
1 b'World' 2 3.0
In [52]: data2 = [{"a": 1, "b": 2}, {"a": 5, "b": 10, "c": 20}]
In [53]: pd.DataFrame(data2)
Out[53]:
a b c
0 1 2 NaN
1 5 10 20.0
In [56]: pd.DataFrame(
....: {
....: ("a", "b"): {("A", "B"): 1, ("A", "C"): 2},
....: ("a", "a"): {("A", "C"): 3, ("A", "B"): 4},
....: ("a", "c"): {("A", "B"): 5, ("A", "C"): 6},
....: ("b", "a"): {("A", "C"): 7, ("A", "B"): 8},
....: ("b", "b"): {("A", "D"): 9, ("A", "B"): 10},
....: }
....: )
....:
116 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Out[56]:
a b
b a c a b
A B 1.0 4.0 5.0 8.0 10.0
C 2.0 3.0 6.0 7.0 NaN
D NaN NaN NaN NaN 9.0
DataFrame.from_records
In [67]: data
Out[67]:
array([(1, 2., b'Hello'), (2, 3., b'World')],
dtype=[('A', '<i4'), ('B', '<f4'), ('C', 'S10')])
You can treat a DataFrame semantically like a dict of like-indexed Series objects. Getting, setting,
and deleting columns works with the same syntax as the analogous dict operations:
In [69]: df["one"]
Out[69]:
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
117 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
In [72]: df
Out[72]:
one two three flag
a 1.0 1.0 1.0 False
b 2.0 2.0 4.0 False
c 3.0 3.0 9.0 True
d NaN 4.0 NaN False
In [75]: df
Out[75]:
one flag
a 1.0 False
b 2.0 False
c 3.0 True
d NaN False
When inserting a scalar value, it will naturally be propagated to fill the column:
In [77]: df
Out[77]:
one flag foo
a 1.0 False bar
b 2.0 False bar
c 3.0 True bar
d NaN False bar
When inserting a Series that does not have the same index as the DataFrame, it will be conformed
to the DataFrame’s index:
In [79]: df
Out[79]:
one flag foo one_trunc
118 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Indexing / selection
In [89]: df.loc["b"]
Out[89]:
one 2.0
bar 2.0
flag False
foo bar
one_trunc 2.0
Name: b, dtype: object
In [90]: df.iloc[2]
Out[90]:
one 3.0
bar 3.0
flag True
foo bar
one_trunc NaN
Name: c, dtype: object
For a more exhaustive treatment of sophisticated label-based indexing and slicing, see the section
on indexing. We will address the fundamentals of reindexing / conforming to new sets of labels in
the section on reindexing.
119 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Data alignment between DataFrame objects automatically align on both the columns and the
index (row labels). Again, the resulting object will have the union of the column and row labels.
In [93]: df + df2
Out[93]:
A B C D
0 0.045691 -0.014138 1.380871 NaN
1 -0.955398 -1.501007 0.037181 NaN
2 -0.662690 1.534833 -0.859691 NaN
3 -2.452949 1.237274 -0.133712 NaN
4 1.414490 1.951676 -2.320422 NaN
5 -0.494922 -1.649727 -1.084601 NaN
6 -1.047551 -0.748572 -0.805479 NaN
7 NaN NaN NaN NaN
8 NaN NaN NaN NaN
9 NaN NaN NaN NaN
In [94]: df - df.iloc[0]
Out[94]:
A B C D
0 0.000000 0.000000 0.000000 0.000000
1 -1.359261 -0.248717 -0.453372 -1.754659
2 0.253128 0.829678 0.010026 -1.991234
3 -1.311128 0.054325 -1.724913 -1.620544
4 0.573025 1.500742 -0.676070 1.367331
5 -1.741248 0.781993 -1.241620 -2.053136
6 -1.240774 -0.869551 -0.153282 0.000430
7 -0.743894 0.411013 -0.929563 -0.282386
8 -1.194921 1.320690 0.238224 -1.482644
9 2.293786 1.856228 0.773289 -1.446531
For explicit control over the matching and broadcasting behavior, see the section on flexible
binary operations.
In [95]: df * 5 + 2
120 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Out[95]:
A B C D
0 3.359299 -0.124862 4.835102 3.381160
1 -3.437003 -1.368449 2.568242 -5.392133
2 4.624938 4.023526 4.885230 -6.575010
3 -3.196342 0.146766 -3.789461 -4.721559
4 6.224426 7.378849 1.454750 10.217815
5 -5.346940 3.785103 -1.373001 -6.884519
6 -2.844569 -4.472618 4.068691 3.383309
7 -0.360173 1.930201 0.187285 1.969232
8 -2.615303 6.478587 6.026220 -4.032059
9 14.828230 9.156280 8.701544 -3.851494
In [96]: 1 / df
Out[96]:
A B C D
0 3.678365 -2.353094 1.763605 3.620145
1 -0.919624 -1.484363 8.799067 -0.676395
2 1.904807 2.470934 1.732964 -0.583090
3 -0.962215 -2.697986 -0.863638 -0.743875
4 1.183593 0.929567 -9.170108 0.608434
5 -0.680555 2.800959 -1.482360 -0.562777
6 -1.032084 -0.772485 2.416988 3.614523
7 -2.118489 -71.634509 -2.758294 -162.507295
8 -1.083352 1.116424 1.241860 -0.828904
9 0.389765 0.698687 0.746097 -0.854483
In [97]: df ** 4
Out[97]:
A B C D
0 0.005462 3.261689e-02 0.103370 5.822320e-03
1 1.398165 2.059869e-01 0.000167 4.777482e+00
2 0.075962 2.682596e-02 0.110877 8.650845e+00
3 1.166571 1.887302e-02 1.797515 3.265879e+00
4 0.509555 1.339298e+00 0.000141 7.297019e+00
5 4.661717 1.624699e-02 0.207103 9.969092e+00
6 0.881334 2.808277e+00 0.029302 5.858632e-03
7 0.049647 3.797614e-08 0.017276 1.433866e-09
8 0.725974 6.437005e-01 0.420446 2.118275e+00
9 43.329821 4.196326e+00 3.227153 1.875802e+00
121 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
In [103]: -df1
Out[103]:
a b
0 False True
1 True False
2 False False
reindex() is the fundamental data alignment method in pandas. It is used to implement nearly all
other features relying on label-alignment functionality. To reindex means to conform the data to
match a given set of labels along a particular axis. This accomplishes several things:
122 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Inserts missing value (NA) markers in label locations where no data for that label
existed
If specified, fill data for missing labels using logic (highly relevant to working with
time series data)
In [2]: s
Out[2]:
a 0.734560
b -0.445120
c -0.703433
d 0.320412
e 0.185202
dtype: float64
Here, the f label was not contained in the Series and hence appears as NaN in the result.
With a DataFrame, you can simultaneously reindex the index and columns:
In [4]: df
Out[4]:
one three two
a 0.851097 NaN -0.429037
b 0.266049 -0.330979 0.963385
c 1.117346 -0.409168 2.243459
d NaN -0.305334 -0.432789
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
For convenience, you may utilize the reindex_axis() method, which takes the labels and a
keyword axis parameter.
Note that the Index objects containing the actual axis labels can be shared between objects. So
if we have a Series and a DataFrame, the following can be done:
In [6]: rs = s.reindex(df.index)
In [7]: rs
Out[7]:
a 0.734560
b -0.445120
c -0.703433
d 0.320412
dtype: float64
The align() method is the fastest way to simultaneously align two objects. It supports
a join argument (related to joining and merging):
In [13]: s1 = s[:4]
In [14]: s2 = s[1:]
In [15]: s1.align(s2)
124 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Out[15]:
(a 0.498698
b -0.643722
c -0.028228
d 0.070209
e NaN
dtype: float64, a NaN
b -0.643722
c -0.028228
d 0.070209
e -0.791176
dtype: float64)
For DataFrames, the join method will be applied to both the index and the columns by default:
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
c 1.117346 2.243459)
You can also pass an axis option to only align on the specified axis:
If you pass a Series to DataFrame.align() , you can choose to align both objects either on the
DataFrame’s index or columns using the axis argument:
reindex() takes an optional parameter method which is a filling method chosen from the
following table:
Method Action
126 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
In [24]: ts
Out[24]:
2000-01-03 0.393495
2000-01-04 2.410230
2000-01-05 -0.368339
2000-01-06 -1.934392
2000-01-07 2.398912
2000-01-08 0.521658
2000-01-09 -2.389278
2000-01-10 0.395639
Freq: D, dtype: float64
In [25]: ts2
Out[25]:
2000-01-03 0.393495
2000-01-06 -1.934392
2000-01-09 -2.389278
dtype: float64
In [26]: ts2.reindex(ts.index)
Out[26]:
2000-01-03 0.393495
2000-01-04 NaN
2000-01-05 NaN
2000-01-06 -1.934392
2000-01-07 NaN
2000-01-08 NaN
2000-01-09 -2.389278
2000-01-10 NaN
Freq: D, dtype: float64
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
2000-01-09 -2.389278
2000-01-10 -2.389278
Freq: D, dtype: float64
These methods require that the indexes are ordered increasing or decreasing.
Note that the same result could have been achieved using fillna (except for method='nearest' )
or interpolate:
In [30]: ts2.reindex(ts.index).fillna(method='ffill')
Out[30]:
2000-01-03 0.393495
2000-01-04 0.393495
2000-01-05 0.393495
2000-01-06 -1.934392
2000-01-07 -1.934392
2000-01-08 -1.934392
2000-01-09 -2.389278
2000-01-10 -2.389278
Freq: D, dtype: float64
128 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
The limit and tolerance arguments provide additional control over filling while reindexing. Limit
specifies the maximum count of consecutive matches:
In contrast, tolerance specifies the maximum distance between the index and indexer values:
A method closely related to reindex is the drop() function. It removes a set of labels from an
axis:
129 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
In [33]: df
Out[33]:
one three two
a 0.851097 NaN -0.429037
b 0.266049 -0.330979 0.963385
c 1.117346 -0.409168 2.243459
d NaN -0.305334 -0.432789
Note that the following also works, but is a bit less obvious / clean:
The rename() method allows you to relabel an axis based on some mapping (a dict or Series) or
an arbitrary function.
In [37]: s
Out[37]:
a 0.498698
b -0.643722
c -0.028228
d 0.070209
e -0.791176
dtype: float64
130 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
In [38]: s.rename(str.upper)
Out[38]:
A 0.498698
B -0.643722
C -0.028228
D 0.070209
E -0.791176
dtype: float64
If you pass a function, it must return a value when called with any of the labels (and must produce
a set of unique values). A dict or Series can also be used:
If the mapping doesn’t include a column/index label, it isn’t renamed. Also extra labels in the
mapping don’t throw an error.
The rename() method also provides an inplace named parameter that is by default False and
copies the underlying data. Pass inplace=True to rename the data in place.
rename() also accepts a scalar or list-like for altering the Series.name attribute.
In [40]: s.rename("scalar-name")
Out[40]:
a 0.498698
b -0.643722
c -0.028228
d 0.070209
e -0.791176
Name: scalar-name, dtype: float64
The Panel class has a related rename_axis() class which can rename any of its three axes.
131 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
import numpy as np
import pandas as pd
In [3]:
index = pd.date_range('1/1/2019', periods=6)
In [4]:
s = pd.Series(np.random.randn(6), index=['a', 'b', 'c', 'd', 'e','f'])
In [7]:
df = pd.DataFrame(np.random.randn(6, 4), index=index,
columns=['P', 'Q', 'R','S'])
To view a small sample of a Series or DataFrame object, use the head() and tail() methods. The
default number
of elements to display is five, but you may pass a custom number.
In [8]:
long_series = pd.Series(np.random.randn(800))
In [9]:
long_series.head()
Out[9]:
0 1.298944
1 -0.677865
2 0.414972
3 0.318461
4 -0.869943
dtype: float64
In [10]:
long_series.tail(3)
Out[10]:
797 0.374511
798 -0.721997
799 0.587586
dtype: float64
In [8]:
import numpy as np
import pandas as pd
In [9]:
s = pd.Series(np.random.randn(5), index=['white', 'black', 'blue', 'red', 'green'])
In [10]:
df = pd.DataFrame({'color':['white', 'black', 'blue', 'red', 'green']})
In [13]:
132 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
df
Out[13]:
Color
0 White
1 Black
2 Blue
3 Red
4 Green
In [16]:
df.tail(4)
Out[16]:
Color
1 Black
2 Blue
3 Red
4 Green
Binary operations¶
bitwise_and(x1, x2, /[, out, where, ...]) Compute the bit-wise AND of two arrays
element-wise.
133 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
bitwise_or(x1, x2, /[, out, where, casting, ...]) Compute the bit-wise OR of two arrays
element-wise.
bitwise_xor(x1, x2, /[, out, where, ...]) Compute the bit-wise XOR of two arrays
element-wise.
invert(x, /[, out, where, casting, order, ...]) Compute bit-wise inversion, or bit-wise
NOT, element-wise.
left_shift(x1, x2, /[, out, where, casting, ...]) Shift the bits of an integer to the left.
right_shift(x1, x2, /[, out, where, ...]) Shift the bits of an integer to the right.
Bit packing
packbits(a, /[, axis, bitorder]) Packs the elements of a binary-valued array into
bits in a uint8 array.
unpackbits(a, /[, axis, count, bitorder]) Unpacks elements of a uint8 array into a binary-
valued output array.
Output formatting
In this Python Bitwise Operators Bitwise AND, OR, XOR, Left-shift, Right-shift, and 1’s complement
Bitwise Operators in Python Programming.
>>> bin(5)
Output
‘0b101’
>>> bin(7)
Output
‘0b111’
>>> 5 and 7
Output
7
134 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
>>> 5&7
Output
5
You would have expected them to return the same thing, but they’re not the same. One acts on
the whole value, and one acts on each bit at once.
Actually, ‘and’ sees the value on the left. If it has a True Boolean value, it returns whatever value is
on the right.
Otherwise, it returns False. So, here, 5 and 7 is the same as True and 7. Hence, it returns 7.
However, 5&7 is the same as 101&111. This results in 101, which is binary for 5. Let’s look at each of
these operators bit by bit (pun intended).
1 has a Boolean value of True, and 0 has that of False. Take a look at the following code.
>>> True/2
Output
0.5
>>> False*2
Output
0
This proves something. Now, the binary and (&) takes two values and performs an AND-ing on
each pair of bits.
>>> 4 & 8
Binary for 4 is 0100, and that for 8 is 1000. So when we AND the corresponding bits, it gives us
0000, which is binary for 0. Hence, the output.
135 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
0&1 0
1&0 0
1&1 1
>>> '$'&'%'
Output
Traceback (most recent call last):File “<pyshell#30>”, line 1, in <module>’$’&’%’
Since Boolean values True and False have equivalent integer values of 1 and 0, we can & them.
>>> False&True
Output
False
>>> True&True
Output
True
>>> 1&True
Output
1
>>> 1.0&1.0
136 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output
Traceback (most recent call last):File “<pyshell#36>”, line 1, in <module>1.0&1.0
You can also type your numbers directly in binary, as we discussed in section 6a in our Python
Numbers tutorial.
Output
4
Here, 110 is binary for 6, and 101 for 5. &-ing them, we get 100, which is binary for 4.
Compared to &, this one returns 1 even if one of the two corresponding bits from the two
operands is 1.
0|1 1
1|0 1
1|1 1
>>> 6|1
Output
7
>>> 0b110|0b001
Output
7
137 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
>>> True|False
Output
True
XOR (eXclusive OR) returns 1 if one operand is 0 and another is 1. Otherwise, it returns 0.
0^1 1
1^0 1
1^1 0
>>> 6^6
Here, this is the same as 0b110^0b110. This results in 0b000, which is binary for 0.
>>> 6^0
Output
6
>>> 6^3
Output
5
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
This one is a bit different from what we’ve studied so far. This operator takes a number’s binary,
and returns its one’s complement.
For this, it flips the bits until it reaches the first 0 from right. ~x is the same as -x-1.
>>> ~2
Output
-3
>>> bin(2)
Output
‘0b10’
>>> bin(-3)
Output
‘-0b11’
To make it clear, we mention the binary values of both. Another example follows.
>>> ~45
Output
-46
>>> bin(45)
Output
‘0b101101’
>>> bin(-46)
Output
‘-0b101110’
Finally, we arrive at left-shift and right-shift operators. The left-shift operator shifts the bits of the
number by the specified number of places.
139 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
This means it adds 0s to the empty least-significant places now. Let’s begin with an unusual
example.
>>> True<<2
Output
4
Here, True has an equivalent integer value of 1. If we shift it by two places to the left, we get 100.
This is binary for 4.
>>> 2<<1
Output
4
10 shifted by one place to the left gives us 100, which is, again, 4.
>>> 3<<2
Output
12
Now, 11 shifted to the left by two places gives us 1100, which is binary for 12.
Now we’ll see the same thing for right-shift. It shifts the bits to the right by the specified number
of places.
>>> 3>>1
Output
1
3 has a binary value of 11, which shifted one place to the right returns 1. But before closing on this
tutorial, we’ll take one last example.
140 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
>>> int(0b11111)
Output
31
>>> 31>>3
Output
3
Indexing in Pandas :
Indexing in pandas means simply selecting particular rows and columns of data from a
DataFrame. Indexing could mean selecting all the rows and some of the columns, some of the
rows and all of the columns, or some of each of the rows and columns. Indexing can also be
known as Subset Selection.
Selecting some rows and some columns
.
Suppose we want to select columns Age, College and Salary for only rows with a labels Amir
Johnson and Terry Rozier
141 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
142 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
143 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
import pandas as pd
first = data["Age"]
print(first)
144 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
In order to select multiple columns, we have to pass a list of columns in an indexing operator.
import pandas as pd
first
145 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
import pandas as pd
146 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
As shown in the output image, two series were returned since there was only one parameter both
of the times.
In order to select multiple rows, we put all the row labels in a list and pass that to .loc function.
import pandas as pd
147 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
print(first)
Output:
import pandas as pd
print(first)
Output:
148 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
In order to select all of the rows and some columns, we use single colon [:] to select all of rows
and list of some columns which we want to select like this:
Dataframe.loc[[:, ["column1", "column2", "column3"]]
import pandas as pd
print(first)
Output:
149 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Function Description
Get item from object for given key (DataFrame column, Panel slice,
DataFrame.get() etc.).
150 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
other.
Computational Tools
A Python program can be executed by any computer, regardless of its manufacturer or operating
system, provided that support for the language is installed.
Missing Data can occur when no information is provided for one or more items or for a whole
unit. Missing Data is a very big problem in a real-life scenarios. Missing Data can also refer to
as NA(Not Available) values in pandas. In DataFrame sometimes many datasets simply arrive with
missing data, either because it exists and was not collected or it never existed. For Example,
Suppose different users being surveyed may choose not to share their income, some users may
choose not to share the address in this way many datasets went missing.
None: None is a Python singleton object that is often used for missing data in Python
code.
NaN : NaN (an acronym for Not a Number), is a special floating-point value recognized
by all systems that use the standard IEEE floating-point representation
Pandas treat None and NaN as essentially interchangeable for indicating missing or null values. To
facilitate this convention, there are several useful functions for detecting, removing, and
replacing null values in Pandas DataFrame :
isnull()
notnull()
dropna()
fillna()
151 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
replace()
interpolate()
In this article we are using CSV file, to download the CSV file used, Click Here.
Checking for missing values using isnull() and notnull()
In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull().
Both function help in checking whether a value is NaN or not. These function can also be used in
Pandas Series in order to find null values in a series.
Checking for missing values using isnull()
In order to check null values in Pandas DataFrame, we use isnull() function this function return
dataframe of Boolean values which are True for NaN values.
Code #1:
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
df = pd.DataFrame(dict)
df.isnull()
Output:
152 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Code #2:
import pandas as pd
data = pd.read_csv("employees.csv")
bool_series = pd.isnull(data["Gender"])
# filtering data
data[bool_series]
Output:
As shown in the output image, only the rows having Gender = NULL are displayed.
153 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
154 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
df = pd.DataFrame(dict)
df.notnull()
Output:
Code #4:
import pandas as pd
data = pd.read_csv("employees.csv")
bool_series = pd.notnull(data["Gender"])
# filtering data
data[bool_series]
Output:
As shown in the output image, only the rows having Gender = NOT NULL are displayed.
155 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
156 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
df = pd.DataFrame(dict)
df.fillna(0)
Output:
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
df = pd.DataFrame(dict)
# previous ones
df.fillna(method ='pad')
157 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
df = pd.DataFrame(dict)
df.fillna(method ='bfill')
Output:
158 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
import pandas as pd
data = pd.read_csv("employees.csv")
data[10:25]
Now we are going to fill all the null values in Gender column with “No Gender”
import pandas as pd
data = pd.read_csv("employees.csv")
159 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
data
Output:
import pandas as pd
data = pd.read_csv("employees.csv")
data[10:25]
160 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
Now we are going to replace the all Nan value in the data frame with -99 value.
import pandas as pd
data = pd.read_csv("employees.csv")
161 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
Code #6: Using interpolate() function to fill the missing values using linear method.
# importing pandas as pd
import pandas as pd
df
162 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Let’s interpolate the missing values using Linear method. Note that Linear method ignore the
index and treat the values as equally spaced.
Output:
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
163 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
df = pd.DataFrame(dict)
df
Now we drop rows with at least one Nan value (Null value)
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
df = pd.DataFrame(dict)
df.dropna()
164 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
Code #2: Dropping rows if all values in that row are missing.
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
df = pd.DataFrame(dict)
df
Now we drop a rows whose all data is missing or contain null values(NaN)
# importing pandas as pd
import pandas as pd
# importing numpy as np
165 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
import numpy as np
# dictionary of lists
df = pd.DataFrame(dict)
df.dropna(how = 'all')
Output:
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
166 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
df = pd.DataFrame(dict)
df
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
df = pd.DataFrame(dict)
df.dropna(axis = 1)
167 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output :
Code #4: Dropping Rows with at least 1 null value in CSV file
import pandas as pd
data = pd.read_csv("employees.csv")
new_data
168 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
Now we compare sizes of data frames so that we can come to know how many rows had at least
1 Null value
Output :
Old data frame length: 1000
New data frame length: 764
Number of rows with at least 1 NA value: 236
Since the difference is 236, there were 236 rows which had at least 1 Null value in any column.
Hierarchical Indexes
Hierarchical Indexes are also known as multi-indexing is setting more than one column name as
the index. In this article, we are going to use homelessness.csv file.
169 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
print(df.head())
Output:
Output:
Index([‘Unnamed: 0’, ‘region’, ‘state’, ‘individuals’, ‘family_members’,
‘state_pop’],
dtype=’object’)
To make the column an index, we use the Set_index() function of pandas. If we want to make one
column an index, we can simply pass the name of the column as a string in set_index(). If we want
to do multi-indexing or Hierarchical Indexing, we pass the list of column names in the set_index().
Below Code demonstrates Hierarchical Indexing in pandas:
Python3
170 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
print(df_ind3.head(10))
Output:
print(df_ind3_region.head(10))
Output:
171 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
We cannot use only level(1) index for getting data from the dataframe, if we do so it will give an
error. We can only use level (1) index or the inner indexes with the level(0) or main index with the
help list of tuples.
print(df_ind3_state.head(10))
Output:
Output:
172 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
A panel is a 3D container of data. The term Panel data is derived from econometrics and is
partially responsible for the name pandas − pan(el)-da(ta)-s.
The names for the 3 axes are intended to give some semantic meaning to describing operations
involving panel data. They are −
items − axis 0, each item corresponds to a DataFrame contained inside.
major_axis − axis 1, it is the index (rows) of each of the DataFrames.
minor_axis − axis 2, it is the columns of each of the DataFrames.
pandas.Panel()
Parameter Description
data Data takes various forms like ndarray, series, map, lists, dict, constants and also
another DataFrame
items axis=0
major_axis axis=1
173 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
minor_axis axis=2
Create Panel
From ndarrays
From dict of DataFrames
data = np.random.rand(2,4,5)
p = pd.Panel(data)
print p
Its output is as follows −
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)
Items axis: 0 to 1
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 4
Note − Observe the dimensions of the empty panel and the above panel, all the objects are
different.
174 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Items
Major_axis
Minor_axis
Using Items
175 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Using major_axis
Using minor_axis
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Unit-5
Data Analysis Application Examples: Data munging,Cleaning data, Filtering, Merging data, Reshaping data,
Data aggregation, Grouping data
Example:
import pandas as pd
# Assign data
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
df = pd.DataFrame(data)
# Display data
df
Output:
Dealing with missing values, as we can see from the previous output, there are NaN values
present in the MARKS column which are going to be taken care of by replacing them with
the column mean.
Example:
# Compute average
c = avg = 0
for ele in df['Marks']:
if str(ele).isnumeric():
c += 1
avg += ele
avg /= c
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# Display data
Df
Reshaping data, in the GENDER column, we can reshape the data by categorizing them into
different numbers.
# Categorize gender
# Display data
df
Output:
179 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Filtering data,
suppose there is a requirement for the details regarding name, gender, marks of the top-
scoring students. Here we need to remove some unwanted data.
# Display data
Df
Output:
180 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Merge operation is used to merge raw data and into the desired format.
Syntax:
Here the field is the name of the column which is similar on both data-frame.
For example: Suppose that a Teacher has two types of Data, first type of Data consist of Details
of Students and Second type of Data Consist of Pending Fees Status which is taken from
Account Office. So The Teacher will use merge operation here in order to merge the data and
provide it meaning. So that teacher will analyze it easily and it also reduces time and effort of
Teacher from Manual Merging.
# import module
import pandas as pd
details = pd.DataFrame({
# printing details
181 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
print(details)
Output:
# Import module
import pandas as pd
fees_status = pd.DataFrame(
# Printing fees_status
print(fees_status)
Output:
182 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# Import module
import pandas as pd
# Creating Dataframe
details = pd.DataFrame({
# Creating Dataframe
fees_status = pd.DataFrame(
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# Merging Dataframe
Output:
The grouping method in Data analysis is used to provide results in terms of various groups taken
out from Large Data. This method of pandas is used to group the outset of data from the large
data set.
Example: There is a Car Selling company and this company have different Brands of various Car
Manufacturing Company like Maruti, Toyota, Mahindra, Ford, etc. and have data where
different cars are sold in different years. So the Company wants to wrangle only that data
where cars are sold during the year 2010. For this problem, we use another Wrangling technique
that is groupby() method.
# Import module
import pandas as pd
# Creating Data
184 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
'Sold': [6, 7, 9, 8, 3, 5,
2, 8, 7, 2, 4, 2]}
df = pd.DataFrame(car_selling_data)
# printing Dataframe
print(df)
Output:
# Import module
import pandas as pd
185 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# Creating Data
'Sold': [6, 7, 9, 8, 3, 5,
2, 8, 7, 2, 4, 2]}
df = pd.DataFrame(car_selling_data)
grouped = df.groupby('Year')
print(grouped.get_group(2010))
Output:
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Pandas duplicates() method helps us to remove duplicate values from Large Data. An important
part of Data Wrangling is removing Duplicate values from the large data set.
Syntax:
DataFrame.duplicated(subset=None, keep='first')
Here subset is the column value where we want to remove Duplicate value.
In keep, we have 3 options :
if keep =’first’ then the first value is marked as original rest all values if occur will be
removed as it is considered as duplicate.
if keep=’last’ then the last value is marked as original rest all above same values will be
removed as it is considered as duplicate values.
if keep =’false’ the all the values which occur more than once will be removed as all
considered as a duplicate value.
For example, A University will organize the event. In order to participate Students have to fill
their details in the online form so that they will contact them. It may be possible that a student
will fill the form multiple time. It may cause difficulty for the event organizer if a single student
will fill multiple entries. The Data that the organizers will get can be Easily Wrangles by
removing duplicate values.
# Import module
import pandas as pd
# Initializing Data
student_data = {'Name': ['Amit', 'Praveen', 'Jagroop',
'Rahul', 'Vishal', 'Suraj',
'Rishab', 'Satyapal', 'Amit',
'Rahul', 'Praveen', 'Amit'],
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
# Printing Dataframe
print(df)
Output:
# import module
import pandas as pd
# initializing Data
'xxxxxx@gmail.com', 'xx@gmail.com',
'xxxx@gmail.com', 'xxxxx@gmail.com',
188 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
'xxxxx@gmail.com', 'xxxxx@gmail.com',
'xxxxx@gmail.com', 'xxxxxx@gmail.com',
'xxxxxxxxxx@gmail.com',
'xxxxxxxxxx@gmail.com']}
# creating dataframe
df = pd.DataFrame(student_data)
non_duplicate = df[~df.duplicated('Roll_no')]
print(non_duplicate)
189 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
DataFrame.stack(level=- 1, dropna=True)
DataFrame.unstack(level=- 1, fill_value=None)
Let’s try these operations with some examples. Use these code snippets:
First, create a dummy DataFrame.
Code:
Output:
Next, we use the stack() function, we will pivot the columns into rows
Code:
data_stack = data.stack()data_stack
Output:
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
type(data_stack)
Output:
pandas.core.series.Series
data_stack.index
Output:
From a hierarchically indexed series, you can rearrange the data back into a DataFrame with the
unstack() function.
Try this code:
data = data_stack.unstack()data
Output:
By default, the innermost level is unstacked. In our example, it was a number. However, you can
unstack a different level by passing a level number or name as a parameter to the unstack
method.
For example, try this code that unstacks data_stack at the level of state, rather than number:
Code:
data_state = data_stack.unstack('state')data_state
Output:
191 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Output:
Data Aggregation
Python has several methods are available to perform aggregations on data. It is done using the
pandas and numpy libraries. The data must be available or converted to a dataframe to apply the
aggregation functions.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('1/1/2000', periods=10),
columns = ['A', 'B', 'C', 'D'])
print df
r = df.rolling(window=3,min_periods=1)
print r
Its output is as follows −
192 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
A B C D
2000-01-01 1.088512 -0.650942 -2.547450 -0.566858
2000-01-02 0.790670 -0.387854 -0.668132 0.267283
2000-01-03 -0.575523 -0.965025 0.060427 -2.179780
2000-01-04 1.669653 1.211759 -0.254695 1.429166
2000-01-05 0.100568 -0.236184 0.491646 -0.466081
2000-01-06 0.155172 0.992975 -1.205134 0.320958
2000-01-07 0.309468 -0.724053 -1.412446 0.627919
2000-01-08 0.099489 -1.028040 0.163206 -1.274331
2000-01-09 1.639500 -0.068443 0.714008 -0.565969
2000-01-10 0.326761 1.479841 0.664282 -1.361169
Rolling [window=3,min_periods=1,center=False,axis=0]
We can aggregate by passing a function to the entire DataFrame, or select a column via the
standard get item method.
df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('1/1/2000', periods=10),
columns = ['A', 'B', 'C', 'D'])
print df
r = df.rolling(window=3,min_periods=1)
print r.aggregate(np.sum)
Its output is as follows −
A B C D
2000-01-01 1.088512 -0.650942 -2.547450 -0.566858
2000-01-02 1.879182 -1.038796 -3.215581 -0.299575
2000-01-03 1.303660 -2.003821 -3.155154 -2.479355
2000-01-04 1.884801 -0.141119 -0.862400 -0.483331
2000-01-05 1.194699 0.010551 0.297378 -1.216695
2000-01-06 1.925393 1.968551 -0.968183 1.284044
2000-01-07 0.565208 0.032738 -2.125934 0.482797
2000-01-08 0.564129 -0.759118 -2.454374 -0.325454
2000-01-09 2.048458 -1.820537 -0.535232 -1.212381
2000-01-10 2.065750 0.383357 1.541496 -3.201469
A B C D
2000-01-01 1.088512 -0.650942 -2.547450 -0.566858
193 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('1/1/2000', periods=10),
columns = ['A', 'B', 'C', 'D'])
print df
r = df.rolling(window=3,min_periods=1)
print r['A'].aggregate(np.sum)
Its output is as follows −
A B C D
2000-01-01 1.088512 -0.650942 -2.547450 -0.566858
2000-01-02 1.879182 -1.038796 -3.215581 -0.299575
2000-01-03 1.303660 -2.003821 -3.155154 -2.479355
2000-01-04 1.884801 -0.141119 -0.862400 -0.483331
2000-01-05 1.194699 0.010551 0.297378 -1.216695
2000-01-06 1.925393 1.968551 -0.968183 1.284044
2000-01-07 0.565208 0.032738 -2.125934 0.482797
2000-01-08 0.564129 -0.759118 -2.454374 -0.325454
2000-01-09 2.048458 -1.820537 -0.535232 -1.212381
2000-01-10 2.065750 0.383357 1.541496 -3.201469
2000-01-01 1.088512
2000-01-02 1.879182
2000-01-03 1.303660
2000-01-04 1.884801
2000-01-05 1.194699
2000-01-06 1.925393
2000-01-07 0.565208
2000-01-08 0.564129
2000-01-09 2.048458
2000-01-10 2.065750
Freq: D, Name: A, dtype: float64
194 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('1/1/2000', periods=10),
columns = ['A', 'B', 'C', 'D'])
print df
r = df.rolling(window=3,min_periods=1)
print r[['A','B']].aggregate(np.sum)
Its output is as follows −
A B C D
2000-01-01 1.088512 -0.650942 -2.547450 -0.566858
2000-01-02 1.879182 -1.038796 -3.215581 -0.299575
2000-01-03 1.303660 -2.003821 -3.155154 -2.479355
2000-01-04 1.884801 -0.141119 -0.862400 -0.483331
2000-01-05 1.194699 0.010551 0.297378 -1.216695
2000-01-06 1.925393 1.968551 -0.968183 1.284044
2000-01-07 0.565208 0.032738 -2.125934 0.482797
2000-01-08 0.564129 -0.759118 -2.454374 -0.325454
2000-01-09 2.048458 -1.820537 -0.535232 -1.212381
2000-01-10 2.065750 0.383357 1.541496 -3.201469
A B
2000-01-01 1.088512 -0.650942
2000-01-02 1.879182 -1.038796
2000-01-03 1.303660 -2.003821
2000-01-04 1.884801 -0.141119
2000-01-05 1.194699 0.010551
2000-01-06 1.925393 1.968551
2000-01-07 0.565208 0.032738
2000-01-08 0.564129 -0.759118
2000-01-09 2.048458 -1.820537
2000-01-10 2.065750 0.383357
Unit-6
Data Visualization: The matplotlib API primer-Line properties, Figures and subplots, Exploring plot types-
Scatter plots, Bar plots, Histogram plots, Legends and annotations, Plotting functions with Pandas
Matplotlib
195 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Matplotlib is a low level graph plotting library in python that serves as a visualization utility.
Matplotlib is mostly written in python, a few segments are written in C, Objective-C and Javascript
for Platform compatibility.
Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under
the plt alias:
Example
plt.plot(xpoints, ypoints)
plt.show()
Result:
196 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Matplotlib Plotting
If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the
plot function.
Example
plt.plot(xpoints, ypoints)
plt.show()
Result:
197 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.
Example
Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):
Result:
Multiple Points
198 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
You can plot as many points as you like, just make sure you have the same number of points in
both axis.
Example
Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):
plt.plot(xpoints, ypoints)
plt.show()
Result:
Default X-Points
If we do not specify the points in the x-axis, they will get the default values 0, 1, 2, 3, (etc.
depending on the length of the y-points.
Example
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
plt.plot(ypoints)
plt.show()
Result:
Matplotlib Line
Linestyle
You can use the keyword argument linestyle, or shorter ls, to change the style of the plotted line:
Example
200 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Matplotlib Subplots
Display Multiple Plots
201 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
With the subplots() function you can draw multiple plots in one figure:
Example
Draw 2 plots:
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()
Result:
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
The subplots() function takes three arguments that describes the layout of the figure.
plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is the first plot.
plt.subplot(1, 2, 2)
#the figure has 1 row, 2 columns, and this plot is the second plot.
So, if we want a figure with 2 rows an 1 column (meaning that the two plots will be displayed on
top of each other instead of side-by-side), we can write the syntax like this:
Example
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 1, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 1, 2)
plt.plot(x,y)
plt.show()
203 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
You can draw as many plots you like on one figure, just descibe the number of rows, columns, and
the index of the plot.
Example
Draw 6 plots:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 1)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 2)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
204 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
plt.subplot(2, 3, 3)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 4)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 5)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 6)
plt.plot(x,y)
plt.show()
205 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Matplotlib Scatter
With Pyplot, you can use the scatter() function to draw a scatter plot.
The scatter() function plots one dot for each observation. It needs two arrays of the same length,
one for the values of the x-axis, and one for values on the y-axis:
Example
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)
plt.show()
206 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
The observation in the example above is the result of 13 cars passing by.
Compare Plots
Example
plt.show()
207 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
By comparing the two plots, I think it is safe to say that they both gives us the same conclusion:
the newer the car, the faster it drives.
Colors
You can set your own color for each scatter plot with the color or the c argument:
Example
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y, color = 'hotpink')
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y, color = '#88c999')
plt.show()
208 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
You can even set a specific color for each dot by using an array of colors as value for
the c argument:
Example
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors =
np.array(["red","green","blue","yellow","pink","black","orange","purple","beige","brown","gra
y","cyan","magenta"])
plt.scatter(x, y, c=colors)
plt.show()
209 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
ColorMap
A colormap is like a list of colors, where each color has a value that ranges from 0 to 100.
210 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
This colormap is called 'viridis' and as you can see it ranges from 0, which is a purple color, and up
to 100, which is a yellow color.
You can specify the colormap with the keyword argument cmap with the value of the colormap,
in this case 'viridis' which is one of the built-in colormaps available in Matplotlib.
In addition you have to create an array with values (from 0 to 100), one value for each of the point
in the scatter plot:
Example
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
211 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
plt.show()
Result:
You can include the colormap in the drawing by including the plt.colorbar() statement:
Example
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
plt.colorbar()
plt.show()
212 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Matplotlib Bars
Creating Bars
With Pyplot, you can use the bar() function to draw bar graphs:
Example
Draw 4 bars:
plt.bar(x,y)
plt.show()
213 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
The bar() function takes arguments that describes the layout of the bars.
The categories and their values represented by the first and second argument as arrays.
Example
x = ["APPLES", "BANANAS"]
y = [400, 350]
plt.bar(x, y)
Horizontal Bars
If you want the bars to be displayed horizontally instead of vertically, use the barh() function:
Example
214 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
plt.barh(x, y)
plt.show()
Result:
Bar Color
The bar() and barh() takes the keyword argument color to set the color of the bars:
Example
215 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Color Names
Example
216 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Color Hex
Example
217 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Bar Width
The bar() takes the keyword argument width to set the width of the bars:
Example
218 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Bar Height
The barh() takes the keyword argument height to set the height of the bars:
Example
219 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Matplotlib Histograms
Histogram
Example: Say you ask for the height of 250 people, you might end up with a histogram like this:
220 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
You can read from the histogram that there are approximately:
Create Histogram
The hist() function will use an array of numbers to create a histogram, the array is sent into the
function as an argument.
For simplicity we use NumPy to randomly generate an array with 250 values, where the values will
concentrate around 170, and the standard deviation is 10. Learn more about Normal Data
Distribution in our Machine Learning Tutorial.
Example
import numpy as np
print(x)
Result:
This will generate a random result, and could look like this:
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
222 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
The hist() function will read the array and produce a histogram:
Example
A simple histogram:
plt.hist(x)
plt.show()
Result:
With Pyplot, you can use the pie() function to draw pie charts:
Example
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
plt.pie(y)
plt.show()
Result:
As you can see the pie chart draws one piece (called a wedge) for each value in the array (in this
case [35, 25, 25, 15]).
By default the plotting of the first wedge starts from the x-axis and move counterclockwise:
Labels
224 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
The label parameter must be an array with one label for each wedge:
Example
Result:
Start Angle
As mentioned the default start angle is at the x-axis, but you can change the start angle by
specifying a startangle parameter.
225 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Example
226 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Explode
Maybe you want one of the wedges to stand out? The explode parameter allows you to do that.
The explode parameter, if specified, and not None, must be an array with one value for each
wedge.
Each value represents how far from the center each wedge is displayed:
Example
Pull the "Apples" wedge 0.2 from the center of the pie:
227 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Shadow
Add a shadow to the pie chart by setting the shadows parameter to True:
Example
Add a shadow:
228 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
Colors
You can set the color of each wedge with the colors parameter.
The colors parameter, if specified, must be an array with one value for each wedge:
Example
229 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
You can use Hexadecimal color values, any of the 140 supported color names, or one of these
shortcuts:
'r' - Red
'g' - Green
'b' - Blue
'c' - Cyan
'm' - Magenta
'y' - Yellow
'k' - Black
'w' - White
Legend
To add a list of explanation for each wedge, use the legend() function:
Example
Add a legend:
230 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
plt.legend()
plt.show()
Result:
To add a header to the legend, add the title parameter to the legend function.
Example
231 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam
CODE: MCA 3.2 PYTHON PROGRAMMING
Result:
232 | P a g e
MCA Department,
Dr Lankapalli Bullayya College,
Vishakapatnam