0% found this document useful (0 votes)
3 views28 pages

Python Lecture 01

The document outlines the fundamentals of programming languages, comparing them to natural languages and detailing their components such as alphabet, lexis, syntax, and semantics. It discusses the evolution of programming languages from machine language to high-level languages, emphasizing the importance of algorithms and the steps involved in program development. Additionally, it introduces tools like pseudocode and flowcharts for expressing algorithms and highlights the significance of testing algorithms for correctness.

Uploaded by

Gayan Indunil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views28 pages

Python Lecture 01

The document outlines the fundamentals of programming languages, comparing them to natural languages and detailing their components such as alphabet, lexis, syntax, and semantics. It discusses the evolution of programming languages from machine language to high-level languages, emphasizing the importance of algorithms and the steps involved in program development. Additionally, it introduces tools like pseudocode and flowcharts for expressing algorithms and highlights the significance of testing algorithms for correctness.

Uploaded by

Gayan Indunil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

PCAP | Programming Essentials

in
Python

1
programming languages vs Natural
languages

2
◼ Language consists of the four elements

◼ an alphabet -a set of symbols used to build words of a certain


language. (e.g., the Latin alphabet for English, the Cyrillic alphabet for
Russian, Kanji for Japanese, and so on)

◼ a lexis (dictionary) - a set of words the language offers its users

◼ a syntax - a set of rules

◼ Semantics - a set of rules determining if a certain phrase makes sense


(e.g., “I ate a doughnut” makes sense, but “A doughnut ate me”
doesn’t)

3
4
Evolution of computer languages

5
6
Program in machine language

1 00000000 00000100 0000000000000000


2 01011110 00001100 11000010 0000000000000010
3 11101111 00010110 0000000000000101
4 11101111 10011110 0000000000001011
5 11111000 10101101 11011111 0000000000010010
6 01100010 11011111 0000000000010101
7 11101111 00000010 11111011 0000000000010111
8 11110100 10101101 11011111 0000000000011110
9 00000011 10100010 11011111 0000000000100001
10 11101111 00000010 11111011 0000000000100100
11 01111110 11110100 10101101
12 11111000 10101110 11000101 0000000000101011
13 00000110 10100010 11111011 0000000000110001
14 11101111 00000010 11111011 0000000000110100
15 00000100 0000000000111101
16 00000100 0000000000111101

7
Note
:
The only language understood by
a computer is machine language.

8
Program in Assembly language

ORG 100 /Origin of program is location 100

LDA A /Load operand from location A

ADD B /Add operation form location B

STA C /Store sum in location C

HLT /Halt computer

A, DEC 83 /Decimal operand

B, DEC –2 /Decimal operand

C, DEC 0 /Sum stored in location C

END

9
◼ Assembly language is one level above machine language.

◼ It is a low level programming language that allows a user to write a


programme using alphanumeric, instead of numeric codes for a set of
instructions.

10
High level Language
▪ Machine Independent Language.

▪ It enables a user to write programs in a language which resembles


English word and familiar mathematical symbols.

▪ A program written in a high-level programming language is called a


source code (in contrast to the machine code executed by computers

▪ The file containing the source code is called the source file.

11
Compiling and interpreting
◼ Many languages require you to compile (translate) your program
into a form that the machine understands.
compile execute
source code byte code output
Hello.java Hello.class

◼ Python is instead directly interpreted into machine instructions.

interpret
source code output
Hello.py

12
13
Languages
▪ Algorithmic languages (LISP,ALGOL)

▪ Business-oriented languages(COBOL,SQL)

▪ Education-oriented languages(BASIC,Pascal)

▪ Object-oriented languages(C++, Java)

▪ Declarative languages

▪ Document formatting languages(TeX,SGML) )

▪ World Wide Web display languages(HTML,XML

▪ Scripting languages (Python)

14
Programs and Programming
❑ A (software) program is an ordered sequence of instructions that
the hardware of a computer can execute.

❑ A computer program is expressed using a programming


language

❑ Computer programming is the art of developing computer


programs.

❑ A person who develops a program using a programming


language is a programmer

15
Program Development steps
1. Define the problem
2. Outline the solution
3. Develop an algorithm
4. Test the algorithm for correctness
5. Code the algorithm in a programming language to obtain a
program
6. Ensure program has no syntax errors
7. Compile to generate translated code (optional)
8. Run the program
9. Test and debug the program
10. Document and maintain the program
16
Define the Problem
❑ The problem can be divided into three components

1. Outputs – what do you want to have?


2. Inputs – what do you have?
3. Processing – how do you go from inputs to outputs?
example
calculate and output the circumference and area of a circle when the
radius is given as an input (C=2πr and a = πr2. ).
Outputs – circumference (c) and area (a) of a circle
Input – the radius (r) of the circle
Processing – need to find out how to produce the outputs from the input

17
Outline the Solution
◼ the major computational tasks and any subtasks
◼ the major variables and data structures (to store data; e.g., inputs,
intermediate results)
◼ the control structures (e.g., sequence, selection, repetition) that can be
used

Consider the above example. The processing required is to compute c = 2πr and a = πr2.
▪ In order to calculate the circumference:

➢ Variables – radius (r), circumference (c)

➢ Computation – c = 2πr

▪ In order to calculate the area:

➢ Variables – radius (r), area (a)

➢ Computation – a = πr2

18
Develop an Algorithm
◼ complex problem, developing an algorithm is generally more difficult
than obtaining a computer program by coding an algorithm.
◼ used to express an algorithm

1. Pseudocode (a structured form of the English language)

2. Flowcharts

19
Pseudocode
◼ Pseudocode (a structured form of the English language) can be used to
express an algorithm.
Step 1. Start
2. Input r
3. Compute circumference
c = 2 * PI * r
4. Compute area
a = PI * r * r
5. Output c and a
6. Stop

‘PI’ is a constant that represents the value of π and ‘*’ is the multiplication operator.

20
flowcharts
◼ Another tool that can be used to express algorithms
◼ A flowchart is an easy to understand pictorial representation of an
algorithm.

21
Start

Input radius r

C=2*PI*r

A=PI*r

Output C and a

Stop

22
Start 1. Start
2. Num =1
Total = 0
Num=1 3. Total =Total + Num
Total=0
Num = Num + 1
4. If Num ≤ 100
Total=Total+Num
go to Step 3 // implied: else continue to Step 5

Num=Num+1 5. Output Total


6. Stop

Num<=100
Yes

No

Output Total
Stop

23
Q. Draw a Pseudocode and flowchart to log in to Facebook account

1.Enter www.facebook.com in your browser. (I/O)


2.facebook Home page loads (PROCESS)
3.Enter your Email ID and Password (I/O)
4.Is Email ID and Password Valid (DECISION)
if NO then
Log in error (PROCESS)
go to step 3
else
Display facebook Account (I/O)
Stop

24
25
Q. Find the sum of 5 numbers

1.Start
2.Initialize sum = 0 and count = 0 (PROCESS)
3.Enter n (I/O)
4.Find sum + n and assign it to sum and then increment count by 1 (PROCESS)
5.Is count < 5 (DECISION)
if YES go to step 2
else
Print sum (I/O)
6.Stop

26
27
Test the Algorithm for Correctness
◼ The programmer must make sure that the algorithm is correct

◼ The objective is to identify any errors early

◼ Correction is easy.

28

You might also like