Python Unit 1
Python Unit 1
1
UNIT I
1. Algorithms,
2. building blocks of algorithms (statements, state, control flow, functions),
notation (pseudo code, flow chart, programming language),
3. algorithmic problem solving,
4. simple strategies for developing algorithms (iteration, recursion).
5. Illustrative problems:
a).find minimum in a list,
b).insert a card in a list of sorted cards,
c).Guess an integer number in a range,
d).Towers of Hanoi.
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
2 PROBLEM SOLVING
Problem solving is the systematic approach to define the problem and creating number of
solutions.
The problem solving process starts with the problem specifications and ends with a correct
program.
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
3 PROBLEM SOLVING TECHNIQUES
1. Algorithms.
2. Flowcharts.
3. Pseudo codes.
4. programs
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
4 ALGORITHM
Properties of Algorithms
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
5 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.
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
6 Example:
Example
Step 1: Start
Step 2: Print “Good Morning”
Step 3: Stop
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
7 BUILDING BLOCKS OF ALGORITHMS
1. statements
2. state
3. control flow
4. functions
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
8 Statements:
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
9 State
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
10 Control flow:
1.Sequence
2.selection
3.iteration
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
11 Sequence:
All the instructions are executed one after another is called sequence
execution.
Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b Step 4:
Display c
Step 5: Stop
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
12 Selection
If the conditional test is true, one part of the program will be executed,
otherwise it will execute the other part of the program.
Example
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 5: Stop
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
13 Iteration:
In some programs, certain set of statements are executed again and again based upon
conditional test.
i.e. executed more than one time.
This type of execution is called looping or iteration.
Example
Write an algorithm to print all natural numbers up to n
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
14
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.
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
15 Example:
Algorithm for addition of two numbers using function
Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop
Sub function add()
Step 1: Function start
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
16
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
17 NOTATIONS
FLOW CHART
PSEUDO CODE
PROGRAMMING LANGUAGE
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
18
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.
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
19
Rules for drawing a flowchart
The flowchart should be clear, neat and easy to follow
The flowchart must have a logical start and finish.
Only one flow line should come out from a process symbol
Only one flow line should enter a decision symbol. However, two or three flow lines may
leave the decision symbol.
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
Advantages of flowchart
20
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
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
Disadvantages of flow chart
21
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.
4. Cost: For large application the time and cost of flowchart drawing becomes costly.
Pollachi Institute of Engineering&Technology -- Department of CSE -- Problem Solving & Python Programming
22
PSEUDO CODE
Pseudo code consists of short, readable and formally styled English languages used for
explain an algorithm.