Document From Karthikeyan
Document From Karthikeyan
Document From Karthikeyan
UNIT II DATA TYPES IN PYTHON: Lists, Tuples, Sets, Strings, Dictionary, Modules: Module Loading
and Execution – Packages – Making Your Own Module – The Python Standard Libraries.
UNIT III FILE HANDLING AND EXCEPTION HANDLING: Files: Introduction – File Path – Opening
and Closing Files – Reading and Writing Files –File Position –Exception: Errors and Exceptions,
Exception Handling, Multiple Exceptions
UNIT IV MODULES, PACKAGES AND FRAMEWORKS: Modules: Introduction – Module Loading and
Execution – Packages – Making Your Own Module – The Python Libraries for data processing, data
mining and visualization- NUMPY, Pandas, Matplotlib, Plotly-Frameworks- -Django, Flask, Web2Py
UNIT V OBJECT ORIENTED PROGRAMMING IN PYTHON: Creating a Class, Class methods, Class
Inheritance, Encapsulation, Polymorphism, class method vs. static methods, Python object
persistence.
o Software development,
o Mathematics,
o System scripting
1
WHY PYTHON? (2M)
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.
Python is Interpreted − Python is processed at runtime by the interpreter. You do not need to
compile your program before executing it. This is similar to PERL and PHP.
Python is Interactive − You can actually sit at a Python prompt and interact with the interpreter
directly to write your programs.
Python is Object-Oriented − Python supports Object-Oriented style or technique of programming
that encapsulates code within objects.
Python is a Beginner's Language − Python is a great language for the beginner-level programmers
and supports the development of a wide range of applications from simple text processing to WWW
browsers to games.
It provides rich data types and easier to read syntax than any other programming languages.
It is a platform independent scripted language with full access to operating system API's.
Compared to other programming languages, it allows more run-time flexibility.
It includes the basic text manipulation facilities of Perl and Awk.
Libraries in Pythons are cross-platform compatible with Linux, Macintosh, and Windows.
For building large applications, Python can be compiled to byte-code.
Python supports functional and structured programming as well as OOP.
It supports interactive mode that allows interacting Testing and debugging of snippets of code
In Python, since there is no compilation step, editing, debugging and testing is fast.
2
APPLICATIONS OF PYTHON (FEATURES) (2M)
Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This
allows the student to pick up the language quickly.
Easy-to-read − Python code is more clearly defined and visible to the eyes.
Easy-to-maintain − Python's source code is fairly easy-to-maintain.
A broad standard library − Python's bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
Interactive Mode − Python has support for an interactive mode which allows interactive testing and
debugging of snippets of code.
Portable − Python can run on a wide variety of hardware platforms and has the same interface on
all platforms.
Extendable − You can add low-level modules to the Python interpreter. These modules enable
programmers to add to or customize their tools to be more efficient.
Databases − Python provides interfaces to all major commercial databases.
GUI Programming − Python supports GUI applications that can be created and ported to many
system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window
system of Unix.
Scalable − Python provides a better structure and support for large programs than shell scripting.
To test a short amount of code in python sometimes it is quickest and easiest not to write the
code in a file. This is made possible because Python can be run as a command line itself. Which will
write "Hello, World!" in the command line:
Whenever you are done in the python command line, you can simply type the following to quit the
python command line interface:
3
PYTHON SYNTAX (2M)
Hello, World!
Or by creating a python file on the server, using the .py file extension, and running it in the Command
Line:
COMMENTS (2M)
EXAMPLE:
OUTPUT:
Hello, World!
PYTHON INDENTATION(2M)
Indentation refers to the spaces at the beginning of a code line. Where in other programming
languages the indentation in code is for readability only, the indentation in Python is very important.
EXAMPLE
if 5 > 2:
4
PYTHON INTERPRETER AND INTERACTIVE MODE (6M)
• Interactive mode
• Script mode
Interactive Mode
Interactive mode, also known as the REPL provides us with a quick way of running blocks or a single
line of Python code. The code executes via the Python shell, which comes with Python installation.
To access the Python shell, open the terminal of your operating system and then type "python".
Press the enter key and the Python shell will appear. This is the same Python executable you use
to execute scripts, which comes installed by default on Mac and Unix-based operating systems.
C:\Windows\system32>python
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> indicates that the Python shell is ready to execute and send your commands to the Python
interpreter. The result is immediately displayed on the Python shell as soon as the Python
interpreter interprets the command. To run your Python statements, just type them and hit the enter
key. You will get the results immediately, unlike in script mode. For example, to print the text "Hello
World", we can type the following:
Using the method demonstrated above, you can run multiple Python statements without having to
create and save a script. You can also copy your code from another source then paste it on the Python
shell. Consider the following example:
5
>>> if 5 > 10:
... print("5 is greater than 10")
... else:
... print("5 is less than 10")
...
5 is less than 10
>>>
The above example also demonstrates how we can run multiple Python statements in interactive
mode. The two print statements have been indented using four spaces. Just like in script mode, if you
don't indent properly you will get an error. Also, to get the output after the last print statement,
you should press the enter key twice without typing anything.
SCRIPT MODE
If you need to write a long piece of Python code or your Python script spans multiple files,
interactive mode is not recommended. Script mode is the way to go in such cases. In script mode, You
write your code in a text file then save it with a .py extension which stands for "Python". Note that
you can use any text editor for this, including Sublime, Atom, notepad++, etc.
If you are in the standard Python shell, you can click "File" then choose "New" or simply hit "Ctrl + N"
on your keyboard to open a blank script in which you can write your code. You can then press "Ctrl
+ S" to save it.
After writing your code, you can run it by clicking "Run" then "Run Module" or simply press F5.
Let us create a new file from the Python shell and give it the name "hello.py". We need to run the
"Hello World" program. Add the following code to the file:
print("Hello World")
Click "Run" then choose "Run Module". This will run the program: Hello World
Other than executing the program from the graphical user interface, we can do it from the terminal
of the operating system. However, you must be aware of the path to the directory where you have
saved the file.
Open the terminal of your operating system then navigate to the location of the file. Of course, you
will use the "cd (change directory)" command for this.
Once you reach the directory with the file, you will need to invoke the Python interpreter on the file.
This can be done using the following syntax:
>python <filename>
6
To run the Python file from the terminal, you just have to type the python keyword followed by the
name of the file. In our case, we need to run a file named "hello.py". We need to type the following on
the terminal of the operating system:
>python hello.py
Hello World
The following example demonstrates how to execute the multiple lines of code using the Python
script.
name = "Nicholas"
age = 26
course = "Computer Science"
print("My name is", name, ",aged", age, ",taking", course)
Key Differences Between Interactive and Script Mode.
In script mode, a file must be created and saved before executing the code to get results. In interactive
mode, the result is returned immediately after pressing the enter key. In script mode, you are
provided with a direct way of editing your code. This is not possible in interactive mode.
Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function
name or any other identifier. They are used to define the syntax and structure of the Python
language. In Python, keywords are case sensitive. There are 33 keywords in Python 3.7.
All the keywords except True, False and None are in lowercase and they must be written as they are.
The list of all the keywords is given below.
7
Rules for writing identifiers (2M)
Names like myClass, var_1 and print_this_to_screen, all are valid example.
(EX.) global = 1
Variables are containers for storing data values. Variables are nothing but reserved memory
locations to store values. This means that when you create a variable you reserve some space in
memory. Based on the data type of a variable, the interpreter allocates memory and decides what
can be stored in the reserved memory. Therefore, by assigning different data types to variables, you
can store integers, decimals or characters in these variables.
Python variables do not need explicit declaration to reserve memory space. The declaration
happens automatically when you assign a value to a variable. The equal sign (=) is used to assign
values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of
the = operator is the value stored in the variable.
For example –
8
Here, 100, 1000.0 and "John" are the values assigned to counter, miles, and name variables,
respectively. This produces the following result –
100
1000.0
John
Multiple Assignment
Python allows you to assign a single value to several variables simultaneously. For example –
a=b=c=1
Here, an integer object is created with the value 1, and all three variables are assigned to the same
memory location. You can also assign multiple objects to multiple variables. For example,
a,b,c = 1,2,"john"
Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one
string object with the value "john" is assigned to the variable c.
Creating Variables: Python has no command for declaring a variable. A variable is created the
moment you first assign a value to it.
Example
x=5 OUTPUT:
y = "shan" 5
print(x) Shan
print(y)
Variables do not need to be declared with any particular type, and can even change type after they
have been set.
x=4 OUTPUT:
x = "Shan" Shan
print(x)
Get the Type: You can get the data type of a variable with the type() function.
x=5 OUTPUT:
y = "John" <class 'int'>
print(type(x)) <class 'str'>
print(type(y))
Single or Double Quotes: String variables can be declared either by using single or double quotes.
x = "Shann" OUTPUT:
print(x) Shann
x = 'Shann' Shann
print(x)
9
Case-Sensitive: Variable names are case-sensitive.
a=4 OUTPUT:
A = "Shanthi" 4
#A will not overwrite a Shanthi
Python - Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, etc.)
Rules for Python variables: (2M)
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
Legal variable names: Illegal variable names:
myvar = "John" 2myvar = "John"
my_var = "John" my-var = "John"
_my_var = "John" my var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John”
MULTI WORDS VARIABLE NAMES
Variable names with more than one word can be difficult to read. There are several techniques
you can use to make them more readable:
Camel Case: Each word, except the first, starts with a capital letter: myVariableName = "John"
Pascal Case: Each word starts with a capital letter: MyVariableName = "John"
Snake Case : Each word is separated by an underscore character: my_variable_name = "John"
PYTHON VARIABLES - ASSIGN MULTIPLE VALUES
Many Values to Multiple Variables
Python allows you to assign values to multiple variables in one line:
x, y, z = "Orange", "Banana", "Cherry" OUTPUT:
print(x) Orange
print(y) Banana
print(z) Cherry
Note: Make sure the number of variables matches the number of values, or else you will get an error.
One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
x = y = z = "Orange"
print(x) Orange
print(y) Orange
print(z) Orange
10
PYTHON - OUTPUT VARIABLES (2M)
The Python print statement is often used to output variables. To combine both text and a variable,
Python uses the + character:
x = "awesome" OUTPUT:
print("Python is " + x) Python is awesome
You can also use the + character to add a variable to another variable:
x = "Python is " OUTPUT:
y = "awesome" Python is awesome
z=x+y
print(z)
For numbers, the + character works as a mathematical operator:
x=5 OUTPUT:
y = 10
print(x + y) 15
If you try to combine a string and a number, Python will give you an error:
x=5 OUTPUT:
y = "John" TypeError: unsupported operand
print(x + y) type(s) for +: 'int' and 'str'
If you create a variable with the same name inside a function, this variable will be local, and can only
be used inside the function. The global variable with the same name will remain as it was, global and
with the original value.
Example:
Create a variable inside a function, with the same name as the global variable
x = "awesome" def myfunc(): OUTPUT:
x = "fantastic" Python is fantastic
print("Python is " + x) Python is awesome
myfunc()
print("Python is " + x)
11
STANDARD DATA TYPES (2M,6M,8M)
The data stored in memory can be of many types. For example, a person's age is stored as a numeric
value and his or her address is stored as alphanumeric characters. Python has various standard data
types that are used to define the operations possible on them and the storage method for each of
them.
Python has five standard data types –
Numbers
String
List
Tuple
Dictionary
Python Numbers: Number data types store numeric values. Number objects are created when you
assign a value to them.
For example −
Var1=1
Coffee=10
Python supports four different numerical types −
int (signed integers)
long (long integers, they can also be represented in octal and hexadecimal)
float (floating point real values)
complex (complex numbers)
Examples:
Int long float complex
90 324393024896 45.222 3.14j
Python allows you to use a lowercase l with long, but it is recommended that you use only an
uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase
L.
A complex number consists of an ordered pair of real floating-point numbers denoted by x + yj, where
x and y are the real numbers and j is the imaginary unit.
PYTHON STRINGS
Strings in Python are identified as a contiguous set of characters represented in the quotation
marks. Python allows for either pairs of single or double quotes.
Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the
beginning of the string and working their way from -1 at the end.
12
The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator.
For example −
str = 'Hello World!'
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
This will produce the following result −
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
Python Lists
Lists are the most versatile of Python's compound data types. A list contains items separated by
commas and enclosed within square brackets([ ]). To some extent, lists are similar to arrays in C.
One difference between them is that all the items belonging to a list can be of different data type.
The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting
at 0 in the beginning of the list and working their way to end -1.The plus (+) sign is the list
concatenation operator, and the asterisk (*) is the repetition operator.
For example –
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # Prints complete list
print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd
print list[2:] # Prints elements starting from 3rd element
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
13
This produce the following result −
['abcd', 786, 2.23, 'john', 70.2]
abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
Python Tuples
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values
separated by commas. Unlike lists, however, tuples are enclosed within parentheses( ). The main
differences between lists and tuples are:
o Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed.
o Tuples are enclosed in parentheses ( ( ) ) and cannot be updated.
o Tuples can be thought of as read-only lists.
For example −
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple # Prints the complete tuple
print tuple[0] # Prints first element of the tuple
print tuple[1:3] # Prints elements of the tuple starting from 2nd till 3rd
print tuple[2:] # Prints elements of the tuple starting from 3rd element
print tinytuple * 2 # Prints the contents of the tuple twice
print tuple + tinytuple # Prints concatenated tuples
This produce the following result −
('abcd', 786, 2.23, 'john', 70.2)
abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
The following code is invalid with tuple, because we attempted to update a tuple, which is not
allowed. Similar case is possible with lists −
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
14
Python Dictionary
Python's dictionaries are kind of hash table type.
They work like associative arrays or hashes found and consist of key-value pairs.
A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the
other hand, can be any arbitrary Python object.
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using
square braces ([]).
For example −
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
This produce the following result −
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
Dictionaries have no concept of order among elements. It is incorrect to say that the elements are "out
of order"; they are simply unordered.
OPERATORS
Operators in general are used to perform operations on values and variables in Python. These are
standard symbols used for the purpose of logical and arithmetic operations. Python divides the
operators in the following groups:
1. Arithmetic operators
2. Assignment operators
3. Relational (or) Comparison operators
4. Logical operators
5. Identity operators
15
6. Membership operators
7. Bitwise operators
Arithmetic operators: Arithmetic operators are used to perform mathematical operations like
addition, subtraction, multiplication and division.
OPERATOR DESCRIPTION SYNTAX
+ Addition: adds two operands x+y
- Subtraction: subtracts two operands x-y
* Multiplication: multiplies two operands x*y
/ Division (float): divides the first operand by the second x/y
// Division (floor): divides the first operand by the second x // y
% Modulus: returns the remainder when first operand is divided by x%y
the second
** Power : Returns first raised to power second x ** y
Examples of Arithmetic Operator
a=9 OUTPUT:
b=4 13
add = a + b # Addition of numbers print(add) # print results 5
sub = a - b # Subtraction of numbers print(sub) 36
mul = a * b # Multiplication of number print(mul) 2.25
div1 = a / b # Division(float) of number print(div1) 2
div2 = a // b # Division(floor) of number print(div2) 1
mod = a % b # Modulo of both number print(mod) 6561
p = a ** b # Power print(p)
16
Examples of Relational Operators:
a = 13 OUTPUT:
b = 33
print(a > b) # a > b is False False
print(a < b) # a < b is True True
print(a == b) # a == b is False False
print(a != b) # a != b is True True
print(a >= b) # a >= b is False False
print(a <= b) # a <= b is True True
Logical operators: Logical operators perform Logical AND, Logical OR and Logical
NOT operations.
OPERATOR DESCRIPTION SYNTAX
AND Logical AND: True if both the operands are true x and y
OR Logical OR: True if either of the operands is true x or y
NOT Logical NOT: True if operand is false not x
Bitwise operators: Bitwise operators acts on bits and performs bit by bit operation.
17
Assignment operators: Assignment operators are used to assign values to the variables.
OPERATOR DESCRIPTION SYNTAX
= Assign value of right side of expression to left side operand x=y+z
+= Add AND: Add right side operand with left side operand and a+=b
then assign to left operand a=a+b
-= Subtract AND: Subtract right operand from left operand and a-=b
then assign to left operand a=a-b
*= Multiply AND: Multiply right operand with left operand and a*=b
then assign to left operand a=a*b
/= Divide AND: Divide left operand with right operand and then a/=b
assign to left operand a=a/b
%= Modulus AND: Takes modulus using left and right operands and a%=b
assign result to left operand a=a%b
//= Divide(floor) AND: Divide left operand with right operand and a//=b
then assign the value(floor) to left operand a=a//b
**= Exponent AND: Calculate exponent(raise power) value using a**=b
operands and assign value to left operand a=a**b
&= Performs Bitwise AND on operands and assign value to left a&=b
operand a=a&b
|= Performs Bitwise OR on operands and assign value to left a|=b
operand a=a|b
^= Performs Bitwise xOR on operands and assign value to left a^=b
operand a=a^b
>>= Performs Bitwise right shift on operands and assign value to a>>=b
left operand a=a>>b
<<= Performs Bitwise left shift on operands and assign value to left a <<= b
operand a= a << b
Special operators:
There are some special type of operators like-
Identity operators: is and is not are the identity operators both are used to check if two values are
located on the same part of the memory. Identity operators compare the memory locations of two
objects. Two variables that are equal does not imply that they are identical.
is True if the operands are identical
is not True if the operands are not identical
Examples of Identity operators
a1 = 3 Output:
b1 = 3 False
a2 = 'Shanthi'
True
b2 = 'Shanthi'
a3 = [1,2,3] False
b3 = (1,2,3)
print(a1 is not b1)
18
print(a2 is b2)
print(a3 is b3)
Membership operators: in and not in are the membership operators; used to test whether a
value or variable is in a sequence. Python’s membership operators test for membership in a
sequence, such as strings, lists, or tuples.
in True if value is found in the sequence
not in True if value is not found in the sequence
Examples of Membership operator
x = 'Be Happy' OUTPUT:
y = {3:'a',4:'b'}
print('B' in x) True
print('happy' not in x) True
print('Be' not in x) False
print(3 in y) True
print('B' in y) False
19
11 is is not
Identity operators
12 in not in
Membership operators
13 not or and
Logical operators
Example 1:
a = 20 OUTPUT:
b = 10
c = 15 Value of (a + b) * c / d is 90
d=5
e=0 Value of ((a + b) * c) / d is 90
e = (a + b) * c / d #( 30 * 15 ) / 5
print( "Value of (a + b) * c / d is ", e) Value of (a + b) * (c / d) is 90
e = ((a + b) * c) / d # (30 * 15 ) / 5
Value of a + (b * c) / d is 50
print ("Value of ((a + b) * c) / d is ", e)
e = a + (b * c) / d; # 20 + (150/5)
print ("Value of a + (b * c) / d is ", e)
Operator Associativity: If an expression contains two or more operators with the same precedence
then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.
20
Examples of Operator Associativity:
# Left-right associativity OUTPUT:
# 100 / 10 * 10 is calculated as
# (100 / 10) * 10 and not 100.0
# as 100 / (10 * 10)
print(100 / 10 * 10) 6
# Left-right associativity 0
# 5 - 2 + 3 is calculated as
# (5 - 2) + 3 and not 512
# as 5 - (2 + 3)
print(5 - 2 + 3)
# left-right associativity
print(5 - (2 + 3))
# right-left associativity
# 2 ** 3 ** 2 is calculated as
# 2 ** (3 ** 2) and not
# as (2 ** 3) ** 2
print(2 ** 3 ** 2)
There comes situations in real life when we need to make some decisions and based on these
decisions, we decide what should, we do next. Similar situations arises in programming also where
we need to make some decisions and based on these decisions we will execute the next block of
code. Decision making statements in programming languages decides the direction of flow of
program execution. Decision making statements available in python are:
if statement
if..else statements
if-elif ladder
nested if statements
IF STATEMENT
If statement is the most simple decision making statement. It is used to decide whether a certain
statement or block of statements will be executed or not. i.e if a certain condition is true then a
block of statement is executed otherwise not.
Syntax
if test_expression :
statement(s)
21
Here, the program evaluates the test_expression and will execute statement(s) only if the test
expression is True.
If the test expression is False, the statement(s) is not executed.
In Python, the body of the if statement is indicated by the indentation.
Flowchart
Example:
3 is a positive number
In the above example, num > 0 is the test expression.
The body of if is executed only if this evaluates to True.
When the variable num is equal to 3, test expression is true and statements inside the body
of if are executed.
If the variable num is equal to -1, test expression is false and statements inside the body of if are
skipped.
22
IF...ELSE STATEMENT
The if statement alone tells us that if a condition is true it will execute a block of statements and if the
condition is false it won’t. But what if we want to do something else if the condition is false. Here comes
the else statement. We can use the else statement with if statement to execute a block of code when the
condition is false.
Syntax :
if test expression:
Body of if
else:
Body of else
The if..else statement evaluates test expression and will execute the body of if only when the test
condition is True. If the condition is False, the body of else is executed. Indentation is used to
separate the blocks.
Flowchart:
Example :
# Program checks if the number is positive or negative and displays an appropriate message
num = 3
# Try these two variations as well.
# num = -5
# num = 0
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
Output :
Positive or Zero
23
In the above example, when num is equal to 3, the test expression is true and the body of if is
executed and the body of else is skipped.
If num is equal to -5, the test expression is false and the body of else is executed and the body
of if is skipped.
If num is equal to 0, the test expression is true and body of if is executed and body of else is
skipped.
IF...ELIF...ELSE STATEMENT
Here, a user can decide among multiple options. The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated with that if is
executed, and the rest of the ladder is bypassed.
If none of the conditions is true, then the final else statement will be executed.
Syntax :
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False, it checks the condition of the next elif block and so on.
If all the conditions are False, the body of else is executed.
Only one block among the several if...elif...else blocks is executed according to the condition.
The if block can have only one else block. But it can have multiple elif blocks.
Flowchart of if...elif...else
24
Example of if...elif...else
'''In this program, we check if the number is positive or negative or zero and display an
appropriate message'''
num = 3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
When variable num is positive, Positive number is printed.
If num is equal to 0, Zero is printed.
If num is negative, Negative number is printed.
NESTED IF STATEMENTS
SYNTAX:
if test expression1:
if test expression2:
25
FLOW CHART:
Example:
EXPRESSIONS (2M)
An expression is defined as a combination of constants, variables and operators. An
expression always evaluates to a value. A value or a standalone variable is also considered as an
expression but a standalone operator is not an expression.
(i) num > 20.4 (ii) 23/3 -5 * 7(14 -2) (iii) 3.0 + 3.14 (iv) "Global"+"Citizen"
26
INPUT AND OUTPUT (2M)
Sometimes, we need to enter data or enter choices into a program. In Python, we have the
input() function for taking values entered by input device such as a keyboard. The input()
function prompts user to enter data. It accepts all user input (whether alphabets, numbers or
special character) as string.
The syntax for input() is:
variable = input([Prompt])
Prompt is the string we may like to display on the screen prior to taking the input, but it is
optional.
The input() takes exactly what is typed from the keyboard, converts it into a string and assigns
it to the variable on left hand side of the assignment operator (=).
Example :
The variable fname gets the string ‘shanthi’ as input. Similarly, the variable age gets '24' as
string. We can change the datatype of the string data accepted from user to an appropriate numeric
value. For example, the int() function will convert the accepted string to an integer. If the entered
string is non-numeric, an error will be generated.
Example:
27
BOOLEAN VALUES
bool() in Python: The bool() method is used to return or convert a value to a Boolean value i.e.,
True or False, using the standard truth testing procedure.
Syntax: bool([x])
The bool() method in general takes only one parameter(here x), on which the standard truth testing
procedure can be applied.
If no parameter is passed, then by default it returns False. So, passing a parameter is optional.
It can return one of the two values.
It returns True if the parameter or value passed is True.
It returns False if the parameter or value passed is False.
Here are a few cases, in which Python’s bool() method returns false. Except these all other values return
True.
Example:
28
if(check(num)):
print("Even")
else:
print("Odd")
Output:
Even
A Loop executes the sequence of statements many times until the stated condition becomes
false. A loop consists of two parts, a body of a loop and a control statement.
The control statement is a combination of some conditions that direct the body of the loop to
execute until the specified condition becomes false.
The purpose of the loop is to repeat the same code a number of times.
In iteration control structures, a statement or block is executed until the program reaches a
certain state, or operations have been applied to every element of a collection.
This is usually expressed with keywords such as while, repeat, for, or do..until
FOR LOOP
The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable
objects. Iterating over a sequence is called traversal.
This is less like for keyword in other programming languages, and works more like an iterator
method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
29
Flowchart of for Loop
Looping Through a String : Even strings are iterable objects, they contain a sequence of characters:
With the break statement we can stop the loop before it has looped through all the items:
30
#Exit the loop when x is "C++": OUTPUT:
lang = ["python", "C++", "C"] python
for x in lang: C++
print(x)
if x == "C++":
break
Exit the loop when x is "C++", but this time the break comes before the print:
With the continue statement we can stop the current iteration of the loop, and continue with the
next:
Nested Loops
31
The range() function
print(range(10))
print(list(range(10)))
print(list(range(2, 8)))
print(list(range(2, 20, 3)))
OUTPUT
range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7]
[2, 5, 8, 11, 14, 17]
FOR LOOP WITH ELSE
A for loop can have an optional else block as well. The else part is executed if the items in the
sequence used in for loop exhausts.
The break keyword can be used to stop a for loop. In such cases, the else part is ignored. Hence,
a for loop's else part runs if no break occurs.
This for...else statement can be used with the break keyword to run the else block only when
the break keyword was not executed.
# program to display student's marks from record
student_name = 'Shan'
marks = {'James': 90, 'Jules': 55, 'Arthur': 77}
for student in marks:
if student == student_name:
print(marks[student])
break
else:
print('No entry with that name found.')
OUTPUT
No entry with that name found.
32
PYTHON WHILE LOOP
The while loop in Python is used to iterate over a block of code as long as the test expression
(condition) is true. We generally use this loop when we don't know the number of times to iterate
beforehand.
while (test_expression):
#Body of while
Statement1
Statement2
Statement3
Statement x
In the while loop, test expression is checked first. The body of the loop is entered only if
the test_expression evaluates to True.
After one iteration, the test expression is checked again. This process continues until
the test_expression evaluates to False.
In Python, the body of the while loop is determined through indentation.
The body starts with indentation and the first unindented line marks the end.
Python interprets any non-zero value as True. None and 0 are interpreted as False.
33
Example: Python while Loop
# Program to print ‘n’ natural numbers When you run the program, the output will be:
# To take input from the user Enter n value: 4
n=int(input("Enter n value:")) 0
i=0 #Loop variable initialization 1
while(i<=n): #condition 2
print(i) 3
i=i+1 #updation 4
print("END OF THE LOOP") END OF THE LOOP
In the above program, the test expression will be True as long as our counter variable i is less than
or equal to n. We need to increase the value of the counter variable in the body of the loop. This is
very important (and mostly forgotten). Failing to do so will result in an infinite loop (never-ending
loop). Finally, the result is displayed.
The break Statement: With the break statement we can stop the loop even if the while condition is
true: Example Exit the loop when i is 3:
i=1 OUTPUT:
while i < 6: 1
print(i) 2
if i == 3: 3
break
i =i+1
The continue Statement: With the continue statement we can stop the current iteration, and
continue with the next:
The else Statement: With the else statement we can run a block of code once when the condition no
longer is true:
34
Example: Print a message once the condition is false:
i=1 1
while i < 6: 2
print(i) 3
i =i+1 4
else: 5
print("i is no longer less than 6") i is no longer less than 6
The break statement terminates the loop containing it. Control of the program flows to the
statement immediately after the body of the loop.
If the break statement is inside a nested loop (loop inside another loop), the break statement will
terminate the innermost loop.
Syntax of break
break
35
Example: Python break
The continue statement is used to skip the rest of the code inside a loop for the current iteration only.
Loop does not terminate but continues on with the next iteration.
Syntax of Continue
continue
Flowchart of continue
The working of continue statement in for and
while loop
36
This program is same as the above example except the break statement has been replaced
with continue. We continue with the loop, if the string is i, not executing the rest of the block. Hence,
we see in our output that all the letters except i gets printed.
FUNCTION
In Python, a function is a group of related statements that performs a specific task.
Functions help break our program into smaller and modular chunks.
As our program grows larger and larger, functions make it more organized and manageable.
Furthermore, it avoids repetition and makes the code reusable.
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. A function is a block of
organized, reusable code that is used to perform a single, related action.
As you already know, Python gives you many built-in functions like print(), etc. but you can also
create your own functions. These functions are called user-defined functions.
Syntax of Function
37
How to call a function in python?
Once we have defined a function, we can call it from another function, program or even the
Python prompt. To call a function we simply type the function name with appropriate
parameters.
>>> greet('shanthi')
Hello, shanthi. Good morning!
_____________________________________________________________________________________________________
def greet(name):
"""This function greets to the person passed in as a parameter """
print("Hello, " + name + ". Good morning!")
greet('Paul')
OUTPUT:
Hello, Paul. Good morning!
Docstrings
The first string after the function header is called the docstring and is short for documentation
string. It is briefly used to explain what a function does.
Although optional, documentation is a good programming practice. Unless you can remember
what you had for dinner last week, always document your code.
In the above example, we have a docstring immediately below the function header.
We generally use triple quotes so that docstring can extend up to multiple lines.
This string is available to us as the __doc__ attribute of the function.
For example:
Try running the following into the Python shell to see the output.
>>> print(greet.__doc__)
This function greets to the person passed in as a parameter
38
If the return statement has no expression or does not exist itself in the function then it returns
the None object.
Example 1
def sum(): # Defining function
a = 10
b = 20
c = a+b
return c
print("The sum is:",sum()) # calling sum() function in print statement
Output:
The sum is: 30
In the above code, we have defined the function named sum, and it has a statement c = a+b, which
computes the given values, and the result is returned by the return statement to the caller
function.
Example 2 : Creating function without return statement
def sum(): # Defining function
a = 10
b = 20
c = a+b
print(sum()) # calling sum() function in print statement
Output:
None
In the above code, we have defined the same function without the return statement as we can
see that the sum () function returned the None object to the caller function.
Arguments in function (parameter passing)
The arguments are types of information which can be passed into the function. The arguments
are specified in the parentheses. We can pass any number of arguments, but they must be
separate them with a comma. Consider the following example, which contains a function that
accepts a string as the argument.
39
Output:
Hi Riswanth
Example 2
1. Required arguments
2. Keyword arguments
3. Default arguments
4. Variable-length arguments
Required Arguments
Till now, we have learned about function calling in Python. However, we can provide the arguments
at the time of the function call. As far as the required arguments are concerned, these are the
arguments which are required to be passed at the time of function calling with the exact match of
their positions in the function call and function definition. If either of the arguments is not
provided in the function call, or the position of the arguments is changed, the Python interpreter will
show the error.
Example 1
def func(name):
return message
print(func(name))
Output:
40
Enter the name: John
Hi John
The function simple_interest accepts three arguments and returns the simple interest accordingly
,
def simple_interest(p,t,r):
return (p*t*r)/100
Output:
Example 3: The function calculate returns the sum of two arguments a and b
def calculate(a,b):
return a+b
Output:
Default Arguments
Python allows us to initialize the arguments at the function definition. If the value of any of the
arguments is not provided at the time of function call, then that argument can be initialized with the
value given in the definition even if the argument is not specified at the function call.
Example 1
def detail(name,age=22):
41
detail(name = "john")
Output:
Example 2
def detail(name,age=22):
detail(name = "john") #the variable age is not passed into the function however the default value of
age is considered in the function
detail(age = 10,name="David") #the value of age is overwritten here, 10 will be printed as age
Output:
In large projects, sometimes we may not know the number of arguments to be passed in advance. In
such cases, Python provides us the flexibility to offer the comma-separated values which are
internally treated as tuples at the function call. By using the variable-length arguments, we can
pass any number of arguments.
However, at the function definition, we define the variable-length argument using the *args (star) as
*<variable - name >.
Example
def printme(*names):
print("type of passed argument is ",type(names))
print("printing the passed arguments...")
for name in names:
print(name)
printme("john","David","smith","nick")
Output:
type of passed argument is <class 'tuple'>
printing the passed arguments...
john
42
David
smith
nick
In the above code, we passed *names as variable-length argument. We called the function and
passed values which are treated as tuple internally. The tuple is an iterable sequence the same as the
list. To print the given values, we iterated *arg names using for loop.
Keyword arguments(**kwargs)
Python allows us to call the function with the keyword arguments. This kind of function call will
enable us to pass the arguments in the random order.
The name of the arguments is treated as the keywords and matched in the function calling and
definition. If the same match is found, the values of the arguments are copied in the function
definition.
Example 1
#function func is called with the name and message as the keyword arguments
def func(name,message):
#name and message is copied with the values John and hello respectively
func(name = "John",message="hello")
Output:
#The function simple_interest(p, t, r) is called with the keyword arguments the order of argument
s doesn't matter in this case
def simple_interest(p,t,r):
return (p*t*r)/100
43
Output:
If we provide the different name of arguments at the time of function call, an error will be thrown.
Example 3
def simple_interest(p,t,r):
return (p*t*r)/100
# doesn't find the exact match of the name of the arguments (keywords)
Output:
The Python allows us to provide the mix of the required arguments and keyword arguments at
the time of function call.
However, the required argument must not be given after the keyword argument, i.e., once the
keyword argument is encountered in the function call, the following arguments must also be the
keyword arguments.
Example 4
def func(name1,message,name2):
func("John",message="hello",name2="David")
Output:
The following example will cause an error due to an in-proper mix of keyword and required
arguments being passed in the function call.
44
Example 5
def func(name1,message,name2):
func("John",message="hello","David")
Output:
Python provides the facility to pass the multiple keyword arguments which can be represented
as **kwargs. It is similar as the *args but it stores the argument in the dictionary format. This type of
arguments is useful when we do not know the number of arguments in advance.
def food(**kwargs):
print(kwargs)
food(a="Apple")
food(fruits="Orange", Vagitables="Carrot")
Output:
{'a': 'Apple'}
Scope of variables
A variable is only available from inside the region it is created. This is called scope.
The scopes of the variables depend upon the location where the variable is being declared. The
variable declared in one part of the program may not be accessible to the other parts.
In python, the variables are defined with the two types of scopes.
Global variables
Local variables
The variable defined outside any function is known to have a global scope, whereas the variable
defined inside a function is known to have a local scope.
45
Local Scope
A variable created inside a function belongs to the local scope of that function, and can only be
used inside that function.
def print_message():
print(message)
print_message()
print(message) # this will cause an error since a local variable cannot be accessible here.
Output:
print(message)
Global Scope
A variable created in the main body of the Python code is a global variable and belongs to the
global scope. Global variables are available from within any scope, global and local.
sum=0
def calculate(*args):
sum=0
46
Output:
The sum is 60
______________________________________________________________________
Recursion
What is recursion?
Recursion is the process of defining something in terms of itself.
In simple words, it is a process in which a function calls itself directly or indirectly.
A physical world example would be to place two parallel mirrors facing each other.
Any object in between them would be reflected recursively.
Python Recursive Function
In Python, we know that a function can call other functions. It is even possible for the function to
call itself. These types of construct are termed as recursive functions.
The following image shows the working of a recursive function called recurse.
47
When we call this function with a positive integer,
it will recursively call itself by decreasing the number.
Each function multiplies the number with the factorial of
the number below it until it is equal to one. This recursive
call can be explained in the following steps.
factorial(3) # 1st call with 3
3 * factorial(2) # 2nd call with 2
3 * 2 * factorial(1) # 3rd call with 1
3*2*1 # return from 3rd call as number=1
3*2 # return from 2nd call
6 # return from 1st call
Let's look at an image that shows a step-by-step process
of what is going on:
Our recursion ends when the number reduces to 1. This is called the base condition.
Every recursive function must have a base condition that stops the recursion or else the function
calls itself infinitely. The Python interpreter limits the depths of recursion to help avoid infinite
recursions, resulting in stack overflows.
By default, the maximum depth of recursion is 1000. If the limit is crossed, it results
in RecursionError. Let's look at one such condition.
Traceback (most recent call last):
File "C:/Users/prave/AppData/Local/Programs/Python/Python39/JGJHJ.PY", line 8, in <module>
print("The factorial of", num, "is", factorial(num))
[Previous line repeated 1021 more times]
File "C:/Users/prave/AppData/Local/Programs/Python/Python39/JGJHJ.PY", line 3, in factorial
if x == 1:
RecursionError: maximum recursion depth exceeded in comparison
Advantages of Recursion
Recursive functions make the code look clean and elegant.
A complex task can be broken down into simpler sub-problems using recursion.
Sequence generation is easier with recursion than using some nested iteration.
Recursive functions render the code look simple and effective.
Disadvantages of Recursion
Sometimes the logic behind recursion is hard to follow through.
Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
Recursive functions are hard to debug
48
UNIT II - DATA TYPES IN PYTHON
In programming, data type is an important concept. Variables can store data of different types,
and different types can do different things.
Data types are the classification or categorization of data items. It represents the kind of
value that tells what operations can be performed on a particular data. Since everything is an
object in Python programming, data types are actually classes and variables are instance (object)
of these classes. Following are the standard or built-in data type of Python:
Numeric
Sequence Type
Boolean
Set
Dictionary
49
EXAMPLE 1:
num = -8 OUTPUT:
# print the data type
<class 'int'>
print(type(num))
EXAMPLE 2:
a=5 j=3 OUTPUT:
b=6 k=5
# Addition # Multiplication Addition: 11
c=a+b l=j*k
print("Addition:",c) print("Multiplication:",l) Subtraction: 3
Division: 4.0
d=9 m = 25
e=6 n=5 Multiplication: 15
# Subtraction # Modulus
f=d-e o=m%n Modulus: 0
print("Subtraction:",f) print("Modulus:",o)
p=6 Exponent: 36
g=8 q=2
True
h=2 # Exponent
# Division r = p ** q
i=g/h print("Exponent:",r)
print("Division:",i) print(isinstance(c, int))
Float type
This 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. Some examples of numbers that are represented as floats are 0.5 and -
7.823457. They can be created directly by entering a number with a decimal point, or by using
operations such as division on integers. Extra zeros present at the number’s end are ignored
automatically.
EXAMPLE 1:
num = 0.5 OUTPUT:
As we have seen, dividing any two integers produces a float. A float is also produced by running an
operation on two floats, or a float and an integer.
50
num = 6 * 7.0 OUTPUT:
Complex type
A complex number is a number which consists of the real and imaginary part. For example, 2
+ 3j is a complex number where 2 is the real component, and 3 multiplied by j is an imaginary part.
Example 1: OUTPUT:
num = 6 + 9j <class 'complex'>
print(type(num))
Example 2: OUTPUT:
a = 1 + 5j g = 1 + 5j Addition: (3+8j)
b = 2 + 3j # Addition h = 2 + 3j # Division Subtraction: (-1+8j)
c=a+b i=g/h Division:
print("Addition:",c) print("Division:",i) (1.307692307692308+0.538461
5384615384j)
d = 1 + 5j j = 1 + 5j Multiplication: (-13+13j)
e = 2 - 3j # Subtractionk = 2 + 3j #Multiplication
f=d-e l=j*k
print("Subtraction:",f) print("Multiplication:",l)
Type Conversion
We can convert one type of number into another. This is also known as coercion. Operations
like addition, subtraction coerce integer to float implicitly (automatically), if one of the operands
is float.
>>>1+2.0
3.0
We can see above that 1 (integer) is coerced into 1.0 (float) for addition and the result is also
a floating point number.
We can also use built-in functions like int(), float() and complex() to convert between types
explicitly. These functions can even convert from strings.
>>>int(2.3) >>>int(-2.8) >>>float(5)
2 -2 5.0
51
When converting from float to integer, the number gets truncated (decimal parts are
removed).
PYTHON MATHEMATICS
Python offers modules like math and random to carry out different mathematics like
trigonometry, logarithms, probability and statistics, etc.
import math OUTPUT
print(math.pi) 3.141592653589793
print(math.cos(math.pi)) -1.0
print(math.exp(10)) 22026.465794806718
print(math.log10(1000)) 3.0
print(math.sinh(1)) 1.1752011936438014
print(math.factorial(6)) 720
PYTHON RANDOM:
Python offers module that can generate random numbers.
import random OUTPUT:
print(random.randrange(10, 20)) 18
x = ['a', 'b', 'c', 'd', 'e']
# Get random choice
print(random.choice(x)) # Shuffle x e
random.shuffle(x)
print(x) ['c', 'e', 'd', 'b', 'a']
PYTHON TUPLE
A tuples are used to store multiple items in a single variable.
A tuple in Python is similar to a list. The difference between the two is that we cannot change
the elements of a tuple once it is assigned whereas we can change the elements of a list.
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.
EXAMPLE:
Fruit =("apple","banana","orange")
print(Fruit)
OUTPUT: ("apple","banana","orange")
52
Creating a Tuple
A tuple is created by placing all the items (elements) inside parentheses (), separated by
commas. The parentheses are optional, however, it is a good practice to use them.
A tuple can have any number of items and they may be of different types (int, float, list, string,
etc.).
# Empty tuple OUTPUT
my_tuple = ()
print(my_tuple) ()
# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple) (1, 2, 3)
# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple) (1, 'Hello', 3.4)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple) ('mouse', [8, 4, 6], (1, 2, 3))
A tuple can also be created without using parentheses. This is known as tuple packing.
my_tuple = 3, 4.6, "dog" OUTPUT
print(my_tuple) (3, 4.6, 'dog')
# tuple unpacking is also possible
a, b, c = my_tuple
print(a) #3 3
print(b) # 4.6 4.6
print(c) # dog Dog
Creating a tuple with one element is a bit tricky. Having one element within parentheses is not
enough. We will need a trailing comma to indicate that it is, in fact, a tuple.
my_tuple = ("hello")
print(type(my_tuple)) # <class 'str'>
# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple)) # <class 'tuple'>
# Parentheses is optional
my_tuple = "hello",
53
print(type(my_tuple)) # <class 'tuple'>
OUTPUT
<class 'str'>
<class 'tuple'>
<class 'tuple'>
ACCESS TUPLE ELEMENTS
There are various ways in which we can access the elements of a tuple.
1. Indexing
We can use the index operator [] to access an item in a tuple, where the index starts from 0.
So, a tuple having 6 elements will have indices from 0 to 5. Trying to access an index outside
of the tuple index range(6,7,... in this example) will raise an IndexError.
The index must be an integer, so we cannot use float or other types. This will result in
TypeError.
Likewise, nested tuples are accessed using nested indexing, as shown in the example below.
my_tuple = ('p','e','r','m','i','t') Output
print(my_tuple[0]) # 'p' p
print(my_tuple[5]) # 't' t
# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
# nested index
print(n_tuple[0][3]) # 's' s
print(n_tuple[1][1]) #4 4
2. Negative Indexing
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and so on.
my_tuple = ('p', 'e', 'r', 'm', 'i', 't') OUTPUT
print(my_tuple[-1]) t
print(my_tuple[-6]) p
3. Slicing
We can access a range of items in a tuple by using the slicing operator colon:
my_tuple = ('p','r','o','g','r','a','m') OUTPUT
print(my_tuple[1:4]) ('r', 'o', 'g')
print(my_tuple[:-5]) ('p', 'r')
54
print(my_tuple[5:]) ('a', 'm')
print(my_tuple[:]) ('p', 'r', 'o', 'g', 'r', 'a', 'm')
Slicing can be best visualized by considering the index to be between the elements as shown
below. So if we want to access a range, we need the index that will slice the portion from the tuple.
Element Slicing in Python
Changing a Tuple
Unlike lists, tuples are immutable. This means that elements of a tuple cannot be changed once
they have been assigned. But, if the element is itself a mutable data type like a list, its nested items
can be changed. We can also assign a tuple to different values (reassignment).
my_tuple = (4, 2, 3, [6, 5]) OUTPUT:
my_tuple[3][0] = 9 (4, 2, 3, [9, 5])
print(my_tuple) ('p', 'r', 'o', 'g', 'r', 'a', 'm')
# Tuples can be reassigned
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm')
print(my_tuple)
Deleting a Tuple
We cannot change the elements in a tuple. It means that we cannot delete or remove items
from a tuple. Deleting a tuple entirely, however, is possible using the keyword del.
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm') Traceback (most recent call last):
del my_tuple File
# NameError: name 'my_tuple' "C:/Users/prave/AppData/Local/Programs/Python/Python
is not defined 39/UYUVHG.py", line 4, in <module>
print(my_tuple) print(my_tuple)
NameError: name 'my_tuple' is not defined
55
Tuple Methods
Methods that add items or remove items are not available with tuple. Only the following two
methods are available. Some examples of Python tuple methods:
my_tuple = ('a', 'p', 'p', 'l', 'e',) OUTPUT
print(my_tuple.count('p')) 2
print(my_tuple.index('l')) 3
PYTHON LIST
List is one of the most frequently used and very versatile data types used in Python. Lists are
used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python
used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different
qualities and usage.
Example : OUTPUT :
fruits = ["apple", "banana", "cherry"] ['apple', 'banana', 'cherry']
print(fruits)
56
# empty list
my_list = [ ]
# list of integers
my_list = [1, 2, 3]
# list with mixed data types
my_list = [1, "Hello", 3.4]
A list can also have another list as an item. This is called a nested list.
# nested list
my_list = ["mouse", [8, 4, 6], ['a']]
How to access elements from a list?
There are various ways in which we can access the elements of a list.
List Index
We can use the index operator [ ] to access an item in a list. In Python, indices start at 0. So,
a list having 5 elements will have an index from 0 to 4. Trying to access indexes other than these
will raise an IndexError. The index must be an integer. We can't use float or other types, this
will result in TypeError. Nested lists are accessed using nested indexing.
# List indexing Output
my_list = ['h', 'a', 'p', 'p', 'y'] h
print(my_list[0]) p
print(my_list[2]) y
print(my_list[4]) a
# Nested List 5
n_list = ["Happy", [2, 0, 1, 5]] Traceback (most recent call last):
# Nested indexing File
print(n_list[0][1]) "C:/Users/prave/AppData/Local/Programs/Python/P
print(n_list[1][3]) ython39/LDLDFL.PY", line 16, in <module>
# Error! Only integer can be print(my_list[4.0])
used for indexing TypeError: list indices must be integers or slices, not
print(my_list[4.0]) float
Negative indexing : Python allows negative indexing for its sequences. The index of -1 refers to the
last item, -2 to the second last item and so on.
# Negative indexing in lists OUTPUT:
my_list = ['h', 'a', 'p', 'p', 'y'] y
57
print(my_list[-1]) h
print(my_list[-5])
Slicing can be best visualized by considering the index to be between the elements as shown
below. So if we want to access a range, we need two indices that will slice that portion from the
list.
58
print(odd_no)
# change 2nd to 4th items
odd_no[1:4] = [3, 5, 7]
print(odd_no)
We can add one item to a list using the append() method or add several items using extend()
method.
odd = [1, 3, 5] Output
odd.append(7) [1, 3, 5, 7]
print(odd) [1, 3, 5, 7, 9, 11, 13]
odd.extend([9, 11, 13])
print(odd)
We can also use + operator to combine two lists. This is also called concatenation.
The * operator repeats a list for the given number of times.
odd = [1, 3, 5] Output
print(odd + [9, 7, 5]) [1, 3, 5, 9, 7, 5]
print(["shan"] * 3) ['shan', 'shan', 'shan']
Furthermore, we can insert one item at a desired location by using the method insert() or insert
multiple items by squeezing it into an empty slice of a list.
# Demonstration of list insert() method Output
odd = [1, 9] [1, 3, 9]
odd.insert(1,3) [1, 3, 5, 7, 9]
print(odd)
odd[2:2] = [5, 7]
print(odd)
59
# delete multiple items NameError: name 'my_list' is not defined
del my_list[1:5]
print(my_list)
# delete entire list
del my_list
# Error: List not defined
print(my_list)
We can use remove() method to remove the given item or pop() method to remove an item
at the given index. The pop() method removes and returns the last item if the index is not
provided.
This helps us implement lists as stacks (first in, last out data structure).
We can also use the clear() method to empty a list.
my_list = ['p','r','o','b','l','e','m'] Output
my_list.remove('p') ['r', 'o', 'b', 'l', 'e', 'm']
print(my_list)
print(my_list.pop(1)) o
print(my_list) ['r', 'b', 'l', 'e', 'm']
print(my_list.pop())
m
print(my_list) ['r', 'b', 'l', 'e']
my_list.clear()
[]
print(my_list)
60
index() - Returns the index of the first matched item
Output
I like apple
I like banana
I like mango
61
Python String
A string is a sequence of characters.
A character is simply a symbol. For example, the English language has 26 characters.
Computers do not deal with characters, they deal with numbers (binary).
Even though you may see characters on your screen, internally it is stored and manipulated
as a combination of 0s and 1s.
This conversion of character to a number is called encoding, and the reverse process is
decoding. ASCII and Unicode are some of the popular encodings used.
In Python, a string is a sequence of Unicode characters. Unicode was introduced to include
every character in all languages and bring uniformity in encoding.
62
We can access a range of items in a string by using the slicing operator :(colon).
str = 'programiz' Output:
print('str = ', str) str = programiz
#first character str[0] = p
print('str[0] = ', str[0]) str[-1] = z
#last character str[1:5] = rogr
print('str[-1] = ', str[-1]) str[5:-2] = am
#slicing 2nd to 5th character
print('str[1:5] = ', str[1:5])
#slicing 6th to 2nd last character
print('str[5:-2] = ', str[5:-2])
If we try to access an index out of the range or use numbers other than an integer, we will get errors.
# index must be in range
>>> my_string[15]
...
IndexError: string index out of range
63
>>> my_string = 'Python'
>>> my_string 'Python'
We cannot delete or remove characters from a string.
But deleting the string entirely is possible using the del keyword.
>>> del my_string[1]
...
TypeError: 'str' object doesn't support item deletion
>>> del my_string
>>> my_string
...
NameError: name 'my_string' is not defined
Python String Operations
There are many operations that can be performed with strings which makes it one of the most
used data types in Python.
Concatenation of Two or More Strings
Joining of two or more strings into a single one is called concatenation. The + operator does
this in Python. Simply writing two string literals together also concatenates them.
The * operator can be used to repeat the string for a given number of times.
# Python String Operations When we run the above program, we get the
str1 = 'Hello' following output:
str2 ='World!' str1 + str2 = HelloWorld!
# using + str1 * 3 = HelloHelloHello
print('str1 + str2 = ', str1 + str2)
# using *
print('str1 * 3 =', str1 * 3)
Writing two string literals together also concatenates them like + operator. If we want to concatenate
strings in different lines, we can use parentheses.
>>> # two string literals together
>>> 'Hello ''World!'
'Hello World!'
>>> # using parentheses
>>> s = ('Hello '
... 'World')
>>> s
'Hello World'
64
Iterating Through a string
We can iterate through a string using a for loop. Here is an example to count the number of
'l's in a string.
# Iterating through a string
count = 0
for letter in 'Hello World':
if(letter == 'l'):
count += 1
print(count,'letters found')
When we run the above program, we get the following output:
3 letters found
String Membership Test
We can test if a substring exists within a string or not, using the keyword in.
>>> 'a' in 'program'
True
>>> 'at' not in 'battle'
False
Built-in functions to Work with Python
Various built-in functions that work with sequence work with strings as well.
Some of the commonly used ones are enumerate() and len().
The enumerate() function returns an enumerate object.
It contains the index and value of all the items in the string as pairs.
This can be useful for iteration.
Similarly, len() returns the length (number of characters) of the string.
str = 'cold'
# enumerate()
list_enumerate = list(enumerate(str))
print('list(enumerate(str) = ', list_enumerate)
#character count
print('len(str) = ', len(str))
When we run the above program, we get the following output:
list(enumerate(str) = [(0, 'c'), (1, 'o'), (2, 'l'), (3, 'd')]
len(str) = 4
65
SET
A set is an unordered collection of items.
Sets are used to store multiple items in a single variable.
Every set element is unique (no duplicates) and must be immutable (cannot be changed).
However, a set itself is mutable. We can add or remove items from it.
Sets can also be used to perform mathematical set operations like union, intersection,
symmetric difference, etc. Sets are written with curly brackets.
Creating Python Sets
A set is created by placing all the items (elements) inside curly braces { }, separated by
comma, or by using the built-in set( ) function.
It can have any number of items and they may be of different types (integer, float, tuple, string
etc.). But a set cannot have mutable elements like lists, sets or dictionaries as its elements.
Example 1: Output
# set of integers
my_set = {1, 2, 3}
print(my_set) {1, 2, 3}
# set of mixed datatypes
my_set = {1.0, "Hello", (1, 2, 3)}
print(my_set) {1.0, (1, 2, 3), 'Hello'}
Example 2: Output
# set cannot have duplicates
my_set = {1, 2, 3, 4, 3, 2}
print(my_set) {1, 2, 3, 4}
# we can make set from a list
my_set = set([1, 2, 3, 2])
print(my_set) {1, 2, 3}
# set cannot have mutable items here Traceback (most recent call last):
[3, 4] is a mutable list this will cause File "<string>", line 15, in <module>
an error. my_set = {1, 2, [3, 4]}
TypeError: unhashable type: 'list'
my_set = {1, 2, [3, 4]}
Creating an empty set is a bit tricky.
Empty curly braces {} will make an empty dictionary in Python.
To make a set without any elements, we use the set() function without any argument.
66
# Distinguish set and dictionary while creating Output
empty set
# initialize a with {}
a = {}
# check data type of a <class 'dict'>
print(type(a))
# initialize a with set( )
a = set( )
# check data type of a <class 'set'>
print(type(a))
67
On the other hand, the remove() function will raise an error in such a condition (if element
is not present in the set).
# initialize my_set Output
my_set = {1, 3, 4, 5, 6}
print(my_set) {1, 3, 4, 5, 6}
# discard an element
my_set.discard(4)
print(my_set) {1, 3, 5, 6}
# remove an element
my_set.remove(6)
print(my_set) {1, 3, 5}
# discard an element
# not present in my_set
my_set.discard(2) {1, 3, 5}
print(my_set)
# remove an element Traceback (most recent call last):
# not present in my_set File "<string>", line 28, in <module>
my_set.remove(2) KeyError: 2
Similarly, we can remove and return an item using the pop() method.
Since set is an unordered data type, there is no way of determining which item will be popped.
It is completely arbitrary. We can also remove all the items from a set using the clear() method.
# initialize my_set Output
my_set = set("HelloWorld")
print(my_set) {'H', 'e', 'r', 'o', 'W', 'd', 'l'}
# pop an element
print(my_set.pop())
# pop another element H
my_set.pop()
print(my_set) {'r', 'W', 'o', 'd', 'e'}
# clear my_set
my_set.clear()
print(my_set) set()
68
Python Set Operations
Sets can be used to carry out mathematical set operations like union, intersection,
difference and symmetric difference. We can do this with operators or methods.
Let us consider the following two sets for the following operations.
Set Union in Python
>>> A = {1, 2, 3, 4, 5}
>>> B = {4, 5, 6, 7, 8}
Intersection of A and B is a set of elements that are common in both the sets.
69
Intersection is performed using & operator. Same can be accomplished using the
intersection() method.
# Intersection of sets Output
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use & operator
print(A & B) {4, 5}
Try the following examples on Python shell.
# use intersection function on A
>>> A.intersection(B)
{4, 5}
# use intersection function on B
>>> B.intersection(A)
{4, 5}
Set Difference in Python
Difference of the set B from set A(A - B) is a set of elements that are only in A but not in B.
Similarly, B - A is a set of elements in B but not in A. Difference is performed using - operator.
Same can be accomplished using the difference() method.
# Difference of two sets Output
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use - operator on A {1, 2, 3}
print(A - B)
Try the following examples on Python shell.
# use difference function on A
>>> A.difference(B)
{1, 2, 3}
# use - operator on B
>>> B - A
{8, 6, 7}
>>> B.difference(A) # use difference function on B
{8, 6, 7}
70
Set Symmetric Difference in Python
Symmetric Difference of A and B is a set of elements in A and B but not in both (excluding the
intersection). Symmetric difference is performed using ^ operator. Same can be accomplished
using the method symmetric_difference().
# Symmetric difference of two sets Output
# initialize A and B
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use ^ operator
print(A ^ B) {1, 2, 3, 6, 7, 8}
Try the following examples on Python shell.
# use symmetric_difference function on A
>>> A.symmetric_difference(B)
{1, 2, 3, 6, 7, 8}
# use symmetric_difference function on B
>>> B.symmetric_difference(A)
{1, 2, 3, 6, 7, 8}
Other Python Set Methods
There are many set methods, some of which we have already used above.
Here is a list of all the methods that are available with the set objects:
Method Description
add() - Adds an element to the set
clear() - Removes all elements from the set
copy() - Returns a copy of the set
difference() - Returns the difference of two or more sets as a new set
difference_update() - Removes all elements of another set from this set
discard() - Removes an element from the set if it is a member. (Do nothing if the element is not in
set)
intersection() - Returns the intersection of two sets as a new set
intersection_update() - Updates the set with the intersection of itself and another
71
isdisjoint() - Returns True if two sets have a null intersection
issubset() - Returns True if another set contains this set
issuperset() - Returns True if this set contains another set
pop() - Removes and returns an arbitrary set element. Raises KeyError if the set is empty
remove() - Removes an element from the set. If the element is not a member, raises a KeyError
symmetric_difference() - Returns the symmetric difference of two sets as a new set
symmetric_difference_update() - Updates a set with the symmetric difference of itself and another
union() - Returns the union of sets in a new set
update() - Updates the set with the union of itself and others
72
enumerate() - Returns an enumerate object. It contains the index and value for all the items
of the set as a pair.
len() - Returns the length (the number of items) in the set.
max() - Returns the largest item in the set.
min() - Returns the smallest item in the set.
sorted() - Returns a new sorted list from elements in the set(does not sort the set itself).
sum() - Returns the sum of all elements in the set.
Python Frozenset
Frozenset is a new class that has the characteristics of a set, but its elements cannot be
changed once assigned. While tuples are immutable lists, frozensets are immutable sets.
Sets being mutable are unhashable, so they can't be used as dictionary keys.
On the other hand, frozensets are hashable and can be used as keys to a dictionary.
Frozensets can be created using the frozenset() function.
This data type supports methods like copy(), difference(), intersection(), isdisjoint(),
issubset(), issuperset(), symmetric_difference() and union().
Being immutable, it does not have methods that add or remove elements.
# initialize A and B
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
Try these examples on Python shell.
>>> A.isdisjoint(B)
False
>>> A.difference(B)
frozenset({1, 2})
>>> A | B
frozenset({1, 2, 3, 4, 5, 6})
>>> A.add(3)
...
AttributeError: 'frozenset' object has no attribute 'add'
Python Dictionary
Dictionaries are used to store data values in key: value pairs.
A dictionary is a collection which is unordered, changeable and does not allow duplicates.
Dictionaries are optimized to retrieve values when the key is known.
Dictionaries are written with curly brackets, and have keys and values.
73
An empty dictionary without any items is written with just two curly braces, { }.
Example:
this_dict = { OUTPUT:
"day": "Monday",
"month": "Jan",
"year": 2021
}
print(this_dict) {'day': 'Monday', 'month': 'Jan', 'year': 2021}
74
print(my_dict['name']) Jack
print(my_dict.get('age')) 26
# Trying to access keys which doesn't exist
throws error
print(my_dict.get('address')) None
Traceback (most recent call last):
# KeyError File "<string>", line 15, in <module>
print(my_dict['address']) print(my_dict['address'])
KeyError: 'address'
75
print(squares) File "<string>", line 30, in <module>
# delete the dictionary itself print(squares)
del squares
NameError: name 'squares' is not
# Throws Error
print(squares) defined
76
squares = {}
for x in range(6): Output
squares[x] = x*x
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print(squares)
A dictionary comprehension can optionally contain more for or if statements. An optional if
statement can filter out items to form the new dictionary.
Other Dictionary Operations
Dictionary Membership Test
We can test if a key is in a dictionary or not using the keyword in.
Notice that the membership test is only for the keys and not for the values.
# Membership Test for Dictionary Keys Output
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81} True
print(1 in squares)
True
print(2 not in squares)
# membership tests for key only not value False
print(49 in squares)
Iterating Through a Dictionary
We can iterate through each key in a dictionary using a for loop.
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81} Output
1
for i in squares: 9
25
print(squares[i]) 49
81
Dictionary Built-in Functions
Built-in functions like all(), any(), len(), cmp(), sorted(), etc. are commonly used with dictionaries
to perform different tasks.
Function Description
all() Return True if all keys of the dictionary are True (or if the dictionary is
empty).
any() Return True if any key of the dictionary is true. If the dictionary is empty,
return False.
len() Return the length (the number of items) in the dictionary.
cmp() Compares items of two dictionaries. (Not available in Python 3)
sorted() Return a new sorted list of keys in the dictionary.
Here are some examples that use built-in functions to work with a dictionary.
squares = {0: 0, 1: 1, 3: 9, 5: 25, 7: 49, 9: 81} Output
print(len(squares)) 6
print(sorted(squares)) [0, 1, 3, 5, 7, 9]
77
UNIT 3- FILE HANDLING AND EXCEPTION HANDLING
Till now, we were taking the input from the console and writing it back to the console to interact
with the user. Sometimes, it is not enough to only display the data on the console.
The data to be displayed may be very large, and only a limited amount of data can be displayed
on the console since the memory is volatile, it is impossible to recover the programmatically
generated data again and again.
The file handling plays an important role when the data needs to be stored permanently into the
file. A file is a named location on disk to store related information. We can access the stored
information (non-volatile) after the program termination.
The file-handling implementation is slightly lengthy or complicated in the other programming
language, but it is easier and shorter in Python.
In Python, files are treated in two modes as text or binary. The file may be in the text or binary
format, and each line of a file is ended with the special character.
Hence, a file operation can be done in the following order.
Open a file
Read or write - Performing operation
Close the file
Printing to the Screen
The simplest way to produce output is using the print statement where you can pass zero or more
expressions separated by commas.
This function converts the expressions you pass into a string and writes the result to standard
output as follow,
print "Python is really a great language,", "isn't it?"
OUTPUT : Python is really a great language, isn't it?
78
This prompts you to enter any string and it would display same string on the screen.
When I typed "Hello Python!", its output is like this −
Enter your input: Hello Python
Received input is : Hello Python
The input Function
The input([prompt]) function is equivalent to raw_input, except that it assumes the input is a valid
Python expression and returns the evaluated result to you.
str = input("Enter your input: ")
print "Received input is : ", str
This would produce the following result against the entered input −
Enter your input: [x*5 for x in range (2,10,2)]
Recieved input is : [10, 20, 30, 40]
OPENING A FILE
Python provides an open() function that accepts two arguments, file name and access mode in
which the file is accessed. The function returns a file object which can be used to perform various
operations like reading, writing, etc.
Syntax:
file object = open(<file-name>, <access-mode>, <buffering>)
Here are parameter details −
file_name − The file_name argument is a string value that contains the name of the file that you
want to access.
access_mode − The access_mode determines the mode in which the file has to be opened, i.e.,
read, write, append, etc. A complete list of possible values is given below in the table. This is
optional parameter and the default file access mode is read (r).
buffering − If the buffering value is set to 0, no buffering takes place. If the buffering value is 1,
line buffering is performed while accessing a file. If you specify the buffering value as an integer
greater than 1, then buffering action is performed with the indicated buffer size. If negative, the
buffer size is the system default(default behavior).
The files can be accessed using various modes like read, write, or append. The following are the
details about the access mode to open a file.
SN ACCESS MODE DESCRIPTION
1 R It opens the file to read-only mode. The file pointer exists at the
beginning. The file is by default open in this mode if no access mode is
passed.
79
2 Rb It opens the file to read-only in binary format. The file pointer exists at
the beginning of the file.
3 r+ It opens the file to read and write both. The file pointer exists at the
beginning of the file.
4 rb+ It opens the file to read and write both in binary format. The file pointer
exists at the beginning of the file.
5 W It opens the file to write only. It overwrites the file if previously exists or
creates a new one if no file exists with the same name. The file pointer
exists at the beginning of the file.
6 Wb It opens the file to write only in binary format. It overwrites the file if it
exists previously or creates a new one if no file exists. The file pointer
exists at the beginning of the file.
7 w+ It opens the file to write and read both. It is different from r+ in the sense
that it overwrites the previous file if one exists whereas r+ doesn't
overwrite the previously written file. It creates a new file if no file exists.
The file pointer exists at the beginning of the file.
8 wb+ It opens the file to write and read both in binary format. The file pointer
exists at the beginning of the file.
9 A It opens the file in the append mode. The file pointer exists at the end of
the previously written file if exists any. It creates a new file if no file
exists with the same name.
10 Ab It opens the file in the append mode in binary format. The pointer exists
at the end of the previously written file. It creates a new file in binary
format if no file exists with the same name.
11 a+ It opens a file to append and read both. The file pointer remains at the
end of the file if a file exists. It creates a new file if no file exists with the
same name.
12 ab+ It opens a file to append and read both in binary format. The file pointer
remains at the end of the file.
Let's look at the simple example to open a file named "file.txt" (stored in the same directory) in read
mode and printing its content on the console.
Example
#opens the file file.txt in read mode
fileptr = open("file.txt","r")
if fileptr:
print("file is opened successfully")
Output:
<class '_io.TextIOWrapper'>
file is opened successfully
80
In the above code, we have passed filename as a first argument and opened file in read mode as we
mentioned r as the second argument. The fileptr holds the file object and if the file is opened
successfully, it will execute the print statement
The close() method
Once all the operations are done on the file, we must close it through our Python script using
the close() method. Any unwritten information gets destroyed once the close() method is called
on a file object.
We can perform any operation on the file externally using the file system which is the currently
opened in Python; hence it is good practice to close the file once all the operations are done.
Syntax
fileobject.close()
EXAMPLE:
# opens the file file.txt in read mode
fileptr = open("file.txt","r")
if fileptr:
print("file is opened successfully")
#closes the opened file
fileptr.close()
After closing the file, we cannot perform any operation in the file. The file needs to be properly
closed. If any exception occurs while performing some operations in the file then the program
terminates without closing the file.
We should use the following method to overcome such type of problem.
try:
fileptr = open("file.txt")
# perform file operations
finally:
fileptr.close()
81
with open(<file name>, <access mode>) as <file-pointer>:
#statement suite
The advantage of using with statement is that it provides the guarantee to close the file regardless
of how the nested block exits.
It is always suggestible to use the with statement in the case of files because, if the break, return,
or exception occurs in the nested block of code then it automatically closes the file, we don't need
to write the close() function. It doesn't let the file to corrupt.
Example
with open("file.txt",'r') as f:
content = f.read();
print(content)
WRITING THE FILE
To write some text to a file, we need to open the file using the open method with one of the
following access modes.
w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file.
a: It will append the existing file. The file pointer is at the end of the file. It creates a new file if no
file exists.
Example
# open the file.txt in append mode. Create a new file if no such file exists.
fileptr = open("file2.txt", "w")
# appending the content to the file
fileptr.write('''''Python is the modern day language. It makes things so simple.
It is the fastest-growing programing language''')
# closing the opened the file
fileptr.close()
Output:
File2.txt
Python is the modern-day language. It makes things so simple. It is the fastest growing
programming language.
Snapshot of the file2.txt
82
We have opened the file in w mode. The file1.txt file doesn't exist, it created a new file and we
have written the content in the file using the write() function.
Example 2
#open the file.txt in write mode.
fileptr = open("file2.txt","a")
#overwriting the content of the file
fileptr.write(" Python has an easy syntax and user-friendly interaction.")
#closing the opened file
fileptr.close()
Output:
Python is the modern day language. It makes things so simple.
It is the fastest growing programing language Python has an easy syntax and user-friendly
interaction.
Snapshot of the file2.txt
We can see that the content of the file is modified. We have opened the file in a mode and it
appended the content in the existing file2.txt.
To read a file using the Python script, the Python provides the read() method. The read() method
reads a string from the file. It can read the data in the text as well as a binary format.
The syntax of the read() method is given below.
Syntax:
fileobj.read(<count>)
Here, the count is the number of bytes to be read from the file starting from the beginning of the
file. If the count is not specified, then it may read the content of the file until the end.
Example
#open the file.txt in read mode. causes error if no such file exists.
fileptr = open("file2.txt","r")
#stores all the data of the file into the variable content
content = fileptr.read(10)
# prints the type of the data stored in the file
print(type(content))
83
#prints the content of the file
print(content)
#closes the opened file
fileptr.close()
Output:
<class 'str'>
Python is
In the above code, we have read the content of file2.txt by using the read() function. We have
passed count value as ten which means it will read the first ten characters from the file.
If we use the following line, then it will print all content of the file.
content = fileptr.read()
print(content)
Output:
Python is the modern-day language. It makes things so simple.
It is the fastest-growing programing language Python has easy an syntax and user-friendly
interaction.
Read file through for loop
We can read the file using for loop. Consider the following example.
#open the file.txt in read mode. causes an error if no such file exists.
fileptr = open("file2.txt","r");
#running a for loop
for i in fileptr:
print(i) # i contains each line of the file
Output:
Python is the modern day language.
It makes things so simple.
Python has easy syntax and user-friendly interaction.
84
Example 1: Reading lines using readline() function
#open the file.txt in read mode. causes error if no such file exists.
fileptr = open("file2.txt","r");
#stores all the data of the file into the variable content
content = fileptr.readline()
content1 = fileptr.readline()
#prints the content of the file
print(content)
print(content1)
#closes the opened file
fileptr.close()
Output:
Python is the modern day language.
It makes things so simple.
We called the readline() function two times that's why it read two lines from the file.
Python provides also the readlines() method which is used for the reading lines. It returns the
list of the lines till the end of file(EOF) is reached.
Example 2: Reading Lines Using readlines() function
#open the file.txt in read mode. causes error if no such file exists.
fileptr = open("file2.txt","r");
#stores all the data of the file into the variable content
content = fileptr.readlines()
#prints the content of the file
print(content)
#closes the opened file
fileptr.close()
Output:
['Python is the modern day language.\n', 'It makes things so simple.\n', 'Python has easy syntax
and user-friendly interaction.']
CREATING A NEW FILE
The new file can be created by using one of the following access modes with the function open().
x: it creates a new file with the specified name. It causes an error a file exists with the same
name.
a: It creates a new file with the specified name if no such file exists. It appends the content to
the file if the file already exists with the specified name.
85
w: It creates a new file with the specified name if no such file exists. It overwrites the existing
file.
Example 1
#open the file.txt in read mode. causes error if no such file exists.
fileptr = open("file2.txt","x")
print(fileptr)
if fileptr:
print("File created successfully")
Output:
<_io.TextIOWrapper name='file2.txt' mode='x' encoding='cp1252'>
File created successfully
FILE POINTER POSITIONS
Python provides the tell() method which is used to print the byte number at which the file pointer
currently exists. Consider the following example.
# open the file file2.txt in read mode
fileptr = open("file2.txt","r")
#initially the filepointer is at 0
print("The filepointer is at byte :",fileptr.tell())
#reading the content of the file
content = fileptr.read();
#after the read operation file pointer modifies. tell() returns the location of the fileptr.
print("After reading, the filepointer is at:",fileptr.tell())
Output:
The filepointer is at byte : 0
After reading, the filepointer is at: 117
86
from: It indicates the reference position from where the bytes are to be moved. If it is set to 0, the
beginning of the file is used as the reference position. If it is set to 1, the current position of the file
pointer is used as the reference position. If it is set to 2, the end of the file pointer is used as the
reference position.
Example
# open the file file2.txt in read mode
fileptr = open("file2.txt","r")
#initially the filepointer is at 0
print("The filepointer is at byte :",fileptr.tell())
#changing the file pointer location to 10.
fileptr.seek(10);
#tell() returns the location of the fileptr.
print("After reading, the filepointer is at:",fileptr.tell())
Output:
The filepointer is at byte : 0
After reading, the filepointer is at: 10
PYTHON OS MODULE
Renaming the file
The Python os module enables interaction with the operating system. The os module provides the
functions that are involved in file processing operations like renaming, deleting, etc. It provides us
the rename() method to rename the specified file to a new name. The syntax to use
the rename() method is given below.
Syntax:
rename(current-name, new-name)
The first argument is the current file name and the second argument is the modified name. We can
change the file name bypassing these two arguments.
Example 1:
import os
#rename file2.txt to file3.txt
os.rename("file2.txt","file3.txt")
Output:
The above code renamed current file2.txt to file3.txt
87
Removing the file
The os module provides the remove() method which is used to remove the specified file. The syntax
to use the remove() method is given below.
remove(file-name)
Example 1
import os;
#deleting the file named file3.txt
os.remove("file3.txt")
FILE PATH
Creating the new directory: The mkdir() method is used to create the directories in the current
working directory. The syntax to create the new directory is given below.
Syntax:
mkdir(directory name)
Example 1
import os
#creating a new directory with the name new
os.mkdir("new")
The getcwd() method: This method returns the current working directory.
Syntax
os.getcwd()
Example
import os
os.getcwd()
Output:
'C:\\Users\\DEVANSH SHARMA'
Changing the current working directory: The chdir() method is used to change the current
working directory to a specified directory. The syntax to use the chdir() method is given below.
Syntax
chdir("new-directory")
Example
import os
# Changing current directory with the new directiory
os.chdir("C:\\Users\\DEVANSH SHARMA\\Documents")
#It will display the current working directory
os.getcwd()
88
Output:
'C:\\Users\\DEVANSH SHARMA\\Documents'
Deleting directory: The rmdir() method is used to delete the specified directory.
Syntax
os.rmdir(directory name)
Example 1
import os
#removing the new directory
os.rmdir("directory_name")
It will remove the specified directory.
The file related methods
The file object provides the following methods to manipulate the files on various operating systems.
SN METHOD DESCRIPTION
1 file.close() It closes the opened file. The file once closed, it can't be read or
write anymore.
2 File.fush() It flushes the internal buffer.
3 File.fileno() It returns the file descriptor used by the underlying
implementation to request I/O from the OS.
4 File.isatty() It returns true if the file is connected to a TTY device, otherwise
returns false.
5 File.next() It returns the next line from the file.
6 File.read([size]) It reads the file for the specified size.
7 File.readline([size]) It reads one line from the file and places the file pointer to the
beginning of the new line.
8 File.readlines([sizehint]) It returns a list containing all the lines of the file. It reads the file
until the EOF occurs using readline() function.
9 File.seek(offset[,from) It modifies the position of the file pointer to a specified offset
with the specified reference.
10 File.tell() It returns the current position of the file pointer within the file.
11 File.truncate([size]) It truncates the file to the optional specified size.
12 File.write(str) It writes the specified string to a file
13 File.writelines(seq) It writes a sequence of the strings to a file.
89
PYTHON ERRORS AND BUILT-IN EXCEPTIONS
We can make certain mistakes while writing a program that lead to errors when we try to run
it. A python program terminates as soon as it encounters an unhandled error. These errors can be
broadly classified into two classes:
Syntax errors
Logical errors (Exceptions)
Error caused by not following the proper structure (syntax) of the language is called syntax
error or parsing error.
>>> if a < 3
File "<interactive input>", line 1
if a < 3
^
SyntaxError: invalid syntax
As shown in the example, an arrow indicates where the parser ran into the syntax error. We can
notice here that a colon : is missing in the if statement.
Errors that occur at runtime (after passing the syntax test) are called exceptions or logical
errors. For instance, they occur when we try to open a file(for reading) that does not exist
(FileNotFoundError), try to divide a number by zero (ZeroDivisionError), or try to import a
module that does not exist (ImportError).
Whenever these types of runtime errors occur, Python creates an exception object. If not handled
properly, it prints a traceback to that error along with some details about why that error occurred.
Let's look at how Python treats these errors:
>>> 1 / 0
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ZeroDivisionError: division by zero
>>> open("imaginary.txt")
Traceback (most recent call last):
90
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'imaginary.txt'
Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that are
raised when corresponding errors occur. We can view all the built-in exceptions using the built-
in local() function as follows:
print(dir(locals()['__builtins__']))
KeyboardInterrupt Raised when the user hits the interrupt key (Ctrl+C or Delete).
MemoryError Raised when an operation runs out of memory.
91
RuntimeError Raised when an error does not fall under any other category.
ZeroDivisionError Raised when the second operand of division or modulo operation is zero.
If required, we can also define our own exceptions in Python. We can handle these built-in and user-
defined exceptions in Python using try, except and finally statements. To learn more about them,
visit Python try, except and finally statements.
Python Exception
92
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.
5. EOFError: It occurs when the end of the file is reached, and yet operations are being
performed.
As we have already discussed, the exception is an abnormal condition that halts the execution of
the program. Suppose we have two variables a and b, which take the input from the user and
perform the division of these values. What if the user entered the zero as the denominator? It will
interrupt the program execution and through a ZeroDivision exception.
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
print("a/b = %d" %c)
#other code:
print("Hi I am other part of the program")
OUTPUT:
Enter a:10
Enter b:0
Traceback (most recent call last):
File "exception-test.py", line 3, in <module>
c = a/b;
ZeroDivisionError: division by zero
93
The above program is syntactically correct, but it through the error because of unusual input.
That kind of programming may not be suitable or recommended for the projects because these
projects are required uninterrupted execution. That's why an exception-handling plays an
essential role in handling these unexpected exceptions. We can handle these exceptions in the
following way.
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.
Syntax
try:
#block of code
except Exception1:
#block of code
except Exception2:
#block of code
#other code
Example 1
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
except:
print("Can't divide with zero")
94
Output:
Enter a:10
Enter b:0
Can't divide with zero
We can also use the else statement with the try-except statement in which, we can place the code
which will be executed in the scenario if no exception occurs in the try block.
The syntax to use the else statement with the try-except statement is given below.
try:
#block of code
except Exception1:
#block of code
else:
#this code executes if no except block is executed
Example 2
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
95
except Exception:
print("can't divide by zero")
print(Exception)
else:
print("Hi I am else block")
Output:
Enter a:10
Enter b:0
can't divide by zero
<class 'Exception'>
The except statement with no exception: Python provides the flexibility not to specify the name of
exception with the exception statement.
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")
The except statement using with exception variable: 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:
96
print("can't divide by zero")
print(e)
else:
print("Hi I am else block")
Output:
Enter a:10
Enter b:0
can't divide by zero
division by zero
POINTS TO REMEMBER
1. Python facilitates us to not specify the exception with the except statement.
2. We can declare multiple exceptions in the except statement since the try block may contain
the statements which throw the different type of exceptions.
3. We can also specify an else block along with the try-except statement, which will be executed
if no exception is raised in the try block.
4. The statements that don't throw the exception should be placed inside the else block.
Example
try:
#this will throw an exception if the file doesn't exist.
fileptr = open("file.txt","r")
except IOError:
print("File not found")
else:
print("The file opened successfully")
fileptr.close()
Output:
97
Declaring Multiple Exceptions
The Python allows us to declare the multiple exceptions with the except clause.
Declaring multiple exceptions is useful in the cases where a try block throws multiple exceptions.
Syntax
try:
#block of code
except (<Exception 1>,<Exception 2>,<Exception 3>,...<Exception n>)
#block of code
else:
#block of code
Example:
try:
a=10/0;
except(ArithmeticError, IOError):
print("Arithmetic Exception")
else:
print("Successfully Done")
Output:
Arithmetic Exception
Python provides the optional finally statement, which is used with the try statement. It is
executed no matter what exception occurs and used to release the external resource. The finally
block provides a guarantee of the execution.
We can use the finally block with the try block in which we can pace the necessary code, which
must be executed before the try statement throws an exception.
98
Syntax
try:
# block of code
# this may throw an exception
finally:
# block of code
# this will always be executed
Example
try:
fileptr = open("file2.txt","r")
try:
fileptr.write("Hi I am good")
finally:
fileptr.close()
print("file closed")
except:
print("Error")
Output:
file closed
Error
99
RAISING EXCEPTIONS
An exception can be raised forcefully by using the raise clause in Python. It is useful in in that
scenario where we need to raise an exception to stop the execution of the program.
For example, there is a program that requires 2GB memory for execution, and if the program tries
to occupy 2GB of memory, then we can raise an exception to stop the execution of the program.
Syntax
raise Exception_class,<value>
Points to remember
1. To raise an exception, the raise statement is used. The exception class name follows it.
2. An exception can be provided with a value that can be given in the parenthesis.
3. To access the value "as" keyword is used. "e" is used as a reference variable which stores the
value of the exception.
4. We can pass the value to an exception to specify the exception type.
Example
try:
age = int(input("Enter the age:"))
if(age<18):
raise ValueError
else:
print("the age is valid")
except ValueError:
print("The age is not valid")
Output:
try:
num = int(input("Enter a positive integer: "))
100
if(num <= 0):
# we can pass the message in the raise statement
raise ValueError("That is a negative number!")
except ValueError as e:
print(e)
Output:
Example 3
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
if b is 0:
raise ArithmeticError
else:
print("a/b = ",a/b)
except ArithmeticError:
print("The value of b can't be 0")
Output:
Enter a:10
Enter b:0
The value of b can't be 0
Custom Exception
The Python allows us to create our own exceptions that can be raised from the program and caught
using the except clause. However, we suggest you read this section after visiting the Python object
and classes.
Example
class ErrorInCode(Exception):
101
def __init__(self, data):
self.data = data
def __str__(self):
return repr(self.data)
try:
raise ErrorInCode(2000)
except ErrorInCode as ae:
print("Received error:", ae.data)
OUTPUT:
INTRODUCTION
Modular programming is the process of breaking a large, unwieldy programming task into
separate, smaller, more manageable subtasks or modules. Individual modules can then be put
together like building blocks to create a larger application.
Simplicity: Rather than focusing on the entire problem at hand, a module typically focuses on one
relatively small portion of the problem. If you’re working on a single module, then you’ll have a
smaller problem domain to wrap your head around. This makes development easier and less error-
prone.
Maintainability: Modules are typically designed so that they enforce logical boundaries between
different problem domains. If modules are written in a way that minimizes interdependency, then
there is decreased likelihood that modifications to a single module will have an impact on other parts
of the program. (You may even be able to make changes to a module without having any knowledge of
the application outside that module.) This makes it more viable for a team of many programmers to
work collaboratively on a large application.
Reusability: Functionality defined in a single module can be easily reused (through an appropriately
defined interface) by other parts of the application. This eliminates the need to recreate duplicate
code.
102
PYTHON MODULES
Here, we have defined a function add() inside a module named example. The function takes in two
numbers and returns their sum.
We can import the definitions inside a module to another module or the interactive interpreter in
Python. We use the import keyword to do this.
To import our previously defined module example, we type the following in the Python prompt.
This does not import the names of the functions defined in example directly in the current symbol
table. It only imports the module name example there.
Using the module name we can access the function using the dot . operator. For example:
>>> example.add(4,5.5)
9.5
Python has tons of standard modules. You can check out the full list of Python standard modules and
their use cases.
These files are in the Lib directory inside the location where you installed Python.
Standard modules can be imported the same way as we import our user-defined modules.
There are various ways to import modules. They are listed below.
103
Python import statement
We can import a module using the import statement and access the definitions inside it using
the dot operator as described above. Here is an example.
import math
import math as m
We have renamed the math module as m. This can save us typing time in some cases. Note that
the name math is not recognized in our scope. Hence, math.pi is invalid, and m.pi is the correct
implementation.
We can import specific names from a module without importing the module as a whole.
Here is an example. Here, we imported only the pi attribute from the math module.
In such cases, we don't use the dot operator. We can also import multiple attributes as follows:
We can import all names (definitions) from a module using the following construct:
104
from math import *
Here, we have imported all the definitions from the math module. This includes all names visible
in our scope except those beginning with an underscore (private definitions).
Importing everything with the asterisk (*) symbol is not a good programming practice. This can
lead to duplicate definitions for an identifier. It also hampers the readability of our code.
While importing a module, Python looks at several places. Interpreter first looks for a built-in
module. Then(if built-in module not found), Python looks into a list of directories defined
in sys.path. The search is in this order. The current directory.PYTHONPATH (an environment
variable with a list of directories). The installation-dependent default directory.
Reloading a module
The Python interpreter imports a module only once during a session. This makes things more
efficient. Here is an example to show how this works. Suppose we have the following code in a
module named my_module.
We can see that our code got executed only once. This goes to say that our module was imported
only once. Now if our module changed during the course of the program, we would have to reload
it. One way to do this is to restart the interpreter. But this does not help much.
105
Python provides a more efficient way of doing this. We can use the reload() function inside
the imp module to reload a module. We can do it in the following ways:
We can use the dir() function to find out names that are defined inside a module.
For example, we have defined a function add() in the module example that we had in the
beginning.
We can use dir in example module in the following way:
>>> dir(example)
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__initializing__',
'__loader__',
'__name__',
'__package__',
'add']
Here, we can see a sorted list of names (along with add). All other names that begin with an
underscore are default Python attributes associated with the module (not user-defined).
For example, the __name__ attribute contains the name of the module.
106
All the names defined in our current namespace can be found out using the dir() function
without any arguments.
>>> a = 1
>>> b = "hello"
>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'b', 'math', 'pyscripter']
What are packages?
We don't usually store all of our files on our computer in the same location. We use a well-
organized hierarchy of directories for easier access. Similar files are kept in the same directory, for
example, we may keep all the songs in the "music" directory. Analogous to this, Python has packages
for directories and modules for files.
As our application program grows larger in size with a lot of modules, we place similar modules
in one package and different modules in different packages. This makes a project (program)
easy to manage and conceptually clear.
Similarly, as a directory can contain subdirectories and files, a Python package can have sub-
packages and modules.
A directory must contain a file named __init__.py in order for Python to consider it as a package. This
file can be left empty but we generally place the initialization code for that package in this file.
Here is an example. Suppose we are developing a game. One possible organization of packages and
modules could be as shown in the figure below.
We can import modules from packages using the dot (.) operator.
For example, if we want to import the start module in the above example, it can be done as follows:
import Game.Level.start
107
Now, if this module contains a function named select_difficulty(), we must use the full name to
reference it.
Game.Level.start.select_difficulty(2)
Packages allow for a hierarchical structuring of the module namespace using dot notation. In the
same way that modules help avoid collisions between global variable names, packages help avoid
collisions between module names.
Creating a package is quite straightforward, since it makes use of the operating system’s inherent
hierarchical file structure. Consider the following arrangement:
Here, there is a directory named pkg that contains two modules, mod1.py and mod2.py.
mod1.py:
def load_data():
print('loading data using mod1.load_data()')
class Customer:
pass
mod2.py:
def clean_data():
print('cleaning data using mod2.clean_data()')
class Location:
pass
Given this structure, if the pkg directory resides in a location where it can be found (in one of the
directories contained in sys.path), you can refer to the two modules with dot
notation (pkg.mod1, pkg.mod2) and import them with the syntax you’re already familiar with:
108
<pkg.mod2.Location object at 0x10818c550>
You can also use this syntax:
109
>>> pkg.mod1
Traceback (most recent call last):
File "<input>", line 1, in <module>
pkg.mod1
AttributeError: module 'pkg' has no attribute 'mod1'
>>> pkg.mod2.Location
Traceback (most recent call last):
File "<input>", line 1, in <module>
pkg.mod2.Location
AttributeError: module 'pkg' has no attribute 'mod2'
>>> pkg.mod1.load_data()
Traceback (most recent call last):
File "<input>", line 1, in <module>
pkg.mod1.load_data()
AttributeError: module 'pkg' has no attribute 'mod1'
PYTHON LIBRARIES FOR DATA ANALYSIS
Python is a general purpose language and is often used for things other than data analysis and data
science. What makes Python extremely useful for working with data? There are libraries that give
users the necessary functionality when crunching data. Below are the major Python libraries that are
used for working with data. You should take some time to familiarize yourself with the basic purposes
of these packages.
Numpy and Scipy – Fundamental Scientific Computing: NumPy stands for Numerical Python.
The most powerful feature of NumPy is n-dimensional array. This library also contains basic
linear algebra functions, Fourier transforms, advanced random number capabilities and tools for
integration with other low level languages like Fortran, C and C++.
SciPy stands for Scientific Python. It is built on NumPy. Scipy is one of the most useful library for
variety of high level science and engineering modules like discrete Fourier transform, Linear
Algebra, Optimization and Sparse matrices.
Pandas – Data Manipulation and Analysis: Pandas for structured data operations and
manipulations. It is extensively used for data munging and preparation. Pandas were added
relatively recently to Python and have been instrumental in boosting Python’s usage in data
scientist community.
Matplotlib – Plotting and Visualization: Matplotlib for plotting vast variety of graphs, starting
from histograms to line plots to heat plots.. You can use Pylab feature in ipython notebook
110
(ipython notebook –pylab = inline) to use these plotting features inline. If you ignore the inline
option, then pylab converts ipython environment to an environment, very similar to Matlab.
Scikit-learn – Machine Learning and Data Mining: Scikit Learn for machine learning. Built on
NumPy, SciPy and matplotlib, this library contains a lot of efficient tools for machine learning and
statistical modeling including classification, regression, clustering and dimensional reduction.
StatsModels – Statistical Modeling, Testing, and Analysis: Statsmodels for statistical
modeling. It is a Python module that allows users to explore data, estimate statistical models, and
perform statistical tests. An extensive list of descriptive statistics, statistical tests, plotting
functions, and result statistics are available for different types of data and each estimator.
Seaborn – For Statistical Data Visualization: Seaborn for statistical data visualization. It is a
library for making attractive and informative statistical graphics in Python. It is based on
matplotlib. Seaborn aims to make visualization a central part of exploring and understanding
data.
Data Visualization is the process of understanding the data in more detail using some plots and
graphs. There are many libraries in Python that help us to do the same. One of the most famous
libraries is matplotlib which can plot almost every type of plot that you can imagine.
1. Seaborn: Seaborn is built on top of the matplotlib library. it has many built-in functions using which
you can create beautiful plots with just simple lines of codes. It provides a variety of advanced
visualization plots with simple syntax like box plots, violin plots, dist plots, Joint plots, pair
plots, heatmap, and many more.
Key Features:
1. It can be used to determine the relationship between two variables.
2. Differentiate when analyzing uni-variate or bi-variate distributions.
3. Plot the linear regression model for the dependent variable.
4. Provides multi-grid plotting
A simple code for displaying trends and distribution of data in iris dataset using seaborn pair plots:
111
2. Plotly
Plotly is an advanced Python analytics library that helps in building interactive dashboards. The
graphs build using Plotly are interactive plots, which means you can easily find value at any particular
point or session of the graphs. Plotly makes it super easy to generate dashboards and deploying them
on the server. It supports Python, R, and the Julia programming language. You can create a wide range
of graphs using Plotly:
Basic Charts
Statistical charts
Scientific charts
Financial Charts
Maps
Subplots
Transforms
3. Geoplotlib
It is a Python toolbox for visualizing geographical data and making maps. You can create a variety of
maps using this library. Some examples of maps you can create with it are heatmaps, dot-density
maps, geographical maps, and many more.
4. Gleam
Gleam is inspired by R’s Shiny package. It lets you convert your graphs into amazing web apps using
just Python code. This is beneficial for people who don’t have knowledge of HTML and CSS. It is not
really a visualization library but works with any visualization library.
5. ggplot
ggplot works differently from matplotlib. It lets you add multiple components as layers to create a
complete graph or plot at the end. For example, at the start you can add an axis, then points, and other
components like a trend line. They always say that you should store your data in a data frame before
using ggplot to get simpler and efficient results.
6. Bokeh
Bokeh library is created by Continuum Analytics to generate a visualization that is friendly for web
interfaces and browsers. Visualizations generated by the bokeh library are interactive in nature,
which lets you convey more information.
112
7. Missingo
Data science is all about finding useful information from the given data and making it visible to
everyone. The best way to do this is to visualize the data. This package can be a boom for all the data
scientist enthusiasts out there. It helps you to find all the missing values — and display them in a
beautiful and graphical way in real-world datasets with no headache and just one line of code. It
supports graphical representations like bar plots, charts, heatmap, dendrograms, and more.
NUMPY (16m)
NumPy stands for numeric python which is a python package for the computation and processing
of the multidimensional and single dimensional array elements.
NumPy stands for numeric python which is a python package for the computation and processing
of the multidimensional and single dimensional array elements.
Travis Oliphant created NumPy package in 2005 by injecting the features
of the ancestor module Numeric into another module Numarray.
It is an extension module of Python which is mostly written in C.
It provides various functions which are capable of performing the numeric
computations with a high speed. NumPy provides various powerful data structures, implementing
multi-dimensional arrays and matrices.
These data structures are used for the optimal computations regarding arrays and matrices.
With the revolution of data science, data analysis libraries like NumPy, SciPy, Pandas, etc. have
seen a lot of growth. With a much easier syntax than other programming languages, python is the first
choice language for the data scientist.
NumPy provides a convenient and efficient way to handle the vast amount of data. NumPy is
also very convenient with Matrix multiplication and data reshaping.
NumPy is fast which makes it reasonable to work with a large set of data. Nowadays, NumPy
in combination with SciPy and Mat-plotlib is used as the replacement to MATLAB as Python is more
complete and easier programming language than MATLAB.
There are the following advantages of using NumPy for data analysis.
113
1. NumPy performs array-oriented computing.
5. NumPy provides the in-built functions for linear algebra and random number generation.
NumPy doesn't come bundled with Python. We have to install it using the python pip installer.
Execute the following command.
NumPy Ndarray
Ndarray is the n-dimensional array object defined in the numpy which stores the collection of
the similar type of elements. In other words, we can define a ndarray as the collection of the data
type (dtype) objects.
The ndarray object can be accessed by using the 0 based indexing. Each element of the Array
object contains the same size in the memory.
Creating a ndarray object: The ndarray object can be created by using the array routine of the numpy
module. For this purpose, we need to import the numpy.
>>> a = numpy.array
We can also pass a collection object into the array routine to create the equivalent n-dimensional
array. The syntax is given below.
>>> numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
SN PARAMETER DESCRIPTION
1 object It represents the collection object. It can be a list, tuple, dictionary, set, etc.
2 dtype We can change the data type of the array elements by changing this option
to the specified type. The default is none.
114
3 copy It is optional. By default, it is true which means the object is copied.
4 order There can be 3 possible values assigned to this option. It can be C (column
order), R (row order), or A (any)
5 subok The returned array will be base class array by default. We can change this
to make the subclasses passes through by setting this option to true.
6 ndmin It represents the minimum dimensions of the resultant array.
To change the data type of the array elements, mention the name of the data type along with the
collection.
Finding the dimensions of the Array: The ndim function can be used to find the dimensions of the
array.
115
Finding the size of each array element: The itemsize function is used to get the size of each array
item. It returns the number of bytes taken by each array element. Example,
#finding the size of each item in the array
import numpy as np
a = np.array([[1,2,3]])
print("Each item contains",a.itemsize,"bytes")
Output:
Finding the data type of each array item:To check the data type of each array item, the dtype
function is used. Consider the following example to check the data type of the array items.
Output:
Finding the shape and size of the array: To get the shape and size of the array, the size and shape
function associated with the numpy array is used.
import numpy as np
a = np.array([[1,2,3,4,5,6,7]])
print("Array Size:",a.size)
print("Shape:",a.shape)
Output:
116
Array Size: 7
Shape: (1, 7)
By the shape of the array, we mean the number of rows and columns of a multi-dimensional array.
However, the numpy module provides us the way to reshape the array by changing the number
of rows and columns of the multi-dimensional array.
The reshape() function associated with the ndarray object is used to reshape the array. It accepts
the two parameters indicating the row and columns of the new shape of the array.
Let's reshape the array given in the following image.
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
print("printing the original array..")
print(a)
a=a.reshape(2,3)
print("printing the reshaped array..")
print(a)
Output:
Slicing in the Array: Slicing in the NumPy array is the way to extract a range of elements from an
array. Slicing in the array is performed in the same way as it is performed in the python list.
117
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
print(a[0,1])
print(a[2,0])
Output:
2
5
The above program prints the 2nd element from the 0th index and 0th element from the 2nd index of
the array.
Finding the maximum, minimum, and sum of the array elements: The NumPy provides the
max(), min(), and sum() functions which are used to find the maximum, minimum, and sum of the
array elements respectively.
import numpy as np
a = np.array([1,2,3,10,15,4])
print("The array:",a)
print("The maximum element:",a.max())
print("The minimum element:",a.min())
print("The sum of the elements:",a.sum())
Output:
The array: [ 1 2 3 10 15 4]
The maximum element: 15
The minimum element: 1
The sum of the elements: 35
Finding square root and standard deviation: The sqrt() and std() functions associated with the
numpy array are used to find the square root and standard deviation of the array elements
respectively. Standard deviation means how much each element of the array varies from the mean
value of the numpy array.
import numpy as np
a = np.array([[1,2,30],[10,15,4]])
118
print(np.sqrt(a))
print(np.std(a))
Output:
NUMPY DATATYPES
The NumPy provides a higher range of numeric data types than that provided by the Python. A list
of numeric data types is given in the following table.
SN DATA DESCRIPTION
TYPE
1 bool_ It represents the boolean value indicating true or false. It is stored as a byte.
2 int_ It is the default type of integer. It is identical to long type in C that contains 64 bit
or 32-bit integer.
3 Intc It is similar to the C integer (c int) as it represents 32 or 64-bit int.
4 Intp It represents the integers which are used for indexing.
5 int8 It is the 8-bit integer identical to a byte. The range of the value is -128 to 127.
6 int16 It is the 2-byte (16-bit) integer. The range is -32768 to 32767.
7 int32 It is the 4-byte (32-bit) integer. The range is -2147483648 to 2147483647.
8 int64 It is the 8-byte (64-bit) integer. The range is -9223372036854775808 to
9223372036854775807.
9 uint8 It is the 1-byte (8-bit) unsigned integer.
10 uint16 It is the 2-byte (16-bit) unsigned integer.
11 uint32 It is the 4-byte (32-bit) unsigned integer.
12 uint64 It is the 8 bytes (64-bit) unsigned integer.
13 float_ It is identical to float64.
14 float16 It is the half-precision float. 5 bits are reserved for the exponent. 10 bits are
reserved for mantissa, and 1 bit is reserved for the sign.
15 float32 It is a single precision float. 8 bits are reserved for the exponent, 23 bits are
reserved for mantissa, and 1 bit is reserved for the sign.
119
16 float64 It is the double precision float. 11 bits are reserved for the exponent, 52 bits are
reserved for mantissa, 1 bit is used for the sign.
17 complex_ It is identical to complex128.
18 complex6 It is used to represent the complex number where real and imaginary part
4 shares 32 bits each.
19 complex1 It is used to represent the complex number where real and imaginary part
28 shares 64 bits each.
NumPy dtype
All the items of a numpy array are data type objects also known as numpy dtypes. A data type
object implements the fixed size of memory corresponding to an array. We can create a dtype
object by using the following syntax.
import numpy as np
d = np.dtype(np.int32)
print(d)
Output:
int32
We can create a map-like (dictionary) data type which contains the mapping between the values. For
example, it can contain the mapping between employees and salaries or the students and the age, etc.
import numpy as np
d=np.dtype([('salary',np.float)])
120
arr = np.array([(10000.12,),(20000.50,)],dtype=d)
print(arr['salary'])
Output:
Numpy contains a large number of mathematical functions which can be used to perform various
mathematical operations. The mathematical functions include trigonometric functions, arithmetic
functions, and functions for handling complex numbers. Let's discuss the mathematical functions.
Trigonometric functions: Numpy contains the trigonometric functions which are used to calculate
the sine, cosine, and tangent of the different angles in radian. The sin, cos, and tan functions return
the trigonometric ratio for the specified angles. Consider the following example.
import numpy as np
arr = np.array([0, 30, 60, 90, 120, 150, 180])
print("\nThe sin value of the angles",end = " ")
print(np.sin(arr * np.pi/180))
print("\nThe cosine value of the angles",end = " ")
print(np.cos(arr * np.pi/180))
print("\nThe tangent value of the angles",end = " ")
print(np.tan(arr * np.pi/180))
Output:
121
PYTHON PANDAS
It has a fast and efficient DataFrame object with the default and customized indexing.
Used for reshaping and pivoting of the data sets.
Group by data for aggregations and transformations.
It is used for data alignment and integration of the missing data.
Provide the functionality of Time Series.
Process a variety of data sets in different formats like matrix data, tabular heterogeneous.
Handle multiple operations of the data sets such as subsetting, slicing, filtering, groupBy, re-
ordering, and re-shaping.
It integrates with the other libraries such as SciPy, and scikit-learn.
Provides fast performance, and If you want to speed it, even more, you can use the Cython.
BENEFITS OF PANDAS
Data Representation: It represents the data in a form that is suited for data analysis through its
DataFrame and Series.
Clear code: The clear API of the Pandas allows you to focus on the core part of the code. So, it
provides clear and concise code for the user.
122
INSTALLATION
The Pandas provides two data structures for processing the data, i.e., Series and DataFrame, which
are discussed below:
1) SERIES
It is defined as a one-dimensional array that is capable of storing various data types. The row labels
of series are called the index. We can easily convert the list, tuple, and dictionary into series using
"series' method. A Series cannot contain multiple columns. It has one parameter:
Before creating a Series, Firstly, we have to import the numpy module and then use array() function
in the program.
Explanation: In this code, firstly, we have imported the pandas and numpy library with
the pd and np alias. Then, we have taken a variable named "info" that consist of an array of some
123
values. We have called the info variable through a Series method and defined it in an "a" variable. The
Series has printed by calling the print(a) method.
It is a widely used data structure of pandas and works with a two-dimensional array with
labeled axes (rows and columns).
DataFrame is defined as a standard way to store data and has two different indexes, i.e., row index
and column index. It consists of the following properties:
The columns can be heterogeneous types like int, bool, and so on.
It can be seen as a dictionary of Series structure where both the rows and columns are indexed. It
is denoted as "columns" in case of columns and "index" in case of rows.
Create a DataFrame using List: We can easily create a DataFrame in Pandas using list.
The Pandas Series can be defined as a one-dimensional array that is capable of storing various
data types. We can easily convert the list, tuple, and dictionary into series using "series' method.
The row labels of series are called the index. A Series cannot contain multiple columns. It has the
following parameter:
index: The value of the index should be unique and hashable. It must be of the same length as
data. If we do not pass any index, default np.arrange(n) will be used.
124
CREATING A SERIES
CREATE AN EMPTY SERIES: We can easily create an empty series in Pandas which means it will
not have any value. The syntax that is used for creating an Empty Series:
The below example creates an Empty Series type object that has no values and having default
datatype, i.e., float64.
import pandas as pd
x = pd.Series()
print (x)
Output
CREATING A SERIES USING INPUTS: We can create Series by using various inputs:
Array
Dict
Scalar value
Before creating a Series, firstly, we have to import the numpy module and then use array()
function in the program. If the data is ndarray, then the passed index must be of the same length.
If we do not pass an index, then by default index of range(n) is being passed where n defines the
length of an array, i.e., [0,1,2,....range(len(array))-1].
import pandas as pd
import numpy as np
info = np.array(['P','a','n','d','a','s'])
a = pd.Series(info)
print(a)
125
Output
0 P
1 a
2 n
3 d
4 a
5 s
dtype: object
We can also create a Series from dict. If the dictionary object is being passed as an input and
the index is not specified, then the dictionary keys are taken in a sorted order to construct
the index. If index is passed, then values correspond to a particular label in the index will be
extracted from the dictionary.
import pandas as pd
import numpy as np
info = {'x' : 0., 'y' : 1., 'z' : 2.}
a = pd.Series(info)
print (a)
Output
x 0.0
y 1.0
z 2.0
dtype: float64
If we take the scalar values, then the index must be provided. The scalar value will be repeated
for matching the length of the index.
126
Output
0 4
1 4
2 4
3 4
dtype: int64
Accessing data from series with Position: Once you create the Series type object, you can access its
indexes, data, and even individual elements. The data in the Series can be accessed similar to that in
the ndarray.
import pandas as pd
x = pd.Series([1,2,3],index = ['a','b','c'])
#retrieve the first element
print (x[0])
Output 1
SERIES OBJECT ATTRIBUTES: The Series attribute is defined as any information related to the
Series object such as size, datatype. etc. Below are some of the attributes that you can use to get the
information about the Series object:
ATTRIBUTES DESCRIPTION
Series.index Defines the index of the Series.
Series.shape It returns a tuple of shape of the data.
Series.dtype It returns the data type of the data.
Series.size It returns the size of the data.
Series.empty It returns True if Series object is empty, otherwise returns false.
Series.hasnans It returns True if there are any NaN values, otherwise returns false.
Series.nbytes It returns the number of bytes in the data.
Series.ndim It returns the number of dimensions in the data.
Series.itemsize It returns the size of the datatype of item.
We can retrieve the index array and data array of an existing Series object by using the attributes index
and values.
127
import numpy as np
import pandas as pd
x=pd.Series(data=[2,4,6,8])
y=pd.Series(data=[11.2,18.6,22.5], index=['a','b','c'])
print(x.index)
print(x.values)
print(y.index)
print(y.values)
Output
You can use attribute dtype with Series object as <objectname> dtype for retrieving the data type of
an individual element of a series object, you can use the itemsize attribute to show the number of
bytes allocated to each data item.
import numpy as np
import pandas as pd
a=pd.Series(data=[1,2,3,4])
b=pd.Series(data=[4.9,8.2,5.6],
index=['x','y','z'])
print(a.dtype)
print(a.itemsize)
print(b.dtype)
print(b.itemsize)
Output
int64
8
float64
8
128
RETRIEVING SHAPE
The shape of the Series object defines total number of elements including missing or empty
values(NaN).
import numpy as np
import pandas as pd
a=pd.Series(data=[1,2,3,4])
b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z'])
print(a.shape)
print(b.shape)
Output
(4,)
(3,)
import numpy as np
import pandas as pd
a=pd.Series(data=[1,2,3,4])
b=pd.Series(data=[4.9,8.2,5.6],
index=['x','y','z'])
print(a.ndim, b.ndim)
print(a.size, b.size)
print(a.nbytes, b.nbytes)
Output
11
43
32 24
To check the Series object is empty, you can use the empty attribute. Similarly, to check if a series
object contains some NaN values or not, you can use the hasans attribute.
129
import numpy as np
import pandas as pd
a=pd.Series(data=[1,2,3,np.NaN])
b=pd.Series(data=[4.9,8.2,5.6],index=['x','y','z'])
c=pd.Series()
print(a.empty,b.empty,c.empty)
print(a.hasnans,b.hasnans,c.hasnans)
print(len(a),len(b))
print(a.count( ),b.count( ))
Output
SERIES FUNCTIONS
FUNCTIONS DESCRIPTION
Pandas Series.map() Map the values from two series that have a common column.
Pandas Series.std() Calculate the standard deviation of the given set of numbers,
DataFrame, column, and rows.
Pandas Series.to_frame() Convert the series object to the dataframe.
Pandas Series.value_counts() Returns a Series that contain counts of unique values.
Human minds are more adaptive for the visual representation of data
rather than textual data. We can easily understand things when they are
visualized. It is better to represent the data through the graph where we
can analyze the data more efficiently and make the specific decision according
to data analysis. Before learning the matplotlib, we need to understand data
visualization and why data visualization is important.
130
Data Visualization
Graphics provides an excellent approach for exploring the data, which is essential for presenting
results. Data visualization is a new term. It expresses the idea that involves more than just
representing data in the graphical form (instead of using textual form). This can be very helpful
when discovering and getting to know a dataset and can help with classifying patterns, corrupt
data, outliers, and much more. With a little domain knowledge, data visualizations can be used to
express and demonstrate key relationships in plots and charts. The static does indeed focus on
quantitative description and estimations of data. It provides an important set of tools for gaining a
qualitative understanding.
There are five key plots that are used for data visualization.
There are five phases which are essential to make the decision for the organization:
131
o Visualize: We analyze the raw data, which means it makes complex data more accessible,
understandable, and more usable. Tabular data representation is used where the user will look
up a specific measurement, while the chart of several types is used to show patterns or
relationships in the data for one or more variables.
o Analysis: Data analysis is defined as cleaning, inspecting, transforming, and modeling data to
derive useful information. Whenever we make a decision for the business or in daily life, is by
past experience. What will happen to choose a particular decision, it is nothing but analyzing
our past. That may be affected in the future, so the proper analysis is necessary for better
decisions for any business or organization.
o Document Insight: Document insight is the process where the useful data or information is
organized in the document in the standard format.
o Transform Data Set: Standard data is used to make the decision more effectively.
MATPLOTLIB
Matplotlib is a Python library which is defined as a multi-platform data visualization library built
on Numpy array. It can be used in python scripts, shell, web application, and other graphical user
interface toolkit.
The John D. Hunter originally conceived the matplotlib in 2002. It has an active development
community and is distributed under a BSD-style license. Its first version was released in 2003,
and the latest version 3.1.1 is released on 1 July 2019.
Matplotlib 2.0.x supports Python versions 2.7 to 3.6 till 23 June 2007. Python3 support started
with Matplotlib 1.2. Matplotlib 1.4 is the last version that supports Python 2.6.
There are various toolkits available that are used to enhance the functionality of the matplotlib.
Some of these tools are downloaded separately, others can be shifted with the matplotlib source
code but have external dependencies.
Bashmap: It is a map plotting toolkit with several map projections, coastlines, and political
boundaries.
Excel tools: Matplotlib provides the facility to utilities for exchanging data with Microsoft
Excel.
Natgrid: It is an interface to the Natgrid library for irregular gridding of the spaced data.
132
MATPLOTLIB ARCHITECTURE
There are three different layers in the architecture of the matplotlib which are the following:
Backend Layer
Artist layer
Scripting layer
Artist Layer: The artist layer is the second layer in the architecture. It is responsible for the various
plotting functions, like axis, which coordinates on how to use the renderer on the figure canvas.
Scripting layer: The scripting layer is the topmost layer on which most of our code will run. The
methods in the scripting layer, almost automatically take care of the other layers, and all we need to
care about is the current state (figure & subplot).
Figure: It is a whole figure which may hold one or more axes (plots). We can think of a Figure as a
canvas that holds plots.
Axes: A Figure can contain several Axes. It consists of two or three (in the case of 3D) Axis objects.
Each Axes is comprised of a title, an x-label, and a y-label.
Axis: Axises are the number of line like objects and responsible for generating the graph limits.
Artist: An artist is the all which we see on the graph like Text objects, Line2D objects, and collection
objects. Most Artists are tied to Axes.
133
INSTALLING MATPLOTLIB
Before start working with the Matplotlib or its plotting functions first, it needs to be installed. The
installation of matplotlib is dependent on the distribution that is installed on your computer.
The python package manager pip is also used to install matplotlib. Open the command prompt
window, and type the following command:
Verify the Installation:To verify that matplotlib is installed properly or not, type the following
command includes calling .__version __ in the terminal.
import matplotlib
matplotlib.__version__
'3.1.1'
It takes only three lines to plot a simple graph using the Python matplotlib. We can add titles, labels
to our chart which are created by Python matplotlib library to make it more meaningful. The example
is the following:
134
plt.show()
The matplotlib.pyplot is the collection command style functions that make matplotlib feel like
working with MATLAB. The pyplot functions are used to make some changes to figure such as
create a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the
plot including labels, etc.
It is good to use when we want to plot something quickly without instantiating any figure or Axes.
While working with matplotlib.pyplot, some states are stored across function calls so that it
keeps track of the things like current figure and plotting area, and these plotting functions are
directed to the current axes.
The pyplot module provide the plot() function which is frequently use to plot a graph. Let's have
a look on the simple example:
In the above program, it plots the graph x-axis ranges from 0-4 and the y-axis from 1-5. If we provide
a single list to the plot(), matplotlib assumes it is a sequence of y values, and automatically generates
the x values. Since we know that python index starts at 0, the default x vector has the same length as
y but starts at 0. Hence the x data are [0, 1, 2, 3, 4].
We can pass the arbitrary number of arguments to the plot(). For example, to plot x versus y, we can
do this following way:
135
FORMATTING THE STYLE OF THE PLOT
There is an optional third argument, which is a format string that indicates the color and line type of
the plot. The default format string is 'b-'which is the solid blue as you can observe in the above plotted
graph. Let's consider the following example where we plot the graph with the red circle.
Character Color
'b' Blue
'g' Green
'r' Red
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
Matplotlib allows us to pass categorical variables directly to many plotting functions: consider the
following example
136
from matplotlib import pyplot
names = ['Abhishek', 'Himanshu', 'Devansh']
marks= [87,50,98]
plt.figure(figsize=(9,3))
plt.subplot(131)
plt.bar(names, marks)
plt.subplot(132)
plt.scatter(names, marks)
plt.subplot(133)
plt.plot(names, marks)
plt.suptitle('Categorical Plotting')
plt.show()
Output:
In the above program, we have plotted the categorical graph using the subplot() function. Let's a
have a look on the subplot() function.
What is subplot()
The Matplotlib subplot() function is defined as to plot two or more plots in one figure. We can
use this method to separate two graphs which plotted in the same axis Matplotlib supports all
kinds of subplots, including 2x1 vertical, 2x1 horizontal, or a 2x2 grid.
It accepts the three arguments: they are nrows, ncols, and index. It denote the number of rows,
number of columns and the index.
137
CREATING DIFFERENT TYPES OF GRAPH
Line graph: The line graph is one of charts which shows information as a series of the line. The
graph is plotted by the plot() function.
We can customize the graph by importing the style module. The style module will be built into a
matplotlib installation. It contains the various functions to make the plot more attractive. In the
below program, we are using the style module:
138
In Matplotlib, the figure (an instance of class plt.Figure) can be supposed of as a single container that
consists of all the objects denoting axes, graphics, text, and labels.
Example-3
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x))
The matplotlib provides the fill_between() function which is used to fill area around the lines based
on the user defined logic.
Example-4
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x))
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2 * np.pi * x)
y2 = 1.2 * np.sin(4 * np.pi * x)
fig, ax = plt.subplots(1, sharex=True)
ax.plot(x, y1, x, y2, color='black')
ax.fill_between(x, y1, y2, where=y2 >= y1, facecolor='blue', interpolate=True)
ax.fill_between(x, y1, y2, where=y2 <= y1, facecolor='red', interpolate=True)
ax.set_title('fill between where')
2. Bar graphs: Bar graphs are one of the most common types of graphs and are used to show data
associated with the categorical variables. Matplotlib provides a bar() to make bar graphs which
accepts arguments such as: categorical variables, their value and color.
139
from matplotlib import pyplot as plt
players = ['Virat','Rohit','Shikhar','Hardik']
runs = [51,87,45,67]
plt.bar(players,runs,color = 'green')
plt.title('Score Card')
plt.xlabel('Players')
plt.ylabel('Runs')
plt.show()
Another function barh() is used to make horizontal bar graphs. It accepts xerr or yerr as arguments
(in case of vertical graphs) to depict the variance in our data as follows:
Pie Chart: A pie chart is a circular graph that is broken down in the segment or slices of pie. It is
generally used to represent the percentage or proportional data where each slice of pie represents a
particular category. Let's have a look at the below example:
140
4. Histogram
First, we need to understand the difference between the bar graph and histogram. A histogram is
used for the distribution, whereas a bar chart is used to compare different entities. A histogram is a
type of bar plot that shows the frequency of a number of values compared to a set of values ranges.
For example we take the data of the different age group of the people and plot a histogram with
respect to the bin. Now, bin represents the range of values that are divided into series of intervals.
Bins are generally created of the same size.
141
fig.tight_layout()
plt.show()
5. Scatter plot
The scatter plots are mostly used for comparing variables when we need to define how much one
variable is affected by another variable. The data is displayed as a collection of points. Each point has
the value of one variable, which defines the position on the horizontal axes, and the value of other
variable represents the position on the vertical axis.
Example-2
142
plt.ylabel('income*1000')
plt.title('Scatter Plot')
plt.legend()
plt.show()
6. 3D graph plot
Matplotlib was initially developed with only two-dimension plot. Its 1.0 release was built with some
of three-dimensional plotting utilities on top of two-dimension display, and the result is a convenient
set of tools for 3D data visualization. Three-dimension plots can be created by importing
the mplot3d toolkit, include with the main Matplotlib installation:
PYTHON PLOTLY
Python Plotly Library is an open-source library that can be used for data visualization and
understanding data simply and easily. Plotly supports various types of plots like line charts, scatter
plots, histograms, cox plots, etc.
Plotly has hover tool capabilities that allow us to detect any outliers or anomalies in a large number
of data points. It is visually attractive that can be accepted by a wide range of audiences. It allows
us for the endless customization of our graphs that makes our plot more meaningful and
understandable for others.
143
INSTALLATION
Plotly does not come built-in with Python. To install it type the below command in the terminal.
plotly.plotly
plotly.graph.objects
plotly.tools
plotly.plotly acts as the interface between the local machine and Plotly. It contains functions that
require a response from Plotly’s server.
plotly.graph_objects module contains the objects (Figure, layout, data, and the definition of the
plots like scatter plot, line chart) that are responsible for creating the plots. The Figure can be
represented either as dict or instances of plotly.graph_objects.Figure and these are serialized as
JSON before it gets passed to plotly.js. Consider the below example for better understanding.
Note: plotly.express module can create the entire Figure at once. It uses the graph_objects internally
and returns the graph_objects.Figure instance.
Example:
import plotly.express as px
print(fig)
144
Figures are represented as trees where the root node has three top layer attributes – data,
layout, and frames and the named nodes called ‘attributes’. Consider the above
example, layout.legend is a nested dictionary where the legend is the key inside the dictionary whose
value is also a dictionary. plotly.tools module contains various tools in the forms of the functions
that can enhance the Plotly experience.
Example:
import plotly.express as px
fig.show()
In the above example, the plotly.express module is imported which returns the Figure instance. We
have created a simple line chart by passing the x, y coordinates of the points to be plotted.
With plotly we can create more than 40 charts and every plot can be created using the plotly.express
and plotly.graph_objects class. Let’s see some commonly used charts with the help of Plotly.
Line Chart
Line plot in Plotly is much accessible and illustrious annexation to plotly which manage a variety of
types of data and assemble easy-to-style statistic. With px.line each data position is represented as a
vertex (which location is given by the x and y columns) of a polyline mark in 2D space.
import plotly.express as px
df = px.data.iris()
fig.show()
145
Bar Chart
A bar chart is a pictorial representation of data that presents categorical data with rectangular bars
with heights or lengths proportional to the values that they represent. In other words, it is the
pictorial representation of dataset. These data sets contain the numerical values of variables that
represent the length or height.
import plotly.express as px
df = px.data.iris()
fig.show()
Output:
Histograms
A histogram contains a rectangular area to display the statistical information which is proportional
to the frequency of a variable and its width in successive numerical intervals. A graphical
representation that manages a group of data points into different specified ranges. It has a special
feature that shows no gaps between the bars and similar to a vertical bar graph.
Example:
import plotly.express as px
df = px.data.iris()
y="petal_width")
fig.show()
146
Scatter Plot and Bubble charts
A scatter plot is a set of dotted points to represent individual pieces of data in the horizontal and
vertical axis. A graph in which the values of two variables are plotted along X-axis and Y-axis, the
pattern of the resulting points reveals a correlation between them.
A bubble plot is a scatter plot with bubbles (color-filled circles). Bubbles have various sizes
dependent on another variable in the data. It can be created using the scatter() method of
plotly.express.
import plotly.express as px
df = px.data.iris()
fig.show()
import plotly.express as px
df = px.data.iris()
size="petal_length", color="species")
fig.show()
Pie Charts: A pie chart is a circular statistical graphic, which is divided into slices to illustrate
numerical proportions. It depicts a special chart that uses “pie slices”, where each sector shows the
relative sizes of data. A circular chart cuts in a form of radii into segments describing relative
frequencies or magnitude also known as circle graph.
import plotly.express as px
df = px.data.tips()
fig.show()
147
3D Scatter Plot Plotly
3D Scatter Plot can plot two-dimensional graphics that can be enhanced by mapping up to three
additional variables while using the semantics of hue, size, and style parameters. All the parameter
control visual semantic which are used to identify the different subsets. Using redundant semantics
can be helpful for making graphics more accessible. It can be created using the scatter_3d function of
plotly.express class.
import plotly.express as px
df = px.data.iris()
y = 'sepal_length',
z = 'petal_width',
color = 'species')
fig.show()
INTRODUCTION
Class
Object
Method
Inheritance
Polymorphism
148
Data Abstraction
Encapsulation
CLASS
A Class is a blueprint for the object. The class can be defined as a collection of objects.
It is a logical entity that has some specific attributes and methods.
For example: if you have an employee class, then it should contain an attribute and method, i.e. an
email id, name, age, salary, etc.
Syntax
class ClassName:
<statement-1>
.
.
<statement-N>
EXAMPLE:
We can think of class as a sketch of a parrot with labels. It contains all the details about the
name, colors, size etc. Based on these descriptions, we can study about the parrot. Here, a parrot is
an object. The example for class of parrot can be :
class Parrot:
pass
Here, we use the class keyword to define an empty class Parrot. From class, we construct instances.
An instance is a specific object created from a particular class.
The pass statement is used as a placeholder for future code. When the pass statement is executed,
nothing happens, but you avoid getting an error when empty code is not allowed.
OBJECT
The object is an entity that has state and behavior. It may be any real-world object like the
mouse, keyboard, chair, table, pen, etc.
An object (instance) is an instantiation of a class. When class is defined, only the description
for the object is defined. Therefore, no memory or storage is allocated.
149
Everything in Python is an object, and almost everything has attributes and methods.
All functions have a built-in attribute __doc__, which returns the docstring defined in the function
source code. When we define a class, it needs to create an object to allocate the memory. Consider
the following example.
class car:
def __init__(self,modelname, year):
self.modelname = modelname
self.year = year
def display(self):
print(self.modelname,self.year)
c1 = car("Toyota", 2016)
c1.display()
Output:
Toyota 2016
In the above example, we have created the class named car, and it has two attributes modelname and
year. We have created a c1 object to access the class attribute. The c1 object will allocate memory for
these values. We will learn more about class and object in the next tutorial.
The self-parameter
The self-parameter refers to the current instance of the class and accesses the class variables.
We can use anything instead of self, but it must be the first parameter of any function which belongs
to the class.
Method
The method is a function that is associated with an object. In Python, a method is not unique to class
instances. Any object type can have methods. Methods are functions defined inside the body of a
class. They are used to define the behaviors of an object.
150
PYTHON CLASSMETHOD()
The classmethod() method returns a class method for the given function.
Example
class Student:
marks = 0
marks = obtained_marks
Student.print_marks = classmethod(Student.compute_marks)
Student.print_marks(88)
classmethod() Syntax
classmethod(function)
A class method is a method that is bound to a class rather than its object. It doesn't require
creation of a class instance, much like staticmethod.
The difference between a static method and a class method is:
Static method knows nothing about the class and just deals with the parameters
Class method works with the class since its parameter is always the class itself.
The class method can be called both by the class and its object.
Class.classmethod()
Or even
Class().classmethod()
151
But no matter what, the class method is always attached to a class with the first argument as the class
itself cls.
class Person:
age = 25
def printAge(cls):
Person.printAge = classmethod(Person.printAge)
Person.printAge()
Output
Here, we have a class Person, with a member variable age assigned to 25.
We also have a function printAge that takes a single parameter cls and not self we usually take.
cls accepts the class Person as a parameter rather than Person's object/instance.
Now, we pass the method Person.printAge as an argument to the function classmethod. This
converts the method to a class method so that it accepts the first parameter as a class (i.e. Person).
In the final line, we call printAge without creating a Person object like we do for static methods.
This prints the class variable age.
1. Factory methods
Factory methods are those methods that return a class object (like constructor) for different use
cases. It is similar to function overloading in C++. Since, Python doesn't have anything as such,
class methods and static methods are used.
152
Example 2: Create factory method using class method
# random Person
class Person:
self.name = name
self.age = age
@classmethod
def display(self):
person.display()
person1.display()
OUTPUT
Here, we have two class instance creator, a constructor and a fromBirthYear method.
The constructor takes normal parameters name and age.
While, fromBirthYear takes class, name and birthYear, calculates the current age by subtracting
it with the current year and returns the class instance.
The fromBirthYear method takes Person class (not Person object) as the first parameter cls and
returns the constructor by calling cls(name, date.today().year - birthYear), which is equivalent
to Person(name, date.today().year - birthYear)
153
Before the method, we see @classmethod. This is called a decorator for
converting fromBirthYear to a class method as classmethod().
Whenever you derive a class from implementing a factory method as a class method, it ensures
correct instance creation of the derived class. You can create a static method for the above
example but the object it creates, will always be hard coded as Base class. But, when you use a
class method, it creates the correct instance of the derived class.
# random Person
class Person:
self.name = name
self.age = age
@staticmethod
@classmethod
def display(self):
class Man(Person):
sex = 'Male'
print(isinstance(man, Man))
154
man1 = Man.fromFathersAge('John', 1965, 20)
print(isinstance(man1, Man))
OUTPUT
True
False
Here, using a static method to create a class instance wants us to hardcode the instance type
during creation. This clearly causes a problem when inheriting Person to Man.
fromFathersAge method doesn't return a Man object but its base class Person's object.
This violates the OOP paradigm. Using a class method as fromBirthYear can ensure the OOP-ness
of the code since it takes the first parameter as the class itself and calls its factory method.
INHERITANCE
Inheritance is the most important aspect of object-oriented programming, which simulates the
real-world concept of inheritance. It specifies that the child object acquires all the properties and
behaviors of the parent object.
By using inheritance, we can create a class which uses all the properties and behavior of another
class. The new class is known as a derived class or child class, and the one whose properties
are acquired is known as a base class or parent class.
It provides the re-usability of the code. Inheritance provides code reusability to the program
because we can use an existing class to create a new class instead of creating it from scratch.
In inheritance, the child class acquires the properties and can access all the data members and
functions defined in the parent class. A child class can also provide its specific implementation to
the functions of the parent class.
In python, a derived class can inherit base class by just mentioning the base in the bracket after
the derived class name. Consider the following syntax to inherit a base class into the derived class.
155
SYNTAX
A class can inherit multiple classes by mentioning all of them inside the bracket. Consider the
following syntax.
SYNTAX
class derive-class(<base class 1>, <base class 2>, ..... <base class n>):
<class - suite>
Example 1
class Animal:
def speak(self):
print("Animal Speaking")
#child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print("dog barking")
d = Dog()
d.bark()
d.speak()
Output:
dog barking
Animal Speaking
156
Syntax
class class1:
<class-suite>
class class2(class1):
<class suite>
class class3(class2):
<class suite>
Example
class Animal:
def speak(self):
print("Animal Speaking")
#The child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print("dog barking")
#The child class Dogchild inherits another child class Dog
class DogChild(Dog):
def eat(self):
print("Eating bread...")
d = DogChild()
d.bark()
d.speak()
d.eat()
157
Output:
dog barking
Animal Speaking
Eating bread...
Python provides us the flexibility to inherit multiple base classes in the child class.
SYNTAX
class Base1:
<class-suite>
class Base2:
<class-suite>
.
class BaseN:
<class-suite>
class Derived(Base1, Base2, ...... BaseN):
<class-suite>
EXAMPLE
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
158
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Output:
30
200
0.5
Method Overriding
We can provide some specific implementation of the parent class method in our child class.
When the parent class method is defined in the child class with some specific implementation, then
the concept is called method overriding. We may need to perform method overriding in the scenario
where the different definition of a parent class method is needed in the child class. Consider the
following example to perform method overriding in python.
class Bank:
def getroi(self):
return 10;
class SBI(Bank):
def getroi(self):
return 7;
class ICICI(Bank):
def getroi(self):
return 8;
b1 = Bank()
b2 = SBI()
b3 = ICICI()
print("Bank Rate of interest:",b1.getroi());
print("SBI Rate of interest:",b2.getroi());
print("ICICI Rate of interest:",b3.getroi());
159
Output:
POLYMORPHISM
Polymorphism contains two words "poly" and "morphs". Poly means many, and morph means
shape. By polymorphism, we understand that one task can be performed in different ways.
In programming, polymorphism means the same function name (but different signatures) being
used for different types.
For example - you have a class animal, and all animals speak. But they speak differently. Here, the
"speak" behavior is polymorphic in a sense and depends on the animal. So, the abstract "animal"
concept does not actually "speak", but specific animals (like dogs and cats) have a concrete
implementation of the action "speak".
print(len("geeks"))
Output:
return x + y+z
# Driver code
print(add(2, 3))
print(add(2, 3, 4))
Output:
160
9
Polymorphism with class methods: The below code shows how Python can use two different
class types, in the same way. We create a for loop that iterates through a tuple of objects. Then call
the methods without being concerned about which class type each object is. We assume that these
methods actually exist in each class.
class India():
def capital(self):
def language(self):
def type(self):
class USA():
def capital(self):
def language(self):
def type(self):
obj_ind = India()
obj_usa = USA()
country.capital()
country.language()
country.type()
OUTPUT:
161
English is the primary language of USA.
Polymorphism with Inheritance: In Python, Polymorphism lets us define methods in the child
class that have the same name as the methods in the parent class. In inheritance, the child class
inherits the methods from the parent class. However, it is possible to modify a method in a child
class that it has inherited from the parent class. This is particularly useful in cases where the
method inherited from the parent class doesn’t quite fit the child class. In such cases, we re -
implement the method in the child class. This process of re-implementing a method in the child
class is known as Method Overriding.
class Bird:
def intro(self):
def flight(self):
class sparrow(Bird):
def flight(self):
class ostrich(Bird):
def flight(self):
obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()
obj_bird.intro()
obj_bird.flight()
obj_spr.intro()
obj_spr.flight()
obj_ost.intro()
obj_ost.flight()
Output:
162
Most of the birds can fly but some cannot.
Polymorphism with a Function and objects: It is also possible to create a function that can take
any object, allowing for polymorphism. In this example, let’s create a function called “func()” which
will take an object which we will name “obj”. Though we are using the name ‘obj’, any instantiated
object will be able to be called into this function. Next, let’s give the function something to do that
uses the ‘obj’ object we passed to it. In this case, let’s call the three methods, viz., capital(),
language() and type(), each of which is defined in the two classes ‘India’ and ‘USA’. Next, let’s create
instantiations of both the ‘India’ and ‘USA’ classes if we don’t have them already. With those, we
can call their action using the same func() function:
def func(obj):
obj.capital()
obj.language()
obj.type()
obj_ind = India()
obj_usa = USA()
func(obj_ind)
func(obj_usa)
class India():
def capital(self):
def language(self):
163
def type(self):
class USA():
def capital(self):
def language(self):
def type(self):
def func(obj):
obj.capital()
obj.language()
obj.type()
obj_ind = India()
obj_usa = USA()
func(obj_ind)
func(obj_usa)
OUTPUT:
164
ENCAPSULATION
VARIABLE: A class is an example of encapsulation as it encapsulates all the data that is member
functions, variables, etc.
Consider a real-life example of encapsulation, in a company, there are different sections like the
accounts section, finance section, sales section etc. The finance section handles all the financial
transactions and keeps records of all the data related to finance. Similarly, the sales section handles
all the sales-related activities and keeps records of all the sales. Now there may arise a situation when
for some reason an official from the finance section needs all the data about sales in a particular
month. In this case, he is not allowed to directly access the data of the sales section. He will first have
to contact some other officer in the sales section and then request him to give the particular data.
This is what encapsulation is. Here the data of the sales section and the employees that can
manipulate them are wrapped under a single name “sales section”. Using encapsulation also hides
the data. In this example, the data of the sections like sales, finance, or accounts are hidden from any
other section.
165
Protected members
Protected members (in C++ and JAVA) are those members of the class that cannot be accessed outside
the class but can be accessed from within the class and its subclasses. To accomplish this in Python,
just follow the convention by prefixing the name of the member by a single underscore “_”.
Although the protected variable can be accessed out of the class as well as in the derived
class(modified too in derived class), it is customary(convention not a rule) to not access the protected
out the class body.
Note: The __init__ method is a constructor and runs as soon as an object of a class is instantiated.
class Base:
def __init__(self):
# Protected member
self._a = 2
class Derived(Base):
def __init__(self):
# Calling constructor of
# Base class
Base.__init__(self)
self._a)
self._a = 3
166
self._a)
obj1 = Derived()
obj2 = Base()
OUTPUT:
Private members
Private members are similar to protected members, the difference is that the class members
declared private should neither be accessed outside the class nor by any base class. In Python, there
is no existence of Private instance variables that cannot be accessed except inside a class. However,
to define a private member prefix the member name with double underscore “__”.
Note: Python’s private and protected members can be accessed outside the class through python
name mangling.
class Base:
def __init__(self):
self.a = "GeeksforGeeks"
self.__c = "GeeksforGeeks"
167
class Derived(Base):
def __init__(self):
# Calling constructor of
# Base class
Base.__init__(self)
print(self.__c)
# Driver code
obj1 = Base()
print(obj1.a)
# raise an AttributeError
Output:
print(obj1.c)
obj2 = Derived()
print(self.__c)
168
AttributeError: 'Derived' object has no attribute '_Derived__c'
Data Abstraction
Data abstraction and encapsulation both are often used as synonyms. Both are nearly synonyms
because data abstraction is achieved through encapsulation.
Abstraction is used to hide internal details and show only functionalities. Abstracting something
means to give names to things so that the name captures the core of what a function or a whole
program does.
169
The @classmethod decorator is a built-in function decorator that is an expression that gets evaluated
after your function is defined. The result of that evaluation shadows your function definition.
A class method receives the class as an implicit first argument, just like an instance method receives
the instance
Syntax:
class C(object):
@classmethod
def fun(cls, arg1, arg2, ...):
....
fun: function that needs to be converted into a class method
returns: a class method for function.
A class method is a method that is bound to the class and not the object of the class.
They have the access to the state of the class as it takes a class parameter that points to the class
and not the object instance.
It can modify a class state that would apply across all the instances of the class. For example, it
can modify a class variable that will be applicable to all the instances.
Static Method
A static method does not receive an implicit first argument.
Syntax:
class C(object):
@staticmethod
def fun(arg1, arg2, ...):
...
returns: a static method for function fun.
A static method is also a method that is bound to the class and not the object of the class.
A static method can’t access or modify the class state.
It is present in a class because it makes sense for the method to be present in class.
Class method vs Static Method
A class method takes cls as the first parameter while a static method needs no specific parameters.
A class method can access or modify the class state while a static method can’t access or modify
it.
170
In general, static methods know nothing about the class state. They are utility-type methods that
take some parameters and work upon those parameters. On the other hand class methods must
have class as a parameter.
We use @classmethod decorator in python to create a class method and we use @staticmethod
decorator to create a static method in python.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# a class method to create a Person object by birth year.
@classmethod
def fromBirthYear(cls, name, year):
return cls(name, date.today().year - year)
# a static method to check if a Person is adult or not.
@staticmethod
def isAdult(age):
return age > 18
person1 = Person('mayank', 21)
person2 = Person.fromBirthYear('mayank', 1996)
print (person1.age)
print (person2.age)
171
print (Person.isAdult(22))
Output:
21
25
True
PYTHON OBJECT PERSISTENCE
The shelve module in Python’s standard library is a simple yet effective tool for persistent data
storage when using a relational database solution is not required. The shelf object defined in this
module is dictionary-like object which is persistently stored in a disk file.
This creates afile similar to dbm database on UNIX like systems. Only string data type can be used
as key in this special dictionary object, whereas any picklable object can serve as value.
Easiest way to form a Shelf object is to use open() function defined in shelve module which return a
DbfilenameShelf object.
Default value for flag parameter is ‘c’ for read/write access. Other flags are ‘w’ (write only) ‘r’
(read only) and ‘n’ (new with read/wxrite)
Protocol parameter denotes pickle protocol writeback parameter by default is false. If set to true,
the accessed entries are cached. Every access calls sync() and close() operations hence process
may be slow.
172
import shelve
s = shelve.open("test")
s['name'] = "Ajay"
s['age'] = 23
s['marks'] = 75
s.close()
This will create test.dir file in current directory and store key-value data in hashed form. The Shelf
object has following methods available −
>>> s=shelve.open('test')
>>> s['age']
23
>>> s['age']=25
>>> s.get('age')
25
The items(), keys() and values() methods return view objects.
>>> list(s.items())
[('name', 'Ajay'), ('age', 25), ('marks', 75)]
>>> list(s.keys())
['name', 'age', 'marks']
173
>>> list(s.values())
['Ajay', 25, 75]
>>> s.pop('marks')
75
>>> list(s.items())
[('name', 'Ajay'), ('age', 25)]
Notice that key-value pair of marks-75 has been removed.
174