Unit-1 Algorithmic Problem Solving and Basics of Python
Unit-1 Algorithmic Problem Solving and Basics of Python
MAHALAKSHMI,
PYTHON PROGRAMMING– K.MAHALAKSHMI, AP/CSE, VELTECH HIGH TECH DR.RR DR.SR ENGINEERING COLLEGE 9
10
8 Reasons Why Python is Good for AI and ML
Artificial Intelligence (AI) and Machine Learning (ML) are the new black of the IT industry.
Today Artificial Intelligence went far beyond science fiction idea. It became the necessity.
Being widely used for processing and analyzing huge volumes of data, AI helps to handle the work
that cannot be done manually anymore because of its significantly increased volumes and intensity.
AI is applied in analytics to build predictions that can help people create strong strategies and look for
TEACH A COURSE 11
FinTech applies AI in investment platforms to do market research and predict where to invest funds for
bigger profits. The traveling industry uses AI to deliver personalized suggestions or launch chatbots, plus
enhance the overall user experience. These examples show that AI and ML are used process loads of data
to offer better user experience, more personal and accurate one.
TEACH A COURSE 12
TEACH A COURSE 13
Python for AI and ML: the best programming language
A GREAT LIBRARY ECOSYSTEM
• Scikit-learn for handling basic ML algorithms like clustering, linear and logistic regressions, regression, classification, and others.
• Pandas for high-level data structures and analysis. It allows merging and filtering of data, as well as gathering it from other external sources
like Excel, for instance.
• Keras for deep learning. It allows fast calculations and prototyping, as it uses the GPU in addition to the CPU of the computer.
• TensorFlow for working with deep learning by setting up, training, and utilizing artificial neural networks with massive datasets.
• Matplotlib for creating 2D plots, histograms, charts, and other forms of visualization.
• NLTK for working with computational linguistics, natural language recognition, and processing.
• Scikit-image for image processing.
• PyBrain for neural networks, unsupervised and reinforcement learning.
• Caffe for deep learning that allows switching between the CPU and the GPU and processing 60+ mln images a day using a single NVIDIA
K40 GPU.
• StatsModels for statistical algorithms and data exploration.
In the PyPI repository, you can discover and compare more Python libraries.
https://djangostars.com/blog/why-python-is-good-for-artificial-intelligence-and-machine-learning/#:~:text=Python%20is%20the%20major%20code,%2C
%20Readability%2C%20and%20Platform%20independence
.
14
TEACH A COURSE 15
ALGORITHM
TEACH A COURSE 16
Properties of Algorithms
TEACH A COURSE 17
Qualities of a good algorithm
The following are the primary factors that are often used to judge the quality of the algorithms.
Time – To execute a program, the computer system takes some amount of time. The lesser
is the time required, the better is the algorithm.
Memory – To execute a program, computer system takes some amount of memory space.
some of these may provide more accurate results than others, and such
algorithms may be suitable.
TEACH A COURSE 18
Example
Write an algorithm to print “PSPP”
Step 1: Start
Step 2: Print “PSPP”
Step 3: Stop
Algorithmic problem solving is solving problem that require the formulation of an
algorithm for the solution. Algorithmic problem solving means solving the problem
by means of algorithm.
TEACH A COURSE 19
Understanding the Problem
standard problems & if a Known algorithm exists. Otherwise a new algorithm has to
be developed.
It is the process of finding the input of the problem that the algorithm solves.
TEACH A COURSE 20
Ascertaining the Capabilities of the Computational Device
device this can be done by knowing the type of the architecture, speed & memory
availability.
TEACH A COURSE 21
Choosing between Exact and Approximate Problem Solving
Once algorithm is developed, it is necessary to show that it computes answer for all
the possible legal inputs. The solution is stated in two forms, exact solution or
approximate solution.
TEACH A COURSE 22
Deciding a data structure
Data structure plays a vital role in designing and analysis the algorithms.
Some of the algorithm design techniques also depend on the structuring data
TEACH A COURSE 23
Algorithm Design Techniques
1. First, they provide guidance for designing algorithms for new problems,
TEACH A COURSE 24
TEACH A COURSE 25
BUILDING BLOCKS OF ALGORITHMS
(statements, state, control flow, functions)
The building blocks of algorithm refer to the blocks such as statements, state,
control flow structures, functions, etc that are used to develop algorithms.
TEACH A COURSE 26
Building blocks of algorithms
TEACH A COURSE 27
2. State:
1. Statements:
• Transition from one process to
Statement is a single action in a computer. another process under specified
condition with in a time is called state.
In a computer statements might include some of
• State is the information that the
the following actions program manipulates to accomplish
some task.
• input data-information given to the
• It is data or information that gets
program changed or manipulated throughout the
runtime of a program.
• process data-perform operation on a given
• The state of an algorithm is defined as
input its condition regarding stored data.
• output data-processed result.
TEACH A COURSE 28
3. Control flow:
The process of executing the individual statements in a given order is called control flow.
Control flow is the order that instructions are executed in a program .
A control statement is a statement that determines the control flow of a set of instructions
The control can be executed in three ways
1. sequence
2. selection
3. Iteration
TEACH A COURSE 29
TEACH A COURSE 30
Selection:
TEACH A COURSE 31
Iteration:
Iteration is a process of executing the set of instructions repeatedly till the condition
in iteration statement becomes false.
The iteration statement includes the initialization, comparison, execution of the
statements inside the iteration statement and finally the updating of the control
variable.
After the control variable is updated it is compared again, and the process repeats
itself, till the condition in iteration statement turns out to be false.
TEACH A COURSE 32
4. Functions:
TEACH A COURSE 33
Benefits of Using Functions
TEACH A COURSE 34
Example:
TEACH A COURSE 35
Types of functions/categories of functions:
TEACH A COURSE 36
TEACH A COURSE 37
SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS
The following strategies are used for developing algorithms:
Iteration:
• Iteration is a process of executing the set of instructions repeatedly till the
condition in iteration statement becomes false.
• The iteration statement includes the initialization, comparison, execution of the
statements inside the iteration statement and finally the updating of the control
variable.
• After the control variable is updated it is compared again, and the process repeats
itself, till the condition in iteration statement turns out to be false.
1. for loop
2. While loop
TEACH A COURSE 38
TEACH A COURSE 39
Problem: To find the factorial of a number (using While loop)
Description: factorial of a number n!=n*(n-1)! Where n is the number for which factorial
is to be calculated
Solution: The algorithm, pseudo code and flowchart are as follows:
step 1. Start
step 2. Read the number n
step 3. Initialize i=1, fact=1
step 4. Repeat step 4 through 6 until i<=n
step 5. fact =fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
TEACH A COURSE 40
Recursions:
A function that calls itself is known as recursion.
Recursion is a process by which a function calls itself repeatedly until some specified
condition has been satisfied.
Recursion is a programming technique that comes from recurrence relation, where
the problem is divided further in sub problems smaller in size but same in nature.
This division stops when the problem cannot be divided further.
This point is called base case from where control moves back towards the original
problem by solving sub problems and assembling results in order to get the final
result.
TEACH A COURSE 41
Advantages:
The main advantage recursion provides to programmers is that it takes less code to
write comparative to iterative version. The code written with help of recursion is
more There are some data structures you will see which are quite easy to code with
help of recursion.
Tree traversals, quick, and merge sort are a few examples.
Disadvantages:
Recursion provides no storage saving, nor time.
TEACH A COURSE 42
PSEUDO CODE
Pseudo code derived from ‘pseudo’ which means imitation and ‘code’ means instruction
is a generic way of describing an algorithm without using any specific programming
language related notations.
Pseudo code is an artificial and informal language that helps programmers to develop
algorithms.
Pseudo code can be defined as the narrative description of the flow and logic of the
intended program, written in plain language that expresses each step of the algorithm.
The pseudo code is also called as Program design language (PDL)
TEACH A COURSE 43
Characteristics/properties of Pseudo code:
TEACH A COURSE 44
Guidelines for developing Pseudo code:
Flow chart is defined as graphical representation of the logic for problem solving.
The purpose of flowchart is making the logic of the program clear in a visual
representation.
TEACH A COURSE 47
TEACH A COURSE 48
Rules for drawing a flowchart
1. The flowchart should be clear, neat and easy to follow.
2. The flowchart must have a logical start and finish.
3. Only one flow line should come out from a process symbol.
4. Only one flow line should enter a decision symbol. However, two or three flow lines may leave the decision symbol.
TEACH A COURSE 50
Disadvantages of flow chart
1. Complex logic: - Sometimes, the program logic is quite complicated. In that case, flowchart becomes
complex and clumsy.
2. Alterations and Modifications: - If alterations are required the flowchart may require re-drawing
completely.
3. Reproduction: - As the flowchart symbols cannot be typed, reproduction of flowchart becomes a
problem.
4. Cost: For large application the time and cost of flowchart drawing becomes costly.
Teach a Course 51
PROGRAMMING LANGUAGE
perform specific tasks. The programmers have to follow all the specified rules before
writing program using programming language.
The user has to communicate with the computer using language which it can
understand.
TEACH A COURSE 52
TYPES OF PROGRAMMING LANGUAGE
1. Machine language
2. Assembly language
3. High level language
https://youtu.be/aYjGXzktatA?si=ihvrXh_KjUXqYWAo
TEACH A COURSE 53
MACHINE LANGUAGE
The computer can understand only machine language which uses 0’s and 1’s.
In machine language the different instructions are formed by taking different
combinations of 0’s and 1’s.
Assembly language is symbolic representation of machine language.
Assembly languages are symbolic programming language that uses symbolic
notation to represent machine language instructions.
They are called low level language because they are so closely related to the
machines.
Ex: ADD a, b
Teach a Course 54
Assembler
in to a machine language.
TEACH A COURSE 55
HIGH LEVEL LANGUAGE
The specified rules are to be followed while writing program in high level language.
The interpreter or compilers are used for converting these programs in to machine readable
form.
The programs that translate high level language in to machine language are called
interpreter or compiler.
TEACH A COURSE 56
Compiler
A compiler is a program which translates the source code written in a high level
language in to object code which is in machine language program.
Compiler reads the whole program written in high level language and translates it
to machine language.
If any error is found it display error message on the screen.
Examples: C and C++ compilers.
TEACH A COURSE 57
INTRODUCTION TO PYTHON
TEACH A COURSE 58
Python is Interactive: You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
Python is Object-Oriented: Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
Python is a Beginner’s Language: Python is a great language for the beginner level
programmers and supports the development of a wide range of applications.
TEACH A COURSE 59
Python Features
Easy-to-learn: Python is clearly defined and easily readable. The structure of the program is very simple.
It uses few keywords.
Easy-to-maintain: Python's source code is fairly easy-to-maintain.
Portable: Python can run on a wide variety of hardware platforms and has the same interface on all platforms.
Interpreted: Python is processed at runtime by the interpreter.
So, there is no need to compile a program before executing it. You can simply run the program.
Extensible: Programmers can embed python within their C, C++, Java script, ActiveX, etc.
Free and Open Source: Anyone can freely distribute it, read the source code, and edit it.
High Level Language: When writing programs, programmers concentrate on solutions of the current
problem, no need to worry about the low level details.
Scalable: Python provides a better structure and support for large programs than shell scripting.
TEACH A COURSE 60
APPLICATIONS
TEACH A COURSE 61
PYTHON INTERPRETER
TEACH A COURSE 62
TEACH A COURSE 63