UNIT 1 python
UNIT 1 python
1. Fundamentals of computing
A computer is an electronic device that accepts data/inputs from its user and processes
it into useful information as per the processing instructions to generate the output.
Functionalities of Computer
If we consider it in a very broad sense, any digital computer performs the following
five operations:
Step 2 − Saves the data/instructions in its memory and utilizes them as and when required.
1
1.1 Generation of Computers
Input Unit:
It accepts the data from the user
The input is given to the computer through the devices like keyboard, mouse, touch screen
etc..
Output Unit:
- It provides the processed data (information) to the user,
- It consists of
2
(i). Arithmetic and Logic Unit:
• ALU consists of 2 units. They are arithmetic unit and logic unit.
• The Arithmetic unit performs arithmetic operations on the data like addition,
• It acts as a supervisor, controls and coordinates the activity of the other units of computer
(iii). Registers
3
Primary Memory:
Primary memory is the main memory of computer. It is used to store data and instructions during
the processing.
It is of 2 kinds: RAM and ROM
RAM:
• RAM is volatile
ROM:
• ROM is non-volatile and read only memory.
• It is a permanent storage
Secondary Memory:
• The secondary memory stores data and instructions permanently.
• It is non-volatile memory
• Examples: Hard disk drive, floppy drive and optical disk drive
Applications:
• Science
• Education
• Engineering/Architecture/Manufacturing
• Entertainment
• Communication
• Business Application
• Publishing
• Banking
4
1.2 TYPES OF COMPUTERS
PC/Personal Computer
These are single-user computer systems having small, relatively reasonable computers
designed for an individual user. This type of computer can easily be moved from one place
to the other comprising a personal storage unit, input & output unit, and a Central
Processing Unit.
Workstation
Regularly a single user system is named a workstation. Workstations usually come with a
high-resolution graphics screen, inbuilt network support, a large amount of RAM, and a
graphical user interface. They are often designed for self-use by an individual and can be
used for multiple purposes. This type of system is not convenient for carrying from one
place to another.
Mini Computer
These come under multiple user computer systems that are capable of holding hundreds of
users simultaneously.
Main Frame
Supercomputer
These are extremely fast computers, which can execute hundreds of millions of
instructions per second. Supercomputers are mostly used in scientific and engineering
operations where processing is difficult. They are costly and complex to work on.
5
We need to four steps related to the identification of problem and its solutions. They are
decomposition, pattern recognition, abstraction, and algorithm design.
Decomposition:
The first step of computational thinking is decomposition. This stage stars by analyzing the
problem, stating it precisely, and establishing the criteria for the solution.
A computational thinking approach to a solution often starts by breaking the problem down
into smaller more familiar components so they can managed easier. The more you can
break a problem down, the easier it to be solve.
Pattern recognition:
If some problems are similar in nature, there is a good chance that they can be solved using
similar or repeated techniques.
This is a key component for making efficient solutions, and saving time in effective
solutions to given problems.
Abstraction:
The abstraction stage involves the identification of key components of the solution
It requires the ability to filter out unnecessary elements of a problem so that only focus on
the important elements.
Algorithm Design:
The final stage within the computational thinking process is algorithm design whereby a
detailed step by step set of instructions are created which explain how to solve the problems
Problem
2.ALGORITHM BUILDING BLOCKS OF ALGORITHMS (statements, state, control
flow, functions)
Algorithms can be constructed from basic building blocks namely, sequence,selection
and iteration.
6
2.1. Statements:
Statement is a single action in a computer.
In a computer statements might include some of the following actions
input data-information given to the program
process data-perform operation on a given input
output data-processed result
2.2. State:
Transition from one process to another process under specified condition with in atime is
called state.
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
Selection:
7
A selection statement causes the program control to be transferred to a specificpart of
8
the program based upon the condition.
If the conditional test is true, one part of the program will be executed, otherwiseit will
execute the other part of the program.
Example
Write an algorithm to check whether he is eligible to vote?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
6: Stop
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 1Step 6:
9
go to step 4
Step 7: Stop
2.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 simplertasks
during algorithm design.
10
3. NOTATIONS
3.1.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 visualrepresentation.
11
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
flowlines may leave the decision symbol.
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.
Pseudo code consists of short, readable and formally styled English languages used for
explain an algorithm.
It does not include details like variable declaration, subroutines.
It is easier to understand for the programmer or non programmer to understand the
general working of the program, because it is not based on any programming
language. It gives us the sketch of the program before actual coding.
It is not a machine readable
Pseudo code can’t be compiled and
executed. There is no standard syntax for
pseudo code.
Guidelines for writing pseudo code:
Write one statement per line
Capitalize initial keyword
Indent to hierarchy
End multiline structure
Keep statements language independent
Common keywords used in pseudocode
The following gives common keywords used in pseudocodes.
1. //: This keyword used to represent a comment.
2. BEGIN,END: Begin is the first statement and end is the last statement.
3. INPUT, GET, READ: The keyword is used to inputting data.
13
4. COMPUTE, CALCULATE: used for calculation of the result of the given expression.
14
5. ADD, SUBTRACT, INITIALIZE used for addition, subtraction and initialization.
6. OUTPUT, PRINT, DISPLAY: It is used to display the output of the program.
7. IF, ELSE, ENDIF: used to make decision.
8. WHILE, ENDWHILE: used for iterative statements.
FOR, ENDFOR: Another iterative Example: Greates of two numbers
incremented/decremented tested
automatically.Syntax for if else:
IF (condition)THEN BEGIN
statement READ
... a,b
ELS IF (a>b) THEN
E DISPLAY a is greater
statement ELSE
... DISPLAY b is greater
ENDIF END IF
END
Syntax for For: Example: Print n natural numbers
FOR( start-value to end-value) DO BEGI
statement N GET
... n
ENDFO INITIALIZE
R i=1 FOR (i<=n)
DOPRINT i
i=i+1
ENDFO
REND
Syntax for While: Example: Print n natural numbers
15
WHILE (condition) DO BEGI
statement N GET
... n
ENDWHILE INITIALIZE i=1
WHILE(i<=n)
DO
PRINT i
i=i+1
ENDWHIL
E
END
Advantages:
Pseudo is independent of any language; it can be used by most programmers.
It is easy to translate pseudo code into a programming language.
It can be easily modified as compared to flowchart.
Converting a pseudo code to programming language is very easy as comparedwith
converting a flowchart to programming language.
Disadvantages:
It does not provide visual representation of the program’s logic.
There are no accepted standards for writing pseudo codes.
It cannot be compiled nor executed.
For a beginner, It is more difficult to follow the logic or write pseudo code as
compared to flowchart.
Example:
Addition of two numbers:
BEGIN
GET
a,b ADD
c=a+b
16
PRINT
17
END
3.3.PROGRAMMING LANGUAGE
18
speed
19
The machine language program is translation free. Since the conversion time issaved,
the execution of machine language program is extremely fast.
Disadvantage:
It is hard to find errors in a program written in the machine language.
Writhing program in machine language is a time consuming process. Machine
dependent: According to architecture used, the computer differs from eachother. So
machine language differs from computer to computer. So a program developed for a
particular type of computer may not run on other type of computer. Assembly language:
To overcome the issues in programming language and make the programming process
easier, an assembly language is developed which is logically equivalent tomachine
language but it is easier for people to read, write and understand.
20
Execution time of assembly language program is more than machine language
program.
Because assembler is needed to convert from assembly language to machine
language.
High level language
High level language contains English words and symbols. The specified rules areto
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.
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. Ifany
error is found it display error message on the screen.
Interpreter
Interpreter translates the high level language program in line by line manner. The
interpreter translates a high level language statement in a source program to a machine code
and executes it immediately before translating the next statement. When an erroris found the
execution of the program is halted and error message is displayed on the screen.
Advantages
Readability
High level language is closer to natural language so they are easier to learn and
understand
Machine independent
High level language program have the advantage of being portable between
machines.
Easy debugging
21
Easy to find and correct error in high level language
Disadvantages
Less efficient
The translation process increases the execution time of the program. Programs in high
level language require more memory and take more execution time to execute.
Scripting language:
Scripting language are programming languages that control an application. Scripts can
execute independent of any other application. They are mostly embedded inthe application
that they control and are used to automate frequently executed tasks like communicating with
external program.
Examples:
Apple script
VB script
Markup languages:
23
A markup language is an artificial language that uses annotations to text thatdefine
hoe the text is to be displayed.
Examples:
HTM
L
XML
Concurrent programming language:
Concurrent programming is a computer programming technique that provides for the
execution of operation concurrently, either with in a single computer or across anumber of
systems.
Examples:
Joule
Limbo
Object oriented programming language:
Object oriented programming is a programming paradigm based on the conceptof
objects which may contain data in the form of procedures often known as methods.
Examples:
Lava
Moto
24
4. ALGORITHMIC PROBLEM SOLVING:
Algorithmic Problem solving is solving problem that require the formulation of analgorithm
25
If the instructions are executed concurrently, it is called parallel algorithm.
Choosing between Exact and Approximate Problem Solving
The next principal decision is to choose between solving the problem exactly
orsolving it approximately.
Based on this, the algorithms are classified as exact algorithm and approximation
algorithm.
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 dataspecifying
a problem’s instance
Algorithm+ Data structure=programs.
In the earlier days of computing, the dominant vehicle for specifying algorithms was a
flowchart, a method of expressing an algorithm by a collection of connected
geometric shapes containing descriptions of the algorithm’s steps.
26
language. We can look at such a program as yet another way of specifying the
algorithm, although it is preferable to consider it as the algorithm’simplementation.
Proving an Algorithm’s Correctness
Once an algorithm has been specified, you have to prove its correctness. That is, you
have to prove that the algorithm yields a required result for every legitimate input in a
finite amount of time.
A common technique for proving correctness is to use mathematical induction because
an algorithm’s iterations provide a natural sequence of steps needed for such proofs.
It might be worth mentioning that although tracing the algorithm’s performance for a
few specific inputs can be a very worthwhile activity, it cannot prove the algorithm’s
correctness conclusively. But in order to show that an algorithm is incorrect, you need
just one instance of its input for which the algorithm fails.
Analysing an Algorithm
1. Efficiency.
Time efficiency, indicating how fast the algorithm runs,
Space efficiency, indicating how much extra memory it
uses.
2. simplicity.
An algorithm should be precisely defined and investigated with mathematical
expressions.
Simpler algorithms are easier to understand and easier to program.
Simple algorithms usually contain fewer bugs.
Coding an Algorithm
Most algorithms are destined to be ultimately implemented as computer programs.
Programming an algorithm presents both a peril and an opportunity.
A working program provides an additional opportunity in allowing an empirical
analysis of the underlying algorithm. Such an analysis is based on timing the program
on several inputs and then analyzing the results obtained.
27
5. SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS:
1. iterations
2. Recursions
5.1. Iterations:
A sequence of statements is executed until a specified condition is true is callediterations.
1. for loop
2. While loop
Syntax for For: Example: Print n natural numbers
BEGI
FOR( start-value to end-value) DO N GET
statement n
... INITIALIZE
ENDFO i=1 FOR (i<=n)
R DOPRINT i
i=i+1
ENDFO
REND
Syntax for While: Example: Print n natural numbers
BEGI
WHILE (condition) DO N GET
statement n
... INITIALIZE i=1
ENDWHILE WHILE(i<=n)
DO
PRINT i
i=i+1
ENDWHIL
EEND
28
5.2. Recursions:
A function that calls itself is known as recursion.
Recursion is a process by which a function calls itself repeatedly until
somespecified condition has been satisfied.
Algorithm for factorial of n numbers using recursion:
Main function:
Step1:
29
Step2: else fact=n*factorial(n-1) and return fact
30
Pseudo code for factorial using recursion:
Main function:
BEGI
N
GET
n
CALL factorial(n)
PRINT fact
BIN
IF(n==1) THEN
fact=1
RETURN
fact
ELSE
RETURN fact=n*factorial(n-1)
31