You are on page 1of 63

SUBJECT HANDLING FACULTY: MRS. K.

MAHALAKSHMI,

ASSISTANT PROFESSOR/ CSE


2
3
4
5
TEACH A COURSE 6
TEACH A COURSE 7
TEACH A COURSE 8
1 2 3 4 5
UNIT I UNIT 2 UNIT 3 UNIT 4 UNIT 5
ALGORITHMIC CONTROL MODULES, LISTS, TUPLES, FILES AND
PROBLEM FLOW, PACKAGES, DICTIONARIES EXCEPTIONS
SOLVING AND FUNCTIONS STRINGS
BASICS OF
PYTHON

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

more effective solutions.

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

 It is defined as a sequence of instructions that describe a method for


solving a problem.
 In other words it is a step by step procedure for solving a problem.

TEACH A COURSE 16
Properties of Algorithms

 Should be written in simple English


 Each and every instruction should be precise and unambiguous.
 Instructions in an algorithm should not be repeated infinitely.
 Algorithm should conclude after a finite number of steps.
 Should have an endpoint
 Derived results should be obtained only after the algorithm terminates.

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.

The lesser is the memory required, the better is the algorithm.


 Accuracy – Multiple algorithms may provide suitable or correct solutions to a given problem,

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

 The problem given should be understood completely. Check if it is similar to some

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

 Once a problem is understood, we need to know the capabilities of the computing

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.

 Examples of problems where an exact solution cannot be obtained are i) Finding a

square root of number. ii) Solutions of nonlinear equations.

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

specifying a problem’s instance

 Algorithm+ Data structure=programs.

TEACH A COURSE 23
Algorithm Design Techniques

 An algorithm design technique (or “strategy” or “paradigm”)is a general approach to


solving problems algorithmically that is applicable to a variety of problems from
different areas of computing.

 Learning these techniques is of utmost importance for the following reasons.

1. First, they provide guidance for designing algorithms for new problems,

2. Second, algorithms are the cornerstone of computer science.

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

 The following are the various building blocks of algorithms:


1. Statements
2. State
3. Control flow
4. Functions

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:

 It is provided by a control statement that selectively executes instructions.


 The selection structure allows the programs to make a choice between two alternate
paths, whether it is true or false.
 These statements are known as conditional or decision making or selection statements
or branching statements.

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:

 Function is a sub program which consists of block of code(set of instructions) that


performs a particular task.
 For complex problems, the problem is been divided into smaller and simpler tasks
during algorithm design.

TEACH A COURSE 33
Benefits of Using Functions

 Reduction in line of code


 code reuse
 Better readability
 Information hiding
 Easy to debug and test
 Improved maintainability

TEACH A COURSE 34
Example:

 Function: piece of prewritten code that performs an operation


 print function: displays output on the screen
 Argument: data given to a function

TEACH A COURSE 35
Types of functions/categories of functions:

 Pre-defined functions-built in functions provided by programming languages


Example: print() in python
 User-defined functions-created by user.
 Example fact()

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:

 It is a “text based” detail (algorithmic) design tool.


 Pseudo code generally consists of short English phrases to express a specific task
 Consists of natural language-like statements that precisely describe the steps of an
algorithm or program
 Statements describe actions
 Focuses on the logic of the algorithm or program
 Avoids language-specific elements

TEACH A COURSE 44
Guidelines for developing Pseudo code:

The general guidelines for developing pseudo codes are :


• Statements should be written in English and should be programming language
independent.
• Steps of the pseudo codes must be understandable.
• It should be concise.
• Each instruction should be written in a separate list and each statement in pseudo code
should express just one action for the computer.
• The keywords like READ, PRINT etc should be written in capital letter.
TEACH A COURSE 45
Pseudo code Language Constructs
The Commonly used keywords Start:
START,BEGIN
STOP:END
Input: READ, OBTAIN, GET, USERINPUT
Output: PRINT, DISPLAY, SHOW, RETURN, OUTPUT
Process/compute: COMPUTE, CALCULATE, DETERMINE
Initialize: SET, INIT
Add one: INCREMENT, BUMP
If: IF condition THEN
statement 1
ELSE
statement 2
END‐IF
While: WHILE condition
statements
END‐WHILE
For: FOR condition
statements
For: FOR condition
statements
END‐FOR
for subprogram BEGIN SUBPROGRAM, END
TEACH A COURSE 46
Flow chart

 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.

5. Only one flow line is used with a terminal symbol.


6. Within standard symbols, write briefly and precisely.
7. Intersection of flow lines should be avoided.
TEACH A COURSE 49
Advantages of flowchart
1. Communication: - Flowcharts are better way of communicating the logic of a system to all concerned.
2. Effective analysis: - With the help of flowchart, problem can be analyzed in more effective way.
3. Proper documentation: -Program flowcharts serve as a good program documentation, which is needed for various
purposes.
4. Efficient Coding: - The flowcharts act as a guide or blueprint during the systems analysis and program
development
phase.
5. Proper Debugging: - The flowchart helps in debugging process.
6. Efficient Program Maintenance: - The maintenance of operating program becomes easy with the help of flowchart.
It helps the programmer to put efforts more efficiently on that part.

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

 A programming language is a set of symbols and rules for instructing a computer to

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

 Assembler is the program which translates assembly language instruction

in to a machine language.

TEACH A COURSE 55
HIGH LEVEL LANGUAGE

 High level language contains English words and symbols.

 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.

 Translating high level language to machine language

 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

 Python is a general-purpose interpreted, interactive, object-oriented, and high-


level programming language.
 It was created by Guido van Rossum during 1985- 1990.
 Python got its name from “Monty Python’s flying circus”.
 Python was released in the year 2000.
 Python is interpreted: Python is processed at runtime by the interpreter.
 You do not need to compile your program before executing it.

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

Bit Torrent file sharing


Google search engine, You tube
Intel, Cisco, HP,IBM
i–Robot
NASA
Face book, Drop box

TEACH A COURSE 61
PYTHON INTERPRETER

Interpreter: To execute a program in a high-level language by


translating it one line at a time.
Compiler: To translate a program written in a high-level
language into a low-level language all at once, in preparation
for later execution.

TEACH A COURSE 62
TEACH A COURSE 63

You might also like