You are on page 1of 8

We have studied the use of basic constructs such as assignment, sequence, selection, iteration, input and output.

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

print “enter first number” //this is a prompt


input num1

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.

In Python there are no character types, however a character


type is considered as a string of length 1.
Logic Statements In Python:-
Implementing Simple Logic, Complex Logic and Nested The Python equivalent code will look like….
IF Statements.
In Python.
//considering our previous example

PRINT “enter a positive number”


INPUT number
If number > 0
if number > 10
then
print “this number is greater than 10”
else if number>=5 and number<=10
then When run in Shell it will result in the following…
print “this number is between 5 and 10 (inclusive)”
else
print “this number is less then 5”
else
print “this number is not positive”
// Consider the following CASE statement.

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

Since there is not alternate in Python for CASE statements


we will have to make us of Else…If structure. 

You might also like