Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Unit 07

PROGRAMMING

G.C.E. A/L ICT Ranma Selani Perera


Lesson Outline
1. Introduction to Programming Language 12. Input Function
2. Generations of Computer Programming Languages 13. Conversion Functions
3. Programme Translators 14. Data Type Conversion
4. Python Programming Language Introduction 15. Concatenation
5. Arithmetic and Relational (Comparison) Operators 16. Operations Precedence
6. Data Types in Python PL 17. Conditional Statements and Other Syntax
7. Logical Operators 18. Identifiers and Keywords (Reserved Words)
8. Shift Operators 19. Arrays, Lists, Tuples and Dictionaries
9. Bitwise AND, OR, XOR 20. User Define Functions
10. Combined Assignment Symbols 21. Text File Handling
11. Variables 22. Sets
What is Programming language?
• A computer language that is used by programmers (Developers) to communicate with
computers.

• A set of instructions written in specific language to perform a specific task.

• Most common steps in every PL are;


• Input – Get data from the keyboard / a file / the network etc.
• Output – Display on the screen / save to a file / send over network etc.
• Math – Perform basic mathematical operation
• Conditional execution – Check for certain conditions and run the appropriate code.
• Repetition – Perform same action repeatedly.
Generations of Computer Programming
Languages
Frist Generation (1GL) (Low – Level PL) Second Generation (2GL) (Low – Level PL)
 Also known as Machine Language  Also known as Assembly Language

 Written in Binary / Machine Code  Written in mnemonics instructions


 Used to write programs for CPU
 Fast Execution and no program translators needed
 Needed an Assembler to execute programs
 Difficult to write / test programs comparing with 3GL and 4GL
 Difficult to write / test programs
 Tied up with the computer architecture
 Tied up with the computer architecture

Third Generation (3GL) – C, C++, Java, Pascal (High– Level PL)  Mapping between assemble instructions and machine instructions is 1 - 1
 Written in with mathematical symbols and natural language words

 Execution of program is slow and needed a valid translator for execution

 Easy to write / test programs compaired 1GL and 2GL


Generations of C P L continues….
Fourth Generation (4GL) (High– Level PL)
 Written in with natural language words
 Execution of program is slow and needed a valid translator for execution

 Easy to write / test programs compaired with 1GL and 2GL

 4GL is a language and it’s environment that is used to develop software rapidly
Program Translators
There are 3 types;
1. Complier
2. Interpreter
3. Assembler

Machine
Source Program
/ Object
Code Translator
Code
Program Translators continues….
Compiler Interpreter
 It translate source code into object code  It translate and executes the source code

 First the entire program is converted into machine code at a time  One instruction at a time is converted into source code

 Then, complied code can be executed on the computer  Then, executed by the processor

Assembler
It translate assembly language mnemonics in to object code / machine code format

 Uses either compiler or interpreter fashion

 This is used in 2GL for Assembly language


Python Programming Language
• Python is most widely used by programmers and it is open source PL
• The Technology areas that used this PL mostly;
1) Machine Learning
2) Artificial Intelligence
3) Big Data
4) General User Interface (GUI) based desktop applications
5) Robotics

• This is a High Level Object Oriented PL


•Advantages:
• Easy to read, understand and write
• It Integrates with other PLs like C, C# and Java
• It Execute line by line which comes handy in error handling
• It platform independent.
Python Programming Language continues…
• Disadvantages:
• It is not suitable to develop mobile applications and games
• It works with a interpreter. So it is slower that other PLs like C and C++

• Other PLs: Python Coding Platforms (IDE’s)


• C, C#, C++ 1) Python IDLE PowerShell
2) Notepad and CMD (Command Prompt)
• Java
3) PyCharm community edition
• Pascal 4) PyCharm professional edition
• FORTRAN 5) Sublime Text
• COBOL 6) Thornny
7) Jupiter Notebook Use in AI and Data Science related fields
• Flutter
8) Matlab
• SWIFT
Arithmetic and Relational (Comparison) Operators
Arithmetic Operators Relational (Comparison) Operators
+ Addition > Greater than
- Subtraction < Lesser than
* Multiplication >= Greater than or equal
/ Division <= Less than or equal
// Integer Division (Only the integer part == Equal
for the answer, without the decimal part)
!= Not equal
** Exponention (Power of)
= Assignment ***
% Modulus (Integer division remainder)
Data types in Python
 Boolean – True or False
 Float – Numbers with decimal point
 String – Words and inputs given within “ ” or ‘’
 Integers – Positive and Negative whole numbers
 Tuples – Ordered Collection of elements (Unchangeable)
 Complex numbers
 Dictionaries – Objects that are stored by keys
Logical Operators
AND – Both conditions need to be true
OR – At lest one condition need to be true
NOT – Opposite results of the input
Bitwise AND, OR, XOR
Bitwise AND Bitwise OR Bitwise XOR
& Ampersand | Vertical Bar ^ Caret
Example: print(5 & 4) Example: print(5 | 4) Example: print(5 ^ 4)
Output 4 Output 5 Output 1
5 101 5 101 5 101
4 100 4 100 4 100
100 4 101 5 001 1
Shift Operators
>> Right Shift Op
Shifts the placement when the number is converted to the Binary form (0 and 1).
>> Left Shift Op Then new value will be given back in Decimal form.
Combined Assignment Symbol
There are 7 combined assignment symbols.
+= -= *= /= //= **= %=

Examples: a = 7
a += 4
print (a)
Output 11

Try out more examples!


Variables
 Variables are used to store data.
assignment operator
variable a = 4 value
 ‘Memory space’ is allocated for storing an integer value.
 In other words, the value ‘5’ is assigned to variable ‘a’ and stored in the memory location.

 Variables are case sensitive, need to begin with a lower-case letter and no spaces allowed
between words in the variable name, use underscore ‘_’ instead (your_name)
Input Function
o This is used to enter user input(s) through keyboard and it takes values as string data type by
default
o Try out below! New line (Line break)

name = input(‘What is your name? \n’)


print(‘My name is’ , name)
o input(), print(), int(), float(), and str() are inbuilt functions in Python

You might also like