0% found this document useful (0 votes)
4 views7 pages

Programming Basics and Python Examples

The document provides an overview of programming concepts, including high-level programming languages, Integrated Development Environments (IDEs), and basic programming constructs such as variables, constants, data types, and operators. It also covers control structures like loops and selection statements, as well as procedures, functions, arrays, and file handling in programming. Examples in pseudocode and Python illustrate these concepts for better understanding.

Uploaded by

sachini.inu
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)
4 views7 pages

Programming Basics and Python Examples

The document provides an overview of programming concepts, including high-level programming languages, Integrated Development Environments (IDEs), and basic programming constructs such as variables, constants, data types, and operators. It also covers control structures like loops and selection statements, as well as procedures, functions, arrays, and file handling in programming. Examples in pseudocode and Python illustrate these concepts for better understanding.

Uploaded by

sachini.inu
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

Ja-Ela Road, Gampaha

Tel: 074 014 3821 | 071 430 7791


Instructor: Inushi Liyanage ([Link]. Sp (Hons) in IT)

Programming

Programming means writing instructions for a computer to follow. These instructions are written
using high-level programming languages such as Python. Programs help computers complete tasks
step by step.

High-level languages:

• Are easy to read and write

• Are portable (work on many systems)

• Need a translator (interpreter or compiler)

Integrated Development Environment (IDE)

An IDE is used to write, run and test programs.

An IDE includes:

• Code editor

• Error messages

• Debugger

• Run/execute options

Example IDE for Python: IDLE

1 Topic 8
Programming
Ja-Ela Road, Gampaha
Tel: 074 014 3821 | 071 430 7791
Instructor: Inushi Liyanage ([Link]. Sp (Hons) in IT)

Variables, Constants & Data Types

Variable Declaration (Pseudocode)

Examples:

Pseudocode requires variables to be declared DECLARE Age : INTEGER


before use.
DECLARE Name : STRING
Format:
DECLARE Price : REAL
DECLARE <variableName> : <dataType>
DECLARE Flag : BOOLEAN

Constants

Constants store values that do not change.

Pseudocode Python

CONSTANT PI ← 3.142 PI = 3.142 # Python uses capital letters by


convention

Input and Output

Pseudocode Python

INPUT Name name = input("Enter your name: ")

OUTPUT "Your name is ", Name print("Your name is", name)

String Handling

Required string methods:

• length – count characters


• substring – extract part of string
• upper – uppercase
• lower – lowercase

2 Topic 8
Programming
Ja-Ela Road, Gampaha
Tel: 074 014 3821 | 071 430 7791
Instructor: Inushi Liyanage ([Link]. Sp (Hons) in IT)
Pseudocode Examples Python Examples

LENGTH("Computer Science") len("Computer Science")

SUBSTRING("Computer Science", 10, 7) "Computer Science"[9:16]

UPPER("hello") "hello".upper()

LOWER("HELLO") "HELLO".lower()

Operators

Arithmetic Operators

• – * / ** DIV MOD

Python Examples: Boolean Operators

a+ b • AND
• OR
a-b
• NOT
a* b
Python:
a/ b
x and y
a ** b
x or y
a // b # integer division (DIV)
not x
a% b # remainder (MOD)

Selection (IF Statements)

Pseudocode Python

IF age > 18 THEN if age > 18:

OUTPUT "Adult" print("Adult")

ELSE else:

OUTPUT "Child" print("Child")

ENDIF

3 Topic 8
Programming
Ja-Ela Road, Gampaha
Tel: 074 014 3821 | 071 430 7791
Instructor: Inushi Liyanage ([Link]. Sp (Hons) in IT)

Loops (Iteration)

1. FOR Loop (Count-controlled)

Pseudocode

FOR Counter ← 1 TO 5 Python

OUTPUT Counter for counter in range(1, 6):

NEXT print(counter)

2. REPEAT…UNTIL Loop (Post-condition)

Pseudocode

REPEAT

INPUT Mark

UNTIL Mark = -1

(Python does not have REPEAT loops.)

3. WHILE Loop (Pre-condition)

Pseudocode

INPUT Mark Python

WHILE Mark <> -1 DO mark = int(input("Enter mark (-1 to stop): "))

INPUT Mark while mark != -1:

ENDWHILE mark = int(input("Enter mark (-1 to stop):


"))

4 Topic 8
Programming
Ja-Ela Road, Gampaha
Tel: 074 014 3821 | 071 430 7791
Instructor: Inushi Liyanage ([Link]. Sp (Hons) in IT)

Procedures and Functions

Procedures

Do a task but do not return a value.

Pseudocode: Python:

PROCEDURE Welcome() def welcome():

OUTPUT "Welcome!" print("Welcome!")

ENDPROCEDURE

Functions

Do a task and return a value.

Pseudocode: Python:

FUNCTION AddNumbers(a, b) def add_numbers(a, b):

RETURN a + b return a + b

ENDFUNCTION

Arrays (1D Arrays)

Array Declaration (Pseudocode) Python Equivalent

DECLARE Marks : ARRAY[1:10] OF INTEGER

Assigning Values: Python uses lists instead of arrays.

Marks[1] ← 56 marks = [56, 72, 88, 45]

Marks[2] ← 72 Accessing items:

print(marks[0])

5 Topic 8
Programming
Ja-Ela Road, Gampaha
Tel: 074 014 3821 | 071 430 7791
Instructor: Inushi Liyanage ([Link]. Sp (Hons) in IT)

Two-Dimensional Arrays (2D Arrays)

A 2D array is an array of arrays. It stores data in rows and columns, like a table.

Purpose of 2D Arrays

Used for:

• Timetables
• Seating plans
• Matrices
• Storing marks of multiple students in multiple subjects

2D Array Declaration (Pseudocode)

DECLARE Marks : ARRAY[1:3, 1:4] OF 2D Arrays in Python


INTEGER
Python represents 2D arrays using lists of
Assigning a value: lists.

Marks[2, 3] ← 75 // Student 2, Subject 3 Declaration Example

Accessing values: marks = [

OUTPUT Marks[2, 3] [65, 70, 80, 75], # Student 1

[55, 60, 72, 68], # Student 2

[90, 85, 88, 92] # Student 3

Accessing data

print(marks[1][2]) # Student 2, Subject 3

Changing a value

marks[1][2] = 75

6 Topic 8
Programming
Ja-Ela Road, Gampaha
Tel: 074 014 3821 | 071 430 7791
Instructor: Inushi Liyanage ([Link]. Sp (Hons) in IT)

File Handling

Programs may need to save or load data.

Pseudocode Python

OPENFILE Students FOR READ file = open("[Link]", "r")

READ Record data = [Link]()

CLOSEFILE Students [Link]()

Writing

file = open("[Link]", "w")

[Link]("Hello")

[Link]()

7 Topic 8
Programming

You might also like