Professional Documents
Culture Documents
Paper 2 Part 4 (Programming Using Python)
Paper 2 Part 4 (Programming Using Python)
We will now study the Python Syntax for these basic constructs.
Some interesting details about Python:-
- Every statement must be on a separate line.
- Indentation is significant. This is known as the 'off-side rule’.
- Keywords are written in lower case.
- Python is case sensitive the identifier Number1 is seen as different from number1 or NUMBER1 .
- Programs are interpreted (refer to previous concepts about compilers, interpreters and assemblers).
- Python files have a .py extension
- Python files can be edited and created using IDLE (a Python editor).
A python file
where code is
written
The same
module is run in
the Python shell
You will learn the application of the following in Python:-
1. Declaration of Variables.
2. Declaration of Constants.
3. Assignment of Variables.
4. Arithmetic Operations.
5. Output to Screen.
6. Writing Comments
7. Datatypes
8. Boolean
9. Selection
10. Loops
11. Arrays
Declaration of Variables:-
SYNTAX:-
DECLARE <Identifier> : <type>
EXAMPLES:-
DECLARE number : INTEGER
//number is declared as an integer
DECLARE name : STRING
//name is declared as string
DECLARE n1,n2,n3 : INTEGER
//three numbers n1,n2,n3 as integer
Variable Assignment:-
SYNTAX:-
<Identifier> <value>
EXAMPLES:-
number 0
name “Hassan”
Declaration of Constants
SYNTAX:-
CONSTANT <Identifier>
<value>
EXAMPLE:-
CONSTANT Pi = 3.142
Arithmatic Operators:- DECLARE num1,num2,sum,prod,divwhole,rem : INTEGER
DECLARE div2 : REAL
input “enter the second number”, num2 // can be written in same line too
// say num1 is 5 and num2 is 2
sum = num1 + num2
//sum will be assigned 7
Operator precedence:- prod = num1*num2
1. Brackets. //prod will be assigned 10
2. Exponents. divwhole = num1 DIV num2
3. Multiplications. //divwhole will be assigned 2
4. Division. rem = num1 MOD num2
5. Addition. //rem will be assigned 1
6. Subtraction. div2 = num1 / num2
//div will 2.5
print “The Sum is=” , sum, “ The Product is =”, prod, “ The Whole Division is=”, divwhole,
“ The Remainder is=”, rem
// this is as example of printing “prompts” in line with variable values.
//will print in the following manner
//The Sum is=7 The Product is=10 The Whole Division is=2 The Remainder is=1
print “The division with decimal places is…”
print div2
//the variable type of div is real therefore it will have complete division as
Data Types in Python:-
Further Data Types
Boolean Expressions:-
These expressions evaluate
to either TRUE or FALSE.
CASE of Grade
“A” : PRINT “you have achieved an A grade”
“F”, “U” : PRINT “you have failed”
“B”…“E”: PRINT “Passed”
OTHERWISE
PRINT “Invalid Grade”
END CASE