Chap - 1 Pythonn
Chap - 1 Pythonn
Chapter
1
Introduction to Programming
Key Topics
1.1. Introduction
1.1. Introduction
1.2. What is a Program ?
It is very important to learn the basics of programming.
1.3. Problem Solving Steps Programming is the process of designing, writing, testing,
1.4. What is the Need of Programming debugging and maintaining the source code. The purpose of
Languages?
1.5. Representation of Data programming is to create a set of instructions that computers can
1.5.1. Number Systems understand to perform a specific task. The process of writing
1.5.2. Decimal (Base 10) Number
System source code often requires expertise in algorithms, flowcharts
1.5.3. Binary (Base 2) Number System and logic. After learning the basics of programming, students
1.5.4. Octal (Base 8) Number System
1.5.5. Hexadecimal (Base 16) Number can feel easy while developing their own programs.
System
1.6. Problem Solving 1.2. What is a Program ?
1.7. Techniques and tools for problems
solving A program is a set of instructions, generally written in some
1.7.1. Algorithms
1.7.1.1 Advantage of writing
programming language which are translated using compilers or
algorithms interpreter into machine language so that computer can
1.7.1.2. D i s a d v a n t a g e s / understand these instructions to solve the problems for you. The
drawbacks of writing
algorithm choice of programming language depends on nature of problem.
1.7.2. Pseudocode codes (structured A problem is an obstacle, which makes it difficult to achieve a
english)
1.7.2.1. Advantage of Using desired goal, objective. It is advised to write an algorithm and
Psedocode flow chart before writing a program.
1.7.2.2. Limitations of using
psedocode
1.7.3. Flowchart 1.3. Problem Solving Steps
1.7.3.1. Guidelines for drawing
a flowchart Learning to programming is about learning to solve the
1.7.3.2. Advantages of using problems. First step in programming is to understand the problem
flowcharts
1.7.3.3. Limitations of using
after successfully understanding the problem we have to plan
flowcharts how to fix the problem i.e understanding methods to solve the
1.8. Program development life cycle problem first then writing algorithms, after that drawing flow chart
and then finally planning to go for programming.
1-2 Introduction to Programming
0 0000 0000 00 0
1 0000 0001 01 1
2 0000 0010 02 2
3 0000 0011 03 3
4 0000 0100 04 4
5 0000 0101 05 5
6 0000 0110 06 6
7 0000 0111 07 7
8 0000 1000 10 8
9 0000 1001 11 9
10 0000 1010 12 A
11 0000 1011 13 B
1-4 Introduction to Programming
12 0000 1100 14 C
13 0000 1101 15 D
14 0000 1110 16 E
15 0000 1111 17 F
16 0001 0000 20 10
17 0001 0001 21 11
18 0001 0010 22 12
You may ask why I always put 8 binary digits there. Well, the smallest unit in the computer's
memory to store data is called a BYTE, which consists of 8 BITS. One Byte allows upto 256
different combinations of data representation (28= 256). What happens when we have numbers
greater than 256? The computer simply uses more Bytes to hold the value, 2 Bytes can hold values
upto 65536 (216) and so forth.
1.7.1. Algorithms
A sequential solution of any program that is written in human language is called algorithm.
Algorithm is first step of the solution process. After the analysis of problem, programmers write the
algorithm of that problem. Step by step procedure designed to perform an operation, which (like a
map or flowchart) will lead to the sought result if followed correctly. Algorithms have a definite
beginning and a definite end, and a finite number of steps.
Algorithms resemble recipes. Recipes tell you how to accomplish a task by performing a
number of steps. For example, to bake a cake, the steps are: preheat the oven; mix flour, sugar, and
eggs thoroughly; pour into a baking pan, and so on.
Notice that the algorithm is described as a series of logical steps in a language that are easily
understood. For a computer to actually use these instructions, they need to be written in a language
that a computer can understand, known as a programming language.
Example of Algorithms:
Write an algorithm to find out whether the given no is even or odd.
Step1 : Start
Step 2 : Input Number
Step 3 : Get the remainder by dividing the number with 2.
Step 4 : If the remainder = 0 then
Step 5 : print Number is even
Step 6 : else
Step 7 : print Number is odd
Step 8 : Stop
Sequence : Sequential control is indicated by writing one action after another, each action on
a line by itself, and all actions aligned with the same indent. The actions are performed in the
sequence (top to bottom) in which they are written.
Example (non-computer)
Brush teeth
Wash face
Comb hair
Smile in mirror
IF-THEN-ELSE
Binary choice on a given Boolean condition is indicated by the use of four keywords: IF,
THEN, ELSE, and ENDIF. The general form is:
IF condition THEN
sequence 1
ELSE
sequence 2
ENDIF
WHILE
The WHILE construct is used to specify a loop with a test at the top. The beginning and ending
of the loop are indicated by two keywords WHILE and ENDWHILE. The general form is:
WHILE condition
sequence
ENDWHILE
1-8 Introduction to Programming
The loop is entered only if the condition is true. The "sequence" is performed for each iteration.
At the conclusion of each iteration, the condition is evaluated and the loop continues for as long as
the condition is true.
CASE
A CASE construct indicates a multiway branch based on conditions that are mutually exclusive.
Four keywords, CASE, OF, OTHERS, and ENDCASE, and conditions are used to indicate the
various alternatives. The general form is:
CASE expression OF
condition 1 : sequence 1
condition 2 : sequence 2
...
condition n : sequence n
OTHERS:
default sequence
ENDCASE
The OTHERS clause with its default sequence is optional.
REPEAT-UNTIL
This loop is similar to the WHILE loop except that the test is performed at the bottom of the
loop instead of at the top. Two keywords, REPEAT and UNTIL are used. The general form is:
REPEAT
sequence
UNTIL condition
The "sequence" in this type of loop is always performed at least once, because the test is
peformed after the sequence is executed. At the conclusion of each iteration, the condition is evaluated,
and the loop repeats if the condition is false. The loop terminates when the condition becomes true.
FOR
This loop is a specialized construct for iterating a specific number of times, often called a
"counting" loop. Two keywords, FOR and ENDFOR are used. The general form is:
FOR iteration bounds
sequence
ENDFOR
In cases where the loop constraints can be obviously inferred, it is best to describe the loop
using problem domain vocabulary.
1.7.3. Flowchart
A flowchart is a diagram that represents a process or algorithm. The steps are represented by
a series of boxes or other specialized symbols, then connected with arrows. Flowcharts are ideal for
communicating a step-by-step process to others. The flowchart is a means of visually presenting the
flow of data through an information processing systems, the operations performed within the system
and the sequence in which they are performed. The program flowchart can be likened to the blueprint
of a building. As we know, a designer draws a blueprint before starting construction on a building.
Similarly, a programmer prefers to draw a flowchart prior to writing a computer program. As in the
case of the drawing of a blueprint, the flowchart is drawn according to defined rules and using
standard flowchart symbols prescribed by the American National Standard Institute, Inc.
Before writing a program, the programmer draws a plan that involves the various steps that are
to be executed. What are the steps and what is the order in which they will be executed is very
important. By making a strategy before making a program, it becomes very easier to make programs.
These are two design approaches, which can be explained as:
You can imagine a tree like structure, in which when you are following top-down appoarch you
move from root node to leaf node and when you follow bottom-up approach, you follow leaf node
to root node.
In a top-down implementation, the implementation starts from the top of the hierarchy and
proceeds to the lower levels. First the main module is implemented, then its subordinates are
implemented, and their subordinates, and so on.
In a bottom-up implementation, the process is the reverse. The development starts with implementing
the modules at the bottom of the hierarchy and proceeds through the higher levels until it reaches the
top.
Top-down and bottom-up implementation should not be confused with top-down and bottom -
up design. When we precede top-down, for testing a set of modules at the top of the hierarchy, stubs
will have to be written for the lower level modules that the set of modules under testing invoke. On
the other hand, when we precede bottom-up, all modules that are lower in the hierarchy have been
developed and driver modules are needed to invoke these modules under testing.
Top-down versus bottom-up is also a pertinent issue when the design is not detailed enough. In
such cases, some of the design decisions have to be made during development.
Off-page connector
Magnetic Tape
Magnetic Disk
Flow lines
Subroutine Symbol
Display
START
Input num
Is True
num%2=0
Number is even
False
Number is odd
STOP
Figure 1.2
1-12 Introduction to Programming
Example 2 : Draw a flow chart to find greatest among three numbers A,B and C.
START
Input A,B,C
False Is True
A>B
print B print A
STOP
Figure 1.3
Example 3 : Draw a flow chart to find greatest among three numbers A,B and C.
START
Input A,B,C
print B
print C print B print A
STOP
Figure 1.4
Introduction to Programming 1-13
Example 4 : Draw a flow chart to find sum of First 10 Natural numbers
Figure 1.5
Example 5 : Draw a flow chart to find sum of First 10 Odd numbers
False
True
Figure 1.6
1-14 Introduction to Programming
Example 6 : Draw a flowchart for computing factorial of a number num (num!)
Where num! = 1 ´ 2 ´ 3 ´ …… num .
True
False
Figure 1.6
Input : num = 4
Output : fact = 24
Introduction to Programming 1-15
Exercise
Q.1. What is an algorithm and how it helps in programming?
Q.2. What is the difference between Machine Level Language and High Level Language ?
Q.3. What is the role of programming in software development?
1-18 Introduction to Programming
Q.4. How documentation helps us to develop a software?
Q.5. What do you means by pseudo codes? What are the advantages of it?
Q.6. How high level languages improves the programming?
Q.7. What does you means by languages? Explain various types of languages?
Q.8. What is flowchart? Explain the various symbols used in flowchart with example?
Q.9. Explain the various tools used to represent program logic?
Q.10. What does you means by translators? Explain various types of translators?
Q.11. What are the various approaches to develop a software?
Q.12. Discuss the various tools which are used to represent the program logic?