Artificail Intellegence and Machine Learning Using Python
Artificail Intellegence and Machine Learning Using Python
in
Computer Applications, Business
Accounting and Multilingual DTP
Quantity : 15000
National Council for Promotion of Urdu Language (NCPUL) is mandated to take action for
making available in Urdu language the knowledge of scientific and technological development as
well as knowledge of ideas evolved in the modern context. In the emerging information
technological scenario, it was necessary that this technology is made available to the Urdu
speaking population of the country with a view to transform Urdu speaking population into
employable technical workforce.
It was in 1999 when a humble beginning was made and some Multilingual DTP Computer
Centres were set up at select locations. From the beginning, attempt was made to provide standard
course contents and ensure quality in computer education at par with the quality of agencies
dedicated to computer awareness and education in the country. In this context, NIELIT, which is an
approved Government of India agency for imparting computer education in non-formal sector,
was engaged for conducting examination and certification. There has been a demand that course
contents need to be upgraded. Therefore, the NCPUL has entered into a MoU with NIELIT and
delegated the powers of regulating academic standards and examinations to it. This course is now
Computer Applications, Business Accounting & Multilingual DTP (CABA-MDTP) and it is
hoped that course will enable the students pursuing this course to get CCC, CABA-MDTP
Diploma and 'O' Level Certification from NCPUL and NIELIT. The important change in the
eligibility will enable students with+2 qualification to pursue 'O' Level Diploma and those who are
with Matric or equivalent qualification to pursue CCC and CABA-MDTP.
I am sure that the new courseware of NIELIT will meet the requirements of the students and
NCPUL will be in a position to discharge its mandate of linking Urdu to the contemporary
requirements particularly, in the context of ever increasing use of information technology in our
life.
1
Artificial Intelligence and Machine Learning using Python
2
Artificial Intelligence and Machine Learning using Python
Exercises............................................................................................................................................. 97
Multiple Choice Questions ............................................................................................................... 97
Chapter 5 ................................................................................................................................................. 100
Functions.................................................................................................................................................. 100
5.1 Top-down Approach of Problem Solving ............................................................................. 100
5.2 Modular Programming and Functions ................................................................................. 101
5.2.1. Modular Programming................................................................................................... 101
5.2.2. Module ............................................................................................................................. 101
5.2.3. Advantages of Modular Design ...................................................................................... 101
5.3 Function and function parameters ........................................................................................ 102
5.3.1. HOW to Define a Function in Python ........................................................................... 102
5.3.2. How to Define and Call a Basic Function in Python .................................................... 103
5.3.3. It just prints hello world whenever it is called. ............................................................. 103
5.3.4. How to Define and Call Functions with Parameters.................................................... 103
5.4 Local Variables........................................................................................................................ 104
5.4.1. Syntax of Local Variable in Python............................................................................... 104
5.4.2 How Local Variable Works in python? ............................................................................... 105
5.5 The Return Statement ............................................................................................................ 105
5.6 Default argument values ......................................................................................................... 105
5.7 keyword arguments ................................................................................................................ 106
5.8 VArArgs parameters. ............................................................................................................. 107
5.9 Library function: .................................................................................................................... 107
5.9.1. input() ............................................................................................................................... 107
5.9.2. eval() ................................................................................................................................. 108
5.9.3. print() function ................................................................................................................ 108
5.9.4. print() Parameters........................................................................................................... 109
5.9.5 String Functions: ............................................................................................................. 109
5.9.6 rfind() function ....................................................................................................................... 111
5.9.6 Date and time functions .................................................................................................. 114
5.9.7 recursion .......................................................................................................................... 115
5.9.8 Packages and modules .................................................................................................... 116
Exercise ................................................................................................................................................ 121
Multiple choice questions ............................................................................................................... 121
State whether statement is true or false ........................................................................................ 122
Fill in the blanks .............................................................................................................................. 122
3
Artificial Intelligence and Machine Learning using Python
4
Artificial Intelligence and Machine Learning using Python
5
Artificial Intelligence and Machine Learning using Python
6
Artificial Intelligence and Machine Learning using Python
7
Chapter 1 : Introduction to Programming
Chapter 1
Introduction to Programming
1.1 The Basic Model of Computation
To complete a task there arises a need to write a set of instructions and these set of instructions are
called program and to solve problems on a computer, one must first write a step-by-step solution
using simple instructions for operations and then run the programme to obtain the results. There
are numerous ways to solve a given problem, and thus solutions may differ depending on the
thought process of an individual.
Nevertheless, some fundamental steps in problem solving remains unchanged. These fundamental
steps are:
a. Defining the problem and choosing the data types.
b. Pinpointing the computation steps and their sequence required to obtain the solution.
c. Choosing decision points, i.e. selecting the appropriate operations at specific state.
d. Knowing what to expect and comparing it to the actual values.
Step 1. Understanding the Problem
First, understand the problem by reading it carefully and try to figure out what is the expected
output. Do not start drawing flowcharts right away. Take some test data and solve the problem
manually on paper. Let us take an example and understand how to solve the problem
systematically:
Software engineer before writing the actual program writes algorithm or flowchart.
Step 2. Generate potential solutions
The next step is to create a list of possible solutions to the problem you’ve discovered. There are
many ways to generate solutions. This can be done individually or in a group setting.
Step 3. Choose one solution
Once a list of possible solutions has been made, it’s time to put your to the test. In order to find
the best solution for the problem, analyse every possible resolution and decide which is best for
the situation you are in. One might want to consider many elements before choosing one solution.
These elements include efficacy, practicality, timeliness, resources, and cost. This is also where
risk management will be used to help make a decision. Like brainstorming, choosing a solution
doesn’t have to be done alone.
ALGORITHM
What is an Algorithm and its features?
Software engineers typically use algorithms to plan and solve problems. An algorithm is a series
of steps to solve a particular problem. Alternatively, an algorithm is an ordered, unique set of steps
that produces a result and finishes in a finite amount of time.
Algorithm has the following characteristics:
1. Can be written in any language suitable to developer.
2. Easy to understand.
3. Very simple, complete, clear and systematic program flow.
4. No standard format and have no defined rules of writing.
8
Chapter 1 : Introduction to Programming
9
Chapter 1 : Introduction to Programming
Logics can be easily interpreted: Logics defined in the flowchart can be easily depicted
as conditions/ decision-making figures can distinguished from others and the
directions can be identified.
Testing becomes easier: Being a graphical representation the testing in case of
flowchart becomes very easy and the program flow in the wrong direction can be easily
identified.
1.2.3 Constraints of flowchart
Flowchart require more time to draw thus it is a laborious work to draw a flowchart.
Modifications are carried out with great difficulty in case of the flowchart.
OPCODE OPERAND
(Operation Code) (Location/ Address)
10
Chapter 1 : Introduction to Programming
11
Chapter 1 : Introduction to Programming
Linking
Machine Code
12
Chapter 1 : Introduction to Programming
development. Moreover, effectively and effectively fulfils the needs the user while managing all
the exceptional and boundary values.
Documentation in software engineering is the canopy term that includes all written
documents and materials dealing with software product development. All software development
products, whether created by a small team or a large corporation, require some related
documentation. Different types of documents are created through the whole (SDLC).
Documentation exists to explain product functionality, unify project-related information, and
allow for discussing all significant questions arising between stakeholders and developers.
Documentation varies at different stages as under:
Types of Software Documentation:
a) Requirement Documentation:
This document contains the requirements on the basis of which the software was developed
and contains the description of how the software shall perform and which environment setup
would be appropriate to have the best out of it. These are generated while the software is under
development and is supplied to the tester groups too.
b) Architectural Documentation:
Architecture documentation is a special type of documentation that concerns the design. It
contains very little code and is more focused on the components of the system, their roles and
working. It also shows the data flows throughout the system.
c) Technical Documentation:
These contain the technical aspects of the software like API, algorithms etc. It is prepared
mostly for the software developers. This form of documentation helps in choosing the technical
manpower requirement when the maintenance and support is to be provided for the software.
d) End-user Documentation
As the name suggests these are made for the end user. It contains support resources for the end
users. This kind of documentation in actual helps end user to understand the functionality of
the software and its features so that the end user could use the available features as per need.
13
Chapter 1 : Introduction to Programming
Exercises
Multiple Choice Questions
1. Which one of the following is a step for starting start development?
a. Understanding problem / requirement
b. Coding
c. Testing
d. None of the above
2. Which of the method is used by software development organization for program
execution and flow before starting actual coding?
a. Algorithm
b. Flowchart
c. Any of the above
d. None of the above
3. An algorithm has a feature that:
a. Algorithm can be written in any language as per user choice
b. Algorithm has defined symbols
c. Algorithm uses specific language
d. None of the above
4. A flowchart has following feature that:
a. Flowchart explains program in a particular language and uses no symbols
b. Flowchart uses special symbols for program flow
c. Flowcharts can be drawn using any symbol as per user choice
d. None of the above
5. Machine language is written in:
a. C language
b. English Language
c. 0’s and 1’s
d. None of the above
STATE WHETHER STATEMENT IS TRUE OR FALSE
1. Computer without the need of translator can understand high-level language. (T/F)
2. Machine language instruction has two parts OPCODE and OPERAND. (T/F)
3. Machine language is very easy to understand. (T/F)
4. Compiler is used to translate High-level language to Machine Language. (T/F)
5. Testing and Debugging are the same processes. (T/F)
FILL IN THE BLANKS
1. Compilation is a technique to translate source code into ______________________.
2. Debugging is a process to identify the ________ and fix them.
3. High-level language is machine __________.
4. Assembly language uses __________ instead of 0’s and 1’s.
5. Written record of the software development is called _____________.
14
Chapter 2:Algoritham and Flowchart
Chapter 2
No
Used for making decision and
Diamond
to choose path accordingly.
Yes
15
Chapter 2:Algoritham and Flowchart
16
Chapter 2:Algoritham and Flowchart
Depending on the result received after the comparison of A and B the next step is executed. This
is an example of decision based processing.
2.3.2 Iterative (Looping) Processing
Many jobs that are required to be done with the help of a computer are repetitive in nature. For
example, the calculation of the salary of different workers in a factory is done by the (No. of hours
worked) x (wage rate). This calculation will be performed by an accountant for each worker every
month. Such types of repetitive calculations can easily be done using a program that has a loop
built into the solution of the problem. The loop is defined as a block of processing steps repeated
a certain number of times. An endless loop repeats infinitely and is always the result of an error.
Figure below illustrates a flowchart depicting the concept of looping. It shows the flowchart for
printing values 1, 2, 3…, 20.
In above figure the current value of A is compared with 21. If the current value of A is less than
21, steps 3 and 4 are repeated. As soon as the current value of A becomes more than 21, the path
corresponding to “NO” is followed, and the repetition process stops.
17
Chapter 2:Algoritham and Flowchart
Step 5: End
Stop
18
Chapter 2:Algoritham and Flowchart
Flowchart Algorithm
sum= sum + N
i= i+1 Step 4: Assign to sum the result value
obtained after adding the previous value of
sum and the read number N i.e. sum =sum
+N.
True Is
i <=10 Step 5: Repeat step 3 to 4 till ‘i’ is less than
equal to 10, i.e. i <= 10.
False
Display sum
Step 6: Display the value of ‘sum’.
Stop
Step 7: End
19
Chapter 2:Algoritham and Flowchart
False
Print Binary
Step 10 : End
Stop
20
Chapter 2:Algoritham and Flowchart
R = A MOD 10
Step 4: Calculate Remainder R using MOD
function i.e. R = A MOD 10
NN = NN * 10 +R
A = A/10 Step 5: Multiply New Number with 10 and
add remainder to product and now the
resultant value will become the new number
i.e NN = NN * 10 +R
Divide the input number with 10 and store
the resultant value. i.e. A = A/10
True
Is
A>0
Step 6: Repeat step 4 to 5 till A > 0.
False
Stop
Step 6 : End
21
Chapter 2:Algoritham and Flowchart
False
Step 8 : If both remainders are 0 then GCD =i.
GCD= i
Step 9 : Increment i by 1. i.e i = i+1
i=i+1
True
Step 10: Repeat step 6 to 9 till ‘i’ is less than ‘small’
Is
i<=small
Display GCD
Step 12: End
Stop
22
Chapter 2:Algoritham and Flowchart
True
Is
A==I
False
True
Step 7: Display PRIME or NOT PRIME.
Display
“A is NOT PRIME” Display
“A is PRIME”
Step 8 : End
Stop
23
Chapter 2:Algoritham and Flowchart
Fact=1
Step 3: Initialize Fact by 1 i.e. Fact=1
Fact = Fact * A
True
Is
A>1
Step 6 : Repeat Step 4 to Step 5 till A
remains greater than 1.
False
Display
“Fact” Step 7: Display the value of Fact which
will be factorial of the given number.
Stop
Step 6 : End
24
Chapter 2:Algoritham and Flowchart
-> show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show
Step 7: End
25
Chapter 2:Algoritham and Flowchart
26
Chapter 2:Algoritham and Flowchart
27
Chapter 2:Algoritham and Flowchart
Exercises
Multiple Choice Questions
1) Flowcharts uses ________ symbol for input.
a. Oval
b. Square
c. Triangle
d. None of the above
2) Flowcharts uses ________ symbol for decision making.
a. Oval
b. Rectangle
c. Diamond
d. None of the above
3) Algorithm uses which symbol for processing.
a. Rectangle
b. Oval
c. Triangle
d. None of the above
4) To repeat some fixed steps until a condition is met is called __________.
a. Decision based processing
b. Iterative processing
c. Sequential processing
d. None of the above
5) Iteration is also called __________.
a. Decision based processing
b. Looping
c. Any of the above
d. None of the above
State whether statement is true or false
1) Flowcharts cannot be used for demonstrate iterative processing. (T/F)
2) Algorithm are only used for decision bases processing demonstration. (T/F)
3) Iterative processing means to repeat a fixed steps until a condition is met. (T/F)
4) Sequential processing and Decision based processing are same. (T/F)
5) Sequential processing and Iterative based processing are same.
Practice Questions
1) Draw a flowchart to write table of 2.
2) Draw a flowchart to find area and perimeter of a rectangle.
3) Draw a flowchart to find maximum and minimum number from given three numbers.
4) Write an algorithm to find cube and square of number.
5) Write an algorithm to find even and odd number.
28
Chapter 3 : Programming with Python
Chapter 3
29
Chapter 3 : Programming with Python
The python code you write is compiled into python bytecode (0’s and 1’s), which creates file with
extension “.py”. The bytecode compilation happened internally, and almost completely hidden
from developer. Compilation is simply a translation step, and byte code is a lower-level,
and platform-independent, representation of your source code. Roughly, each of your source
statements is translated into a group of byte code instructions. This byte code translation is
performed to speed execution byte code can be run much quicker than the original source code
statements.
Python is an interpreted language since the programs written in Python are executed using an
interpreter not by a compiler. In case of the languages like C, C++ the program written are
compiled first and the source code is converted into byte code i.e. (0’s and 1’s).
Python, doesn’t need to converted into binary. You just run the program directly from the source
code.
3.1.3 Using comments
Commenting your code helps explain your thought process, and helps you and others to understand
later about your code and flow of program. This allows you to more easily find errors, to fix them,
to improve the code later on, and to reuse it in other applications as well.
Commenting is important to all kinds of projects, no matter whether they are small, medium, or
large. It is an essential part of your workflow, and is seen as good practice for developers. Without
comments, things can get confusing, real fast.
3.1.3.1 Single-Line Comments: Such comment starts with a hash character (#), and is
followed by text that contains further explanations.
# Program to add two numbers
a=5
b=7
c=a+b
print(c)
In above example, single-line comment has been written using (#) statement in the beginning. By
writing comments the user could easily understand that the program has been developed for
adding two numbers.
30
Chapter 3 : Programming with Python
3.1.3.2 Multiple-Line Comments: We can add a multiline string (triple quotes) in your code,
and place your comment inside it as explained below:
‘‘‘
Program will do :
1. Addition
2. Subtraction
’’’
a=5
b=7
c=a+b
3.1.4 Literals
Literal is actually a data value assigned to a variable or given in a constant. Like 29, 1, “Python”,
‘Yes’ etc. Literals supported by Python:
3.1.4.1 String Literals
Strings literals are formed by surrounding text between single or double quotes. Example,
‘Python’, “Hello World”, ‘We are learning Python” etc. Strings are sequence of characters and
even numeric digits are treated as characters once enclosed in quotes. Multiline string literals are
also allowed like
“This is world of programming
Python is a best to learn
We are working”
3.1.4.2 Numeric Literals
Python supports the following types of numeric literals:
A = 99 # Integer literal
B = 21.98 # Float literal
C = 5.13j # Complex literal
3.1.4.3 Boolean Literals
Booleans literals are also supported Python in which have the values in True or False. Like
X = True
Y = False
3.1.5 Constants
Constants are those items which holds the values directly and these values can’t be changed
during the execution of the program thus they are called as constants. For example,
>>> print(123)
>>>print(23.56)
>>>print(“Python World”)
31
Chapter 3 : Programming with Python
Now, during the execution of the program the value of 123 or 23.56 or “Python World” cannot
be modified. When you run the program the output will be:
3.1.6.1 Numeric
Numeric data types are used to store the numeric values in a variable. For example,
A = 99
B = 21.5
Numeric data is of various types so accordingly the numeric data types are of the following types
a) int
b) float
c) complex
32
Chapter 3 : Programming with Python
int(integer): Whole numbers are stored in variables as integer data type. For example,
a = 12
b = 16
float: The numbers having decimal are stored as float data types. For example,
a = 12.6
b = 17.9
Complex: Complex data type is used to contain complex numbers, which have real and
imaginary part. For example,
a = 5 + 6j
b = 8 + 9j
A = 12
B = float(15.6)
C = 5 + 7j
print(A)
print(B)
print(C.real, C.imag)
Output:
12
15.6
5.0 7.0
3.1.6.2 Dictionary
Python provides another composite called a dictionary, which is similar to a list in that it is a
collection of objects.
Dictionaries are Python’s implementation of a data structure that is more generally known as an
associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair
maps the key to its associated value.
You can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces
({}). A colon (:) separates each key from its associated value:
# empty dictionary
my_dict = {}
33
Chapter 3 : Programming with Python
# using dict()
my_dict = dict({1:'apple', 2:'ball'})
As you can see from above, we can also create a dictionary using the built-in dict() function.
3.1.6.3 Boolean
Python Boolean type is one of the built-in data types provided by Python, which represents one
of the two values i.e. True or False. Generally, it is used to represent the truth values of the
expressions. For example, 1==1 is True whereas 2<1 is False. For Example:
a = 10
b = 20
print(a == b)
Output:
False
3.1.6.4 Set
Set is another data type in Python and it works as mathematical sets. Very much similar to lists
and sets are denoted as curly brackets. Set is an unordered collection of data types that is
iterable, mutable and has no duplicate elements. For example:
result = set([1, 2, 4, 4, 3, 3, 3, 6, 5])
print(result)
Output:
{1, 2, 3, 4, 5, 6}
3.1.6.5 Sequences
Sequences are containers with items stored in a deterministic ordering. Each sequence data type
comes with its unique capabilities.
a) Strings:
In python, the string is a sequence of Unicode characters written inside a single or
double-quote. Python does not have any char type as in other languages (C, C++),
therefore, a single character inside the quotes will be of type str only.
34
Chapter 3 : Programming with Python
1. To declare an empty string, use str() or it can be defined using empty string inside
quotes.
Example of String in Python:
name = "LearnPython"
print(name)
Output:
LearnPython
2. Strings are immutable data types, therefore once declared, we can’t alter the string.
Though, we can reassign it to a new string.
b) Lists:
Lists are a single storage unit to store multiple data items together. It’s a mutable data
structure, therefore, once declared, it can still be altered.
A list can hold strings, numbers, lists, tuples, dictionaries, etc.
1. To declare a list, either use list() or square brackets [], containing comma-separated
values.
Example of Lists in Python:
list_1 = ["LearnPython ", "Sequences", "Tutorial"] # [all string list]
print(f'List 1: {list_1}')
35
Chapter 3 : Programming with Python
Output:
tuple 1: (‘PythonGeeks’, ‘Sequences’, ‘Tutorial’)
tuple 2: ()
tuple 3: [2021, (‘hello’, 2020), 2.0]
tuple 4: [{‘language’: ‘Python’}, [1, 2]]
In above example, the constant value 10 is assigned to the variable A and it can be verified with
the print statement that A is holding the value 10.
Example 2. Program to explain the assignment statement.
A = 10
B=A
print(B)
Output:
10
In above example, A is assigned the value of 10 and thereafter B is assigned with the value of A
i.e. so in result B will be holding the value i.e. 10 as shown in the above code.
3.1.8 Expressions
Expressions are used to obtain the desired intermediate or final results. Expressions means
combination of values, which can be constants, strings, variables and operators. Few examples of
expressions are as follows:
12 + 3
12 / 3 * (1+2)
12 / a
a*b*c
36
Chapter 3 : Programming with Python
With the above example, it is clear that expressions are combination of operands and operators
and produce desired final or intermediate results. In examples given 12, 3, 1, 2, a, b, c are
operands and ‘+’, ‘/’, ‘*’ are operators.
Expressions are written on the RHS of the assignment operator and their result value is stored in
a variable for future reference.
Example 3. Program for explaining the working of expressions.
A=2+3
B = A* 5
C=B/A
D = (A+ B) – (C + A)
print(A)
print(B)
print(C)
print(D)
Output:
5
25
5
20
In above example, the value of A is printed as 5 after evaluating the expression ‘2+3’, value of B
is 25 and assigned after evaluating the expression ‘A * 5’ since the value of A is ‘5’. Value of C
is evaluated using an expression where both operands are variables and D is evaluated using
more than one expression.
3.1.9 Arithmetic Operator
Operators are applied on the operand to obtain the desired results and there are different types of
operators. The arithmetic operators are the most basic operators used for in general calculations
or arithmetic operations. The arithmetic operators include +, -, /, *, % (modulus), ** (exponent)
etc.
Example 4. Explaining Binary Operators with Program
A=2+2
B=5*2
C = 10 / 2
D = 10 % 2
print(A)
print(B)
print(C)
print(D)
Output:
4
10
5
0
37
Chapter 3 : Programming with Python
In above example, all operators are binary operators i.e. operators are applied on two operands to
obtain the desired output. The modulus (%) operator returns the remainder value of the division
process when 10 is divided by 2 and store the remainder value in the variable D.
Example 5. Explaining Binary Operators with Program
#End of Program
Output:
Sum of 6+2 is: 8
Subtraction of 6-2 is: 4
Multiplication of 6*2 is: 12
Division of 6/2 is: 3.0
Modulus of 6%2 is: 0
Exponential of 6**2 is: 36
P = SP - CP
#End of Program
38
Chapter 3 : Programming with Python
Output:
Enter the Cost Price of the Item: 100
Enter the Selling Price of the Item: 150
Profit earned is: 50.0
#End of Program
Output:
Enter the length: 5
Enter the width: 4
Area of Rectangle is: 20
quotient = num1/num2
remainder = num1%num2
# Division operator (/) returns the quotient after dividing num1 by num2
# Modulus operator (%) returns the remainder after dividing num1 by num2
print("Quotient is : ",quotient)
print("Remainder is: ",remainder)
#End of Program
Output:
Enter the first number : 15
Enter the second number : 3
Quotient is : 5.0
Remainder is: 0
39
Chapter 3 : Programming with Python
A = (4+5)/3*2-1
B = 6/3+2*(3+2)
print("Value of A is: ",A)
print("Value of B is: ",B)
#End of Program
Output:
Value of A is: 5.0
Value of B is: 12.0
According to operator precedence the value of A and B is calculated after evaluating the
expressions as under:
Value of A is calculated as : Value of B is calculated as :
Step 1: (4+5) is evaluated and result is 9. Step 1: (3+2) is evaluated and result is 5.
Step 2: 9/3 is evaluated and result is 3. Step 2: 6/3 is evaluated and result is 2.
Step 3: 3 * 2 is calculated and result is 6. Step 3: 2*5 is calculated i.e. result is 10.
Step 4: 6-1 is calculated and result is 5. Step 4: 2 + 10 is calculated and result is 12.
40
Chapter 3 : Programming with Python
> Greater than x>y Returns True if x is greater than y; else False
< Less than x<y Returns True if x is less than y; else False
Output:
41
Chapter 3 : Programming with Python
In the above example, the value given to x is 12 and y is 15. Since x is less than y thus x>y returns
False whereas x<y returns True.
Example 11. Program to explain the relational operators ==, !=.
Output:
#End of Program
Output:
42
Chapter 3 : Programming with Python
and x > y and a >b Returns True; if both statements are True.
not not ( x>y or a>b) Opposite the result, returns False if the result is true
x=5
y = 10
a=7
b=9
C = (x>y) or (a<b)
#End of Program
Output:
43
Chapter 3 : Programming with Python
x=5
y = 10
a=7
b=9
C = (x>y) and (a<b)
#End of Program
Output:
In example above, logical operator ‘and’ is applied on the two statements (x>y), (a>b)
having output False and True respectively. Since ‘and’ logical operator produces True output only
when both statements are True and in this case only one statement is True (a<b). Thus, the output
is False.
Example 15. Program to explain the ‘not’ logical operator.
x=5
y = 10
a=7
b=9
#End of Program
Output:
In example above, logical operator ‘not’ is applied on the result of the statement ((x>y) and
(a>b)) which is False. Since ‘not’ logical operator reverses, the input provided. Thus, the final
output becomes True.
44
Chapter 3 : Programming with Python
x=5
y = 10
a=7
b=9
Output:
Result: False
Precedence order of the logical operators from highest to lowest is ‘not’, ‘and’ then ‘or’.
Precedence order could be understood from the above example. According to the precedence first
of all the ‘not(a<b)’ is evaluated and output of the statement is False. In second step, (x>y) and
(a<b) is evaluated which produces output False since (x>y) is False. Finally, the preference is
given to ‘or’ and the output of step1, step2 is combined using ‘or’ operator. The result produced is
False since output of both steps 1 and step 2 is False.
3.1.12. bitwise operators
Bitwise operators are similar to other operators but they operate on bits instead of integers or
characters etc. The smallest unit of data storage is bit which is represented in 0 and 1. Bitwise
operators works on the bits i.e. 0 and 1.
The functionality of bitwise operators is:
Symbol Function
>> Right shift
<< Left shift
& AND
| OR
^ XOR
~ One’s Compliment
45
Chapter 3 : Programming with Python
Example 17. Program to explain the working of Right Shift Operator (>>)
num1 = 9
num2 = 10
Output:
In above example, variable ‘num’ has been initialized to ‘9’. Assume computer is using eight digits
to represent a binary number then number ‘9’ is represented as 0000 1001. After applying right
shift operator on digit ‘9’ the number in binary form becomes 0000 0100 i.e. 4 which is the
new_num1. Understand that the digits are shifted to right by 1 position i.e. 1 bit is lost and the
empty position created on the left is filled by ‘0’ digit.
Similarly, variable ‘num2’ has been initialized to ‘10’ and using eight digits to represent a binary
number the number ‘10’ is represented as 0000 1010. After applying right shift operator on digit
‘10’ the number in binary form becomes 0000 0001 i.e. 1 which is the new_num2. Understand that
the digits are shifted to right by 3 positions i.e. 3 bits are lost and the empty position created on
the left is filled by ‘0’ digit.
Example 18. Program to explain the working of Left Shift Operator (<<)
num1 = 10
num2 = 5
Output:
46
Chapter 3 : Programming with Python
In above example, variable ‘num1’ has been initialized to ‘10’. Assume computer is using eight
digits to represent a binary number then number ‘10’ is represented as 0000 1010. After applying
right shift operator on digit ‘10’ the number in binary form becomes 0001 0100 i.e. 20 which
new_num1. Understand that the digits are shifted to left by 1 position i.e. 1 bit is lost and the empty
position created on the right is filled by ‘0’ digit.
Similarly, variable ‘num2’ has been initialized to ‘5’ and using eight digits to represent a binary
number then number ‘5’ is represented as 0000 0101. After applying right shift operator on digit
‘5’ the number in binary form becomes 0001 0100 i.e. 20 which new_num2. Understand that the
digits are shifted to left by 2 positions i.e. 2 bits are lost and the empty position created on the right
is filled by ‘0’ digit.
Example 19. Program to explain the working of Bitwise AND Operator (&)
num1 = 8
num2 = 7
num3 = 10
res1 = num1 & num2
res2 = num1 & num3
print("Result1 : ",res1)
print("Result2 : ",res2)
Output:
Result1 : 0
Result2 : 8
In above example, variables ‘num1’, ‘num2’, ‘num3’ has been initialized to ‘8’, ‘7’ and ‘10’
respectively.
Now in binary format:
num1 = 8 = 0000 1000
num2 = 7 = 0000 0111
res1 = 0 = 0000 0000 (num1 & num2)
Bitwise AND (&) Operator gives output 1 if both the corresponding bits are 1, otherwise 0. So,
we can notice that in variable ‘res1’ all bits are 0 since none of the corresponding bits are 1 in
variable ‘num1’ and ‘num2’.
num1 = 8 = 0000 1000
num3 = 10 = 0000 1010
res2 = 8 = 0000 1000 (num1 & num3)
Bitwise AND (&) Operator gives output 1 if both the corresponding bits are 1, otherwise 0. So,
we can notice that in variable ‘res2’ 1 is present where the corresponding bit is also 1 in variable
‘num1’ and ‘num2’.
47
Chapter 3 : Programming with Python
num1 = 8
num2 = 7
num3 = 10
res1 = num1 | num2
res2 = num1 | num3
print("Result1 : ",res1)
print("Result2 : ",res2)
Output:
Result1 : 15
Result2 : 10
In above example, variables ‘num1’, ‘num2’, ‘num3’ has been initialized to ‘8’, ‘7’ and
‘10’ respectively.
Now in binary format:
num1 = 8 = 0000 1000
num2 = 7 = 0000 0111
res1 = 15 = 0000 1111 (num1 & num2)
Bitwise OR ( | ) Operator gives output 1 if any of the corresponding bits are 1, otherwise 0. So,
we can notice that in variable ‘res1’, 1 is present where any of the corresponding bit is 1 in
variable ‘num1’ and ‘num2’.
num1 = 8 = 0000 1000
num3 = 10 = 0000 1010
res2 = 10 = 0000 1010 (num1 & num3)
Bitwise OR ( | ) Operator gives output 1 if any of the corresponding bits are 1, otherwise 0. So,
we can notice that in variable ‘res2’, 1 is present where any of the corresponding bit is 1 in
variable ‘num1’ and ‘num2’.
num1 = 8
num2 = 7
num3 = 10
print("Result1 : ",res1)
print("Result2 : ",res2)
48
Chapter 3 : Programming with Python
Output:
Result1 : 15
Result2 : 2
In above example, variables ‘num1’, ‘num2’, ‘num3’ has been initialized to ‘8’, ‘7’ and ‘10’
respectively.
Now in binary format:
num1 = 8 = 0000 1000
num2 = 7 = 0000 0111
res1 = 15 = 0000 1111 (num1 & num2)
Bitwise XOR ( ^ ) Operator gives output 1 if one of the corresponding bits is 1 and other is 0,
otherwise it gives output as 0. So, we can notice that in variable ‘res1’, 1 is present where any of
the corresponding bit is 1 in variable ‘num1’ and ‘num2’.
num1 = 8 = 0000 1000
num3 = 10 = 0000 1010
res2 = 2 = 0000 0010 (num1 & num3)
Bitwise XOR ( ^ ) Operator gives output 1 if one of the corresponding bits is 1 and other is 0,
otherwise it gives output as 0. So, we can notice that in variable ‘res2’, 1 is present where any of
the corresponding bit is 1 in variable ‘num1’ and ‘num2’.
Example 22. Program to explain the working of Bitwise One’s complement Operator ( ~ ).
num1 = 8
num2 = 7
res1 = ~num1
res2 = ~num2
print("Result1 : ",res1)
print("Result2 : ",res2)
Output:
Result1 : -9
Result2 : -8
In above example, variables ‘num1’, ‘num2’, ‘num3’ has been initialized to ‘8’, ‘7’ and ‘10’
respectively. Bitwise One’s complement Operator ( ~ ) is a unary operator.
Now in binary format:
num1 = 8 = 0000 1000
res1 = -9 = - 0000 1001 (~num1)
num2 = 7 = 0000 0111
res1 = -8 = - 0000 1000 (~num2)
49
Chapter 3 : Programming with Python
False
Condition Statement
True
Statement
3.1.8.1 if STATEMENT
The if statement test a condition and when the condition is ‘true’ a statement or a set of statements
are executed and the actions are performed as per the instructions given in the statements otherwise
the statements attached with the if statement are not executed.
Syntax of the if statement:
if (expression) :
statement
Example 23. Program to explain the working of if statement.
if(name =="Kapil"):
print("You entered name Kapil")
50
Chapter 3 : Programming with Python
#End of Program
Output:
In the above example, user entered marks as 85 and the if condition is checked since marks are
greater than 80 so the condition becomes ‘true’ and the print statement is executed and output
‘Grade A’ is printed on the screen.
Then, the second input was asked and user entered name as Prashant and the if condition is
checked since name entered is not ‘Kapil’ so the condition becomes ‘false’ and the print
statement is not executed.
So, from the execution of the program we can conclude that the statement attached with if
statement is executed only when if condition is ‘true’ otherwise they are not executed and behave
like a comment.
Example 24. Program to check the correct input entered by the user.
if(num1>1):
print("Correct Number")
# End of Program
Output:
Since the user entered number value as 5, so the if statement becomes true and the statement
attached to it gets printed.
51
Chapter 3 : Programming with Python
if (marks >=50):
print("Student is Pass")
else :
print("Student is Fail")
# End of Program
Output:
Student is Fail
In above example, user has been asked for the input and user entered marks as 47 since the
marks are less than 50 so the if condition becomes false. Consequently, the block attached with if
is not executed and the control is transferred to statements attached to the else block and are
executed.
if(num1>num2):
print("FIRST number is Bigger")
else:
print("SECOND number is Bigger")
# End of Program
Output:
52
Chapter 3 : Programming with Python
if(num1%2==0):
print("Number is EVEN")
else:
print("Number is ODD")
# End of Program
Output:
Example 28. Program to display a menu and calculate area of a square and volume of a
cube.
# End of Program
Output:
53
Chapter 3 : Programming with Python
if(marks>=75):
print("Student got DISTINCTION")
elif(marks <75 and marks >=50):
print("Student is PASS")
else:
print("Student is FAIL")
#End of Program
Output:
In above example, user entered marks as 67. The first if condition becomes false since marks are
less than 75 so the print statement in block1 is not executed and the control is transferred to elif
condition which become true since the marks lies in the range and the block2 print statement is
executed.
if(leaves < 5 ):
sal= BP + OA
54
Chapter 3 : Programming with Python
else:
sal = 0
# End of Program
Output:
if (marks>= 90):
print("Grade A")
# End of Program
Output:
55
Chapter 3 : Programming with Python
i =i +1
False
Flowchart for the printing of table of 2 where a set of statements are repeated again and again until
the value of i is greater than 10. We can notice that the flow of program is based on the result of
the condition.
56
Chapter 3 : Programming with Python
# Print table of 2
i=1
while(i<=10):
T= 2*i
print("2 *",i," = ",T)
i=i+1
#End of Program
Output:
2*1=2
2*2=4
2*3=6
2*4=8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
In above program, value of loop index ‘i’ in this case is tested and statements attached to the
while loop are executed till the value of i remains less than equal to 10 i.e. till condition remains
‘true’. When the value of ‘i’ becomes greater than 10 the loop terminates and program ends.
while loop is an entry controlled loop i.e. entry into the loop for executing the statements are
allowed only when entry condition is true.
3.1.12 for STATEMENT
for statement is another way in Python for iteration or looping through which a set of statements
are repeatedly executed till the condition remains ‘true’.
Syntax of for statement is:
for <variable> in [sequence]:
block of statements
In the case of for loop the variable i.e. loop index takes the value of the elements present in a list
one by one and executes the statements attached with the loop till the last element and loop
terminates after completing the process for the last element in the list.
57
Chapter 3 : Programming with Python
In the above example, the loop index ‘i’ took the value of the elements present in the list one by
one in sequence and the statement attached to the for loop are executed number of times equal to
the number of elements present in the list. In above case, since the number of elements in the list
are four i.e. 5, 7, 9, and 11. So the statement attached is executed four times.
for i in ['cat','dog','horse','lion','tiger']:
print("Hello World")
#End of Program
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
In this case, loop index ‘i’ will take values of the elements of list which are string values and the
statement attached with the loop is executed 5 times since the number of elements in the list are 5.
With this example, it is clear that the index value can be either string or integer and loop iteration
depends on the number of elements in the list.
58
Chapter 3 : Programming with Python
Example 35. Program to explain the working of for statement using range () function.
Using range(n) function the for loop can be implemented and index variable ‘i’ automatically
initialized by 0 and takes value ranging from 0,1,2,3,4, …, n-1 where n is the upper limit. Index
variable is incremented by 1 till it reached the upper limit decremented by 1. In this case the n is 5
so ‘i’ is initialized to 0 and upper limit is 5 so the loop is executed 5 times for the value 0,1,2,3,4
and same is printed as output.
Example 36. Program to explain the working of for statement using range() function.
for i in range(3,7):
print("value of i: ", i)
#End of Program
Output:
value of i: 3
value of i: 4
value of i: 5
value of i: 6
In this case, the range(a,n) function takes two parameters, with first value of parameter index
variable ‘i’ is initialized and the second parameter is the upper limit. Index variable is incremented
by 1 till it reach the upper limit decremented by 1. In this example, ‘i’ is initialized to 3 and ‘i’
takes the values from 3,4,5,6 i.e. since 7 is the upper limit and the statements attached with the
loop are executed 4 times.
Example 37. Program to explain the working of for statement using range() function.
for i in range(3,10,2):
print("value of i: ", i)
#End of Program
59
Chapter 3 : Programming with Python
Output:
value of i: 3
value of i: 5
value of i: 7
value of i: 9
In this case, the range(a,n,b) function takes three parameters, with first value of parameter index
variable ‘i’ of the for loop is initialized, the second parameter is the upper limit and third parameter
is the incremental value. Index variable is incremented by the value ‘b’ till it reach the upper limit
decremented by 1. In this example, ‘i’ is initialized to 3 and ‘i’ takes the values from 3,5,7,9 since
index value ‘i’ is incremented by 2 and the statements attached with the loop are executed 4 times.
Example 38. Program to count numbers from 1 to 5 using while and for statements.
#End of Program
Output : Output :
1 1
2 2
3 3
4 4
5 5
num = int(input("Enter the number: ")) num = int(input("Enter the number: "))
60
Chapter 3 : Programming with Python
5*1=5 5*1=5
5 * 2 = 10 5 * 2 = 10
5 * 3 = 15 5 * 3 = 15
5 * 4 = 20 5 * 4 = 20
5 * 5 = 25 5 * 5 = 25
5 * 6 = 30 5 * 6 = 30
5 * 7 = 35 5 * 7 = 35
5 * 8 = 40 5 * 8 = 40
5 * 9 = 45 5 * 9 = 45
5 * 10 = 50 5 * 10 = 50
61
Chapter 3 : Programming with Python
Example 41. Write a program to read 5 numbers from keyboard and find their sum.
i=1 total=0
total=0
for i in range(5):
while(i<=5): num = int(input("Enter the number: "))
num = int(input("Enter the number: ")) total= total+num
total= total+num
i=i+1 print("Sum is : ", total)
i=1 num=1
num=1
for i in range(1,11):
while(i<=10): print(num, end =", ")
print(num, end=", ") num= num+2
num= num+2
i=i+1 #End of Program
#End of Program
Output : Output :
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
62
Chapter 3 : Programming with Python
Palindrome is a word that reads the same backwards as forwards, e.g. madam, refer, malayalam.
#using while statement #using for statement
i=1 num=1
num=1
for i in range(1,11):
while(i<=10): print(num, end =", ")
print(num, end=", ") num= num+2
num= num+2
i=i+1 #End of Program
#End of Program
Output : Output :
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
*
**
***
****
*****
#using while statement #using for statement
#End of Program
63
Chapter 3 : Programming with Python
64
Chapter 3 : Programming with Python
#End of Program
Output : Output :
Enter a number greater than 1: = 7 Enter a number greater than 1: = 97
Number is PRIME Number is PRIME
65
Chapter 3 : Programming with Python
66
Chapter 3 : Programming with Python
In above example, it can be seen that when the alphabets ‘o’ and ‘y’ occurs in ‘Python’ the if
statement becomes true and the continue statement is executed due to which statement of ‘print’ is
not executed or ignored and control is being transferred back to start of the loop. Thus, alphabet ‘o’
and ‘y’ is not printed in output.
67
Chapter 3 : Programming with Python
Example 49. Write a program to enter the marks of five subjects of a student and if the
marks in any subject is less than 50 don’t print the marks.
68
Chapter 3 : Programming with Python
Example 50. Write a program to if the marks entered is greater than 50 then display
‘Great’ and if marks are less than 50 then display ‘Do Hard work’.
In above example, user gave input marks as 50 since pass statement is attached with that
condition and no code is written in this block so nothing appears in the output.
3.1.15.1 assert STATEMENT
In Python assert statement is used to check the whether a condition or a logical expression is true
or false. The assert statement is very useful in tracking the errors and terminating the program
on occurrence of an error.
Syntax:
assert (condtion)
Example 51. Write a program for explaining working of assert statement.
In above example, in case the user enters any other password than ‘PYTHON’ the error message
occurs and program is not executed further.
69
Chapter 3 : Programming with Python
Exercises
Multiple Choice Questions
1) What are the features of Python?
a. Open Source
b. Portable
c. Have extensive library
d. All of the above
2) Comments in Python starts with the character:
a. %
b. &
c. *
d. #
3) Multiline comments in Python starts with:
a. {
b. ]
c. ‘‘‘
d. ^
4) Which of the following is True in case of compiler?
a. Compiler converts bits i.e. 0’s and 1’s into High level language
b. Compiler translates source code into machine language.
c. Compiler translates low level language to high level language
d. None of the above
5) Python is an interpreted language because:
a. Python programs are first compiled then executed
b. Python program needs no compilation and executed directly
c. Python programs need not to be converted into machine language.
d. None of the above.
6) Which of the following is not numeric literal?
a. “123”
b. 125.25
c. 123
d. None of the above
7) Which of the following a string literal?
a. “abc’
b. “Python”
c. ‘Python”
d. None of the above
8) What will be value of A and B in the given expression:
A = (2+3) * 2 + 6
B = (6/2) + 3 * 2
a. A = 16, B = 12
b. A = 40, B = 12
c. A = 16, B = 9
d. None of the above
70
Chapter 3 : Programming with Python
71
Chapter 3 : Programming with Python
Lab Excercises
1) Write a program to calculate the multiplication and sum of two numbers.
2) Write a program to display characters from a string that are present at an even index
number.
3) Write a program to generate the following output using for and while statement.
1
12
123
1234
12345
4) Write a program to generate the following output using for and while statement.
1
22
333
4444
55555
5) Write a program to generate the following output using for and while statement.
54321
4321
321
21
1
6) Write a program to display first ten prime numbers using for and while statement.
7) Write a program to find factorial of number using for and while statement.
8) Write a program to count the total number of digits in a number using for and while
statement.
9) Write a program to display Fibonacci series up to 10 terms
10) Write a program to calculate the cube of all numbers from 1 to a given number
72
Chapter 4 : String Handling and Sequence Types
Chapter 4
73
Chapter 4 : String Handling and Sequence Types
String indexing in Python is zero-based: the first character in the string has index 0, the next has
index 1, and so on. The index of the last character will be the length of the string minus one.
For example, a schematic diagram of the indices of the string 'SCHOOL' would look like this:
S C H O O L
0 1 2 3 4 5
In case a situation arise where we like to access the last element of a string and we are not aware
of the length of the string then in such case negative indexing is used as follows:
74
Chapter 4 : String Handling and Sequence Types
In above example with using negative index we are able to access the last element and second last
element easily without requiring any knowledge of length of the string. Negative indexing is as
under:
S C H O O L
-6 -5 -4 -3 -2 -1
String Example 2: Negative Indexing
75
Chapter 4 : String Handling and Sequence Types
76
Chapter 4 : String Handling and Sequence Types
my_string1 = “Middle”
my_string2 = “School”
final_string = my_string1 + my_string2
String Example 4: Concatenation of String
77
Chapter 4 : String Handling and Sequence Types
We can combine more than two strings which are explained with the program as under :
78
Chapter 4 : String Handling and Sequence Types
‘<’ This checks if the string on its left is smaller than that on its right
‘<=’ This checks if the string on its left is smaller than or equal to that on its right
‘>’ This checks if the string on its left is greater than that on its right
‘>=’ This checks if the string on its left is greater than or equal to that on its right
Comparison of strings is performed character by character comparison rules for ASCII and
Unicode. ASCII values of numbers 0 to 9 are from 48 to 57, uppercase (A to Z) are from 65 to 90,
and lowercase (a to z) are from 97 to 122.
79
Chapter 4 : String Handling and Sequence Types
Here we write command and to execute the command just press enter key and your
command will be interpreted. For coding in Python you must know the basics of the console used
in Python. The primary prompt of the python console is the three greater than symbols “>>>”
80
Chapter 4 : String Handling and Sequence Types
You are free to write the next command on the shell only when after executing the first
command these prompts have appeared. The Python Console accepts command in Python which
you write after the prompt.
User enters the values in the Console and that value is then used in the program as it was required.
To take input from the user we make use of a built-in function input().
String Figure 12: Input from user using build-in function input()
Though it is not necessary to pass arguments in the print() function, it requires an empty
parenthesis at the end that tells python to execute the function rather calling it by name. Now, let’s
explore the optional arguments that can be used with the print() function.
81
Chapter 4 : String Handling and Sequence Types
82
Chapter 4 : String Handling and Sequence Types
4.2.2. tuple
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. Tuples are
also used to store multiple items in a single variable.
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 (integer, float, list, etc.).
Tuple is created using parenthesis () as explained below:
83
Chapter 4 : String Handling and Sequence Types
4.2.3. Dictionary
Python dictionary is an unordered collection of items. Each item of a dictionary has
a key/value pair.
Creating a dictionary
Creating a dictionary is as simple as placing items inside curly braces {} separated by commas.
An item has a key and a corresponding value that is expressed as a pair (key: value).While the
values can be of any data type and can repeat, keys must be of immutable type ( with immutable
elements) and must be unique.
84
Chapter 4 : String Handling and Sequence Types
85
Chapter 4 : String Handling and Sequence Types
86
Chapter 4 : String Handling and Sequence Types
Output:
my_list = [1, 2, 7, 4, 5] [7, 4, 5]
print(my_list[2:])
87
Chapter 4 : String Handling and Sequence Types
Output:
my_list = [1, 3, 7, 4, 5] [1, 3]
print(my_list[:2])
88
Chapter 4 : String Handling and Sequence Types
Sequence Example 10: get all the items from one position to another position
Slicing in Tuple
# Accessing tuple elements using slicing Output:
my_tuple = ('c','o','m','p','u','t','e','r',’s’) ('o', 'm', 'p')
('p','r','o','g','r','a','m','i','z') ('c', 'o')
# elements 2nd to 4th ('r', 's')
print(my_tuple[1:4]) ('c', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's')
89
Chapter 4 : String Handling and Sequence Types
Two list can be combined using set () and list() functions so that the final list contains only the
unique value.
list_1 = [5, 'P'] Output:
list_2 = [5,8,0,3] [0, 3, 5, 8, 'P']
list_joined = list(set(list_1 +
list_2))
print(list_joined)
Sequence Example 14: Concatenation of two lists using set()
In above example, the set () selects the unique values and list () converts the set into list.We can
concatenate a list to another list or simply merge two lists using extend() function.
list_1 = [5, 'a'] Output:
list_2 = [7, 5, 5] [7, 5, 5, 5, 'a']
list_2.extend(list_1)
print(list_2)
Sequence Example 15: Concatenation of two lists using extend()
Concatenation of Tuple.
Concatenation of two separate tuples can be done using ‘+’ operation.Concatenation of two
tuples.
my_tuple_1 = (22, 14, 0, 67, 98, 11) Output:
my_tuple_2 = (11, 96, 0,1, 23, 44) The first tuple is:
print("The first tuple is: ") (22, 14, 0, 67, 98, 11)
print(my_tuple_1) The second tuple is:
print("The second tuple is: ") (11, 96, 0, 1, 23, 44)
print(my_tuple_2) The tuple after concatenation is:
my_result = my_tuple_1 + my_tuple_2
print("The tuple after concatenation is: " )
print(my_result)
(22, 14, 0, 67, 98, 11, 11, 96, 0, 1, 23, 44)
Sequence Example 16: Concatenation of two tuples.
Concatenation of Dictionary.
Concatenation of two separate dictionaries can be done using ‘|’ operation.Concatenation of
dictionaries using ‘|’ operator:
90
Chapter 4 : String Handling and Sequence Types
Objects in Python
In Python, everything is treated as an object. Every object has these three attributes:
Identity – This refers to the address that the object refers to in the computer’s memory.
Type – This refers to the kind of object that is created. For example- integer, list, string
etc.
Value – This refers to the value stored by the object. For example – List=[1,2,3] would
hold the numbers 1,2 and 3
Identity and Type cannot be changed once it’s created, values can be changed for Mutable objects.
Explanation of mutable objects using List.
Lists are mutable, meaning their elements can be changed unlike We can use the assignment
operator = to change an item or a range of items.
91
Chapter 4 : String Handling and Sequence Types
92
Chapter 4 : String Handling and Sequence Types
93
Chapter 4 : String Handling and Sequence Types
94
Chapter 4 : String Handling and Sequence Types
# Output: {}
print(cubes)
# delete the dictionary itself
del cubes
# Throws Error
print(cubes)
Example 21: Removing elements from Dictionary
95
Chapter 4 : String Handling and Sequence Types
# Mean of a Tuple 1
odd = (1,3,5,7,9) End of Operation on Tuple
print(statistics.mean(odd))
print(" End of Operation on Tuple ")
Example 23: Finding Mean in List and Tuple
96
Chapter 4 : String Handling and Sequence Types
Exercises
Multiple Choice Questions
1)A string is series of _______
a. characters
b. integers
c. double
d. float
2)String index starts from :
a. 1
b. 0
c. -1
d. None of the above
3)Slicing in string is used to extract :
a. part of the string
b. complete string
c. used to empty string
d. None of the above
4)In negative indexing the last character of a string can be accessed using index value
a. Length of string
b. 0
c. -1
d. None of the above
5)Which of the following are sequence data type:
a. List
b. Tuple
c. Dictionary
d. All of the above
a. ‘+’
b. ‘-’
c. ‘=’
d. ‘*’
97
Chapter 4 : String Handling and Sequence Types
a. Duplicate elements
b. Presence of an element
c. Absence of an element
d. None of the above
a. [ ]
b. ()
c. {}
d. Any of the above
a. add an item
b. delete an item
c. merge items
d. None of the above
10)Concatenation operator is :
a. ‘+’
b. ‘-’
c. ‘&’
d. ‘%’
98
Chapter 4 : String Handling and Sequence Types
LAB exercises
1. Write a program to display index value against each character in string.
2. Write a program to maximize frequency in a string..
3. Write a program to split / join string
4. Write a program to find length of list.
5. Write a program for reversing a list.
6. Write a program for finding largest and smallest number in a list.
7. Write a program to print all odd numbers in tuple.
8. Write a program to join two tuples if there fist element is same.
9. Write a program to explain min(), max() and mean() functions using List.
10. Write a program to explain mutability using List, Tuple and Dictionary.
99
Chapter 5 : Functions
Chapter 5
Functions
5.1 Top-down Approach of Problem Solving
Top down analysis is a problem solving mechanism whereby a given problem is successively
broken down into smaller and smaller sub-problems or operations until a set of easily solvable (by
computer) sub-problems is arrived at.
Using the top-down approach. It is possible to achieve a very detailed breakdown, however, it
should be remembered that our aim is to identify easily solvable sub-problems.
The top-down approach is used in the system analysis and design process.
The top-down approach, starting at the general levels to gain an understanding of the system and
gradually moving down to levels of greater detail is done in the analysis stage. In the process of
moving from top to bottom, each component is exploded into more and more details.
Thus, the problem at hand is analysed or broken down into major components, each of which is
again broken down if necessary.
The top-down process involves working from the most general down to the most specific.
The design of modules is reflected in hierarchy charts such as the one shown in Figure below:
The purpose of procedure Main is to coordinate the three branch operations e.g. Get, Process, and
Put routines. These three routines communicate only through Main. Similarly, Sub1 and Sub2 can
communicate only through the Process routine.
Advantages of Top-down Approach
The advantages of the top-down approach are as follows:
This approach allows a programmer to remain “on top of” a problem and view the developing
solution in context. The solution always proceeds from the highest level downwards.
By dividing the problem into a number of sub-problems, it is easier to share problem development.
For example, one person may solve one part of the problem and the other person may solve another
part of the problem.
100
Chapter 5 : Functions
Since debugging time grows quickly when the program is longer, it will be to our advantage to
debug a long program divided into a number of smaller segments or parts rather than one big
chunk. The top-down development process specifies a solution in terms of a group of smaller,
individual subtasks. These subtasks thus become the ideal units of the program for testing and
debugging.
101
Chapter 5 : Functions
More experienced programmers can be given a more complex module to write, and the
junior programmers can work on simpler modules. Modules can be tested independently,
thereby shortening the time taken to get the whole project working.
If a programmer leaves a project, it is easier for someone else to take over a set of self-
contained modules.
A large project becomes easier to monitor as well as to control.
102
Chapter 5 : Functions
Function_name(arguments)
The function name has to be followed by parentheses. If there are any required
arguments, they have to be passed in the parentheses. If the function doesn't take in any
arguments, you still need the parentheses.
To call the function from the example above, which doesn't take in any arguments, do the
following.
hello_world_func()
#Output
#hello world
def hello_to_you(name):
print("Hello " + name)
103
Chapter 5 : Functions
Parameters are a named placeholder that pass information into functions. They act as variables
that are defined locally in the function's definition line.
In the example above, there is one parameter, name.We can pass more than one parameters in the
function as shown below :
def hello_to_you(name):
print(f"Hello {name}")
hello_to_you("Timmy")
#Output
# Hello Timmy
The function can be called many times, passing in different values each time.
def hello_to_you(name):
print(f"Hello {name}")
hello_to_you("Timmy")
hello_to_you("Kristy")
hello_to_you("Jackie")
hello_to_you("Liv")
#Output:
#"Hello Timmy"
#"Hello Kristy"
Code 5: Function can be called many times
#"Hello Jackie"
5.4 Local Variables #"Hello Liv"
Python local variable plays an important role in the entire python programming language as it is
used for any scope definition and manipulation. A local variable in Python is always declared
within a specific scope like it is mostly present within any function’s body where other members
can access it. Therefore, it is very difficult and rare that local variables will be present outside the
scope or outside the function. If a variable is present outside the scope, it is considered a global
variable, and all the members become unreachable to a local variable.
5.4.1. Syntax of Local Variable in Python
The syntax flow for the local variable declaration in function for Python includes the following
representation:
Function_declaration ():
Variable= “var_assign”
Logic statement ()
Function_declaration () //calling of the function
Syntax 2: Declaration of the Local Variable
104
Chapter 5 : Functions
def dinner_prep():
dine = 'Pizza_with_extra_topping'
print('Please have a pizza with extra_topping', dine)
dinner_prep()
Output
Please have a pizza with extra_topping Pizza_with_extra_topping
A function is declared, and then the variable is taken, which creates the memory, and on top of it,
a variable is assigned, which makes it a local variable after which the function is called and then
the following logic statement is called to perform a lot of manipulation and work.
5.4.2 How Local Variable Works in python?
This program demonstrates the local variable when defined within the function where the variable
is declared within function and then a statement followed by the function calling as shown in the
output below.
A local variable in Python plays a significant role in the sense it helps in making the function and
the code snippet access to other member variables with manipulation simple and easy. In addition,
local variables help in making the entire workflow with the global variable compatible and less
complex. Also, the nested functions or statements play a very nice blend with local variables.
5.5 The Return Statement
A return statement is used to end the execution of the function call and “returns” the result (value
of the expression following the return keyword) to the caller. The statements after the return
statements are not executed. If the return statement is without any expression, then the special
value None is returned. A return statement is overall used to invoke a function so that the passed
statements can be executed.
Note: Return statement can not be used outside the function.
defun():
statements
return [expression]
Syntax 3: The return statement
print(add(5,7))
Output
12
Code 6: Python Program to Demonstrate Return statement
105
Chapter 5 : Functions
greet("Rajesh")
greet("Kapil", "How do you do?")
Example 27: Example to explain default arguments value.
In this function, the parameter name does not have a default value and is required (mandatory)
during a call.
On the other hand, the parameter msg has a default value of " Greeting of Day!". So, it is optional
during a call. If a value is provided, it will overwrite the default value.
Any number of arguments in a function can have a default value. But once we have a default
argument, all the arguments to its right must also have default values.
5.7 keyword arguments
When we call a function with some values, these values get assigned to the arguments according
to their position.
For example, in the above function greet(), when we called it as greet("Kapil", "How do you do?"),
the value "Kapil" gets assigned to the argument name and similarly "How do you do?" to msg.
Python allows functions to be called using keyword arguments. When we call functions in this
way, the order (position) of the arguments can be changed. Following calls to the above function are
all valid and produce the same result.
def greet(name, msg="Greeting of Day!"): Output:
""" Hello Rajesh, Greeting of Day!
This function greets to Hello Kapil, How do you do?
the person with the
provided message.
106
Chapter 5 : Functions
5.9.1. input()
The input() function reads a line from the input (usually from the user), converts the line into a
string by removing the trailing newline, and returns it.
If EOF is read, it raises an EOFError exception.
inputString = input() # get input from user Output:
Yes we are learning
print('The inputted string is:', inputString) The inputted string is: Yes we are
learning
107
Chapter 5 : Functions
5.9.2. eval()
The eval() method parses the expression passed to this method and runs python expression
(code) within the program.
number = 10 Output:
# eval performs the multiplication passed as argument 100
square_number = eval('number * number')
print(square_number)
Example 32: Explanation eval() function
108
Chapter 5 : Functions
objects - object to the printed. * indicates that there may be more than one object
sep - objects are separated by sep. Default value: ' '
end - end is printed at last
file - must be an object with write(string) method. If omitted, sys.stdout will be used which
prints objects on the screen.
flush - If True, the stream is forcibly flushed. Default value: False
print("Computer") Output:
a = 10 Computer
# Two objects are passed a = 10
print("a =", a) a = 10 = b
b=a
# Three objects are passed
print('a =', a, '= b')
a = 10 Output:
print("a =", a, sep='******', end='\n\n\n') a =******10
print("a =", a, sep='+++++', end='\n\n\n')
a =+++++10
Example 36: print() with separator and end parameters
109
Chapter 5 : Functions
count() Parameters : count() method only requires a single parameter for execution. However, it
also has two optional parameters:
substring - string whose count is to be found.
start (Optional) - starting index within the string where search starts.
end (Optional) - ending index within the string where search ends.
Note: Index in Python starts from 0, not 1. count() method returns the number of occurrences of
the substring in the given string.
find() function
The find() method returns the index of first occurrence of the substring (if found). If not found, it returns -1.
print(message.find('good'))
find() Syntax:
str.find(sub[, start[, end]] )
Syntax 5: The syntax of the find() method
find() Parameters
The find() method takes maximum of three parameters:
sub - It is the substring to be searched in the str string.
start and end (optional) - The range str[start:end] within which substring is searched.
find() Return Value
110
Chapter 5 : Functions
rfind() Parameters
rfind() method takes a maximum of three parameters:
sub - It's the substring to be searched in the str string.
start and end (optional) - substring is searched within str[start:end]
Return Value from rfind()
rfind() method returns an integer value.
If substring exists inside the string, it returns the highest index where substring is found.
If substring doesn't exist inside the string, it returns -1.
The capitalize() method converts the first character of a string to an uppercase letter and
all other alphabets to lowercase.
The islower() method returns True if all alphabets in a string are lowercase alphabets. If
the string contains at least one uppercase alphabet, it returns False.
The upper() method converts all lowercase characters in a string into uppercase characters
and returns it.
The title() method returns a string with first letter of each word capitalized; a title cased
string.
111
Chapter 5 : Functions
The swapcase() method returns the string by converting all the characters to their opposite
letter case( uppercase to lowercase and vice versa).
Example 40: Various string functions capitalize(), title(), lower(), upper() and swapcase()
112
Chapter 5 : Functions
print("---Check String3---")
print(my_string3.islower())
print(my_string3.isupper())
print(my_string3.istitle())
Output:
Computer programming is good
---Check String1---
True
False
False
---Check String2---
False
True
False
---Check String3---
False
False
True
The strip() method returns a copy of the string by removing both the leading and the trailing
characters (based on the string argument passed).
message = ' This is Python ' Output:
# remove leading and trailing whitespaces Message: This is Python
print('Message:', message.strip())
Example 43: Explanation for strip() function usage.
113
Chapter 5 : Functions
numeric Functions:
The min() function returns the smallest item in an iterable. It can also be used to find the smallest
item between two or more parameters.
The max() function returns the largest item in an iterable. It can also be used to find the largest
item between two or more parameters.
The pow() method computes the power of a number by raising the first argument to the second
argument
number = [10, 20, 8, 5, 100, 600] Output:
for i in number: 100
print(pow(i,2)) 400
64
25
10000
360000
Example 45: Explanation of pow().
Python has a module named datetime to work with dates and times. Let's create a few simple
programs related to date and time before we dig deeper.
import datetime Output:
datetime_object = datetime.datetime.now() 2022-09-25 12:03:39.574930
print(datetime_object)
Example 46: Code to get Current Date and Time
114
Chapter 5 : Functions
5.9.7 recursion
Recursion is the process of defining something in terms of itself.
In Python, we know that a 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.
115
Chapter 5 : Functions
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(5) # 1st call with 5
5 * factorial(4) # 2nd call with 4
5 * 4 * factorial(3) # 3rd call with 3
5 * 4 * 3 *factorial(2) # 4th call with 2
5 * 4 *3*2* factorial(1) # 5th call with 1
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.
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.
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.
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.
116
Chapter 5 : Functions
What is namespace:
A namespace is a system that has a unique name for each and every object in Python. An
object might be a variable or a method. Python itself maintains a namespace in the form of a Python
dictionary. Let’s go through an example, a directory-file system structure in computers. Needless
to say, that one can have multiple directories having a file with the same name inside every
directory. But one can get directed to the file, one wishes, just by specifying the absolute path to
the file. Real-time example, the role of a namespace is like a surname. One might not find a single
“Alice” in the class there might be multiple “Alice” but when you particularly ask for “Alice Lee”
or “Alice Clark” (with a surname), there will be only one (time being don’t think of both first name
and surname are same for multiple students)
117
Chapter 5 : Functions
On similar lines, the Python interpreter understands what exact method or variable one is
trying to point to in the code, depending upon the namespace. So, the division of the word itself
gives a little more information. Its Name (which means name, a unique identifier) + Space(which
talks something related to scope). Here, a name might be of any Python method or variable and
space depends upon the location from where is trying to access a variable or a method.
Types of namespaces :
When Python interpreter runs solely without any user-defined modules, methods, classes, etc.
Some functions like print(), id() are always present, these are built-in namespaces. When a user
creates a module, a global namespace gets created, later the creation of local functions creates the
local namespace. The built-in namespace encompasses the global namespace and the global
namespace encompasses the local namespace.
118
Chapter 5 : Functions
Importing Module
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.
119
Chapter 5 : Functions
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.
# This module shows the effect of
# multiple imports and reload
print("This code got executed")
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.
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:
120
Chapter 5 : Functions
Exercise
Multiple choice questions
11) In Top-down approach a problem _______
a. Combined to form bigger modules
b. Divided into smaller modules
c. No change done in problem
d. None of the above
12) The keyword that tell Python that a new function is defined :
a. def
b. next
c. import
d. None of the above
13) To pass some extra data to a function ______ is used:
a. import
b. export
c. parameter
d. module
14) A variable with a specific scope is called ______.
a. Local variable
b. Global variable
c. Argument
d. None of the above
15) Function used to take input from the console is ________.
a. print()
b. isupper()
c. istitle()
d. None of the above
16) _________ function used to convert method converts the first character of a string to an
uppercase letter and all other alphabets to lowercase.
a. capitalize()
b. upper()
c. lower()
d. None of the above
17) Using today() function we will bale to know
a. Current day
b. Current month
c. Current year
d. All of the above
121
Chapter 5 : Functions
1) The islower() method returns _____ if all alphabets in a string are lowercase alphabets.
2) The rfind() method returns the ______ index of the substring (if found).
3) A _________ statement is used to end the execution of the function call and “returns” the
result to the caller.
4) The _______ method returns the number of occurrences of a substring in the given string.
5) The _______ method returns a string with first letter of each word capitalized; a title cased
string.
122
Chapter 5 : Functions
6) The ________ method returns the string by converting all the characters to their opposite
letter case (uppercase to lowercase and vice versa).
7) A _________ is a system that has a unique name for each and every object in Python.
8) Recursive functions are ________ to debug.
9) ______ keyword used to include packages or modules to a program.
10) The ________ method replaces each matching occurrence of the old character/text in the
string with the new character/text.
LAB exercise
123
Chapter 6 : File Processing
Chapter 6
File Processing
6.1 Concept of Files
Python too supports file handling and allows users to handle files i.e., to read and write files, along
with many other file handling options, to operate on files. The concept of file handling has
stretched over various other languages, but the implementation is either complicated or lengthy,
but like other concepts of Python, this concept here is also easy and short.
Syntax:
file_object = open(“filename” ,”mode”)
Closing a file:
In Python, it is not system critical to close all your files after using them, because the file will auto
close after Python code finishes execution. You can close a file by using the close() method.
Syntax:
file_object.close()
6.3 Reading from a file
For reading text data, different text-encoding schemes are used, such as ASCII (American Standard
Code for Information Interchange), UTF-8 (Unicode Transformation Format), UTF-16.
124
Chapter 6 : File Processing
Similarly, for writing data to files, we have to use open() with 'wt' mode, clearing and overwriting
the previous content. Also, we have to use the write() function to write into a file.
Syntax: file = open("file.txt", "wt")
file.write('hi there, this is a first line of file.\n')
file.write('and another line\n')
Output:
hi there, this is a first line of file.
and another line.
The open() function returns a file object which can used to read, write and modify the file.
If the file is not found, it raises the FileNotFoundError exception.
Example 1: How to open a file in Python?
# opens test.text file of the current directory
f = open("test.txt")
Python's default encoding is ASCII. You can easily change it by passing the encoding parameter.
6.5 close()
Python file method close() closes the opened file. A closed file cannot be read or written any more.
You should always close your files, in some cases, due to buffering, changes made to a file may
not show until you close the file. Python automatically closes a file when the reference object of a
file is reassigned to another file. It is a good practice to use the close() method to close a file.
Syntax:
f.close()
125
Chapter 6 : File Processing
6.6 read()
The read() method in Python is a pre-defined function which returns the read data in the form of
a string.
Syntax:
f.read(n)
Figure 7: new_file.txt
Consider the contents to be read belong to the above-shown file, named new_file.txt. Hence
using read() we can read the information present inside new_file. Let us see how we can do that,
file = open("new_file.txt", "r")
print(file.read())
Output:
Python
C
C++
Java
Kotlin
Again for reading a specific number of bytes, we can use read() in the following way,
file = open("new_file.txt", "r")
print(file.read(6))
Output:
Python
6.7 readline()
readline() is yet another pre-defined method in Python, which returns a read line in the form of
a string. Below is the syntax for readline() function,
f.readline( n )
126
Chapter 6 : File Processing
Similarly, here f is the object created while opening the file and ‘n’ is the number of bytes which
the function would read almost. Noteworthy, if n exceeds the length of a line, the function doesn’t
consider the next line.
Output:
Python\n
6.8 readlines()
readlines() reads all the lines present inside a specified file and returns a list containing the string
forms of the read lines.
file.readlines()
Using the readlines() method,
file = open("new_file.txt", "r")
print(file.readlines())
Output:
['Python\n', 'C\n', 'C++\n', 'Java\n', 'Kotlin']
6.9 write()
The write() method writes a specified text to the file. Where the specified text will be inserted
depends on the file mode and stream position.
"a": The text will be inserted at the current file stream position, default at the end of the
file.
"w": The file will be emptied before the text will be inserted at the current file stream
position, default 0.
Syntax: f.write(“”)
Example
f = open("new_file.txt", "a")
f.write("\nSee you soon!")
f.close()
127
Chapter 6 : File Processing
Output:
Python
Java
C
C++
Kotlin
See you again!
Output 1: write a file
6.10 writelines()
The writelines() method writes the items of a list to the file. Where the texts will be inserted
depends on the file mode and stream position.
"a": The texts will be inserted at the current file stream position, default at the end of the
file.
"w": The file will be emptied before the texts will be inserted at the current file stream
position, default 0.
Syntax :f.writelinrs(list)
Example
f = open("new_file.txt", "a")
f.writelines(["\nSee you soon!", "\nOver and out."])
f.close()
Code 8: writelines
Output:
Python
Java
C
C++
Kotlin
See you again!
Over and out.
Output 2: Writelines
128
Chapter 6 : File Processing
6.11 tell()
The tell() function in Python, used to find the current position of the file handler or file object.
Most of the time, the tell() function becomes useful to check whether the position of the file
handler is at the beginning of the file or not.
Syntax: file.tell()
Example
file = open("myfile.txt", "w")
text = "What's Up!"
file.write(text)
print("The current position of file handler is:", file.tell())
file.close()
Code 9: tell () Function
Output:
Here are the list of 10 characters of the text written to the file:
1. W
2. h
3. a
4. t
5. '
6. s
7. (a space)
8. U
9. p
10. !
6.12 seek()
If we want to move the file pointer to another position, we can use the seek() method.
129
Chapter 6 : File Processing
Syntax:
file. Seek(offset, from where), where offset represents how many bytes to move
from where, represents the position from where the bytes are moving.
Example 1
>>> # test.txt contents:
>>> # ABCDE
>>> f = open(r'C:\test.txt')
>>> f.seek(3)
>>> f.read() # starts reading from the 3rd character
'DE'
Example 2
>>> f = open(r'C:\test.txt')
>>> f.seek(2) # move two characters ahead
>>> f.seek(2, 1) #move two characters ahead from the current pos
>>> f.read()
'E'
Example 3
>>> f = open(r'C:\test.txt')
>>> f.seek(-3, 2) # move to the 3rd character from the end of the file
>>> f.read()
6.13 Command Line arguments
The arguments that are given after the name of the program in the command line shell of
the operating system are known as Command Line Arguments. Python provides various
ways of dealing with these types of arguments. The three most common are:
a) Using sys.argv:
The sys module provides functions and variables used to manipulate different parts of the Python
runtime environment. This module provides access to some variables used or maintained by the
interpreter and to functions that interact strongly with the interpreter.One such variable is sys.argv
which is a simple list structure. Its main purpose is:
It is a list of command line arguments.
len(sys.argv) provides the number of command line arguments.
sys.argv[0] is the name of the current Python script.
130
Chapter 6 : File Processing
Example: Let’s suppose there is a Python script for adding two numbers and the numbers are
passed as command-line arguments.
import sys
# total arguments
n = len(sys.argv)
print("Total arguments passed:", n)
# Arguments passed
print("\nName of Python script:", sys.argv[0])
print("\nArguments passed:", end = " ")
for i in range(1, n):
print(sys.argv[i], end = " ")
# Addition of numbers
Sum = 0
# Using argparse module
for i in range(1, n):
Sum += int(sys.argv[i])
print("\n\nResult:", Sum)
Output:
Name of Python script: gfg.py
Arguments passed: 2 3 5 6
Result: 16
Output 4: Python Output for sys.argv
Python getopt module is similar to the function of C. Unlike sys module getopt module extends
the separation of the input string by parameter validation. It allows both short, and long options
including a value assignment. However, this module requires the use of the sys module to
process input data properly. To use getopt module, it is required to remove the first element from
the list of command-line arguments.
Parameters:
args: List of arguments to be passed.
options: String of option letters that the script want to recognize. Options that require an
argument should be followed by a colon (:).
131
Chapter 6 : File Processing
long_options: List of string with the name of long options. Options that require arguments
should be followed by an equal sign (=).
Return Type: Returns value consisting of two elements: the first is a list of (option, value) pairs.
The second is the list of program arguments left after the option list was stripped.
Example:
132
Chapter 6 : File Processing
Using argparse module is a better option than the above two options as it provides a lot of options
such as positional arguments, default value for arguments, help message, specifying data type of
argument etc.
Note: As a default optional argument, it includes -h, along with its long version –help.
Example 1: Basic use of argparse module.
Output:
133
Chapter 6 : File Processing
Output:
Output:
134
Chapter 6 : File Processing
Exercise
135
Chapter 6 : File Processing
136
Chapter 6 : File Processing
137
Chapter 7 : Machine Learning and AI
Chapter 7
Machine Learning and AI
Artificial Intelligence (AI) is when a computer algorithm does intelligent work. On the other hand,
Machine Learning is a part of AI that learns from the data that also involves the information
gathered from previous experiences and allows the computer program to change its behavior
accordingly. Artificial Intelligence is the superset of Machine Learning i.e. all Machine
Learning is Artificial Intelligence but not all AI is Machine Learning.
Future Scope –
Artificial Intelligence and Machine Learning are likely to replace the current model of
technology that we see these days, for example, traditional programming packages
like ERP and CRM are certainly losing their charm.
Firms like Facebook, and Google are investing a hefty amount in AI to get the desired outcome
at a relatively lower computational time.
Artificial Intelligence is something that is going to redefine the world of software and IT in the
near future.
138
Chapter 7 : Machine Learning and AI
139
Chapter 7 : Machine Learning and AI
The main goal of the supervised learning technique is to map the input variable(x) with the output
variable(y). Some real-world applications of supervised learning are Risk Assessment, Fraud
Detection, Spam filtering, etc.
Categories of Supervised Machine Learning
Supervised machine learning can be classified into two types of problems, which are given below:
o Classification
o Regression
Unsupervised Learning can be further classified into two types, which are given below:
o Clustering
o Association
140
Chapter 7 : Machine Learning and AI
learning.
Figure 11: Feature Engineering diagram
141
Chapter 7 : Machine Learning and AI
Validation data provides an initial check that the model can return useful predictions in a real-
world setting, which training data cannot do. The ML algorithm can assess training data and
validation data at the same time.
Validation data is an entirely separate segment of data, though a data scientist might carve out part
of the training dataset for validation — as long as the datasets are kept separate throughout the
entirety of training and testing.
142
Chapter 7 : Machine Learning and AI
For example, let’s say an ML algorithm is supposed to analyze a picture of a vertebrate and provide
its scientific classification. The training dataset would include lots of pictures of mammals, but
not all pictures of all mammals, let alone all pictures of all vertebrates. So, when the validation
data provides a picture of a squirrel, an animal the model hasn’t seen before, the data scientist can
assess how well the algorithm performs in that task. This is a check against an entirely different
dataset than the one it was trained on.
1. Linear regression: Linear regression is one of the easiest and most popular Machine
Learning algorithms. It is a statistical method that is used for predictive analysis.
Linear regression makes predictions for continuous/real or numeric variables such
as sales, salary, age, product price, etc.
2. Logistic regression : Logistic regression is one of the most popular Machine Learning
algorithms, which comes under the Supervised Learning technique. It is used for
predicting the categorical dependent variable using a given set of independent
variables.
143
Chapter 7 : Machine Learning and AI
3. Decision tree : Decision Tree is a Supervised learning technique that can be used for
both classification and Regression problems, but mostly it is preferred for solving
Classification problems. It is a tree-structured classifier, where internal nodes
represent the features of a dataset, branches represent the decision rules and each leaf
node represents the outcome.
4. SVM algorithm : Support Vector Machine or SVM is one of the most popular
Supervised Learning algorithms, which is used for Classification as well as Regression
problems. However, primarily, it is used for Classification problems in Machine
Learning.
5. Naive Bayes algorithm : Naïve Bayes algorithm is a supervised learning algorithm,
which is based on Bayes theorem and used for solving classification problems. It is
mainly used in text classification that includes a high-dimensional training dataset.
6. KNN algorithm : K-nearest neighbours (KNN) algorithm is a type of supervised ML
algorithm which can be used for both classification as well as regression predictive
problems. However, it is mainly used for classification predictive problems in industry.
7. K-means : K-means clustering algorithm computes the centroids and iterates until we
it finds optimal centroid. It assumes that the number of clusters are already known. It
is also called flat clustering algorithm. The number of clusters identified from data by
algorithm is represented by ‘K’ in K-means.In this algorithm, the data points are
assigned to a cluster in such a manner that the sum of the squared distance between the
data points and centroid would be minimum. It is to be understood that less variation
within the clusters will lead to more similar data points within same cluster.
7.5. Training the Machine learning model and predicting the results
Problem Statement:
Create the model that can classify the different species of the Iris flower.
Problem solving:
1. create the dataset.
2. Build the model
3. Train the model
4. Make predictions.
Iris Flower:
Iris is the family in the flower which contains the several species such as the iris.setosa,
iris.versicolor, iris.virginica etc.
144
Chapter 7 : Machine Learning and AI
So now let us write the python code to load the Iris dataset.
145
Chapter 7 : Machine Learning and AI
classifier.fit(x_train,y_train)
Now the model is ready to make predictions
4) Make predictions:
Predictions can be done with predict function
predictions=classifier.predict(x_test)
these predictions can be matched with the expected output to measure the accuracy value.
146
Chapter 7 : Machine Learning and AI
Full code:
iris=datasets.load_iris()
x=iris.data
y=iris.target
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.5)
classifier.fit(x_train,y_train)
predictions=classifier.predict(x_test)
print(accuracy_score(y_test,predictions))
Code 15: Code for Iris flower classification using Decision tree classifier
Output:
D:\iris_classification>python classifier.py
0.96
Output 9: Result for iris flower classification
147
Chapter 7 : Machine Learning and AI
Machine learning is a buzzword for today's technology, and it is growing very rapidly day-by-day.
We are using machine learning in our daily life even without knowing it such as Google Maps,
Google assistant, Alexa, etc. Below are some most trending real-world applications of Machine
Learning:
1. Image Recognition:
Image recognition is one of the most common applications of machine learning. It is used to
identify objects, persons, places, digital images, etc. The popular use case of image recognition
and face detection is, Automatic friend tagging suggestion:Facebook
148
Chapter 7 : Machine Learning and AI
2. Speech Recognition:
While using Google, we get an option of "Search by voice," it comes under speech recognition,
and it's a popular application of machine learning.
Speech recognition is a process of converting voice instructions into text, and it is also known as
"Speech to text", or "Computer speech recognition." At present, machine-learning algorithms are
widely used by various applications of speech recognition. Google assistant, Siri, Cortana,
and Alexa are using speech recognition technology to follow the voice instructions.
3. Traffic prediction:
If we want to visit a new place, we take help of Google Maps, which shows us the correct path
with the shortest route and predicts the traffic conditions.
It predicts the traffic conditions such as whether traffic is cleared, slow moving, or heavily
congested with the help of two ways:
o Real Time location of the vehicle form Google Map app and sensors
o Average time has taken on past days at the same time.
Everyone who is using Google Map is helping this app to make it better. It takes information from
the user and sends back to its database to improve the performance.
4. Product recommendations:
Machine learning is widely used by various e-commerce and entertainment companies such
as Amazon, Netflix, etc., for product recommendation to the user. Whenever we search for some
product on Amazon, then we started getting an advertisement for the same product while internet
surfing on the same browser and this is because of machine learning. Google understands the user
interest using various machine-learning algorithms and suggests the product as per customer
interest. As similar, when we use Netflix, we find some recommendations for entertainment series,
movies, etc., and this is also done with the help of machine learning.
149
Chapter 7 : Machine Learning and AI
5. Self-driving cars:
One of the most exciting applications of machine learning is self-driving cars. Machine learning
plays a significant role in self-driving cars. Tesla, the most popular car manufacturing company is
working on self-driving car. It is using unsupervised learning method to train the car models to
detect people and objects while driving.
Whenever we receive a new email, it is filtered automatically as important, normal, and spam. We
always receive an important mail in our inbox with the important symbol and spam emails in our
spam box, and the technology behind this is Machine learning. Below are some spam filters used
by Gmail:
150
Chapter 7 : Machine Learning and AI
o Content Filter
o Header filter
o General blacklists filter
o Rules-based filters
o Permission filters
Some machine learning algorithms such as Multi-Layer Perceptron, Decision tree, and Naïve
Bayes classifier are used for email spam filtering and malware detection.
We have various virtual personal assistants such as Google assistant, Alexa, Cortana, Siri. As the
name suggests, they help us in finding the information using our voice instruction. These assistants
can help us in various ways just by our voice instructions such as Play music, call someone, open
an email, Scheduling an appointment, etc.
These virtual assistants use machine learning algorithms as an important part. These assistant
record our voice instructions, send it over the server on a cloud, and decode it using ML algorithms
and act accordingly.
Machine learning is making our online transaction safe and secure by detecting fraud transaction.
Whenever we perform some online transaction, there may be various ways that a fraudulent
transaction can take place such as fake accounts, fake ids, and steal money in the middle of a
transaction. So to detect this, Feed Forward Neural network helps us by checking whether it is a
genuine transaction or a fraud transaction.
For each genuine transaction, the output is converted into some hash values, and these values
become the input for the next round. For each genuine transaction, there is a specific pattern which
gets change for the fraud transaction hence, it detects it and makes our online transactions more
secure.
9. Stock Market trading:
Machine learning is widely used in stock market trading. In the stock market, there is always a risk
of up and downs in shares, so for this machine learning's long short term memory neural network is
used for the prediction of stock market trends.
10. Medical Diagnosis:
In medical science, machine learning is used for diseases diagnoses. With this, medical technology
is growing very fast and able to build 3D models that can predict the exact position of lesions in
the brain.
It helps in finding brain tumors and other brain-related diseases easily.
151
Chapter 7 : Machine Learning and AI
Nowadays, if we visit a new place and we are not aware of the language then it is not a problem at
all, as for this also machine learning helps us by converting the text into our known languages.
Google's GNMT (Google Neural Machine Translation) provide this feature, which is a Neural
Machine Learning that translates the text into our familiar language, and it called as automatic
translation.
The technology behind the automatic translation is a sequence to sequence learning algorithm,
which is used with image recognition and translates the text from one language to another
language.
In today's world, technology is growing very fast, and we are getting in touch with different new
technologies day by day.
Here, one of the booming technologies of computer science is Artificial Intelligence which is ready
to create a new revolution in the world by making intelligent machines. The Artificial Intelligence
is now all around us. It is currently working with a variety of subfields, ranging from general to
specific, such as self-driving cars, playing chess, proving theorems, playing music, Painting, etc.AI
is one of the fascinating and universal fields of Computer science which has a great scope in future.
AI holds a tendency to cause a machine to work as a human.
Artificial Intelligence is composed of two words Artificial and Intelligence, where Artificial
defines "man-made," and intelligence defines "thinking power", hence AI means "a man-made
thinking power."
"It is a branch of computer science by which we can create intelligent machines which can behave
like a human, think like humans, and able to make decisions."
152
Chapter 7 : Machine Learning and AI
Artificial Intelligence exists when a machine can have human based skills such as learning,
reasoning, and solving problems
With Artificial Intelligence you do not need to pre-program a machine to do some work, despite
that you can create a machine with programmed algorithms which can work with own intelligence,
and that is the awesomeness of AI.
It is believed that AI is not a new technology, and some people says that as per Greek myth, there
were Mechanical men in early days which can work and behave like humans.
Before Learning about Artificial Intelligence, we should know that what is the importance of AI
and why should we learn it. Following are some main reasons to learn about AI:
With the help of AI, you can create such software or devices which can solve real-world
problems very easily and with accuracy such as health issues, marketing, traffic issues, etc.
With the help of AI, you can create your personal virtual Assistant, such as Cortana, Google
Assistant, Siri, etc.
With the help of AI, you can build such Robots which can work in an environment where
survival of humans can be at risk.
AI opens a path for other new technologies, new devices, and new Opportunities.
Artificial Intelligence is not just a part of computer science even it's so vast and requires lots of
other factors which can contribute to it. To create the AI first, we should know that how
intelligence is composed, so the Intelligence is an intangible part of our brain which is a
combination of Reasoning, learning, problem-solving perception, language understanding, etc.
To achieve the above factors for a machine or software Artificial Intelligence requires the
following discipline:
o Mathematics
o Biology
o Psychology
o Sociology
o Computer Science
o Neurons Study
o Statistics
153
Chapter 7 : Machine Learning and AI
The function and popularity of Artificial Intelligence are soaring by the day. Artificial intelligence
is the ability of a system or a program to think and learn from the experience. AI applications have
significantly evolved over the past few years and has found its applications in almost every
business sector. Top artificial intelligence applications in the real world are:
Personalized Shopping
Artificial Intelligence technology is used to create recommendation engines through which
you can engage better with your customers. These recommendations are made in
accordance with their browsing history, preference, and interests. It helps in improving
your relationship with your customers and their loyalty towards your brand.
AI-powered Assistants
Virtual shopping assistants and chatbots help improve the user experience while shopping
online. Natural Language Processing is used to make the conversation sound as human and
personal as possible. Moreover, these assistants can have real-time engagement with your
customers. Did you know that on amazon.com, soon, customer service could be handled
by chatbots?
Although the education sector is the one most influenced by humans, Artificial Intelligence has
slowly begun to seep its roots in the education sector as well. Even in the education sector, this
slow transition of Artificial Intelligence has helped increase productivity among faculties and
helped them concentrate more on students than office or administration work.
154
Chapter 7 : Machine Learning and AI
Artificial Intelligence has a lot of influence on our lifestyle. Let us discuss a few of them.
Autonomous Vehicles
Automobile manufacturing companies like Toyota, Audi, Volvo, and Tesla use machine
learning to train computers to think and evolve like humans when it comes to driving in
any environment and object detection to avoid accidents.
Spam Filters
The email that we use in our day-to-day lives has AI that filters out spam emails sending
them to spam or trash folders, letting us see the filtered content only. The popular email
provider, Gmail, has managed to reach a filtration capacity of approximately 99.9%.
Facial Recognition
Our favorite devices like our phones, laptops, and PCs use facial recognition techniques by
using face filters to detect and identify in order to provide secure access. Apart from
personal usage, facial recognition is a widely used Artificial Intelligence application even
in high security-related areas in several industries.
Recommendation System
Various platforms that we use in our daily lives like e-commerce, entertainment websites,
social media, video sharing platforms, like YouTube, etc., all use the recommendation
system to get user data and provide customized recommendations to users to increase
engagement. This is a very widely used Artificial Intelligence application in almost all
industries.
155
Chapter 7 : Machine Learning and AI
Did you know that companies use intelligent software to ease the hiring process?
Artificial Intelligence helps with blind hiring. Using machine learning software, you can examine
applications based on specific parameters. AI drive systems can scan job candidates' profiles, and
resumes to provide recruiters an understanding of the talent pool they must choose from.
Artificial Intelligence finds diverse applications in the healthcare sector. AI applications are used
in healthcare to build sophisticated machines that can detect diseases and identify cancer cells.
Artificial Intelligence can help analyze chronic conditions with lab and other medical data to
ensure early diagnosis. AI uses the combination of historical data and medical intelligence for the
discovery of new drugs.
Artificial Intelligence is used to identify defects and nutrient deficiencies in the soil. This is done
using computer vision, robotics, and AI can analyze where weeds are growing. AI bots can help
to harvest crops at a higher volume and faster pace than human laborers.
156
Chapter 7 : Machine Learning and AI
The gaming industry is another area where AI technologies have gained popularity. AI can be
utilised to develop intelligent, human-like NPCs that communicate with players. In order to
improve game design and testing, it can also be used to forecast human behaviour. The 2014 Alien
Isolation video games employ AI to follow the player around at all times. Two artificial
intelligence systems are used in the game: the "Director AI," who frequently knows where you
are, and the "Alien AI," which is controlled by sensors and behaviours and persistently pursues the
player.
157
Chapter 7 : Machine Learning and AI
Increase dependency on machines: With the increment of technology, people are getting
more dependent on devices and hence they are losing their mental capabilities.
No Original Creativity: As humans are so creative and can imagine some new ideas but
still AI machines cannot beat this power of human intelligence and cannot be creative and
imaginative.
158
Chapter 7 : Machine Learning and AI
159
Chapter 7 : Machine Learning and AI
Using surgical robots to reduce errors and variances and ultimately aid in improving
the effectiveness of surgeons is a potent application of artificial intelligence in decision-
making. The Da Vinci is one such surgical robot that gives experienced surgeons more
flexibility and control than they would have with traditional techniques to carry out difficult
operations.
160
Chapter 7 : Machine Learning and AI
Exercise
Objective Type Questions
5. Select the most appropriate situation for that a blind search can be used.
a) Real-life situation
b) Small Search Space
c) Complex game
d) All of the above
6. A robot is able to change its own trajectory as per the external conditions, then the
robot is considered as the__
a) Mobile
b) Non-Servo
c) Open Loop
d) Intelligent
161
Chapter 7 : Machine Learning and AI
162
Chapter 7 : Machine Learning and AI
163
Chapter 8 : Data Science and Analytics Concepts
Chapter 8
Data Science and Analytics Concepts
8.1. What is Data Science and Analytics? The Data Science Process
Data Science is a field that gives insights from structured and unstructured data, using different
scientific methods and algorithms, and consequently helps in generating insights, making
predictions and devising data driver solutions. It uses a large amount of data to get meaningful
insights using statistics and computation for decision making.
The data used in Data Science is usually collected from different sources, such as e-commerce
sites, surveys, social media, and internet searches. All this access to data has become possible due
to the advanced technologies for data collection. This data helps in making predictions and
providing profits to the businesses accordingly. Data Science is the most discussed topic in today’s
time and is a hot career option due to the great opportunities it has to offer.
164
Chapter 8 : Data Science and Analytics Concepts
Data Analyst:
165
Chapter 8 : Data Science and Analytics Concepts
The role of a Data Analyst is quite similar to a Data Scientist in terms of responsibilities, and skills
required. The skills shared between these two roles include SQL and data query knowledge, data
preparation and cleaning, applying statistical and mathematical methods to find the insights, data
visualizations, and data reporting.
The main difference between the two roles is that Data Analysts do not need to be skilled in
programming languages and do not need to perform data modeling or have the knowledge of
machine learning.
The tools used by both Data Scientists and Data Analysts are also different. The tools used by Data
Analysts Are Tableau, Microsoft Excel, SAP, SAS, and Qlik.Data Analysts also perform the task
of data mining and data modeling, but they use SAS, Rapid Miner, KNIME, and IBM SPSS
Moderator. They are provided with the problem statement and the goal. They just have to perform
the data analysis and deliver data reporting to the managers.
166
Chapter 8 : Data Science and Analytics Concepts
8.3. Collecting
Before an analyst begins collecting data, they must answer three questions first:
What’s the goal or purpose of this research?
What kinds of data are they planning on gathering?
What methods and procedures will be used to collect, store, and process the information?
Additionally, we can break up data into qualitative and quantitative types. Qualitative data covers
descriptions such as color, size, quality, and appearance. Quantitative data, unsurprisingly, deals
with numbers, such as statistics, poll numbers, percentages, etc. Data collection could mean a
telephone survey, a mail-in comment card, or even some guy with a clipboard asking passersby
some questions. Data collection breaks down into two methods.
Primary.: As the name implies, this is original, first-hand data collected by the data
researchers. This process is the initial information gathering step, performed before anyone
carries out any further or related research. Primary data results are highly accurate provided
the researcher collects the information. However, there’s a downside, as first-hand research
is potentially time-consuming and expensive.
Secondary.:Secondary data is second-hand data collected by other parties and already
having undergone statistical analysis. This data is either information that the researcher has
tasked other people to collect or information the researcher has looked up. Simply put, it’s
second-hand information. Although it’s easier and cheaper to obtain than primary
information, secondary information raises concerns regarding accuracy and authenticity.
Quantitative data makes up a majority of secondary data.
167
Chapter 8 : Data Science and Analytics Concepts
8.4. Processing
Data processing occurs when data is collected and translated into usable information. Usually
performed by a data scientist or team of data scientists, it is important for data processing to be
done correctly as not to negatively affect the end product, or data output. Data processing starts
with data in its raw form and converts it into a more readable format (graphs, documents, etc.),
giving it the form and context necessary to be interpreted by computers and utilized by employees
throughout an organization.
1. Data collection
Collecting data is the first step in data processing. Data is pulled from available sources, including .
It is important that the data sources available are trustworthy and well-built so the data collected
(and later used as information) is of the highest possible quality.
2. Data preparation
Once the data is collected, it then enters the stage. Data preparation, often referred to as “pre-
processing” is the stage at which raw data is cleaned up and organized for the following stage of
data processing. During preparation, raw data is diligently checked for any errors. The purpose of
this step is to eliminate bad data (incomplete, or incorrect data) and begin to create high-quality
data for the best .
168
Chapter 8 : Data Science and Analytics Concepts
3. Data input
The clean data is then entered into its destination (perhaps a CRM like Salesforce or a data
warehouse like and translated into a language that it can understand. Data input is the first stage in
which raw data begins to take the form of usable information.
4. Processing
During this stage, the data inputted to the computer in the previous stage is actually processed for
interpretation. Processing is done using algorithms, though the process itself may vary slightly
depending on the source of data being processed (data lakes, social networks, connected devices
etc.) and its intended use (examining advertising patterns, medical diagnosis from connected
devices, determining customer needs, etc.).
5. Data output/interpretation
The output/interpretation stage is the stage at which data is finally usable to non-data scientists. It
is translated, readable, and often in the form of graphs, videos, images, plain text, etc.). Members
of the company or institution can now begin for their own data analytics projects.
6. Data storage
The final stage of data processing is After all of the data is processed, it is then stored for future
use. While some information may be put to use immediately, much of it will serve a purpose later
on. Plus, properly stored data is a necessity for compliance with data protection legislation
like When data is properly stored, it can be quickly and easily accessed by members of the
organization when needed.
When working with data, your analysis and insights are only as good as the data you use. If you’re
performing data analysis with dirty data, your organization can’t make efficient and effective
decisions with that data. Data cleaning is a critical part of data management that allows you to
validate that you have a high quality of data.
169
Chapter 8 : Data Science and Analytics Concepts
Data cleaning includes more than just fixing spelling or syntax errors. It’s a fundamental aspect of
data science analytics and an important machine learning technique. Today, we’ll learn more about
data cleaning, its benefits, issues that can arise with your data
Data cleaning, or data cleansing, is the important process of correcting or removing incorrect,
incomplete, or duplicate data within a dataset. Data cleaning should be the first step in your
workflow. When working with large datasets and combining various data sources, there’s a strong
possibility you may duplicate or mislabel data. If you have inaccurate or incorrect data, it will lose
its quality, and your algorithms and outcomes become unreliable.
Data cleaning differs from data transformation because you’re actually removing data that
doesn’t belong in your dataset. With data transformation, you’re changing your data to a different
format or structure. Data transformation processes are sometimes referred to as data
wrangling or data munging. The data cleaning process is what we’ll focus on today.
To determine data quality, you can study its features and weigh them according to what’s important
to your organization and your project.
There are five main features to look for when evaluating your data:
Now that we know how to recognize high-quality data, let’s dive deeper into the process of data
science cleaning, why it’s important, and how to do it effectively.
Exploratory Data Analysis refers to the critical process of performing initial investigations on data
so as to discover patterns, to spot anomalies, to test hypothesis and to check assumptions with the
help of summary statistics and graphical representations. It is a good practice to understand the data
first and try to gather as many insights from it. EDA is all about making sense of data in hand,
before getting them dirty with it.
To understanding the concept and techniques we’ll take an example of white variant of which is
available on UCI Machine Learning Repository and try to catch hold of as many insights from the
data set using EDA.
To starts with, import necessary libraries (for this example pandas, numpy, matplotlib and seaborn)
and loaded the data set.
Note : Whatever inferences are extracted, is mentioned with bullet points.
170
Chapter 8 : Data Science and Analytics Concepts
171
Chapter 8 : Data Science and Analytics Concepts
The describe() function in pandas is very handy in getting various summary statistics. This function
returns the count, mean, standard deviation, minimum and maximum values and the quantiles of
the data.
172
Chapter 8 : Data Science and Analytics Concepts
Exercise
5.The result of an operation between unaligned Series will have the ________ of the indexes
involved.
a) intersection
b) union
c) total
d) all of the mentioned
a). Keyframe
b). DataFrame
c). Statistics
d). Econometrics
173
Chapter 8 : Data Science and Analytics Concepts
11. Which of the following makes use of pandas and returns data in a series or dataFrame?
a)PandaSDMX
b). freedapi
c). OutPy
d). Inpy
174
Chapter 8 : Data Science and Analytics Concepts
14. Which of the following takes a dict of dicts or a dict of array-like sequences and returns
a DataFrame?
a) DataFrame.from_items
b) DataFrame.from_records
c) DataFrame.from_dict
d) All of the mentioned
15. Which of the following operation works with the same syntax as the analogous dict
operations?
a) Getting columns
b) Setting columns
c) Deleting columns
d) All of the mentioned
ANSWERS
1. d 6. d 11. b
2. d 7. b 12. c
3. a 8. c 13. a
4. a 9. d 14. a
5. b 10. d 15. d
175
Chapter 9 : Introduction to NumPy
Chapter 9
Installation:
pip install Numpy
Example:
176
Chapter 9 : Introduction to NumPy
Output:
Example 1
Output:
177
Chapter 9 : Introduction to NumPy
Example 2
Output:
Example 3
Output:
178
Chapter 9 : Introduction to NumPy
Example 1
Slice from the index 3 from the end to index 1 from the end:
Output:
From the second element, slice elements from index 1 to index 4 (not included):
Output:
179
Chapter 9 : Introduction to NumPy
Example 2
Output:
180
Chapter 9 : Introduction to NumPy
Output:
181
Chapter 9 : Introduction to NumPy
Output:
182
Chapter 9 : Introduction to NumPy
Output:
Code 26: Numpy average along with axis (with Axis name)
Output:
Output 20: Numpy average along with axis (with Axis name)
The Python numpy min function returns the minimum value in an array or a given axis.
183
Chapter 9 : Introduction to NumPy
Output:
Code 28: Numpy minimum function with and without axis name
Output:
Output 22: Numpy minimum function with and without axis name
The Python numpy max function returns the maximum number from a given array or in a given
axis.
184
Chapter 9 : Introduction to NumPy
Output:
Find the maximum value in the X and Y-axis using numpy max function.
Output:
The type of items in the array is specified by a separate data-type object (dtype), one of which is
associated with each ndarray.
185
Chapter 9 : Introduction to NumPy
Like other container objects in Python, the contents of an ndarray can be accessed and modified
by indexing or slicing the array (using, for example, N integers), and via the methods and
attributes of the ndarray.
1 object
Any object exposing the array interface method returns an array, or any (nested)
sequence.
2 dtype
Desired data type of array, optional
3 copy
Optional. By default (true), the object is copied
4 order
C (row major) or F (column major) or A (any) (default)
5 subok
By default, returned array forced to be a base class array. If true, sub-classes passed
through
6 ndmin
Specifies minimum dimensions of resultant array
Table 9: Numpy array Parameters
Example:
186
Chapter 9 : Introduction to NumPy
Output:
Example:
More than one dimensions:
Output:
9.7. Broadcasting
The term broadcasting describes how numpy treats arrays with different shapes during arithmetic
operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array
so that they have compatible shapes. Broadcasting is a powerful mechanism that allows numpy
to work with arrays of different shapes when performing arithmetic operations. Frequently we
have a smaller array and a larger array, and we want to use the smaller array multiple times to
perform some operation on the larger array.
For example, suppose that we want to add a constant vector to each row of a matrix. We could
do like this:
187
Chapter 9 : Introduction to NumPy
This works; however, when the matrix x is very large, computing an explicit loop in Python
could be slow.
Note that adding the vector v to each row of the matrix x is equivalent to forming a matrix
vv by stacking multiple copies of v vertically.
Then performing elementwise summation of x and vv. We could implement this approach
like this:
Using Tile:
Code 34: Adding a constant vector to each row of a matrix using tile() function
188
Chapter 9 : Introduction to NumPy
Output:
Output 28: Adding a constant vector to each row of a matrix using tile() function
Broadcasting:
Output:
The line y = x + v works even though x has shape (4, 3) and v has shape (3,) due to
broadcasting; this line works as if v actually had shape (4, 3), where each row was a copy
of v, and the sum was performed elementwise.
189
Chapter 9 : Introduction to NumPy
Broadcasting Rules:
The trailing axes of both arrays must either be 1 or have the same size for broadcasting to occur.
Otherwise, a “ValueError: frames are not aligned” exception is thrown.
Broadcasting in Action:
190
Chapter 9 : Introduction to NumPy
In Fancy Indexing, we pass array of indices instead of single scalar(numbers) to fetch elements
at different index points. Remember that the shape of the output depends on the shape of
the index arrays rather than the shape of the array being indexed.
Let’s go through some examples to understand this concept:
Output:
191
Chapter 9 : Introduction to NumPy
Case A
a. 1D Array:
For 1D array, let’s suppose we want to access elements at index position
of 0, 4 and -1.
Output:
192
Chapter 9 : Introduction to NumPy
Example:
Output:
Output:
193
Chapter 9 : Introduction to NumPy
Exercise
194
Chapter 9 : Introduction to NumPy
7. Which of the following sets the size of the buffer used in ufuncs?
a) setsize(size)
b) bufsize(size)
c) setbufsize(size)
d) All of the mentioned
8. Which of the following attribute should be used while checking for type combination
input
and output?
a) .types
b) .class
c) .type
d) None of the above
9. Which of the following function stacks 1D arrays as columns into a 2D array?
a) column_stack
b) com_stack
c) row_stack
d) All of the above
10. The ________ function returns its argument with a modified shape, whereas the
________
method modifies the array itself.
a) resize, reshape
b) reshape, resize
c) reshape2, resize
d) None of the above
11. Point out the correct statement in NumPy.
a) Numpy array class is called ndarray
b) In Numpy, dimensions are called axes
c) NumPy main object is the homogeneous multidimensional array
d) All of the mentioned
12. Which of the following method creates a new array object that looks at the same data?
a) copy
b) paste
c) view
d) All of the above
195
Chapter 9 : Introduction to NumPy
13. Which of the following function take only single value as input?
a) fmin
b) minimum
c) iscomplex
d) None of the above
14. Which of the following set the floating-point error callback function or log object?
a) settercall
b) setter
c) setterstack
d) All of the above
15. What will be output for the following code?
import numpy as np
a = np.array([1, 2, 3], dtype = complex)
print a
a) [ 1.+0.j]
b) [[ 1.+0.j, 3.+0.j]]
c) [[ 1.+0.j, 2.+0.j, 3.+0.j]]
d) [ 1.+0.j, 2.+0.j, 3.+0.j]
Subjective Type Questions
1. Why to use NumPy?
2. Explain what is ndarray in NumPy
3. What is the difference between ndarray and array in NumPy?
4. How to convert a numeric array to a categorical (text) array?
5. How would you reverse a NumPy array?
6. What are the differences between NumPy arrays and matrices?
7. What are the advantages of NumPy over regular Python lists?
8. What are the differences between np.mean() vs np.average() in Python NumPy?
9. What are the differences between NumPy arrays and matrices?
10. Explain what is Vectorization in NumPy
11. Why is NumPy Array good compared to Python Lists?
12. How can you reshape NumPy array?
13. How many dimensions can a NumPy array have?
14. How are NumPy Arrays better than Lists in Python?
15. Explain the data types supported by NumPy.
196
Chapter 10 : Data Analysis Tool : Pandas
Chapter 10.
Data Analysis Tool: Pandas
Given that Pandas is built on top of the Numpy package, Numpy is necessary in order to use
Pandas. Before Pandas, Python was capable for data preparation, but it only provided limited
support for data analysis. So, Pandas came into the picture and enhanced the capabilities of data
analysis. It can perform five significant steps required for
processing and analysis of data irrespective of the origin of the
data, i.e., load, manipulate, prepare, model, and analyze.
History:
Pandas were initially developed by Wes McKinney in 2008 while
he was working at AQR Capital Management. He convinced the
AQR to allow him to open source the Pandas. Another AQR
employee, Chang She, joined as the second major contributor to the
library in 2012. Over time many versions of pandas have been
released. The latest version of the pandas is 1.4.4.
197
Chapter 10 : Data Analysis Tool : Pandas
The parameters for the constructor of a Python Pandas Series are detailed as under:-
Parameters Remarks
data : array-like, Contains data stored in Series. Changed in version 0.23.0: If data
Iterable, dict, or scalar is a dict, argument order is maintained for Python 3.6 and later.
value
index : array-like or Values must be hashable and have the same length as data. Non-
Index (1d) unique index values are allowed. Will default to RangeIndex (0,
1, 2, …, n) if not provided. If both a dict and index sequence are
used, the index will override the keys found in the dict.
dtype : str, numpy.dtype, Data type for the output Series. If not specified, this will be
or ExtensionDtype, inferred from data.
optional
copy : bool, default False Copy input data.
Table 10: Pandas Series Parameters
198
Chapter 10 : Data Analysis Tool : Pandas
Output:
Parameters Remarks
data : ndarray (structured or Dict can contain Series, arrays, constants, or list-like objects
homogeneous), Iterable, dict, Changed in version 0.23.0: If data is a dict, column order follows
or DataFrame insertion-order for Python 3.6 and later. Changed in version 0.25.0:
If data is a list of dicts, column order follows insertion-order for
Python 3.6 and later.
index : Index or array-like Index to use for resulting frame. Will default to RangeIndex if no
indexing information part of input data and no index provided
columns : Index or array-like Column labels to use for resulting frame. Will default to
RangeIndex (0, 1, 2, …, n) if no column labels are provided
dtype, default None Data type to force. Only a single dtype is allowed. If None, infer
copy : bool, default False Copy data from inputs. Only affects DataFrame / 2d ndarray input
Table 11: Parameters for Pandas Dataframe
199
Chapter 10 : Data Analysis Tool : Pandas
You can create an empty Pandas Dataframe using pandas.Dataframe() and later on you can add
Output:
Output:
Indexing in Pandas:
In Pandas, picking specific rows and columns of data from a DataFrame constitutes indexing.
Selecting all the rows and part of the columns, some of the rows and all the columns, or a portion
of each row and each column is what is referred to as indexing. Another name for indexing is
subset selection.
200
Chapter 10 : Data Analysis Tool : Pandas
There are a lot of ways to pull the elements, rows, and columns from a DataFrame. There are
some indexing method in Pandas which help in getting an element from a DataFrame. These
indexing methods appear very similar but behave very differently. Pandas support four types of
Multi-axes indexing they are:
Dataframe.[ ] ; This function also known as indexing operator
Collectively, they are called the indexers. These are by far the most common ways to index
data. These are four function which help in getting the elements, rows, and columns from a
DataFrame.
Indexing a Dataframe using indexing operator [] :Indexing operator is used to refer to the square
brackets following an object. The indexers also use the indexing operator to make selections.
In this indexing operator to refer to df[].
Selecting a single column
In order to select a single column, we simply put the name of the column in-between the brackets
201
Chapter 10 : Data Analysis Tool : Pandas
Output:
This function selects data by the label of the rows and columns. The df.loc indexer selects data
in a different way than just the indexing operator. It can select subsets of rows or columns. It can
also simultaneously select subsets of rows and columns.
202
Chapter 10 : Data Analysis Tool : Pandas
203
Chapter 10 : Data Analysis Tool : Pandas
Output
Code 48: Selecting two rows and three columns using .loc[]
Output:
Output 42: Selecting two rows and three columns using .loc[]
Selecting all of the rows and some columns
In order to select all of the rows and some columns, we use single colon [:] to select all of rows
and list of some columns which we want to select like this:
Syntax:
Dataframe.loc[:, ["column1", "column2", "column3"]]
Code 49: Selecting all rows and some columns using .loc[]
204
Chapter 10 : Data Analysis Tool : Pandas
Output:
Output 43: Selecting all rows and some columns using .loc[]
Indexing a DataFrame:
This function allows us to retrieve rows and columns by position. In order to do that, we’ll need
to specify the positions of the rows that we want, and the positions of the columns that we want
as well. The df.iloc indexer is very similar to df.loc but only uses integer locations to make its
selections.
Selecting a single row
In order to select a single row using .iloc[], we can pass a single integer to .iloc[] function.
205
Chapter 10 : Data Analysis Tool : Pandas
Code #1:
Before manipulating the dataframe with pandas we have to understand what is data manipulation.
The data in the real world is very unpleasant & unordered so by performing certain operations
we can make data understandable based on one’s requirements, this process of converting
unordered data into meaningful information can be done by data manipulation.
Pandas is an open-source library that is used from data manipulation to data analysis & is very
powerful, flexible & easy to use tool which can be imported using import pandas as pd. Pandas
deal essentially with data in 1-D and 2-D arrays; Although, pandas handles these two differently.
In pandas, 1-D arrays are stated as a series & a dataframe is simply a 2-D array.
207
Chapter 10 : Data Analysis Tool : Pandas
Output:
We can read the dataframe by using head() function also which is having an argument (n)
i.e. number of rows to be displayed.
Output:
208
Chapter 10 : Data Analysis Tool : Pandas
Counting the rows and columns in DataFrame using shape(). It returns the no. of rows and
columns enclosed in a tuple.
Code 54: Counting the rows and columns in DataFrame using shape().
Output:
Output 48: Counting the rows and columns in DataFrame using shape().
Summary of Statistics of DataFrame using describe() method.
209
Chapter 10 : Data Analysis Tool : Pandas
Output:
Output:
210
Chapter 10 : Data Analysis Tool : Pandas
Creating another column in DataFrame, Here we will create column name percentage which
will calculate the percentage of student score by using aggregate function sum().
211
Chapter 10 : Data Analysis Tool : Pandas
10.6 Grouping
Pandas groupby is used for grouping the data according to the categories and apply a function to
the categories. It also helps to aggregate data efficiently.
Pandas dataframe.groupby() function is used to split the data into groups based on some
criteria. pandas objects can be split on any of their axes. The abstract definition of grouping is
to provide a mapping of labels to group names.
Syntax:
Parameters :
Example #1: Use groupby() function to group the data based on the “Team”.
212
Chapter 10 : Data Analysis Tool : Pandas
Output:
Example #2: Use groupby() function to form groups based on more than one category (i.e. Use
more than one column to perform the splitting).
Code 62: groupby() function to form groups based on more than one category
213
Chapter 10 : Data Analysis Tool : Pandas
Output:
Output 56: groupby() function to form groups based on more than one category
groupby() is a very powerful function with a lot of variations. It makes the task of splitting the
dataframe over some criteria really easy and efficient.
10.7 Filtering
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem
of data-centric python packages. Pandas is one of those packages and makes importing and
analyzing data much easier.
Pandas dataframe.filter() function is used to Subset rows or columns of dataframe according to
labels in the specified index. Note that this routine does not filter a dataframe on its contents.
The filter is applied to the labels of the index.
Syntax:
214
Chapter 10 : Data Analysis Tool : Pandas
The items, like, and regex parameters are enforced to be mutually exclusive. axis defaults to the
info axis that is used when indexing with [].
Example #1: Use filter() function to filter out any three columns of the dataframe.
Output:
Code 64: Use filter() function to filter out any three columns of the dataframe.
215
Chapter 10 : Data Analysis Tool : Pandas
Output:
Output 58: Use filter() function to filter out any three columns of the dataframe.
Example #2: Use filter() function to subset all columns in a dataframe which has the letter ‘a’
or ‘A’ in its name
Note : filter() function also takes a regular expression as one of its parameter.
Code 65: Use filter() function to subset all columns in a dataframe which has the letter ‘a’ or ‘A’
in its name.
216
Chapter 10 : Data Analysis Tool : Pandas
Output:
Output 59: Use filter() function to subset all columns in a dataframe which has the letter ‘a’ or
‘A’ in its name.
The regular expression ‘[aA]’ looks for all column names which has an ‘a’ or an ‘A’ in its name.
10.8 Slicing
With the help of Pandas, we can perform many functions on data set like Slicing, Indexing,
Manipulating, and Cleaning Data frame.
Case 1: Slicing Pandas Data frame
Example 1: Slicing Rows
217
Chapter 10 : Data Analysis Tool : Pandas
Output:
Output:
218
Chapter 10 : Data Analysis Tool : Pandas
Output:
219
Chapter 10 : Data Analysis Tool : Pandas
Output:
10.9 Sorting
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_pos
ition='last', ignore_index=False, key=None)
Parameters:
By: str or list of str
Name or list of names to sort by.
if axis is 0 or ‘index’ then by may contain index levels and/or column labels.
if axis is 1 or ‘columns’ then by may contain column levels and/or index labels.
Axis: {0 or ‘index’, 1 or ‘columns’}, default 0
Axis to be sorted.
Ascending: bool or list of bool, default True
Sort ascending vs. descending. Specify list for multiple sort orders. If this is a list of bools, must
match the length of the by.
Inplace: bool, default False
If True, perform operation in-place.
Kind: {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, default ‘quicksort’
Choice of sorting algorithm. See also numpy.sort() for more
information. mergesort and stable are the only stable algorithms. For Data Frames, this option
is only applied when sorting on a single column or label.
na_position: {‘first’, ‘last’}, default ‘last’
Puts NaNs at the beginning if first; last puts NaNs at the end.
ignore_index: bool, default False
If True, the resulting axis will be labelled 0, 1, …, n - 1.
Key: callable, optional
Apply the key function to the values before sorting. This is similar to the key argument in the
built-in sorted() function, with the notable difference that this key function should be vectorised.
It should expect a Series and return a Series with the same shape as the input. It will be applied
to each column in by independently.
Table 15: Sorting parameters
220
Chapter 10 : Data Analysis Tool : Pandas
Output:
In order to sort the data frame in pandas, function sort_values() is used. Pandas sort_values() can
sort the data frame in Ascending or Descending order.
221
Chapter 10 : Data Analysis Tool : Pandas
Output:
10.10 Ufunc
Universal functions in Numpy are simple mathematical functions. It is just a term that we gave
to mathematical functions in the Numpy library. Numpy provides various universal functions
that cover a wide variety of operationsThese functions include standard trigonometric functions,
functions for arithmetic operations, handling complex numbers, statistical functions, etc.
Universal functions have various characteristics which are as follows:
These functions operates on ndarray (N-dimensional array) i.e Numpy’s array class.
It performs fast element-wise array operations.
It supports various features like array broadcasting, type casting etc.
Numpy, universal functions are objects those belongs to numpy.ufunc class.
Python functions can also be created as a universal function using frompyfunc library
function.
Some ufuncs are called automatically when the corresponding arithmetic operator is used
on arrays. For example when addition of two array is performed element-wise using ‘+’
operator then np.add() is called internally.
222
Chapter 10 : Data Analysis Tool : Pandas
Exercise
223
Chapter 10 : Data Analysis Tool : Pandas
224
Chapter 10 : Data Analysis Tool : Pandas
225
Chapter 10 : Data Analysis Tool : Pandas
226